-2

GETTING THIS ERROR MESSAGES 1

GETTING THIS ERROR MESSAGES 2

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);
    }
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • Do you know if opencv has befined this: `greater()`? If not, you have to define it yourself... – Damien Mar 03 '22 at 15:01
  • what is the error message ? – Jaziri Rami Mar 03 '22 at 15:07
  • THESE ARE THE ERROR MESSAGES – Hanishkaran Muneswaran Mar 03 '22 at 15:24
  • /usr/include/c++/9/bits/predefined_ops.h:177:11: error: no match for call to ‘(std::greater >) (std::vector >&, std::vector >&)’ – Hanishkaran Muneswaran Mar 03 '22 at 15:24
  • 1
    1) define when a point A is greater than a point B. 2) create a lambda function for the sort comparator. 3) you need to sort a vector, not a vector> as you're doing now. Or, if you want to sort each contour (as opposed to the points in each contour), you can compute the bounding box of each contour, and sort according to top left y of each bbox. – Miki Mar 03 '22 at 15:26
  • @HanishkaranMuneswaran Please [edit] clarifications and updates into the original question itself rather than posting them in the comments where they can't be formatted and may disappear at any time. – beaker Mar 03 '22 at 16:29
  • @Miki Can you show me an example of creating a function for the sort comparator by using this vector which contains contours point so that i can follow up – Hanishkaran Muneswaran Mar 03 '22 at 19:07
  • @beaker Im sorry. I am a newbie to stackoverflow. Instead i was searching for the edit option after posted my first question but i could not find it, so i have no other choise rather then posting them in the comments – Hanishkaran Muneswaran Mar 03 '22 at 19:12
  • @HanishkaranMuneswaran There's a little [edit] link at the bottom of your question, right next to the blue box with your name in it. I also added a link to it in my previous comment, and this one. – beaker Mar 03 '22 at 19:23
  • @beaker thank you for your concerned i have saw the edit link by your guidance . I included the error messages on my post in a picture form. I hope it will be more visible now – Hanishkaran Muneswaran Mar 03 '22 at 19:40

1 Answers1

0

contours is not a vector of Points. It is a vector of vectors of Points. I.e. each element is in itself a vector of Points.

If you want to sort such a vector of vectors, 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 Points). 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.

wohlstad
  • 12,661
  • 10
  • 26
  • 39