narges
narges

Reputation: 751

Give contours from right to left in Opencv c++

I want to print center of contours with "findContours" function from right to left contours, in opencv c++. when i print centers, i see contours Not have particular order. Is there a way to implement an order to contours in opencv c++?

Upvotes: 2

Views: 1414

Answers (1)

z.gomar
z.gomar

Reputation: 370

You can use:

// Find all the contours
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

findContours(Image, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
sort(contours.begin(), contours.end(), Right_Left_contour_sorter());

and " Right_Left_contour_sorter" function is :

struct Right_Left_contour_sorter // 'less' for contours
{
    bool operator ()( const vector<Point>& a, const vector<Point> & b )
    {
        Rect ra(boundingRect(a));
        Rect rb(boundingRect(b));
        return (ra.x > rb.x);
    }
};

Upvotes: 4

Related Questions