Ahsan
Ahsan

Reputation: 2982

OCRTesseract in OpenCV3 not working properly for relatively simple image

Edit:: made some code change and at least am not getting the empty page error. Update code below.

I am using OpenCV3 and Tesseract and have done some processing on a relatively simple image and I was expecting the ocr part to work smoothly but it's not.

Image:Image to run OCR on

Code:

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

Output:

I

Any idea what's going on?

Thanks.

Upvotes: 1

Views: 1292

Answers (1)

Janco de Vries
Janco de Vries

Reputation: 204

Removing the blobs connected to the borders will help improve tesseract. So we take your image:

example You want to invert the image so the character is white and background black:

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

Then we want to remove the blobs connected to the borders using the floodFill method

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

This will produce the following image:

output image

Running tesseract on this image will result in 'T' instead of 'I' Hope this helps you solving your problem. :)

Upvotes: 1

Related Questions