ord('q') 和 0xFF 的用法 [英] Usage of ord('q') and 0xFF

查看:150
本文介绍了ord('q') 和 0xFF 的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解以下代码片段-

I am not able to understand the following code snippet-

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

在这段代码中-

    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()

ord('q') 和 0xFF 是什么意思?这里是如何使用的?

What does ord('q') and 0xFF mean? How is it being used here?

推荐答案

  • ord('q') 返回 q 的 Unicode 代码点
  • cv2.waitkey(1) 返回与按下的键对应的 32 位整数
  • <代码>&0xFF 是一个位掩码,它将左边的 24 位设置为零,因为 ord() 返回一个介于 0 和 255 之间的值,因为您的键盘只有有限的字符集
  • 因此,一旦应用了掩码,就可以检查它是否是相应的键.
    • 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.
    • 这篇关于ord('q') 和 0xFF 的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆