Ask Your Question

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

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

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

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

berak gravatar image

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

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


Thank you !

yannb gravatar imageyannb ( 2016-10-08 03:10:59 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


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

Seen: 299 times

Last updated: Oct 08 '16