Ask Your Question
2

Recognizing some text (but not OCR) in opencv 2.4.9

asked 2017-03-13 03:35:32 -0500

Using opencv, I identify interesting contours in images, individually rotate them back to angle zero, and crop them to obtain exactly what is needed. These images are then saved to disk or inserted into a database, etc. So far, so good.

The problem is rotating them back to zero is not necessary correct. From there, I may need to rotate them another 90, 180, or 270 degrees to make sure the final saved images are right-side-up.

Each image contains English-language text. I don't need to perform OCR to "read" the text, but I was thinking that maybe I could leverage this and attempt to detect a few letters. If a few letters were correctly detected, then I'd know the image was rotated correctly. Otherwise I'd rotate another 90° and attempt again.

Can this text recognition be done in OpenCV? I see on the web that v3 has some new OCR capabilities, but on Ubuntu we're still on v2.4.9 of OpenCV.

Here is an example image I've captured, rotated to zero, and cropped, but which obviously still needs to be rotated another 90° CCW:

image description

edit retag flag offensive close merge delete

Comments

2

For OCR, it is usually Tesseract OCR that is used in free projects.

It should be possible to detect 90° CCW and 90° CW assuming the text is left-aligned and some "statistics" but I think just running 4 passes of OCR should be good and easier.

Eduardo gravatar imageEduardo ( 2017-03-13 05:58:47 -0500 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2017-03-14 00:43:43 -0500

 int main(int argc, char* argv[])
{
     Mat dst;
     Mat src = imread("e:/sandbox/ocrtext.jpg",0);
     threshold(src,src,0,255,THRESH_OTSU);
     bitwise_not(src,src);
     // cut ROI
     dst = src (Rect(10,10,src.cols - 20,src.rows -20));
     //projection
     vector<int> vecUpX;
     vector<int> vecDownX;
     projection2(dst,vecUpX,vecDownX,DIRECTION_X,3);

     vector<int> vecUpY;
     vector<int> vecDownY;
     projection2(dst,vecUpY,vecDownY,DIRECTION_Y,3);

     waitKey();

}



 //get projection of Mat
void projection2(Mat src,vector<int>& vup,vector<int>& vdown,int direction,int gap){
    Mat tmp = src.clone();
    vector<int> vdate;
    if (DIRECTION_X == direction){
        for (int i=0;i<tmp.cols;i++){
            Mat data = tmp.col(i);
            int itmp = countNonZero(data);
            vdate.push_back(itmp);
        }
    }else{
        for (int i=0;i<tmp.rows;i++){
            Mat data = tmp.row(i);
            int itmp = countNonZero(data);
            vdate.push_back(itmp);
        }
    }
    if (vdate.size()<=gap)
        return;
    for (int i=0;i<vdate.size()-gap;i++){
        if (vdate[i]>0 && vdate[i+gap]>0){
            for (int j=i;j<i+gap;j++){
                vdate[j] = 1;
            }
            i = i+gap-1;
        }
    }
    //up and down edge
    for (int i=1;i<vdate.size();i++){
        if (vdate[i-1] == 0 && vdate[i]>0)
            vup.push_back(i);
        if (vdate[i-1]>0 && vdate[i] == 0)
            vdown.push_back(i);
    }
}

You can get the direction by simplely "projection method".in the code above,the size of vecUpX (and vecDownX) is "13" and the size of vecUpY(and vecDownY) is 1,SO the picture is in DIRECTION Y.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-03-13 03:35:32 -0500

Seen: 543 times

Last updated: Mar 14 '17