Text Recognition [closed]

asked 2016-08-31 00:52:10 -0500

Junglee gravatar image

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;

    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


return 0;

Any suggestion will be helpful.

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-09-05 04:55:42.253330



somehow, i do not understand your question. why don't you just try with the end_to_end_recognition example (which already comes with its own text detection) ?

berak gravatar imageberak ( 2016-08-31 01:03:19 -0500 )edit

@berak , i tried it first and the results were very bad, only few words were recognized and i want to recognize atleast 70% of text in input image. input image is white background with black text. Here's the image FYR which is output of end_to_end Reco. this is not exact image i want to recognize but its similar, results of my images are even poor.

Junglee gravatar imageJunglee ( 2016-08-31 01:36:51 -0500 )edit