Search code examples
opencvobject-detection

OpenCv pointPolygonTest. How does one supply the contours as input?


I've been trying to use the OpenCV function:

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

I have a contour specified by 4 Points in 2D (x1,y1), ..., (x4,y4). I want to test if a Point (x,y) is inside or outside of the contour. But I can't seem to find any reference how to specify the contours as input for the function correctly.

I've tried the following implementation without getting a correct result:

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);

Am I missing something?


Solution

  • Function works for me without any problem (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;
    

    Output:

    -1
    1
    0