Search code examples
c++opencvocrtesseract

Improve OCR result


I am working on project which base on detecton of license plates from video.

This is how it looks like: enter image description here

My problem starts when I want to use OCR on the car plate. I was testing it on some pictures and it works really nice. Here is some example: enter image description here

But when I put my detected plate the result is very bad:

enter image description here

So I want to ask you if you have some advise for me how improve OCR results?

Here is how I get binary image:

cv::Mat equalized;
    cv::equalizeHist(gray, equalized);
    cv::imshow("Hist. Eq.", equalized);

    /* Bilateral filter helps to improve the segmentation process */

    cv::Mat blur;
    cv::bilateralFilter(equalized, blur, 9, 75, 75);
    cv::imshow("Filter", blur);


    /* Threshold to binarize the image */

    cv::Mat thres;
    cv::adaptiveThreshold(blur, thres, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY, 15, 2); //15, 2
    cv::imshow("Threshold", thres);

Maybe some more filters? If it will be helpful I can put more code.

My first idea was getting a little bit bigger rect with my plate.

Here is how I get vector with my rects:

std::vector< std::vector< cv::Point> > LP_contours;
    cv::findContours(img_threshold, LP_contours, 0, 1);
    std::vector<std::vector<cv::Point> > contours_poly(LP_contours.size());
    for (int ii = 0; ii < LP_contours.size(); ii++)
        if (LP_contours[ii].size() > 100 && contourArea(LP_contours[ii]) > 3000 && contourArea(LP_contours[ii]) < 10000) 
        {
            cv::approxPolyDP(cv::Mat(LP_contours[ii]), contours_poly[ii], 3, true);
            cv::Rect appRect(boundingRect(cv::Mat(contours_poly[ii])));

            if (appRect.width > appRect.height && appRect.width>160 && appRect.width < 190 && appRect.height>40 && appRect.height < 60)
                boundRect.push_back(appRect);
        }

I want change size of appRect by simple code:

appRect.height += 10;
appRect.width += 10;

But it doesn't work. I am new in openCV and I have problem with this kind of staff. Have you some advise how to get bigger rect?

Thank you for all your time and help.


Solution

  • Here's a simplified snippet how you can manually change the boundRect parameters to achieve the desired ROI in openCV :

    int main()
    {
        cv::Mat image = cv::imread("car.jpg",0);
    
        cv::Rect boundRect{ 200,164,140,25 };
        cv::Mat cropped_image = image(boundRect);
    
        cv::namedWindow("Image");
        cv::imshow("Image", image);
    
        cv::namedWindow("Original cropped Image");
        cv::imshow("Original cropped Image", cropped_image);
    
        cv::Rect new_boundRect = boundRect;
        new_boundRect.x += 10;
        new_boundRect.y += 2;
        new_boundRect.width -= 10;
        new_boundRect.height -= 10;
    
        cv::Mat new_cropped_image = image(new_boundRect);
        cv::namedWindow("New cropped Image");
        cv::imshow("New cropped Image", new_cropped_image);
    
        cv::waitKey();
        return 0;
    }
    

    Picture:

    enter image description here