contour is not equal to contour[i]?

吃可爱长大的小学妹 提交于 2019-12-20 07:39:06

问题


This is based from this question, which is focusing more on OpenCV C++ so I decided to make this question. This is one part of my program:

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

Basically the program will do:

  1. Scans every contours detected in the image sequences/video
  2. Labels the contours as contours[i]
  3. Calculates the area of every contours
  4. Compares the contours[i] based on the area. The bigger area becomes largest_area and biggest contour will become largest_contours
  5. Finally, DrawContours and imshow

The line with the problem will show this message over the mouse:

Error: No operator "=" matches these operands

The question is, why is contours[i] is NOT equal to largest_contours despite they have the same class (vector<vector<Point> >) and have only one value for each contour at a time? Can anyone explain why and how to resolve it?

Thanks in advance.

EDIT(1): Changed contourArea(contours) to contourArea(contours[i]). Added declaration for largest_contours and contours.


回答1:


You seem to be getting mixed up between when you have a collection of something and when you don't. I'm guessing that a vector<Point> is what you consider a "contour" and a vector<vector<Point>> is a set of contours.

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]);.




回答2:


There are several issues here, the exact reason for your issues can't be determined without full declarations, however there are a few things looking odd here:

double area = contourArea(contours);

This looks like you determine the area of all contours in total - every iteration. This sounds wrong.

largest_contours = contours[i]; 

This most likely fails as there's no assignment operator for contours. How about saving the index instead (unless you want to keep the whole structure(?)).



来源:https://stackoverflow.com/questions/14582270/contour-is-not-equal-to-contouri

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!