简体   繁体   English

cv2.waitKey(1) 中的 0xFF 是什么意思?

[英]What's 0xFF for in cv2.waitKey(1)?

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?有任何想法吗?

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).同样重要的是要注意,如果您激活了 NumLock, ord('q') 可以返回不同的数字(也许其他键也会发生这种情况)。 For example, when pressing c, the code:例如,当按下 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.然后有必要只取最后一个字节,因为其余的都是由 NumLock 的状态引起的。 Thus, we perform:因此,我们执行:

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'):

0xFF is a hexadecimal constant which is 11111111 in binary. 0xFF是十六进制常量,二进制为11111111 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).通过对这个常量使用按位 AND ( & ),它只留下原始的最后 8 位(在这种情况下,无论cv2.waitKey(0)是什么)。

cv2.waitKey() returns a 32 Bit integer value (might be dependent on the platform). cv2.waitKey() 返回一个 32 位整数值(可能取决于平台)。 The key input is in ASCII which is an 8 Bit integer value.键输入是 ASCII,它是一个 8 位整数值。 So you only care about these 8 bits and want all other bits to be 0. This you can achieve with:所以你只关心这 8 位,并希望所有其他位都为 0。这可以通过以下方式实现:

cv2.waitKey(0) & 0xFF

In this code,在这段代码中,

if cv2.waitKey(0) & 0xFF == ord('q'):
    break

The waitKey(0) function returns -1 when no input is made whatsoever.当没有任何输入时, waitKey(0)函数返回-1 As soon the event occurs ie a Button is pressed it returns a 32-bit integer .一旦事件发生,即按下按钮,它会返回一个32 位整数

The 0xFF in this scenario is representing binary 11111111 a 8 bit binary, since we only require 8 bits to represent a character we AND waitKey(0) to 0xFF .这个场景中的0xFF表示二进制11111111一个8 位二进制,因为我们只需要 8 位来表示一个字符我们和waitKey(0)0xFF As a result, an integer is obtained below 255.结果,得到一个小于 255 的整数。

ord(char) returns the ASCII value of the character which would be again maximum 255. ord(char)返回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)值进行比较,我们可以检查按键事件并中断循环。

Truthfully in this case you don't need 0xFF.说实话,在这种情况下,您不需要 0xFF。 If you did cv2.waitkey(0) == ord(q) it would work all the same.如果你做了cv2.waitkey(0) == ord(q)它会起作用。 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 Table to find the numerical values of any keyboard character. 0xFF仅用于屏蔽序列的最后 8 8bits ,并且任何键盘字符的 ord() 不会大于 255。您可以参考此ASCII 表来查找任何键盘字符的数值。

**READ THIS IT WILL SAVE YOUR TIME ** **阅读此内容将节省您的时间**

Note 1: cv2.waitKey() will return the keyword that you press in case if u just click on the close button when the window is opened then it will return -1注意 1: cv2.waitKey() 将返回您按下的关键字,以防您在窗口打开时单击关闭按钮,然后它将返回 -1

Note 2: let us assume that you have pressed 'q' then cv2.waitkey() will return that 'q' but the format it returns will be in string data type in order to change it to binary we are performing bitwise AND operation with help of & symbol with 0xFF .注意 2:让我们假设您按下了 'q' 然后 cv2.waitkey() 将返回该 'q' 但它返回的格式将是字符串数据类型,以便将其更改为二进制我们正在执行按位与操作& 符号与 0xFF 的帮助。 0xFF is in hexadecimal format also know as hexadecimal constant which is 255 in decimal format or 11111111 in binary format. 0xFF 是十六进制格式,也称为十六进制常量,十进制格式为 255,二进制格式为 11111111。


    Decimal=255
    Binary=11111111
    Hexadecimal=0xff

**Note it is the same value in different formats **

Note 3: we all know that '&' in python is used to perform bitwise 'And' operation, Bitwise in the sense we perform the And operation at binary level AND operation logic: **注3:我们都知道python中的'&'是用来进行按位的'And'操作的, Bitwise的意义在于我们在二进制级别执行And操作的AND操作逻辑: **

        0&0=0
        0&1=0
        1&0=0
        1&1=1

** **

Below represents the small table of asci value and binary value of letter 'q'下面代表字母'q'的asci值和二进制值的小表

**Letter    ASCII Code  Binary  **
  q           113      01110001    

Note 4 : since we have given the hexadecimal constant 0xFF whose value in binary is 11111111 let's perform the bit AND OPERATION with the binary value of letter 'q' which is 01110001.注 4 :由于我们给出了十六进制常数0xFF,其二进制值为 11111111,让我们用字母“q”的二进制值 01110001 执行位与运算。

        q= 01110001
      0xFF=11111111
          ----------
           01110001   ----->q so when do bitwise and operation we get the same value of q
          ----------

Note 5:注 5:

Since we are performing bitwise And operation with 0xFF which is a hexadecimal constant, once the bitwise operation is completed or performed, the result will change to the decimal format, so since we are using ord('q') function which will return the decimal value or ASCII value of 'q' so both will be equal the condition if condition becomes true and the loop will break由于我们使用的是十六进制常量 0xFF 执行按位与运算,一旦按位运算完成或执行,结果将更改为十进制格式,因此由于我们使用 ord('q') 函数将返回十进制'q' 的值或 ASCII 值,因此如果条件变为真并且循环将中断,则两者都将等于条件

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 0xFF hexadecimal constant (which is representation of binary string 11111111 ) that results in the last 8 bits of it.在 64 位系统的情况下, cv2.waitKey(0)的值是按位 AND(&) 与0xFF十六进制常量(它是二进制字符串11111111 的表示),结果是它的最后 8 位。 Thus checking eqality with ord(c).因此用 ord(c) 检查相等性。

cv2.waitKey() returns the value -1 if no key is pressed.如果没有按下任何键, cv2.waitKey()返回值 -1。 When you press a key, it returns the ASCII value of that key.当您按下一个键时,它会返回该键的 ASCII 值。 So if you do所以如果你这样做

 k = cv2.waitKey(0)
 if k == ord('b'):
    break

when the b key is pressed, the value of k will be 98 which is equal to the value of ord('b') , ie 98 and this conditional will be true resulting in break.b键被按下时, k的值将是 98,它等于ord('b') ,即 98 并且这个条件为真导致中断。 However, depending on the platform or a keyboard modifier, a 32bit integer value will be returned.但是,根据平台或键盘修饰符,将返回 32 位整数值。 In this case, it is better to use cv2.waitKey() & 0xFF , which, in a binary AND operation, will result in the value of the key pressed, and can be compared with ord('key')在这种情况下,最好使用cv2.waitKey() & 0xFF ,在二进制 AND 运算中,这将导致按下的键的值,并且可以与ord('key')进行比较

cv2.waitKey() is the function which bind your code with keyboard and any thing you type will be returned by this function. cv2.waitKey() 是将您的代码与键盘绑定的函数,您输入的任何内容都将由此函数返回。 output from waitKey is then logically AND with 0xFF so that last 8 bits can be accessed. waitKey 的输出然后与 0xFF 进行逻辑与运算,以便可以访问最后 8 位。 Therefore last 8 bit represent input from keyboard and this is compared using == with the character you want.因此,最后 8 位代表来自键盘的输入,并使用 == 与您想要的字符进行比较。

The behavior of waitKey has changed in v3.2 (December 2016) . waitKey的行为在 v3.2(2016 年 12 月)中发生了变化

The current behavior is this:目前的行为是这样的:

  • waitKey returns -1 or the lowest 8 bits of the keycode waitKey返回-1或键码的最低 8 位
  • waitKeyEx returns -1 or the full keycode, which can contain additional flags for special keys (eg arrow keys, modifiers, ...) waitKeyEx返回-1完整的键码,其中可以包含特殊键的附加标志(例如箭头键、修饰符...)

You do not need the & 0xFF stuff with waitKey .不需要带有waitKey& 0xFF东西。 Just use只需使用

if cv2.waitKey(...) == ord('q'):
    ...

This checks if the key q was pressed.这将检查是否按下了键q ord() converts the string/character q into its ASCII integer code. ord()将字符串/字符q转换为其 ASCII 整数代码。 That is needed because waitKey returns an integer.这是必需的,因为waitKey返回一个整数。

The historical behavior was that there was only waitKey , no waitKeyEx .历史行为是只有waitKey ,没有waitKeyEx waitKey returned the full keycode. waitKey返回完整的键码。

If the keycode contained additional flags, it wouldn't be exactly ord('q') anymore.如果键码包含额外的标志,它就不再是ord('q')了。 The & 0xFF is a bitwise operation that amounts to a mask. & 0xFF是相当于掩码的按位运算。 Only the bottom eight bits of the keycode remain, hence discarding any flags in higher bits.仅保留密钥代码的底部八位,因此丢弃较高位中的任何标志。 The comparison would succeed if you pressed the q key, even if any flags were set.如果您按下q键,即使设置了任何标志,比较也会成功。

  1. cv2.waitKey(0) -- 0 means that what ever is your output will stay on screen for 0ms ie infinite time period cv2.waitKey(0) -- 0 表示您的输出将在屏幕上停留 0 cv2.waitKey(0) ,即无限时间段
  2. 0xFF == ord('q') -- means taking keyboard input. 0xFF == ord('q') -- 表示接受键盘输入。 here its 'q'这里是'q'

in normal term i say this is trying to say keep output open until user press 'q' in its keyboard.在正常情况下,我说这是试图保持输出打开,直到用户在其键盘上按下'q'

It's like if you waited for 0 milliseconds and press 'Q' on the key bord then the loop will break.这就像如果您等待 0 毫秒并在键盘上按“Q”然后循环就会中断。

# If we've waited at least 1 ms And we've pressed the Esc
while True:
   if cv2.waitKey(1) & 0xFF == 27:
      break
cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM