2

I want to have a set of all Pixel Coordinates of an image. Unfortunately i get the following error message:

"error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const cv::Point' (or there is no acceptable conversion)"

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));
    }
}

I suspect that every type that goes into a set has to provide functions for comparison and cv::Point fails to do that. Unfortunately I'm new to C++ and OpenCV and don't know how to check if my suspicion is true.

Sebastian Schmitz
  • 1,884
  • 3
  • 21
  • 41
  • also, [the current opencv docs are here](http://docs.opencv.org/) – berak Jan 06 '14 at 10:17
  • duplicate of http://stackoverflow.com/questions/15913857/stdset-insert-wont-compile-with-custom-class and http://stackoverflow.com/questions/14784620/problems-with-c-set-container ... – Jarod42 Jan 06 '14 at 10:36

1 Answers1

3

the long story : if you want to use a set of points, you need to supply a compare operation for points:

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;
}

on the otther hand, you'd only need a set, if there were duplicate points to avoid. not so here.

so it's probably easier just to use a vector instead:

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;
}
berak
  • 39,159
  • 9
  • 91
  • 89
  • 1
    Is your `comparePoints` a Strict Weak Ordering binary predicate? See http://www.sgi.com/tech/stl/StrictWeakOrdering.html and http://stackoverflow.com/questions/979759/operator-and-strict-weak-ordering – Alessandro Jacopson Sep 08 '14 at 14:52
  • Following above comment and other answers like https://stackoverflow.com/questions/34047772/stdset-custom-comparator-for-2d-points a better compare would be: return a.x < b.x || (a.x == b.x && a.y < b.y); – dpetrini Aug 29 '19 at 12:39