I'm trying to sort vector of contours point in decending order but whenever I used:
sort(contours.begin(), contours.end() , greater<>()),
It is poping out an error.
How to sort vector that contains contour points in decending order?
Mat img;
im.copyTo(img);
vector<vector<Point>>contours;
vector<Vec4i>hierachy;
findContours(Canny_img , contours , hierachy , RETR_EXTERNAL , CHAIN_APPROX_SIMPLE);
sort(contours.begin(), contours.end() , greater<Point>()); //This line getting error
for(int i = 0; i < contours.size(); i++)
{
drawContours(img , contours , i , Scalar(0,255,0) , 2);
imshow("Contour Image" , img);
waitKey(0);
}
contours
is not a vector
of Point
s. It is a vector
of vector
s of Point
s. I.e. each element is in itself a vector
of Point
s.
If you want to sort such a vector
of vector
s, you should supply some kind of a "greater" function.
One of the convenient ways would be using a lambda function:
std::sort(contours.begin(),
contours.end(),
[](std::vector<cv::Point> const & v1, std::vector<cv::Point> const & v2)
{ return true; });
As you can see the lambda simply returns true. This is just a stub. Instead you should implement the criterion you want for sorting.
You have to determine when you consider one element (which is a vector
of Points
) to be greater than another (also a vector
of Point
s).
It depends on what you want to actualy do with the contours.
Note: it is better to avoid using namespace std
- see here Why is "using namespace std;" considered bad practice?. In my opinion it's better to avoid using namespace cv
as well, from a similar reason.