OpenCV依据颜色的车牌定位

OpenCV依据颜色的车牌定位

http://blog.csdn.net/superdont/article/details/24936341

OpenCV依据颜色的车牌定位

 分类:
 
 

 

使用颜色属性:

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. Mat srcImage=imread("image/t10.jpg");  
  2.     Mat srcShowImage;  
  3.     srcImage.copyTo(srcShowImage);  
  4.     //imshow("a",srcImage);  
  5.     int i,j;  
  6.     int cPointB,cPointG,cPointR;  
  7.     for(i=1;i<srcImage.rows;i++)  
  8.         for(j=1;j<srcImage.cols;j++)  
  9.         {  
  10.             cPointB=srcImage.at<Vec3b>(i,j)[0];  
  11.             cPointG=srcImage.at<Vec3b>(i,j)[1];  
  12.             cPointR=srcImage.at<Vec3b>(i,j)[2];  
  13.             if(cPointB>80&cPointR<80&cPointG<80)    //提取蓝色,将该区域设置为黑色  
  14.             {  
  15.                 srcImage.at<Vec3b>(i,j)[0]=0;  
  16.                 srcImage.at<Vec3b>(i,j)[1]=0;  
  17.                 srcImage.at<Vec3b>(i,j)[2]=0;  
  18.             }  
  19.   
  20.             else if(cPointB>200&cPointR>200&cPointG>200)  //提取白色,将其设置为黑色  
  21.             {  
  22.                 srcImage.at<Vec3b>(i,j)[0]=0;  
  23.                 srcImage.at<Vec3b>(i,j)[1]=0;  
  24.                 srcImage.at<Vec3b>(i,j)[2]=0;  
  25.             }  
  26.   
  27.             else  
  28.             {  
  29.                 srcImage.at<Vec3b>(i,j)[0]=255;  
  30.                 srcImage.at<Vec3b>(i,j)[1]=255;  
  31.                 srcImage.at<Vec3b>(i,j)[2]=255;  
  32.             }  
  33.   
  34.         }  
  35.         cvtColor(srcImage,srcImage, CV_BGR2GRAY);    
  36.         threshold(srcImage,srcImage,127, 255,CV_THRESH_BINARY);     
  37.         //使用差分法,去掉不相关的区域。  
  38.         for(i=1;i<srcImage.rows;i++)  
  39.             for(j=1;j<srcImage.cols-1;j++)  
  40.             {  
  41.                 srcImage.at<uchar>(i,j)=srcImage.at<uchar>(i,j+1)-srcImage.at<uchar>(i,j);  
  42.   
  43.             }  
  44.   
  45.             threshold(srcImage,srcImage,127, 255,CV_THRESH_BINARY_INV);//通过二值化的方式来取反。  
  46.             //erode(srcImage,srcImage,Mat(5,5,CV_8U),Point(-1,-1),2);  //腐蚀  
  47.             //  dilate(src,src,Mat(5,5,CV_8U),Point(-1,-1),2); //膨胀  
  48.             //  morphologyEx(src,src,MORPH_OPEN,Mat(3,3,CV_8U),Point(-1,-1),1);   //开运算  
  49.             //   morphologyEx(src,src,MORPH_CLOSE,Mat(3,3,CV_8U),Point(-1,-1),1);  //闭运算  
  50.             erode(srcImage,srcImage,Mat(3,3,CV_8U),Point(-1,-1),5);  
  51.             threshold(srcImage,srcImage,127,255,CV_THRESH_BINARY_INV);  
  52.             imshow("a",srcImage);  
  53.             vector<vector<Point> > contours;  
  54.             vector<Vec4i> hierarchy;  
  55.             findContours(srcImage, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );  
  56.             Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );  
  57.             for( int i = 0; i < contours.size(); i++ )  
  58.             {    
  59.                 //使用边界框的方式    
  60.                 CvRect aRect =  boundingRect(contours[i]);  
  61.                 int tmparea=aRect.height*aRect.height;    
  62.                 if (((double)aRect.width/(double)aRect.height>2)&& ((double)aRect.width/(double)aRect.height<6)&& tmparea>=2000&&tmparea<=25000)    
  63.                 {    
  64.                     rectangle(srcShowImage,cvPoint(aRect.x,aRect.y),cvPoint(aRect.x+aRect.width ,aRect.y+aRect.height),color,2);    
  65.                     //cvDrawContours( dst, contours, color, color, -1, 1, 8 );    
  66.                 }    
  67.             }   
  68.   
  69.   
  70.             imshow("da",srcShowImage);  

效果如下:

 

 

 

颜色可以考虑更细致,或者考虑在其他颜色空间内实现。

posted @ 2016-03-01 09:32  donaldlee  阅读(350)  评论(0编辑  收藏  举报