Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Text Recognition

I have done half of Text detection using below Program, so next part is the text recognition. I would like to know how to integrate end_to_end_recognition with the below code???

#include <opencv2/core/core.hpp>
#include <opencv2/text/ocr.hpp>
#include <opencv2/text.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;
using namespace cv::text;

#define INPUT_FILE              "Example3.png"
#define OUTPUT_FOLDER_PATH      string("C:\\Users\\<UserEx>\\Desktop\\Reco_Pics\\")

int main(int argc, char* argv[])
{
#pragma region Text Detection(OpenCV)

Mat large = imread(INPUT_FILE);
imshow("Original Image", large);
Mat rgb;
// downsample and use it for processing -- Grayscale
//pyrDown(large, rgb);
Mat small;
//cvtColor(rgb, small, CV_BGR2GRAY);
cvtColor(large, small, CV_BGR2GRAY);
imshow("BGR2GRAY", small);

// morphological gradient -- Morph
Mat grad;
Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(2, 2));
morphologyEx(small, grad, MORPH_GRADIENT, morphKernel);
imshow("MORPH_GRADIENT", grad);

// binarize -- Threshold
Mat bw;
threshold(grad, bw, 255.0, 255.0, THRESH_BINARY | THRESH_OTSU);
imshow("Threshold", bw);

// connect horizontally oriented regions -- dilate
Mat connected;
morphKernel = getStructuringElement(MORPH_RECT, Size(9, 1));
morphologyEx(bw, connected, MORPH_CLOSE, morphKernel);
imshow("MORPH_CLOSE", connected);

// find contours
Mat mask = Mat::zeros(bw.size(), CV_8UC1);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;

try
{
    findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
    imshow("Contours", connected);
}
catch (Exception ex)
{
    cout << ex.what();
}

// filter contours
for (int idx = 0; idx >= 0; idx = hierarchy[idx][0])
{
    Rect rect = boundingRect(contours[idx]);
    Mat maskROI(mask, rect);
    maskROI = Scalar(0, 0, 0);
    // fill the contour
    drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);
    // ratio of non-zero pixels in the filled region
    double r = (double)countNonZero(maskROI) / (rect.width*rect.height);

    if (r > .05 /* assume at least 45% of the area is filled if it contains text */
        &&
        (rect.height > 8 && rect.width > 8) /* constraints on region size */
        /* these two conditions alone are not very robust. better to use something
        like the number of significant peaks in a horizontal projection as a third condition */
        )
    {
        //rectangle(rgb, rect, Scalar(0, 255, 0), 2);
        rectangle(large, rect, Scalar(0, 255, 0), 2);
    }
}
//imshow("Final_Output", rgb);
imshow("Final_Output", large);
//imwrite(OUTPUT_FOLDER_PATH + string("_Output.jpg"), rgb);

#pragma endregion

waitKey();

return 0;
}

Any suggestion will be helpful.