Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'):

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...