【问题标题】:How to use Contours in OpenCV 2.3.1?如何在 OpenCV 2.3.1 中使用轮廓?
【发布时间】:2011-11-21 00:56:09
【问题描述】:

我最近在 OpenCV 中从使用 C 接口更改为 C++ 接口。在 C 接口中,有很多 C++ 中似乎不存在的东西。有谁知道这些问题的解决方案:

1) 在 C 接口中有一个称为轮廓扫描仪的对象。它用于一一查找图像中的轮廓。我将如何在 C++ 中做到这一点?我不想一次找到所有轮廓,而是一次找到它们。

2) 在 C 中 CvSeq 用于表示轮廓,但在 C++ 中使用 vector <vector<Point> >。在 C 语言中,我可以使用h_next 访问下一个轮廓。 h_next 的 C++ 等价物是什么?

【问题讨论】:

  • +1 天哪!很难跟上 OpenCV 中的 API 变化,对吧?!

标签: c++ opencv vector contour


【解决方案1】:

我不确定您是否可以一次获得一个轮廓。但是,如果您有 vector<vector<Point> >,您可以按如下方式遍历每个轮廓:

using namespace std;

vector<vector<Point> > contours;

// use findContours or other function to populate

for(size_t i=0; i<contours.size(); i++) {
   // use contours[i] for the current contour
   for(size_t j=0; j<contours[i].size(); j++) {
      // use contours[i][j] for current point
   }
}

// Or use an iterator
vector<vector<Point> >::iterator contour = contours.begin(); // const_iterator if you do not plan on modifying the contour
for(; contour != contours.end(); ++contour) {
   // use *contour for current contour
   vector<Point>::iterator point = contour->begin(); // again, use const_iterator if you do not plan on modifying the contour
   for(; point != contour->end(); ++point) {
      // use *point for current point
   }
}

因此,为了更好地回答您关于h_next 的问题。给定vector 中的迭代器it,下一个元素将是it+1。示例用法:

vector<vector<Point> >::iterator contour = contours.begin();
vector<vector<Point> >::iterator next = contour+1; // behavior like h_next

【讨论】:

    猜你喜欢
    • 2021-06-16
    • 1970-01-01
    • 2021-09-07
    • 2016-09-25
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多