rekrut
rekrut

Reputation: 155

Search contours on the image

I'm trying solve the recognition problem with a help OpenCV library for C++.

I have a some text(below) and i want to separate each symbol in this text using by cvFindContours(...) function. After, I want to send each separated symbol on the input of neural network for recognition it. It's all ok. I will can get all contours in my image and i can drawn it on my image with a help cvDrawContours(...) function(below). But cvFindContours(...) returns unordered sequence(pointer on the first contour in this sequence) where contains all the found contours. For my task order is very important.

CVAPI(int)  cvFindContours( CvArr* image, CvMemStorage* storage, CvSeq** first_contour,
                            int header_size CV_DEFAULT(sizeof(CvContour)),
                            int mode CV_DEFAULT(CV_RETR_LIST),
                            int method CV_DEFAULT(CV_CHAIN_APPROX_SIMPLE),
                            CvPoint offset CV_DEFAULT(cvPoint(0,0)));

-image- source image

-storage- for storing where contains contours

-first_contour- pointer to the first contour in the storage

-mode- mode of search (I use the CV_RETR_EXTERNAL for search external contours)

-method- method of approximation (I'm using the CV_CHAIN_APPROX_SIMPLE by default)

Source image

Source image with drawn a bound rectangles

How can I make the cvFindContours(...) function that returns the contours in the order in which they in the picture? Is it possible?

Thanks!

Upvotes: 2

Views: 203

Answers (2)

Jeru Luke
Jeru Luke

Reputation: 21203

You have found bounding rectangles for all the contours present in your image. Instead of going about with the left-most point approach, you can sort your contours based on the centroid of each contour, which is more robust since your approach is being for text.

THIS ANSWER from the OpenCV community might help provide a start

Upvotes: 1

Soltius
Soltius

Reputation: 2261

You can't directly force findContours to yield contours in a certain order (I mean there is no parameter to tune this in the function call).

To sort your contours in a "read text" order, you could do a loop which goes through all your contours and retrieves for each contour the top-leftest point, either by directly going through all points in each contour object, or by using a boundingbox (see minAreaRect for example).
Once you have all these points, sort them from left to right and bottom to top (some adjustments will probably have to be made, like detecting all contours starting within a range of heights to be all part of the same text line)

Upvotes: 2

Related Questions