How to select a contour on mouse click
Please explain the way for selecting a contour on mouse click
add a mouse callback to your window.
cv2.setMouseCallback('image',mousecallback)
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!
ERROR: Traceback (most recent call last): File "changingcolorspaces.py", line 31, in mousecallback for i in range(0,contours.size): AttributeError: 'list' object has no attribute 'size'
As I said, the code is untested! It's to explain the solution, not to solve your problem.
I think it's obvious that the loop iterates through the contours, going from 0 to the number of contours. And if the list doesn't have a size
function, you could have guessed that it's the len(contours)
function (sometimes I mistake the C++ and Python syntax).
Asked: 2018-05-20 23:25:08 -0600
Seen: 2,545 times
Last updated: May 21 '18