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

Question:

I’m trying understand what 0xFF does under the hood in the following code snippet:

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

Any ideas?

Asked By: Dora

||

Answers:

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).

Answered By: Kevin W.

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.

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. Thus checking eqality with ord(c).

Answered By: jaykay

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:

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:

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'):
Answered By: Sheila

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(0) & 0xFF
Answered By: johnny b

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 english keyboard character will not be greater than 255. You can reference this ASCII Table to find the numerical values of any keyboard character.

Answered By: Daniel

In this code,

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

The waitKey(0) function returns -1 when no input is made whatsoever. As soon the event occurs i.e. a Button is pressed it returns a 32-bit integer.

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. As a result, an integer is obtained below 255.

ord(char) returns the ASCII value of the character which would be again maximum 255.

Hence by comparing the integer to the ord(char) value, we can check for a key pressed event and break the loop.

Answered By: Eiconic

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.

Answered By: user8559913

**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

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 .
0xFF is in hexadecimal format also know
as hexadecimal constant
which is 255 in decimal format or 11111111 in binary format.


    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:
**

        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’

**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.

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

Note 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

Answered By: Indratej Reddy
  1. cv2.waitKey(0) — 0 means that what ever is your output will stay on screen for 0ms i.e infinite time period
  2. 0xFF == ord('q') — means taking keyboard input. here its 'q'

in normal term i say this is trying to say keep output open until user press 'q' in its keyboard.

Answered By: Harjeet Singh

cv2.waitKey() returns the value -1 if no key is pressed. When you press a key, it returns the ASCII value of that key. 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.
However, depending on the platform or a keyboard modifier, a 32bit integer value will be returned. 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')

Answered By: Carlos Sanches

The behavior of waitKey has changed in v3.2 (December 2016).

The current behavior is this:

  • waitKey returns -1 or the lowest 8 bits of the keycode
  • waitKeyEx returns -1 or the full keycode, which can contain additional flags for special keys (e.g. arrow keys, modifiers, …)

You do not need the & 0xFF stuff with waitKey. Just use

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

This checks if the key q was pressed. ord() converts the string/character q into its ASCII integer code. That is needed because waitKey returns an integer.

The historical behavior was that there was only waitKey, no waitKeyEx. waitKey returned the full keycode.

If the keycode contained additional flags, it wouldn’t be exactly ord('q') anymore. The & 0xFF is a bitwise operation that amounts to a mask. 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.

Answered By: Christoph Rackwitz

It’s like if you waited for 0 milliseconds and press ‘Q’ on the key bord then the loop will break.

# 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()
Answered By: Md. Imrul Kayes
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.