2012-04-05 108 views
1

我有一個vector<vector<Point> > X,但我需要將它傳遞給函數cvConvexityDefects,該函數接受輸入CvArr*。我已閱讀主題Convexity defects C++ OpenCv。這需要在輸入這些變量:將一個矢量<vector <Point>> X轉換爲IplImage *或cv :: Mat *

vector<Point>& contour, vector<int>& hull, vector<Point>& convexDefects 

我不能得到解決工作,因爲我有一個船體參數是vector<Point>,我不知道如何在vector<int>改變它。

所以現在有兩個問題! :)

如何將vector<vector<Point> >轉換爲vector<int>

由於提前,有一個美好的一天:)

+0

哪個版本的OpenCV你使用? – Alex 2012-04-05 10:43:37

+0

2.3.1,最後一個...從網站下載http://opencv.willowgarage.com/wiki/ – 2012-04-05 10:59:38

回答

0

使用std::for_each和積累對象:

class AccumulatePoints 
{ 
public: 
    AccumulatePoints(std::vector<int>& accumulated) 
    : m_accumulated(accumulated) 
    { 
    } 

    void operator()(const std::vector<Point>& points) 
    { 
     std::for_each(points.begin(), points.end(), *this); 
    } 

    void operator()(const Point& point) 
    { 
     m_accumulated.push_back(point.x); 
     m_accumulated.push_back(point.y); 
    } 
private: 
    std::vector<int>& m_accumulated; 
}; 

像這樣來使用:

int main() 
{ 
    std::vector<int> accumulated; 
    std::vector<std::vector<Point>> hull; 

    std::for_each(hull.begin(), hull.end(), AccumulatePoints(accumulated)); 

    return 0; 
} 
+0

非常感謝oyu :)這是完美的!只是一件事:我評論行m_accumulated.push_back(point.z),因爲點對象中沒有point.z變量(只是point.x和point.y)。再次感謝 – 2012-04-05 12:58:33

+0

@MarcoMaisto我不知道OpenCV,所以這只是一個猜測。已修復。 – 2012-04-05 13:01:36

相關問題