轮廓不等于轮廓[i]? [英] contour is not equal to contour[i]?

查看:112
本文介绍了轮廓不等于轮廓[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:

  1. 扫描图像序列/视频中检测到的每个轮廓
  2. 将轮廓标记为contours[i]
  3. 计算每个轮廓的面积
  4. 根据面积比较contours[i].较大的区域变为largest_area,最大的轮廓变为largest_contours
  5. 最后,DrawContoursimshow
  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

问题是,为什么 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_contourscontours的声明.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆