提高车牌识别率

beg*_*ner 6 c++ opencv

我正在研究基于车牌识别的学校项目.我在简单的电影上测试它:一辆车,静态相机等.

这是它的样子:

在此输入图像描述

我的第一步是在这个框架上只找到汽车(我认为它对于更"困难"的视频会有所帮助): 在此输入图像描述

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

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);
        } 
Run Code Online (Sandbox Code Playgroud)

结果你可以看到第二张图片.

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

  1. 鉴于亮度的急剧变化,通过直方图均衡:

在此输入图像描述

  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);
    
    Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在此输入图像描述

  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]);
        }
    
    Run Code Online (Sandbox Code Playgroud)

    在此输入图像描述

也许你有一些想法如何改善结果?我尝试更改一些参数,但它仍然不能很好地工作.

感谢帮助

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

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

      .\vcpkg install tesseract:x64-windows-static
    
    Run Code Online (Sandbox Code Playgroud)

    在安装结束时,我收到一些错误: 在此输入图像描述

  3. 但当我检查它似乎很好: 在此输入图像描述

  4. 集成安装后我得到了这个: 在此输入图像描述

  5. 将lib添加到项目中: 在此输入图像描述

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

在此输入图像描述

解决了:

更改

.\vcpkg install tesseract:x64-windows-static
Run Code Online (Sandbox Code Playgroud)

.\vcpkg install tesseract:x64-windows
Run Code Online (Sandbox Code Playgroud)

它很好用.

sec*_*pur 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;
}
Run Code Online (Sandbox Code Playgroud)