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

猜你喜欢

转载自www.cnblogs.com/ldxsuanfa/p/10013814.html