opencv的waitKey()函数使用其他键 [英] Using other keys for the waitKey() function of opencv

查看:34
本文介绍了opencv的waitKey()函数使用其他键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序(python、opencv),在该程序中我使用 spacebar 转到下一帧,并使用 Esc 退出程序.这是我唯一可以使用的两个键.我试图找出更多的键,为它们尝试了各种代码,但没有用.尤其是方向键.

I'm working on a program (python ,opencv) in which I use the spacebar to go to the next frame, and Esc to exit the program. These are the only two keys i've got working. I tried to find out about more keys , tried various codes for them but didnt work. especially arrow keys.

我发现这个关于waitkey,但它不起作用.

I found this about waitkey, but it doesn't work.

所以我的问题是,除了 escspacebar 之外,我如何捕捉其他键来触发我的 python-opencv 程序中的某些功能?

So my question is, How do I catch other keys besides esc and spacebar to trigger certain functions in my python-opencv program?

推荐答案

你可以使用 Python 中的 ord() 函数来解决这个问题.

You can use ord() function in Python for that.

例如,如果你想触发'a'键按下,请执行以下操作:

For example, if you want to trigger 'a' key press, do as follows :

if cv2.waitKey(33) == ord('a'):
   print "pressed a"

在此处查看示例代码:绘制直方图

See a sample code here: Drawing Histogram

更新:

要找到任何键的键值是使用一个简单的脚本打印键值,如下所示:

To find the key value for any key is to print the key value using a simple script as follows :

import cv2
img = cv2.imread('sof.jpg') # load a dummy image
while(1):
    cv2.imshow('img',img)
    k = cv2.waitKey(33)
    if k==27:    # Esc key to stop
        break
    elif k==-1:  # normally -1 returned,so don't print it
        continue
    else:
        print k # else print its value

使用此代码,我得到以下值:

With this code, I got following values :

Upkey : 2490368
DownKey : 2621440
LeftKey : 2424832
RightKey: 2555904
Space : 32
Delete : 3014656
...... # Continue yourself :)

这篇关于opencv的waitKey()函数使用其他键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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