Ask Your Question
0

How to crop digits automatically in sequence for recognition?

asked 2016-11-17 23:01:16 -0600

KirtiSwagat gravatar image

updated 2016-11-22 05:12:25 -0600

berak gravatar image

I am trying to implement Automatic Digit Recognition. For this I am using C++ with OpenCv. I have successfully trained SVM classifier ,successfully can able to crop the digits from the group of digits. Manually it works fine. But my problem is when i am trying to extract the individual digits the output is coming in random manner , not maintaining the sequence. The below attached image is the output in which bounding box successfully drawn on individual digits and while extracting the individual digits the sequences should be i.e. for the number 0240408:-

1st Position= 0, 
2nd Position=2, 
3rd Position=4, 
4th Position=0, 
5th Position=4, 
6th Position= 0, 
7th Position=8.

But I am not getting the same, rather i am getting

1st Position= 8, 
2nd Position= 0, 
3rd Position= 4, 
4th Position= 0, 
5th Position= 0, 
6th Position= 4, 
7th Position= 2.

Due to which the output number becomes= 8040042. Please suggest any techniques to overcome this problem.

image description

//here I am finding the bounding boxes

for( size_t i = 0; i < contours.size(); i++ )
  {
    approxPolyDP( Mat(contours[i]), contours_poly[i], 5, false );
    boundRect[i] = boundingRect( Mat(contours_poly[i]) );
    rectangle( grayImage, boundRect[i].tl(), boundRect[i].br(), Scalar(0,255,0),2, 8,0 );

  }

//Here I am cropping the bounded region and saving it with the a number.jpg

 for(int i=0;i<kirti;i++)
  {
    if(boundRect[i].width>50 && boundRect[i].height>50)
    {
    ROI[i]= grayImage(boundRect[i]);
    string name= format("%d.jpg",i);
    imwrite(name,ROI[i]);
    cout<<boundRect[i].height<<endl;
    }
  }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-11-22 05:21:24 -0600

berak gravatar image

you can sort the vector<vector<Point>> contours by the x-coord of the boundingRect

// from left to right
bool sort_by_x(const vector<Point> &ca, const vector<Point> &cb) {
    return boundingRect(ca).x < boundingRect(cb).x;
}

sort(contours.begin(), contours.end(), sort_by_x);
edit flag offensive delete link more

Comments

1

Thank You @berak.. Now It Works Fine

KirtiSwagat gravatar imageKirtiSwagat ( 2016-11-22 05:38:54 -0600 )edit

Can u please write the above code in python?

rrr gravatar imagerrr ( 2016-11-22 14:14:20 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-11-17 23:01:16 -0600

Seen: 1,697 times

Last updated: Nov 22 '16