python中waitKey实例用法讲解

时间:2022-11-08 17:01:17

1、说明

用于等待按钮。当用户按下按钮时,句子将被执行并获得返回值。

2、语法

?
1
retval=cv2.waitKey([delay])
  • Retval:表示返回值;
  • Delay:键触发的时间,单位为ms。

3、实例

?
1
2
3
4
5
import cv2
lena=cv2.imread("D:\pmjcv\lena.bmp")
cv2.namedWindow("lesson")
cv2.imshow("lesson",lena)
key=cv2.waitKey()

实例扩展:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import waitKeyEx [as 别名]
def on_process_messages(self, sleep_time=0):
 
        has_windows = False
        has_capture_keys = False
 
        if len(self.named_windows) != 0:
            has_windows = True
 
        if len(self.capture_keys_windows) != 0:
            has_capture_keys = True
 
        if has_windows or has_capture_keys:
            wait_key_time = max(1, int(sleep_time*1000) )
            ord_key = cv2.waitKeyEx(wait_key_time)
            
            shift_pressed = False
            if ord_key != -1:
                chr_key = chr(ord_key) if ord_key <= 255 else chr(0)
 
                if chr_key >= 'A' and chr_key <= 'Z':
                    shift_pressed = True
                    ord_key += 32
                elif chr_key == '?':
                    shift_pressed = True
                    ord_key = ord('/')
                elif chr_key == '<':
                    shift_pressed = True
                    ord_key = ord(',')
                elif chr_key == '>':
                    shift_pressed = True
                    ord_key = ord('.')
        else:
            if sleep_time != 0:
                time.sleep(sleep_time)
 
        if has_capture_keys and ord_key != -1:
            self.add_key_event ( self.focus_wnd_name, ord_key, False, False, shift_pressed)

到此这篇关于python中waitKey实例用法讲解的文章就介绍到这了,更多相关python中waitKey如何使用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.py.cn/jishu/jichu/29786.html