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<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

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

superdont

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值