提高车牌识别度

时间:2018-08-20 15:22:28

标签: c++ opencv

我正在从事基于车牌识别的学校项目。我正在简单的电影上测试它:一辆汽车,静态照相机等。

这是什么样子

enter image description here

我的第一步是在此框架上只找到汽车(我认为这对于拍摄更多“困难”的视频会有所帮助): enter image description here

然后我搜索车牌。这是我的代码:

std::vector<cv::Rect> boundRect;
    cv::Mat img_gray, img_sobel, img_threshold, element;
    cvtColor(detectedMats[i], img_gray, CV_BGR2GRAY);
    cv::Sobel(img_gray, img_sobel, CV_8U, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT);
    cv::threshold(img_sobel, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY);
    element = getStructuringElement(cv::MORPH_RECT, cv::Size(30, 30));
    //element = getStructuringElement(cv::MORPH_RECT, cv::Size(17, 3) ); 
    cv::morphologyEx(img_threshold, img_threshold, CV_MOP_CLOSE, element);
    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) //można się pobawić parametrami
        {
            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)
                boundRect.push_back(appRect);
        } 

结果您可以在第二张图片中看到。

然后尝试获得良好的检测板轮廓。我做了几个步骤。

  1. 通过直方图均衡化,使亮度发生急剧变化:

enter image description here

  1. 使用过滤器和阈值:

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

enter image description here

enter image description here

  1. 最后我找到了轮廓,但轮廓并不是很好。数字有点模糊:

        std::vector<std::vector<cv::Point> > contours;
        cv::findContours(thres, contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
        //cv::findContours(thres, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
    
        double min_area = 50;
        double max_area = 2000;
        std::vector<std::vector<cv::Point> > good_contours;
        for (size_t i = 0; i < contours.size(); i++)
        {
            double area = cv::contourArea(contours[i]);
            if (area > min_area && area < max_area)
                good_contours.push_back(contours[i]);
        }
    

    enter image description here

也许您对提高结果有一些想法?我尝试更改一些参数,但仍然无法正常工作。

感谢帮助

---------------------------编辑:tesseract安装---------------- --------

  1. 我安装了vcpkg。
  2. 使用

      .\vcpkg install tesseract:x64-windows-static
    

    在安装结束时出现一些错误:    enter image description here

  3. 但是当我检查时似乎很好: enter image description here

  4. 集成安装后,我得到以下信息: enter image description here

  5. 将lib添加到项目: enter image description here

所以看起来不错,但是当我尝试运行示例时,VS看不到库:

enter image description here

已解决:

更改

.\vcpkg install tesseract:x64-windows-static

进入

.\vcpkg install tesseract:x64-windows

,效果很好。

1 个答案:

答案 0 :(得分:1)

使用tesseract OCR检测文本。成功安装tesseract之后,在其他依赖项中添加tesseract305.lib和leptonica-1.74.4.lib。使用以下代码(来自教程):

#include "stdafx.h"
#include "winsock2.h"

#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>

#pragma comment(lib, "ws2_32.lib")

int main()
{
    char *outText;

    tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();

    // Initialize tesseract-ocr with English, without specifying tessdata path
    if (api->Init(NULL, "eng")) {
        fprintf(stderr, "Could not initialize tesseract.\n");
        exit(1);
    }

    // Open input image with leptonica library
    Pix *image = pixRead("test.tif");
    api->SetImage(image);
    // Get OCR result
    outText = api->GetUTF8Text();
    printf("OCR output:\n%s", outText);

    // Destroy used object and release memory
    api->End();
    delete[] outText;
    pixDestroy(&image);

    return 0;
}