Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
  1. add a mouse callback to your window.

    cv2.setMouseCallback('image',mousecallback)
    
  2. in the callback function, run a point in polygon (cv.pointPolygonTest) test for each contour

    def mousecallback(event,x,y,flags,param):
        if event == cv2.EVENT_LBUTTONCLK: 
            for i in range(0,contours.size)
                r=cv.pointPolygonTest(contour[i],Point(y,x),False)
                if r>0:
                    print("Selected contour "+i)
    

I hope you get the idea. Note that the code is untested.

P.S. you might also use the path.contains_points() from the Matplotlib library. Check the mouse handling tutorial in opencv too!