如何在OpenCV 2.3.1中使用Contours?

fdh*_*fdh 3 c++ opencv vector contour

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

1)在C接口中有一个名为Contour Scanner的对象.它被用于逐个查找图像中的轮廓.我将如何在C++中执行此操作?我不想一次找到所有轮廓,而是希望一次找到一个.

2)在C CvSeq中用于表示轮廓,但是在C++ vector <vector<Point> >中使用.在CI中,能够通过使用来访问下一个轮廓h_next.什么是C++相当于 h_next

Tyl*_*man 9

我不确定你是否可以一次获得一个轮廓.但是,如果你有一个,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
   }
}
Run Code Online (Sandbox Code Playgroud)

因此,以更好地回答您的问题h_next.考虑到迭代器,itvector中,下一个元素会it+1.用法示例:

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