OpenCV3 中的 OCRTesseract 对于相对简单的图像无法正常工作

OCRTesseract in OpenCV3 not working properly for relatively simple image

Edit:: 更改了一些代码,至少我没有收到空页错误。更新下面的代码。

我正在使用 OpenCV3 和 Tesseract 并对相对简单的图像进行了一些处理,我希望 ocr 部分能够顺利工作,但事实并非如此。

图片:

代码:

Ptr<cv::text::OCRTesseract> ocr =
    cv::text::OCRTesseract::create(NULL /*datapath*/, "eng" /*lang*/, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /*whitelist*/, 2 /*oem*/, 10 /*psmode*/);

string output;
vector<Rect>   boxes;
vector<string> words;
vector<float>  confidences;
ocr->run(gray3, output, &boxes, &words, &confidences, cv::text::OCR_LEVEL_WORD);

输出:

I

知道发生了什么事吗?

谢谢。

移除连接到边界的斑点将有助于改进 tesseract。所以我们拍下你的照片:

您想反转图像,使字符为白色,背景为黑色:

    Mat img = imread("T2.png"); // reading the example image
    cvtColor(img, img, CV_RGB2GRAY);
    bitwise_not(img, img); // invert the image

然后我们要使用floodFill方法

移除连接到边框的blob
// Remove blobs attached on corners
    uchar white(255);
    // do top and bottom row
    for (int y = 0; y < img.rows; y += img.rows - 1)
    {
        uchar* row = img.ptr<uchar>(y);
        for (int x = 0; x < img.cols; ++x)
        {
            if (row[x] == white)
            {
                floodFill(img, Point(x, y), Scalar(0), (Rect*)0, Scalar(), Scalar(200));
            }
        }
    }
    // fix left and right sides
    for (int y = 0; y < img.rows; ++y)
    {
        uchar* row = img.ptr<uchar>(y);
        for (int x = 0; x < img.cols; x += img.cols - 1)
        {
            if (row[x] == white)
            {
                floodFill(img, Point(x, y), Scalar(0), (Rect*)0, Scalar(), Scalar(200));
            }
        }
    }

这将产生以下图像:

运行 此图像的 tesseract 将导致 'T' 而不是 'I' 希望这可以帮助您解决问题。 :)