点坐标数据集中的模式

时间:2014-02-12 05:56:25

标签: c++ opencv

我有一组存储在矢量中的点坐标(x,y)。我想在向量中找到模式(最常见的一点)。我发现了许多算法如何计算模式,但是如何在点坐标数据集中找到模式?

typedef struct Points{
    int x;
    int y;
}Point

vector<Points> myPoint;

示例,myPoint:(0,0),(2,3),(6,2),(2,3),(4,1),... 模式是(2,3),

Points currpoint = myPoint[0]; 
int counter = 1;
int maxcounter = 1;
Points mode = myPoint[0];

int n = myPoints.size();
for (int i = 1; i < n; ++i) {
    if ( myPoint[i] == currpoint  )  // !!! "==" produces error
        ++counter;
    else {                              // next point started...
       if (counter > maxcounter) { // new mode candidate
           maxcounter = counter;
           mode = currvalue;
       }
       currpoint = myPoint[i]; // ready to count next point
       counter = 1;
     }
}
cout << "Mode: " << mode << endl;

&#34; ==&#34;操作数会产生错误,我是通过比较结构&#34; Points&#34;?

来做正确的方法

[改正]

Points currpoint = myPoint[0]; 
int counter = 1;
int maxcounter = 1;
Points mode = myPoint[0];

int n = myPoints.size();
for (int i = 1; i < n; ++i) {
    if ( myPoint[i].x == currpoint.x && myPoint[i].y == currpoint.y )  
        ++counter;
    else {                              
       if (counter > maxcounter) { 
           maxcounter = counter;
           mode = currvalue;
       }
       currpoint = myPoint[i]; 
       counter = 1;
     }
}

if (counter > maxcounter) { 
    maxcounter = counter;
    mode = currvalue;
 }

cout << "Mode: " << mode.x <<", "<< mode.y << endl;

1 个答案:

答案 0 :(得分:1)

您收到错误是因为==没有超载struct Points

两种解决方案:

  1. ==重载struct Points运算符:

    typedef struct Points{
        int x;
        int y;
    
        bool operator==(const Points& a) const
        {
            return (x == a.x && y == a.y);
        }
    }Point;
    
  2. 替换

    if ( myPoint[i] == currpoint )
    

    if ( myPoint[i].x==currpoint.x && myPoint[i].y==currpoint.y)