License Plate detection with Connected Component Analysis

If you have read my previous posts then probably you know everything that went into developing this.

First I will describe the fine prints of the process.

Step 1: Standard smoothing process using the Gaussian technique.

Step 2:Morphological Operations:TOP HAT

Now in most cases generally people use Black Hat operation for connected component analysis.But here we have used TOP HAT simply because we are trying to detect the License plates which will be lighter than its neighbors.TOP HAT exaggerates those portions which are LIGHTER than the surroundings.Our logic is that the license plate mostly contain light portions.


As you can see the depending on the difference of the light regions with the surrounding regions ,the intensity depends.Unfortunately our license plate isn’t the lightest region.

Step 3 ,

Now the obvious result is to threshold it so that the light regions are more prominent for contour extraction.

I have thresholded the entire pixels above 128 to 255.This based on the general features.

Step 4,

Contour extraction.Now First I want to show the contours normally detected in this image.


These are the blobs(contours) that are present.You can see we have a large collection of insignificant contours that are either too large or too small.

So now we would like to ignore too large bobs or too small bobs.

So we first filter the blobs by area.I have kept the out put are to anything between 600 to 10000.Anything above it gets discarded.

Next we filter again on the basis of ratio.We check each of the the blobs to find the ratio.I have kept the Width/height ratio range 2 to 5.4

So if we filter out the blobs we get



Some other examples.

In Indian scenarios this doesn’t always work cause vehicles don’t obey the norms.But then the method is good enough.But it detects some false positives too.




So now the code is openCV.I have used the BLOB RESULT library.It is the best for handling bobs as it supports contour labeling.

Here goes the code.

  1: 	IplImage* img1=cvCreateImage(cvSize(img->width,img->height),img->depth, 1 );
  2: 	cvConvertImage(img, img1,0);
  3: 	IplImage* img2=cvCreateImage(cvSize(img->width,img->height),img->depth, 1 );
  4: 	IplImage* img3=cvCreateImage(cvSize(img->width,img->height),img->depth, 1 );
  5: 	cvSetZero(img2);
  6: 	cvSetZero(img3);
  7: 	IplImage* img_temp=cvCreateImage(cvSize(img->width,img->height),img->depth, 1 );
  8: 	IplImage* cc_color=cvCreateImage(cvSize(img->width,img->height),img->depth, 1 );
 10: 	 CvMemStorage *mem;
 11: 	mem = cvCreateMemStorage(0);
 12: 	CvSeq *contours = 0;
 13:     CvSeq *ptr,*polygon;
 14: 	  cvSmooth( img1, img1, CV_GAUSSIAN, 3, 3 );
 15: 	cvMorphologyEx(img1,img1,img2,cvCreateStructuringElementEx(21,3,10,2,CV_SHAPE_RECT,NULL),CV_MOP_TOPHAT,1);
 16: 	display(img1);
 17: 	cvThreshold(img1,img1,128,255,CV_THRESH_BINARY | CV_THRESH_OTSU);
 19: 	 cvSaveImage("thres.png",img1,0);
 20: 	display(img1);
 21: 	cvSetZero(img2);
 23: 	CBlobResult blobs;
 24: 	blobs=CBlobResult(img1,NULL,0);
 26: 	blobs.Filter(blobs,B_INCLUDE,CBlobGetArea(),B_INSIDE,600,10000);
 28: 	cvConvertImage(img, img_temp,0);
 29: 	cvCopy(img_temp,cc_color);
 30: 	CBlob *currentBlob;
 33: 	int i;
 34: 	for (i = 0; i < blobs.GetNumBlobs(); i++ )
 35: 	{
 36: 			currentBlob = blobs.GetBlob(i);
 38: 			//currentBlob->FillBlob(img, CV_RGB(255,100,0));
 39: 			int s_x=currentBlob->GetBoundingBox().x;
 40: 			int s_y=currentBlob->GetBoundingBox().y;
 41: 			float width=currentBlob->GetBoundingBox().width;
 42: 			float height=currentBlob->GetBoundingBox().height;
 43: 			float ratio= width/height;
 44: 			if(ratio>2 && ratio<5.4)
 45: 			cvRectangle(img, cvPoint(s_x,s_y),cvPoint(s_x+width,s_y+height),CV_RGB(255,0,0),2,8,0);
 46: 			//display(img);
 47: 	}
 48: 	display(img);

4 responses to “License Plate detection with Connected Component Analysis

  1. Hey great tutorial. when i try the first display image nothing shows up. after i load the image in img i’m guessing cvCovert passing that image to img1? or how do i have to pass the image to img1?

  2. Hey Great tutorial. When i display img1 nothing shows up. is cvConvert suppose to pass along the image from img to img1? if not how do i pass along the loaded image to img1?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s