Python OpenCV – waitKeyEx()函数
Python OpenCv waitKeyEx() 方法类似于 waitKey() 方法,但它也返回完整的密钥代码。返回的关键代码是特定于实现的,取决于使用的后端:QT/GTK/Win32/etc。
Syntax: cv2.waitKey(delay)
Parameters:
- delay: The time in milliseconds after which windows needs to destroyed. If given 0 it waits for infinite till any key is pressed to destroy window.
Return : This method return the full key code of the key which is pressed. If no key is pressed it return -1.
示例 1:
在下面的示例中,我们实现了 waitKeyEx() 方法,我们制作了一个窗口,其中包含一个名为“gfg_logo.png”的图像,然后显示它,并使用 waitKeyEx() 方法延迟窗口关闭,然后按下一个键关闭它。我们将返回值存储在 full_key_code 变量中并打印出来。
Python
# importing cv2 module
import cv2
# read the image
img = cv2.imread("gfg_logo.png")
# showing the image
cv2.imshow('gfg', img)
# waiting using waitKeyEX method and storing
# the returned value in full_key_code
full_key_code = cv2.waitKeyEx(0)
# printing the variable
print("The key code is:"+str(full_key_code))
Python
# importing cv2 module
import cv2
# read the image
img = cv2.imread("gfg_logo.png")
# showing the image
cv2.imshow('gfg', img)
# waiting using waitKeyEX method and
# storing the returned value in full_key_code
full_key_code = cv2.waitKeyEx(5000)
# printing the variable
print("The key code is:"+str(full_key_code))
输出:
The key code is:13
在输出中,full_key_code 的值将根据按下的键打印出来,当我们按下 enter 时,打印的值如下。
示例 2:
我们可以看到的另一个例子是我们不按任何键并等待窗口在给定的延迟后自动销毁。我们将传递 5000 作为参数等待 5 秒,然后窗口将自动关闭,无需按任何键。在这种情况下,函数将返回 -1,因为没有按下任何键。
Python
# importing cv2 module
import cv2
# read the image
img = cv2.imread("gfg_logo.png")
# showing the image
cv2.imshow('gfg', img)
# waiting using waitKeyEX method and
# storing the returned value in full_key_code
full_key_code = cv2.waitKeyEx(5000)
# printing the variable
print("The key code is:"+str(full_key_code))
输出:
The key code is:-1