2011-11-21 108 views
3

我最近從在OpenCV中使用C接口轉換到C++接口。在C界面中,有許多東西似乎並不存在於C++中。有誰知道這些問題的解決方案:如何在OpenCV 2.3.1中使用輪廓?

1)在C界面有一個對象稱爲輪廓掃描儀。它被用來逐個查找圖像中的輪廓。我將如何在C++中執行此操作?我不想一次找到所有的輪廓,而是想一次找到它們。

2)在C CvSeq被用來表示輪廓,但是在C++ vector <vector<Point> >被使用。在C中,我可以通過使用h_next訪問下一個輪廓。什麼是C++相當於h_next

+0

+1好天啊!很難跟上OpenCV中的API更改,對吧?! – Geoff

回答

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 
+0

謝謝,它工作完美。 – fdh