如何在 OpenCV 2.3.1 中使用轮廓?

发布于 2024-12-16 20:46:33 字数 358 浏览 2 评论 0 原文

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

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

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

I recently changed from using the C interface to the C++ interface in OpenCV. In the C interface, there were a variety of things that don't seem to exist in the C++ one. Does anyone know the solution to these problems:

1) In the C interface there was an object called a Contour Scanner. It was used to find contours in an image one by one. How would I do this in C++? Instead of finding all the contours at once, I want to find them one at a time.

2) In C CvSeq was used to represent contours, however in C++ vector <vector<Point> > is used. In C I was able to access the next contour by using h_next. Whats the C++ equivalent of h_next?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欲拥i 2024-12-23 20:46:33

我不确定你是否可以一次获取一个轮廓。但是如果你有一个向量<向量<点> > 您可以按如下方式迭代每个轮廓:

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 中的迭代器 it,下一个元素将是 it+1。用法示例:

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

I'm not sure if you can get the contours one at a time. But if you have a vector<vector<Point> > you can iterate over each contour as follows:

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
   }
}

Therefore to better answer your question about h_next. Given the iterator, it, in the vector, the next element would be it+1. Example Usage:

vector<vector<Point> >::iterator contour = contours.begin();
vector<vector<Point> >::iterator next = contour+1; // behavior like h_next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文