Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the plate rectangle in a given picture

This is my original picture and i want to find the plate in order to search for the number plate in this rectangle instead of searching in whole picture
original image test_1.jpg:

enter image description here

using the following code in javacv:

IplImage originalImage = cvLoadImage("test_1.jpg");
IplImage resultImage = IplImage.create(originalImage.width(),
                originalImage.height(), IPL_DEPTH_8U, 1);
cvCvtColor(originalImage, resultImage, CV_BGR2GRAY);
cvAdaptiveThreshold(resultImage, resultImage, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV, 7, 7);
cvSaveImage("test_2.jpg", resultImage);

The resultant picture is test_2.jpg looks like this:

enter image description here

and adding this code by giving thresholdImg the resultImg

static void findLines(IplImage thresholdImg)
{
    IplImage dst;
    IplImage colorDst;
    dst = cvCreateImage(cvGetSize(thresholdImg), thresholdImg.depth(), 1);
    colorDst = cvCreateImage(cvGetSize(thresholdImg), thresholdImg.depth(), 3);    
    cvCanny(thresholdImg, dst, 100, 200, 3);
    CvSeq lines = new CvSeq();
    CvMemStorage storage = cvCreateMemStorage(100000); 
    cvSaveImage("test_3.jpg", dst);
}

The resultant picture test_3.jpg :

enter image description here

Is there any picture of what i generated can be used for continuing my code in order to find the rectangle containing the plate in the image

like image 980
HADEV Avatar asked Nov 28 '13 23:11

HADEV


1 Answers

You're going in the right direction.

  1. You will want to perform a smooth/blur of some kind before thresholding the image, in order to eliminate unwanted noise

  2. Apply threshold to the image like you are doing now

  3. Use the openCv library's dilate function to slightly dilate your detected edges. This is useful because once the license plate's characters are dilated, they will sort of fill the rectangle in which they are contained

  4. openCV has a function called cvRectangle that searches for rectanglular shapes in the image. There's plenty of documenation online to assist you in using it

  5. Finally, you'll want to filter the rectangular shapes found that are license plate candidates based on ratio (width / height) and area (width * height). For example, portuguese license plates, at least in my test images, had a ratio between 2 and 3.5 and an area of around 5000-7000 pixels. Obviously this depends on the license plate's shape, the image size, etc..

I assure you this approach works. I've used it myself and it detects the license plate area in 99.5% of the vehicle photos I had for tests.

One thing you might want to do is use a Sobel filter (also available in the opencv library) before the threshold, if your pictures are too bright. That implies a change only in step 2, the others stay the same.

Hope I was of help :)

like image 124
Rafael Matos Avatar answered Oct 24 '22 04:10

Rafael Matos