Python cv2.waitKey(1) 中的 0xFF 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35372700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's 0xFF for in cv2.waitKey(1)?
提问by Dora
I'm trying understand what 0xFF does under the hood in the following code snippet:
我正在尝试了解 0xFF 在以下代码片段中的作用:
if cv2.waitKey(0) & 0xFF == ord('q'):
break
Any ideas?
有任何想法吗?
采纳答案by Kevin W.
0xFF
is a hexadecimal constant which is 11111111
in binary. By using bitwise AND (&
) with this constant, it leaves only the last 8 bits of the original (in this case, whatever cv2.waitKey(0)
is).
0xFF
是一个11111111
二进制的十六进制常数。通过&
对这个常量使用按位 AND ( ),它只留下原始的最后 8 位(在这种情况下,无论cv2.waitKey(0)
是什么)。
回答by jaykay
ord(c)returns an integer representing the Unicode code point of the character(c) when the argument is a unicode object, or value of the byte when argument is an 8-bit string.
ord(c)返回一个整数,当参数是一个 unicode 对象时,表示字符(c) 的 Unicode 代码点,或者当参数是一个 8 位字符串时,返回字节的值。
In case of 64-bit systems the value of cv2.waitKey(0)is bitwise AND(&) with the 0xFFhexadecimal constant (which is representation of binary string 11111111) that results in the last 8 bits of it. Thus checking eqality with ord(c).
在 64 位系统的情况下,cv2.waitKey(0)的值是按位 AND(&) 与0xFF十六进制常量(它是二进制字符串11111111 的表示),结果是它的最后 8 位。因此用 ord(c) 检查相等性。
回答by Sheila
It is also important to note that ord('q') can return different numbers if you have NumLock activated (maybe it is also happening with other keys). For example, when pressing c, the code:
同样重要的是要注意,如果您激活了 NumLock, ord('q') 可以返回不同的数字(也许其他键也会发生这种情况)。例如,当按下 c 时,代码:
key = cv2.waitKey(10)
print(key)
returns
返回
1048675 when NumLock is activated
99 otherwise
Converting these 2 numbers to binary we can see:
将这两个数字转换为二进制我们可以看到:
1048675 = 100000000000001100011
99 = 1100011
As we can see, the last byte is identical. Then it is necessary to take just this last byte as the rest is caused because of the state of NumLock. Thus, we perform:
正如我们所见,最后一个字节是相同的。然后有必要只取最后一个字节,因为其余的都是由 NumLock 的状态引起的。因此,我们执行:
key = cv2.waitKey(33) & 0b11111111
# 0b11111111 is equivalent to 0xFF
and the value of key will remain the same and now we can compare it with any key we would like such as your question
并且键的值将保持不变,现在我们可以将它与我们想要的任何键进行比较,例如您的问题
if key == ord('q'):
回答by johnny b
cv2.waitKey() returns a 32 Bit integer value (might be dependent on the platform). The key input is in ASCII which is an 8 Bit integer value. So you only care about these 8 bits and want all other bits to be 0. This you can achieve with:
cv2.waitKey() 返回一个 32 位整数值(可能取决于平台)。键输入是 ASCII,它是一个 8 位整数值。所以你只关心这 8 位,并希望所有其他位都为 0。这可以通过以下方式实现:
cv2.waitKey(0) & 0xFF
回答by Daniel Copley
Truthfully in this case you don't need 0xFF. If you did cv2.waitkey(0) == ord(q)
it would work all the same. 0xFF
is just used to mask off the last 8bits
of the sequence and the ord() of any keyboard character will not be greater than 255. You can reference this ASCII Tableto find the numerical values of any keyboard character.
说实话,在这种情况下,您不需要 0xFF。如果你这样做了,cv2.waitkey(0) == ord(q)
它会起作用。0xFF
仅用于屏蔽8bits
序列的最后一个,并且任何键盘字符的 ord() 都不会大于 255。您可以参考此ASCII 表来查找任何键盘字符的数值。
回答by Kuldeep Thakre
In this code,
在这段代码中,
if cv2.waitKey(0) & 0xFF == ord('q'):
break
The waitKey(0)
function returns -1when no input is made whatsoever. As soon the event occurs i.e. a Button is pressedit returns a 32-bit integer.
当没有任何输入时,该waitKey(0)
函数返回-1。一旦事件发生,即按下按钮,它会返回一个32 位整数。
The 0xFF
in this scenario is representing binary 11111111a 8 bitbinary, since we only require 8 bits to represent a character we AND waitKey(0)
to 0xFF
. As a result, an integer is obtained below 255.
在0xFF
此方案中表示二进制11111111一个8位二进制,因为我们只需要8个比特来表示一个字符,我们与waitKey(0)
来0xFF
。结果,得到一个小于 255 的整数。
ord(char)
returns the ASCII value of the character which would be again maximum 255.
ord(char)
返回字符的 ASCII 值,最大为 255。
Hence by comparing the integer to the ord(char)
value, we can check for a key pressed event and break the loop.
因此,通过将整数与ord(char)
值进行比较,我们可以检查按键事件并中断循环。
回答by user8559913
cv2.waitKey() is the function which bind your code with keyboard and any thing you type will be returned by this function. output from waitKey is then logically AND with 0xFF so that last 8 bits can be accessed. Therefore last 8 bit represent input from keyboard and this is compared using == with the character you want.
cv2.waitKey() 是将您的代码与键盘绑定的函数,您输入的任何内容都将由此函数返回。waitKey 的输出然后与 0xFF 进行逻辑与运算,以便可以访问最后 8 位。因此,最后 8 位代表来自键盘的输入,并使用 == 与您想要的字符进行比较。