[英]Usage of ord('q') and 0xFF
I am not able to understand the following code snippet- 我无法理解以下代码段-
if cv2.waitKey(0) & 0xFF == ord('q'):
break
in this code- 在此代码中-
1 import numpy as np
2 import cv2
3
4 cap = cv2.VideoCapture(0)
5
6 while(True):
7 # Capture frame-by-frame
8 ret, frame = cap.read()
9
10 # Our operations on the frame come here
11 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
12
13 # Display the resulting frame
14 cv2.imshow('frame',gray)
15 if cv2.waitKey(1) & 0xFF == ord('q'):
16 break
17
18 # When everything done, release the capture
19 cap.release()
20 cv2.destroyAllWindows()
What does ord('q') and 0xFF mean? ord('q')和0xFF是什么意思? How is it being used here? 在这里如何使用?
ord('q')
returns the Unicode code point of q ord('q')
返回ord('q')
的Unicode代码点 cv2.waitkey(1)
returns a 32-bit integer corresponding to the pressed key cv2.waitkey(1)
返回与按下的键对应的32位整数 & 0xFF
is a bit mask which sets the left 24 bits to zero, because ord()
returns a value betwen 0 and 255, since your keyboard only has a limited character set & 0xFF
是一个位掩码,可将剩余的24位设置为零,因为ord()
返回的值介于0和255之间,因为您的键盘仅具有有限的字符集 As per the cv2.waitkey
docs : 根据cv2.waitkey
docs :
It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed. 它返回所按下键的代码;如果在经过指定时间之前未按下任何键,则返回-1。
Generally in the OpenCV tutorials and blogs, it is a general convention to use " q
" key for halting any indefinite operation such as capturing frames from camera in your case. 通常,在OpenCV教程和博客中,使用“ q
”键停止任何不确定的操作(例如从您的情况下从摄像机捕获帧)是一种通用约定。 In your case, the program indefinitely checks at each iteration if " q
" key is being pressed using cv2.waitKey(1) & 0xFF == ord('q')
statement. 在您的情况下,程序将使用cv2.waitKey(1) & 0xFF == ord('q')
语句在每次迭代中无限期地检查是否按下了“ q
”键。 If True
then it simply brakes the infinite while
loop. 如果为True
则它会制动无限的while
循环。 You can set it to any key of your choice. 您可以将其设置为您选择的任何键。
First, cv2.waitKey(1) & 0xFF
will be executed. 首先,将执行cv2.waitKey(1) & 0xFF
。
waitKey
return DECIMAL VALUE
of q is 113. In Binary , It is expressed as 0b01110001 . 如果用户按下,例如,Q然后waitKey
返回DECIMAL VALUE
Q的是113在二进制 ,它表示为0b01110001。 AND
operator is excuted with two inputs are 0b01110001 and 0xFF ( 0b11111111 ). 接下来, AND
运算符被算作两个输入,分别是0b01110001和0xFF( 0b11111111 )。 0b01110001 AND
0b11111111 = 0b01110001 . 0b01110001 AND
0b11111111 = 0b01110001 。 Exactly result is DECIMAL VALUE
of q 确切的结果是q的 DECIMAL VALUE
Second, compare value of left expression 0b01110001 with ord('q')
. 其次,将左表达式0b01110001的值与ord('q')
。 Clearly, these values is the same as another value. 显然,这些值与另一个值相同。 And final result is break
is invoked. 最后的结果是break
被调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.