Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

opencv

hex

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?

like image 868
Dora Avatar asked Feb 12 '16 21:02

Dora


People also ask

What does cv2 waitKey 1 & 0xFF do?

Answer #1: 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.

What does cv2 waitKey do?

Python OpenCV – waitKey() Function waitkey() function of Python OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed.

Is cv2 waitKey necessary?

4. cv2. waitKey() This function is very important, without this function cv2.


1 Answers

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'): 
like image 77
Sheila Avatar answered Oct 12 '22 13:10

Sheila