Ask Your Question

Revision history [back]

License plate position through green rectangles

strong text Hello to everyone! I've a problem to solve: I must detect the license plate position of a car. I know that this problem is already treated into other forums, but my case is different. I've detected the license plate into an image with Opencv 2.4.9 in language C++ by ratio width / height and followings processing on image. Here is the c++ code: #include "opencv2/highgui/highgui.hpp" #include "iostream" #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

int main(int argc, char** argv){
    const char *absolutePath;
    absolutePath = "C:\\Users\\liall_000\\Documents\\visual studio 2013\\Projects\\ESAME_SMU\\ESAME_SMU\\1.jpg";//Change this path according to your image file path  

    /************************ 1  Load the image from file     ****************************/
    Mat originalImage, resizedImage;
    originalImage = cvLoadImage(absolutePath, 1);
    if (originalImage.empty()){
        cout << "Cannot load original image!" << endl;
        return -1;
    }

    Size size(400, 300); // 2 Rectangular size
    resize(originalImage, resizedImage, size);//resize image
    //imshow("Original resized image", resizedImage);

    /************************   3 Convert the image to grayscale     ****************************/
    Mat grayImage;
    cvtColor(resizedImage, grayImage, CV_BGR2GRAY);
    //imshow("Gray Image", grayImage);

    /************************4   Threshold image  ****************************/
    Mat thresholdImage;
    threshold(grayImage, thresholdImage, 100, 255, THRESH_BINARY);
    //imshow("Threshold", thresholdImage);

    /************************5   Morphological image  ****************************/
    Mat morp;
    // Create a structuring element
    int erosion_size = 1; //fra 1 e 2 รจ ok!
    Mat element = getStructuringElement(cv::MORPH_CROSS,
        //      cv::Size(2 * erosion_size + 1, 2 * erosion_size + 1),

        cv::Size(3, 3),
        cv::Point(erosion_size, erosion_size));

    // Apply erosion or dilation on the image
    erode(thresholdImage, morp, element);  
    //dilate(image,dst,element);
    //imshow("erosion window", morp);


    /************************    Draw   contours  ****************************/
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;

    findContours(thresholdImage, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    vector<vector<Point> > contours_poly(contours.size());
    vector<Rect> boundRect(contours.size());
    vector<Point2f>center(contours.size());

    for (int i = 0; i < contours.size(); i++)
    {
        approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
        boundRect[i] = boundingRect(Mat(contours_poly[i]));
    }

    Mat drawing = Mat::zeros(thresholdImage.size(), CV_8UC3);
    Mat rectangleImage = resizedImage.clone();
    Mat mask_image(drawing.size(), CV_8U, Scalar(0));
    Mat mask_image2(drawing.size(), CV_8U, Scalar(0));
    Mat masked, masked2;

    for (int i = 0; i < contours.size(); i++)
    {
        //drawContours(drawing, contours_poly, i, CV_RGB(255, 0, 0), 1, 8, vector<Vec4i>(), 0, Point());
        rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), CV_RGB(0, 255, 0), 1, 8, 0); 
        int s_x = boundRect[i].x;
        int s_y = boundRect[i].y;
        float width = boundRect[i].width;
        float height = boundRect[i].height;     

        if (width != 0 && height != 0)
        {
            //if (width>height && width / height>2.5 && width / height<5 && width*height> 1000 && width*height> 2000)
            if (width>height && width / height>3.7 && width / height<5.6)
            {
                rectangle(drawing, Point(s_x, s_y), cvPoint(s_x + width, s_y + height), CV_RGB(0, 0, 255), 2, 8, 0);
                drawContours(mask_image, contours, i, Scalar(255), CV_FILLED);  
                cout << contours[i] << endl;

                imshow("mask_image", mask_image);
                drawing.copyTo(drawing, mask_image);
                rectangle(mask_image, Point(s_x, s_y), cvPoint(s_x + width, s_y + height), CV_RGB(0, 0, 255), 2, 8, 0);         
                drawing.copyTo(masked, mask_image);
                imshow("masked", masked);

                rectangle(rectangleImage, Point(s_x, s_y), cvPoint(s_x + width, s_y + height), CV_RGB(0, 0, 255), 2, 8, 0);

            /*  Mat cutGreen, newGreen;
                inRange(masked, Scalar(0, 255, 0), Scalar(0, 255, 0), cutGreen);//individua i verdi
                imshow("cutGreen", cutGreen);*/         

            }
        }
    }
    imshow("detected rectangle", rectangleImage);

    waitKey(0);
    return 0;
}

This is the mask image: C:\fakepath\mask.PNG And this is the 'masked ' image (please don't see names! xd) image description

In this second image we can see a green section, that represents the license plate cyphers. I'm interesting to extract the position of the blue rectangle that contains these green little rectangles and to display it into the final image, as the following:

image description

Here, in fact, there are more and more rectangles displayed and also, obviously the right one, but the majority of them not contain the license plate. Please help me, I must to isolate the green rectangles and it blue one, in the final image! Thanks a lot!!!