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

India Pakistan Friendship…is it at all possible?


I had a very different opinion regarding this until few days back the word cup started.I used to think that basically Indian people and Pakistan people have the same culture and hence it makes sense for the country to be friends.I know there are ample reasons to hate Pakistan ,but still there was a possibility that someday we could achieve the so called “Friendship” .

So what was it that changed my opinion ?.It’s the Indian Pakistan Match during the world cup.The 5 or 7 days before the match ….I received hundreds of Hate messages about Pakistan and innumerable number of posts in which Pakistani people and cricketers has been called dogs and terrorists.I understand “Cricket”-ing emotions run high in the country,but that is no reason to insult another Cricket team or the common people of Pakistan.This emotions were not a result of Pakistan being just a Cricketing opponent.It was because of the long standing hatred towards Pakistanis.It was because of the cricketing event that the real emotions came out.

The Pakistani cricket team was abused in all possible ways by the Indians.They were called “Dogs” and branded as “terrorists”

Though IMHO ,it isn’t correct to “abhor” and “hate” Pakistanis for what their Government has been doing.But the post is not about my opinion about what Indians should be doing.It is about whether there is any hope that the two states can have any sort of positive relation.

Here ,I must point out that it wasn’t only India,Pakistan too was passing derogatory comments towards India.Similar to India ,they criticize Indian Government as well.There is nothing on being done (in either of the country) to ease the tension between the “common man”

Just to get the feel about what the general population is thinking ….. here are some of the responses.

I have asked one question “Is any sort of friendship or positive relations possible,not from the diplomatic point of view but  from the Common man’s point of view ?”

Priyanko Das,Age 22

I don’t think we will able to consider people from Pakistan the same way we consider people from Srilanka or Bangladesh.Though this may not be right,but the atrocities committed by Pakistan,has been the main hindrance.Another thing I must mention is the negativity that is being spread by the Media (On both sides of the border) and the surroundings.

Kaustav Mittra,Age 26

My personal opinion is “No” ,we can never be friends with Pakistan.Pakistan reminds us of the terrorism that has been crippling India for quite sometime.It has been a safe haven for terrorists including Osama Bin Laden.One could definitely say that the ways of the government does not reflect the mentality of people.But then again it is the people that choose the government,and in the end of the day we must admit they do “Hate” India .Pakistan has had their share of derogatory comments towards India too.

Like any Indian I also feel that “good relations” are not possible in the immediate future.But I really hope that someday this comes true ,for the best interests of both the countries.