如何检测何时在 OpenCV 中释放密钥?
How to detect when a key is released in OpenCV?
如果我运行下面的代码
import cv2
cv2.namedWindow('Window')
while True:
key = cv2.waitKey(50)
print(key)
if key == ord('q'):
break
print('quit')
然后按下i
(键码105
)一会儿,我得到输出
-1
-1
-1
-1
-1
-1
105
-1
-1
-1
-1
-1
-1
-1
-1
-1
105
105
105
105
105
105
105
105
105
105
-1
-1
第一次出现105
是我按下i
的时候。即使我一直按下 i
,我还是连续九次得到 -1
。然后我不断得到 105
直到我释放 i
.
在第一次出现 105
后,我如何知道 i
是否仍然按下或在两种情况下都得到 -1
时已被释放?或者换句话说,您如何检测 OpenCV 中何时释放密钥?
OpenCV 有一个非常基本的接口属性。正如the documentation提到的,它没有谈论发布的事件:
The function waitKey waits for a key event infinitely (when
≤0 ) or for delay milliseconds, when it is positive. Since
the OS has a minimum time between switching threads, the function will
not wait exactly delay ms, it will wait at least delay ms, depending
on what else is running on your computer at that time. It returns the
code of the pressed key or -1 if no key was pressed before the
specified time had elapsed.
在你的情况下,如果你只使用按下事件,它会正常工作。不幸的是,您无法使用按下事件获得释放事件。您可以使用 MouseEvent types 而不是 keyEvent 作为发布事件或评论中提到的 Micka,最好使用基于 ui 的程序。
如果我运行下面的代码
import cv2
cv2.namedWindow('Window')
while True:
key = cv2.waitKey(50)
print(key)
if key == ord('q'):
break
print('quit')
然后按下i
(键码105
)一会儿,我得到输出
-1
-1
-1
-1
-1
-1
105
-1
-1
-1
-1
-1
-1
-1
-1
-1
105
105
105
105
105
105
105
105
105
105
-1
-1
第一次出现105
是我按下i
的时候。即使我一直按下 i
,我还是连续九次得到 -1
。然后我不断得到 105
直到我释放 i
.
在第一次出现 105
后,我如何知道 i
是否仍然按下或在两种情况下都得到 -1
时已被释放?或者换句话说,您如何检测 OpenCV 中何时释放密钥?
OpenCV 有一个非常基本的接口属性。正如the documentation提到的,它没有谈论发布的事件:
The function waitKey waits for a key event infinitely (when ≤0 ) or for delay milliseconds, when it is positive. Since the OS has a minimum time between switching threads, the function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is running on your computer at that time. It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.
在你的情况下,如果你只使用按下事件,它会正常工作。不幸的是,您无法使用按下事件获得释放事件。您可以使用 MouseEvent types 而不是 keyEvent 作为发布事件或评论中提到的 Micka,最好使用基于 ui 的程序。