轮廓不等于轮廓[i]? [英] contour is not equal to contour[i]?
问题描述
这是基于这个问题,它更加关注OpenCV C ++,所以我决定提出这个问题.这是我程序的一部分:
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:
- 扫描图像序列/视频中检测到的每个轮廓
- 将轮廓标记为
contours[i]
- 计算每个轮廓的面积
- 根据面积比较
contours[i]
.较大的区域变为largest_area
,最大的轮廓变为largest_contours
- 最后,
DrawContours
和imshow
- 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_area
and biggest contour will becomelargest_contours
- Finally,
DrawContours
andimshow
出现问题的行将通过鼠标显示此消息:
The line with the problem will show this message over the mouse:
Error: No operator "=" matches these operands
问题是,为什么 contours[i]
不等于 largest_contours
,尽管它们具有相同的类(vector<vector<Point> >
)并且仅具有一个值每个轮廓一次?谁能解释为什么以及如何解决这个问题?
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?
谢谢.
EDIT(1):将contourArea(contours)
更改为contourArea(contours[i])
.添加了largest_contours
和contours
的声明.
EDIT(1): Changed contourArea(contours)
to contourArea(contours[i])
. Added declaration for largest_contours
and contours
.
推荐答案
在您收集某物与不收集某物之间,您似乎感到困惑.我猜想vector<Point>
是您认为的轮廓",而vector<vector<Point>>
是一组轮廓.
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.
当您从0循环到contours.size()
时,您正在计算contourArea(contours)
,每次都将完全相同,因为您从未修改过contours
.在我看来,您想要算出单个轮廓的区域,并且应该执行类似contourArea(contours[i])
的操作.
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])
.
然后,如果要获得最大轮廓的列表,该列表的类型也为vector<vector<Point>>
,则需要将找到的每个轮廓推入该vector
中.如果contours[i]
是要添加到列表中的轮廓,则可以使用largest_contours.push_back(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]);
.
这篇关于轮廓不等于轮廓[i]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!