问题
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