2014-01-06 13 views
1

我想擁有一組圖像的所有像素座標。不幸的是,我收到以下錯誤信息:創建從點設置的錯誤

「錯誤C2678:二進制'<':沒有發現操作符需要類型'const cv :: Point'的左側操作數(或沒有可接受的轉換)」

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 :: Point不能這樣做。不幸的是,我是C++和OpenCV的新手,不知道如何檢查我的懷疑是否屬實。

+0

也,[當前OpenCV的文檔是在這裏(http://docs.opencv.org/) – berak

+0

重複的http:// stackoverflow.com/questions/15913857/stdset-insert-wont-compile-with-custom-class和http://stackoverflow.com/questions/14784620/problems-with-c-set-container ... – Jarod42

回答

2

長的故事:如果你想使用一組點,你需要爲點提供一個比較操作:

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

是你的' comparePoints「嚴格弱排序二元謂詞?請參閱http://www.sgi.com/tech/stl/StrictWeakOrdering.html和http://stackoverflow.com/questions/979759/operator-and-strict-weak-ordering –