Ask Your Question
0

pointPolygonTest in OpenCV

asked 2014-10-26 10:57:22 -0600

jossyy gravatar image

I want to find center of palm. Firstly,I found contours and select max area contour. And I used pointPolygonTest. My code and result image are below, but I didn't find any point using pointPolygonTest. What is the problem?

`

     double dist, maxdist = -1;
     Point center;

    for(int i = 0; i< drawing.cols; i += 10) {
    for(int j = 0; j< drawing.rows; j += 10) {

        dist = pointPolygonTest(contours[maxAreaIndex], Point(i,j),true);
        cout << "   dist " << dist << endl;
        if(dist > maxdist)
        {
            maxdist = dist;
            center = cv::Point(i,j);
        }
    }
}
cout << "maxdist = " << maxdist << endl;
circle(drawing, center, maxdist, cv::Scalar(220,75,20),1,CV_AA);
/// Show in a window
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing );`

The drawing image is below image description

edit retag flag offensive close merge delete

Comments

This is a basic debugging task: 1) how does your contour look like? (e.g. use drawContours and show the image) 2) What output do you get right now?

FooBar gravatar imageFooBar ( 2014-10-26 11:37:28 -0600 )edit

I see some cout in your code. As suggested by @FooBar, can you post the output you get?

petititi gravatar imagepetititi ( 2014-10-26 12:28:13 -0600 )edit

I used drawContours and there is a result image in my question text. All calculated distance are negative now.

jossyy gravatar imagejossyy ( 2014-10-26 13:59:58 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-10-26 14:23:42 -0600

Your shape doesn't seem to be close. So the function can't know if the point is inside or outside the shape... Just add the first point at the end of your shape to close it:

contours[maxAreaIndex].push_back(contours[maxAreaIndex][0]);

It should do the trick...

edit flag offensive delete link more

Comments

I tried it but it didn't work.

jossyy gravatar imagejossyy ( 2014-10-26 14:46:03 -0600 )edit

thanks @petititi. I close the shape another way, and it works :) thank you again.

jossyy gravatar imagejossyy ( 2014-10-26 15:30:59 -0600 )edit

Question Tools

Stats

Asked: 2014-10-26 10:57:22 -0600

Seen: 1,266 times

Last updated: Oct 26 '14