问题
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:
- Scans every contours detected in the image sequences/video
- Labels the contours as
contours[i] - Calculates the area of every contours
- Compares the
contours[i]based on the area. The bigger area becomeslargest_areaand biggest contour will becomelargest_contours - Finally,
DrawContoursandimshow
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