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

前端 未结 8 2031
走了就别回头了
走了就别回头了 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:15

    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'):
    
    0 讨论(0)
  • 2020-12-02 11:16

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

    0 讨论(0)
提交回复
热议问题