Ask Your Question
0

How to select a contour on mouse click

asked 2018-05-20 23:25:08 -0600

Please explain the way for selecting a contour on mouse click

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-05-21 04:40:33 -0600

kbarni gravatar image
  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!

edit flag offensive delete link more

Comments

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'

muskanbansal98 gravatar imagemuskanbansal98 ( 2018-06-06 23:02:54 -0600 )edit

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).

kbarni gravatar imagekbarni ( 2018-06-07 04:40:09 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-05-20 23:25:08 -0600

Seen: 2,407 times

Last updated: May 21 '18