5

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? How is it being used here?

chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
IronVenom
  • 83
  • 1
  • 3
  • 9

3 Answers3

14
  • ord('q') returns the Unicode code point of q
  • cv2.waitkey(1) returns a 32-bit integer corresponding to the pressed key
  • & 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
  • Therefore, once the mask is applied, it is then possible to check if it is the corresponding key.
The Pineapple
  • 567
  • 4
  • 16
  • Thanks! Is there any way we can add a pause - play effect? – IronVenom Nov 18 '18 at 04:43
  • https://stackoverflow.com/questions/38064777/use-waitkey-in-order-pause-and-play-video – The Pineapple Nov 18 '18 at 04:50
  • So, if we do that, then we don't have to add the 0xFF part right? – IronVenom Nov 18 '18 at 04:53
  • It cant hurt to add that part, but for what your doing it should matter. – The Pineapple Nov 18 '18 at 04:54
  • 1
    @IronVenom just to add my 2 cents, in some systems the waitKey returns the ascii representation of the key pressed and some bits are flipped for alt, ctrl, caps lock and other special keys. This are usually the first bits and that is why the 0xFF is used or sometimes just 255, since it will do a bitwise and and the first bits will be set to 0. – api55 Nov 18 '18 at 10:54
6

First, cv2.waitKey(1) & 0xFF will be executed.

  • Doing wait 1 ms for user press.
  • If user press, for example, q then waitKey return DECIMAL VALUE of q is 113. In Binary, It is expressed as 0b01110001.
  • Next, AND operator is excuted with two inputs are 0b01110001 and 0xFF (0b11111111).

0b01110001 AND 0b11111111 = 0b01110001. Exactly result is DECIMAL VALUE of q

Second, compare value of left expression 0b01110001 with ord('q'). Clearly, these values is the same as another value. And final result is break is invoked.

gia huy
  • 329
  • 1
  • 6
  • 12
3

As per the cv2.waitkey docs:

It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.

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. In your case, the program indefinitely checks at each iteration if "q" key is being pressed using cv2.waitKey(1) & 0xFF == ord('q') statement. If True then it simply brakes the infinite while loop. You can set it to any key of your choice.

ZdaR
  • 22,343
  • 7
  • 66
  • 87