如何在OpenCV 2.3.1中使用轮廓?

3

我最近从OpenCV的C接口转换到了C++接口。在C接口中,有许多在C++接口中似乎不存在的东西。有没有人知道以下问题的解决方法:

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

2)在C中,CvSeq被用来表示轮廓,但在C++中使用vector <vector<Point> >。在C中,我可以通过使用h_next来访问下一个轮廓。那么,在C++中,有什么相当于h_next的东西吗?


天哪!跟上OpenCV API的变化真是太难了,不是吗?! - Geoff
1个回答

10

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


我不确定能否逐个获取轮廓,但如果你有一个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的问题。给定迭代器itvector中,下一个元素将是it+1。示例用法:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接