0

这是基于this question,它更多地关注OpenCV C++,所以我决定提出这个问题。这是我的程序的一部分:

vector<vector<Point> > contours;
vector<vector<Point> > largest_contours;

double largest_area = 0;
for(int i= 0; i < contours.size(); i++){
    double area = contourArea(contours[i]);
    if(area >= largest_area){
        largest_area = area;
        largest_contours = contours[i];  <---THIS is the problem
    }
}

基本上该程序将执行以下操作:

  1. 扫描在图像序列/视频中检测到的每个轮廓
  2. 将轮廓标记为contours[i]
  3. 计算每个轮廓的面积
  4. 比较contours[i]基于区域。面积越大largest_area,轮廓越大largest_contours
  5. 最后,DrawContoursimshow

有问题的行将在鼠标上显示此消息:

Error: No operator "=" matches these operands

问题是,为什么 contours[i] 不等于 largest_contours尽管它们具有相同的类 ( vector<vector<Point> >) 并且一次每个轮廓只有一个值?谁能解释为什么以及如何解决它?

提前致谢。

编辑(1):更改contourArea(contours)contourArea(contours[i]). 添加了largest_contours和的声明contours

4

2 回答 2

1

这里有几个问题,如果没有完整的声明,您的问题的确切原因无法确定,但是这里有一些看起来很奇怪的事情:

double area = contourArea(contours);

这看起来像您确定了所有轮廓的总面积 - 每次迭代。这听起来不对。

largest_contours = contours[i]; 

这很可能会失败,因为没有轮廓的赋值运算符。如何保存索引(除非您想保留整个结构(?))。

于 2013-01-29T11:58:25.100 回答
1

您似乎在收藏某物和没有收藏之间感到困惑。我猜 avector<Point>是您认为的“轮廓”,而 avector<vector<Point>>是一组轮廓。

As you loop from 0 to contours.size(), you are working out contourArea(contours) which will be exactly the same every time because you never modify contours. It seems to me that you want to work out the area of an individual contour and should be doing something like contourArea(contours[i]).

Then, if you want a list of your largest contours, which is also of type vector<vector<Point>>, then you need to push each of the contours you find into this vector. If contours[i] is the contour you want to add to the list, you would do that with largest_contours.push_back(contours[i]);.

于 2013-01-29T12:00:22.723 回答