Sorting contours from left to right and top to bottom
I need help to sort contours from left to right and top to bottom. Is there any easy code example?
I need help to sort contours from left to right and top to bottom. Is there any easy code example?
since it's std::vectors, all we need is a fitting 'less' operator, then we can just sort() it.
// mock data for demonstration:
vector<vector<Point>> contours(4);
contours[0].push_back(Point(3,111));
contours[0].push_back(Point(3,121));
contours[1].push_back(Point(81,13));
contours[1].push_back(Point(84,14));
contours[2].push_back(Point(33,55));
contours[2].push_back(Point(36,57));
contours[3].push_back(Point(133,25));
contours[3].push_back(Point(136,27));
for ( int i=0; i<contours.size(); i++ )
cerr << Mat(contours[i]) << endl;
struct contour_sorter // 'less' for contours
{
bool operator ()( const vector<Point>& a, const vector<Point> & b )
{
Rect ra(boundingRect(a));
Rect rb(boundingRect(b));
// scale factor for y should be larger than img.width
return ( (ra.x + 1000*ra.y) < (rb.x + 1000*rb.y) );
}
};
// apply it to the contours:
std::sort(contours.begin(), contours.end(), contour_sorter());
for ( int i=0; i<contours.size(); i++ )
cerr << Mat(contours[i]) << endl;
[3, 111; 3, 121]
[81, 13; 84, 14]
[33, 55; 36, 57]
[133, 25; 136, 27]
[81, 13; 84, 14]
[133, 25; 136, 27]
[33, 55; 36, 57]
[3, 111; 3, 121]
Asked: 2014-04-10 04:11:14 -0600
Seen: 14,823 times
Last updated: Apr 10 '14
intelligent sort by location countors
Area of a single pixel object in OpenCV
Which is more efficient, use contourArea() or count number of ROI non-zero pixels?
Tricky image segmentation in Python
How to extract only top-level contours?
MSER Sample in OpenCV 2.4.2 on Visual Studio 2012