将其他键用于opencv的waitKey()函数 [英] Using other keys for the waitKey() function of opencv
问题描述
我正在开发一个程序(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.
我找到了关于
I found this about waitkey
, but it doesn't work.
所以我的问题是,如何捕获esc
和spacebar
以外的其他键来触发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屋!