OpenCV使用邊緣提取、腐蝕、輪廓進行車牌定位
阿新 • • 發佈:2018-11-24
採用OpenCV249利用邊緣檢測、輪廓檢測、腐蝕實現的車牌定位,詳細為:
Mat srcImage=imread("image/000.jpg"); //imshow("a",srcImage); int i,j; int cPointR,cPointG,cPointB,cPoint;//currentPoint; Mat resizeImage; resize(srcImage,resizeImage,Size(400,300)); Mat grayImage; cvtColor(resizeImage,grayImage, CV_BGR2GRAY); Mat medianImage; medianBlur(grayImage,medianImage,3); //最後一個引數須要為奇數 Mat sobelImage; //引數為:源影象。結果影象,影象深度,x方向階數。y方向階數。核的大小。尺度因子,新增的值 Sobel(medianImage,sobelImage,CV_8U,1,0,3,0.4,128); Mat normalizeImage; normalize(sobelImage,normalizeImage,255,0,CV_MINMAX); Mat binaryImage; threshold(normalizeImage,binaryImage, 100, 255, THRESH_BINARY_INV ); Mat closeImage; //morphologyEx(binaryImage,closeImage,MORPH_CLOSE,Mat(3,1,CV_8U),Point(0,0),10); //閉運算 Mat openImage(closeImage.rows,closeImage.cols,CV_8UC1); //morphologyEx(closeImage,openImage,MORPH_OPEN,Mat(3,3,CV_8U),Point(0,0),1); //開運算 // erode(openImage,openImage,Mat(3,3,CV_8U),Point(-1,-1),10); dilate(binaryImage,openImage,Mat(3,3,CV_8U),Point(-1,-1),6); /* Mat rgbImage; cvtColor(openImage,rgbImage, CV_GRAY2BGR); */ //cvtColor(openImage,openImage, CV_BGR2GRAY); //vector<vector<Point> > contours; //vector<Vec4i> hierarchy; //openImage=imread("test.png"); imshow("openImage",openImage); /// Detect edges using canny // Canny( src_gray, canny_output, thresh, thresh*2, 3 ); /// Find contours /* Mat thresholdImage; cvtColor(openImage,openImage, CV_BGR2GRAY); threshold( openImage,thresholdImage,127, 255, THRESH_BINARY ); openImage=thresholdImage;*/ vector<vector<Point> > contours; vector<Vec4i> hierarchy; findContours(openImage, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) ); Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) ); for( int i = 0; i < contours.size(); i++ ) { //使用邊界框的方式 CvRect aRect = boundingRect(contours[i]); int tmparea=aRect.height*aRect.height; if (((double)aRect.width/(double)aRect.height>2)&& ((double)aRect.width/(double)aRect.height<6)&& tmparea>=200&&tmparea<=25000) { rectangle(resizeImage,cvPoint(aRect.x,aRect.y),cvPoint(aRect.x+aRect.width ,aRect.y+aRect.height),color,2); //cvDrawContours( dst, contours, color, color, -1, 1, 8 ); } } imshow("contour",resizeImage);
效果一般,部分測試影象:
測試了非常多圖片。這幾張基本有個樣子。通過調整腐蝕的次數。能夠針對不同的影象進行定位。
參考資料:
學習OpenCV——車牌檢測(定位):http://blog.csdn.net/yangtrees/article/details/7444470