python/opencv/OSX中的鼠标回调事件标志? [英] Mouse callback event flags in python/opencv/OSX?

查看:100
本文介绍了python/opencv/OSX中的鼠标回调事件标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在OSX(10.10.5)的python中使用OpenCV.我才刚刚起步,所以我可能在这里犯了一个愚蠢的错误.但是,我还没有找到解决此问题的方法.

I'm using OpenCV in python on OSX(10.10.5). I'm just starting out, so I may have made a silly mistake here. However, I haven't found anything that addresses this issue.

我有一个鼠标回调函数,该函数创建一个列表,其中包含在加载的图像中单击的每个点的坐标.

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

现在,我想使用鼠标回调函数中的标志来控制是否将坐标附加到列表中,或者是否附加"NA,NA"(例如,如果图像中缺少点,则可以保持Shift键并点击图片,然后会添加一个占位符而不是坐标).

Now I'd like to use the flags in the mouse callback function to control whether I append coordinates to the list or whether I instead append "NA,NA" (e.g. if a point is missing from an image, I could hold the shift key and click on the image, and a placeholder would be appended instead of coordinates).

尽管鼠标回调的事件"部分起作用*,但标志"信息似乎不可用.

Although the "event" part of the mouse callback works*, the "flags" information doesn't seem to be available.

这是鼠标回调函数:

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

我尝试了此版本的不同版本,据我所知,问题是该功能无法使用event_flag_shiftkey(或任何其他event_flag信息).

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

如果我以不同方式更改代码,则会发生以下情况:

Here's what happens if I alter the code in different ways:

-如果我不包含有关标志的任何内容,则单击的每个点的坐标都将附加到img_points上,因此事件部分似乎还可以.

-If I don't include anything about flags, the coordinates of every point that is clicked on are appended to img_points, so the event part seems to be ok.

-如果使用上面编写的函数,则将附加单击的每个点的坐标(无论是否按下Shift键).因此,标志!= cv2.EVENT_FLAG_SHIFTKEY必须始终为true.

-If I use the function as written above, the coordinates of every point that is clicked on (regardless of whether the shift key is pressed) are appended. So flags != cv2.EVENT_FLAG_SHIFTKEY must always be true.

-如果我使用上面编写的代码,但是将标志!= cv2.EVENT_FLAG_SHIFTKEY替换为标志== cv2.EVENT_FLAG_CTRLKEY,那么无论我是否按住任何按钮,单击都不会发生任何反应.这表明不管我用键盘做什么,标志== cv2.EVENT_FLAG_CTRLKEY和标志== cv2.EVENT_FLAG_SHIFTKEY都始终为false.

-If I use the code as written above, but replace flags != cv2.EVENT_FLAG_SHIFTKEY with, say, flags == cv2.EVENT_FLAG_CTRLKEY, then nothing happens when I click regardless of whether I'm holding any buttons. That would suggest that regardless of what I do with the keyboard, flags == cv2.EVENT_FLAG_CTRLKEY and flags == cv2.EVENT_FLAG_SHIFTKEY are both always false.

任何想法在这里有什么问题吗?我是否错误地使用了事件标志? OSX编码键/右键单击的方式是否有问题?我该如何解决?

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

*补充说明:实际上,事件EVENT_LBUTTONDOWN是唯一起作用的事件.与此帖子类似,EVENT_RBUTTONDOWN和双击事件均无效. (双击将记录为两次单击,并附加两组坐标).我已经使用触控板和外部鼠标尝试过此操作. (另一篇文章的答案不能解决问题.)

*Additional note: actually, the event EVENT_LBUTTONDOWN is the only event that works. Similar to this post, EVENT_RBUTTONDOWN and the double click events don't work. (A double click registers as two clicks and appends two sets of coordinates). I've tried this both with the trackpad and with an external mouse. (The answer to the other post didn't solve the issue).

推荐答案

我遇到了类似的问题,一个简单的打印语句调试帮助了我.

I had a similar issue and a simple print statement debugging helped me out.

我建议打印您获得的标志"值,并将其与cv2.EVENT_FLAG_SHIFTKEY中存储的值进行比较.

I recommend to print the "flags" value you are getting and compare with the value stored in cv2.EVENT_FLAG_SHIFTKEY.

就我而言,我使用的是Windows 7 opencv 2.4.9和cv2.EVENT_FLAG_ALTKEY为32L,但返回的实际flags参数为33.

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

类似地,cv2.EVENT_FLAG_CTRLKEY存储8L,但是我获得9的flags参数.因此,cv2界面中可能存在一些错误. Mac版本也可能.

Similarly, cv2.EVENT_FLAG_CTRLKEY stores 8L but I get 9 for flags param. So, probably there are some bug in cv2 interface. Likely, the Mac version too.

(编辑) 2.4.12似乎已经解决了此问题.

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

这篇关于python/opencv/OSX中的鼠标回调事件标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆