Ask Your Question
0

How to select the contour bounding a point (mouse event)

asked 2016-10-07 16:45:04 -0600

yannb gravatar image

Hello everyone.

I am working with OpenCV and I need to do the following :

I have a contours vector, built with cv::findContours. I need to click the picture, select the first contour bounding the click-point, and draw it on the picture.

But I don't know a good way to select in the contours vector the first contour bounding a point. Could you help me on this, please ?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-10-07 23:41:48 -0600

berak gravatar image

updated 2016-10-08 03:18:30 -0600

opencv has pointPolygonTest for this:

Point mouse(x,y); // picked point
int hitId = -1; // id of picked contour

for (size_t i=0; i<contours.size(); i++) {
     if (pointPolygonTest(contours[i], mouse,false) > 0) {
         hitId = i;
         break;
    }
}
// now, you still might end up with hitId == -1
// there will be the case, where you did **not** hit any contour, take care !
edit flag offensive delete link more

Comments

Thank you !

yannb gravatar imageyannb ( 2016-10-08 03:10:59 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-10-07 16:45:04 -0600

Seen: 647 times

Last updated: Oct 08 '16