创建“从点设置”时出错

error in creation of Set from Points

本文关键字:出错 从点设置 设置 创建      更新时间:2023-10-16

我想要一组图像的所有像素坐标。不幸的是,我收到以下错误消息:

"错误 C2678:二进制'<':未找到采用类型为'const cv::P oint'的左操作数的运算符(或没有可接受的转换)"

Mat img;
img = imread( "[...]\picture.jpg", 1 );
set<Point> pointset;
for( int x = 0 ; x < img.cols ; x++)
{
    for (int y = 0 ; y < img.rows ; y++)
    {
        pointset.insert(Point(x,y));
    }
}

我怀疑进入集合的每个类型都必须提供用于比较的函数,而 cv::P oint 无法做到这一点。不幸的是,我是C++和OpenCV的新手,不知道如何检查我的怀疑是否属实。

长话短说:如果你想使用一组点,你需要为点提供一个比较操作:

struct comparePoints {
    bool operator()(const Point & a, const Point & b) {
        return ( a.x<b.x && a.y<b.y );
    }
};
int main()
{
    Mat img = imread( "clusters.png", 1 );
    set<Point,comparePoints> pointset;
    for( int x = 0 ; x < img.cols ; x++)
    {
        for (int y = 0 ; y < img.rows ; y++)
        {
            pointset.insert(Point(x,y));
        }
    }
    return 0;
}

在 otther 手上,您只需要一组,如果有重复的点需要避免。 这里不是这样。

因此,使用向量可能更容易:

int main()
{
    Mat img = imread( "clusters.png", 1 );
    vector<Point> points;
    for( int x = 0 ; x < img.cols ; x++)
    {
        for (int y = 0 ; y < img.rows ; y++)
        {
            points.push_back(Point(x,y));
        }
    }
    return 0;
}