License plate position through green rectangles

asked 2016-02-23 10:16:35 -0600

LMia gravatar image

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 ...
(more)
edit retag flag offensive close merge delete

Comments

a ready to use solution for "Automatic License Plate Recognition" is OpenALPR

sturkmen gravatar imagesturkmen ( 2016-02-23 10:52:16 -0600 )edit

Thanks a lot sturkmen! I'm new in opencv and libraries, Can you explain how to use it? I want to understand the code in order to reproduce it in my program through commands. Thanks a lot.

LMia gravatar imageLMia ( 2016-02-24 01:33:09 -0600 )edit