OpenCV C++ 按降序对轮廓点向量进行排序

问题描述 投票:0回答:1

GETTING THIS ERROR MESSAGES 1

GETTING THIS ERROR MESSAGES 2

我正在尝试按降序对轮廓点的向量进行排序,但每当我使用时:

sort(contours.begin(), contours.end() , greater<>()),

它正在弹出一个错误。
如何对包含轮廓点的向量进行降序排序?

    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);
    }
c++ opencv contour
1个回答
0
投票

contours
not
vector
Point
s。它是
vector
vector
s 的
Point
s。 IE。每个元素本身就是
vector
Point
s.

如果你想对

vector
的这样一个
vector
进行排序,你应该提供某种 “更大” 功能。

一种方便的方法是使用 lambda 函数:

std::sort(contours.begin(), 
          contours.end(), 
          [](std::vector<cv::Point> const & v1, std::vector<cv::Point> const & v2) 
                                                                 { return true; });

正如您所见,lambda 简单地返回 true。这只是一个存根。 相反,您应该实施您想要的排序标准。

你必须确定什么时候你认为一个元素(这是

vector
Points
)大于另一个(也是
vector
s的
Point
)。 这取决于你想用轮廓做什么。

注意: 最好避免

using namespace std
- 看这里为什么“使用命名空间标准;”被认为是不好的做法?。在我看来,出于类似的原因,最好也避免
using namespace cv

© www.soinside.com 2019 - 2024. All rights reserved.