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

前端 未结 8 2076
走了就别回头了
走了就别回头了 2020-12-02 10:36

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

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

A

8条回答
  •  情歌与酒
    2020-12-02 11:13

    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.

提交回复
热议问题