Mouse callback event icons in python / opencv / OSX?

I am using OpenCV in python on OSX (10.10.5). I'm just starting out, so maybe I made a stupid mistake. However, I did not find anything that could solve this problem.

I have a mouse callback function that makes a list containing the coordinates of each point that the downloaded image is clicked on.

Now I would like to use flags in the mouse callback function to control whether I add coordinates to the list or add "NA, NA" (for example, if there is no dot in the image, I could hold the shift key and click on the image, and placeholder will be added instead of coordinates).

Although the "event" portion of the mouse callback part works *, the flags information does not seem to be available.

Here's the mouse callback function:

img_points = [] def write_points(event, x, y, flags, param): global img_points if event == cv2.EVENT_LBUTTONDOWN and flags != cv2.EVENT_FLAG_SHIFTKEY: img_points.append((x,y)) print img_points elif event == cv2.EVENT_LBUTTONDOWN and flags == cv2.EVENT_FLAG_SHIFTKEY: img_points.append(('NA','NA')) print img_points 

I tried different versions of this, and as far as I can tell, the problem is that the function event_flag_shiftkey (or any other event_flag information) is not available to the function.

Here's what happens if I change the code differently:

-If I do not add anything about the flag, the coordinates of each point clicked on are added to img_points, so part of the event looks normal.

-If I use the function written above, the coordinates of each point that is pressed will be added (regardless of whether the shift key is pressed). Therefore, the flags! = Cv2.EVENT_FLAG_SHIFTKEY must always be true.

-If I use the code as described above, but replace the flags! = Cv2.EVENT_FLAG_SHIFTKEY, say flags == cv2.EVENT_FLAG_CTRLKEY, then nothing happens when I press regardless of whether I hold any buttons. This suggests that no matter what I do with the keyboard, the flags == cv2.EVENT_FLAG_CTRLKEY and flags == cv2.EVENT_FLAG_SHIFTKEY are both always false.

Any ideas what is wrong here? Am I using event flags incorrectly? Is this a problem with how OSX encodes keys / right clicks? How can i fix this?

* Note: in fact, the EVENT_LBUTTONDOWN event is the only event that works. Like this post , EVENT_RBUTTONDOWN and double-click events do not work. (A double click is registered as two clicks and adds two sets of coordinates). I tried this with both the trackpad and the external mouse. (Responding to another post did not resolve the issue).

0
python callback opencv keyboard-events mouseevent
source share
3 answers

I had a similar problem and a simple print debugging helped me.

I recommend printing the "flags" value you get and compare with the value stored in cv2.EVENT_FLAG_SHIFTKEY.

In my case, I am using Windows 7 opencv 2.4.9, and cv2.EVENT_FLAG_ALTKEY is 32L, but the actual return parameter is 33.

Similarly, cv2.EVENT_FLAG_CTRLKEY saves 8L, but I get 9 for the param parameter. So, maybe there are some errors in the cv2 interface. Probably the Mac version too.

(edit) 2.4.12 seems to have already fixed this problem.

0
source share

I know this is an old question, but I just got it to work and thought I would share my solution:

I think that the flags registering the shift do not only use the flag themselves, but you also need to add a flag to click. I mean that you need to use:

flags == (cv2.EVENT_FLAG_SHIFTKEY + cv2.EVENT_FLAG_LBUTTON)

not just

flags == cv2.EVENT_FLAG_SHIFTKEY

It worked for me.

However, I also could not get

events == cv2.EVENT_RBUTTONDOWN

to work with MAC OS X.

Hope this helps!

0
source share

In fact, you need to do bitwise and (&), because the button flag and the key flag are added (EG EVENT_FLAG_LBUTTONDOWN = 1 plus EVENT_SHIFTKEY = 16) gives the value of flag 17, so an example for capturing a double left mouse click on a control key;

if event == cv2.EVENT_LBUTTONDBLCLK and (flags and cv2.EVENT_FLAG_CTRLKEY):

0
source share

All Articles