OpenCV使用边缘提取、腐蚀、轮廓进行车牌定位

网友投稿 317 2022-08-22

OpenCV使用边缘提取、腐蚀、轮廓进行车牌定位

采用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 > contours; //vector 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 > contours; vector 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);

效果一般,部分测试图像:

测试了很多图片,这几张基本有个样子,通过调整腐蚀的次数,可以针对不同的图像进行定位。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python最简编码规范(python语言编码规范)
下一篇:在视频帧的特定位置加上可视水印
相关文章

 发表评论

暂时没有评论,来抢沙发吧~