Give contours from right to left in Opencv c++

淺唱寂寞╮ 提交于 2019-12-11 04:21:38

问题


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++?


回答1:


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);
    }
};


来源:https://stackoverflow.com/questions/43954947/give-contours-from-right-to-left-in-opencv-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!