Ask Your Question

Recognizing some text (but not OCR) in opencv 2.4.9

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

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



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 -0600 )edit

1 answer

Sort by » oldest newest most voted

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

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

     vector<int> vecUpY;
     vector<int> vecDownY;



 //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);
        for (int i=0;i<tmp.rows;i++){
            Mat data = tmp.row(i);
            int itmp = countNonZero(data);
    if (vdate.size()<=gap)
    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)
        if (vdate[i-1]>0 && vdate[i] == 0)

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

Question Tools

1 follower


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

Seen: 1,075 times

Last updated: Mar 14 '17