2011-09-20 58 views
2

我一直在嘗試使用OpenCV的功能:OpenCv pointPolygonTest。如何提供輪廓作爲輸入?

double pointPolygonTest(InputArray contour, Point2f pt, bool measureDist)

我有4點在2D(X1,Y1)指定的輪廓,...,(X4,Y4)。我想測試Point(x,y)是否在輪廓內部或外部。但我似乎無法找到任何參考如何將輪廓指定爲正確的函數輸入。

我試過沒有得到正確的結果下面的實現:

vector<Point2f> contour; 

contour.push_back(Point2f(x1, y1)); 
contour.push_back(Point2f(x2, y2)); 
contour.push_back(Point2f(x3, y3)); 
contour.push_back(Point2f(x4, y4)); 

int inCont; 
inCont = pointPolygonTest(contour, Point2f(x, y), false); 

我缺少的東西?

回答

4

功能工作對我來說沒有任何問題(OpenCV的2.3.1):

vector<Point2f> points; 

points.push_back(Point2f(0,0)); 
points.push_back(Point2f(0,4)); 
points.push_back(Point2f(4,4)); 
points.push_back(Point2f(4,0)); 

cout << pointPolygonTest(points, Point2f(5,1), false) << endl; 
cout << pointPolygonTest(points, Point2f(1,1), false) << endl; 
cout << pointPolygonTest(points, Point2f(0,0), false) << endl; 

輸出:

-1 
1 
0 
+0

我錯過了可能的-1返回值。這傾向於在測試「if(inCont)」爲真之後評估我的條件語句。謝謝你指點我。問題解決了。 – tisch