OPENCV based on the color of the license plate location
http://blog.csdn.net/superdont/article/details/24936341
OPENCV based on the color of the license plate location2014-05-03 22:05 4626 People read Comments (3) favorite reports Classification:image Processing (a)OpenCV (in)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Use Color properties:
[CPP]View PlainCopy
- Mat Srcimage=imread ("image/t10.jpg");
- Mat Srcshowimage;
- Srcimage.copyto (Srcshowimage);
- //imshow ("a", srcimage);
- int i,j;
- int cpointb,cpointg,cpointr;
- For (i=1;i<srcimage.rows;i++)
- For (j=1;j<srcimage.cols;j++)
- {
- Cpointb=srcimage.at<vec3b> (I,J) [0];
- Cpointg=srcimage.at<vec3b> (I,J) [1];
- Cpointr=srcimage.at<vec3b> (I,J) [2];
- if (cpointb>80&cpointr<80&cpointg<80) //extract blue, set the area to black
- {
- Srcimage.at<vec3b> (I,J) [0]=0;
- Srcimage.at<vec3b> (I,J) [1]=0;
- Srcimage.at<vec3b> (I,J) [2]=0;
- }
- Else if (cpointb>200&cpointr>200&cpointg>200) //Extract white, set it to black
- {
- Srcimage.at<vec3b> (I,J) [0]=0;
- Srcimage.at<vec3b> (I,J) [1]=0;
- Srcimage.at<vec3b> (I,J) [2]=0;
- }
- Else
- {
- Srcimage.at<vec3b> (I,J) [0]=255;
- Srcimage.at<vec3b> (I,J) [1]=255;
- Srcimage.at<vec3b> (I,J) [2]=255;
- }
- }
- Cvtcolor (Srcimage,srcimage, Cv_bgr2gray);
- Threshold (srcimage,srcimage,127, 255,cv_thresh_binary);
- //Use the difference method to remove irrelevant areas.
- For (i=1;i<srcimage.rows;i++)
- For (j=1;j<srcimage.cols-1;j++)
- {
- Srcimage.at<uchar> (i,j) =srcimage.at<uchar> (i,j+1)-srcimage.at<uchar> (I,J);
- }
- Threshold (srcimage,srcimage,127, 255,CV_THRESH_BINARY_INV); //By the two value of the way to take the reverse.
- //erode (Srcimage,srcimage,mat (5,5,cv_8u), point ( -1,-1), 2); Corrosion
- //Dilate (Src,src,mat (5,5,cv_8u), point ( -1,-1), 2); //expansion
- //Morphologyex (Src,src,morph_open,mat (3,3,cv_8u), point ( -1,-1), 1); Open Operation
- //Morphologyex (Src,src,morph_close,mat (3,3,cv_8u), point ( -1,-1), 1); Closed Operation
- Erode (Srcimage,srcimage,mat (3,3,cv_8u), point ( -1,-1), 5);
- Threshold (SRCIMAGE,SRCIMAGE,127,255,CV_THRESH_BINARY_INV);
- Imshow ("a", srcimage);
- vector<vector<point> > contours;
- Vector<vec4i> hierarchy;
- Findcontours (srcimage, 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++)
- {
- //How to use the bounding box
- 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>=2000&&tmparea<=25000)
- {
- Rectangle (Srcshowimage,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 ("Da", Srcshowimage);
The effect is as follows:
Colors can be considered more nuanced, or considered to be implemented in other color spaces.
OPENCV based on the color of the license plate location