码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-05-04 08:42:10      阅读:819      评论:0      收藏:0      [点我收藏+]

标签:des   cWeb   blog   class   code   tar   

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

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

bubuko.com,布布扣


bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

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

参考资料:

学习OpenCV——车牌检测(定位):http://blog.csdn.net/yangtrees/article/details/7444470

OpenCV使用边缘提取、腐蚀、轮廓进行车牌定位,布布扣,bubuko.com

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

标签:des   cWeb   blog   class   code   tar   

原文地址:http://blog.csdn.net/superdont/article/details/24935383

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!