3

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

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

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

4

1 回答 1

10

我不确定您是否可以一次获得一个轮廓。但如果你有一个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. 给定迭代器 ,it在 中vector,下一个元素将是it+1。示例用法:

vector<vector<Point> >::iterator contour = contours.begin();
vector<vector<Point> >::iterator next = contour+1; // behavior like h_next
于 2011-11-21T01:59:41.183 回答