简体   繁体   English

cv2.waitKey(1)和0xff == ord('q')如何工作?

[英]How cv2.waitKey(1) & 0xff == ord('q') works?

How this line works? 这条线是如何工作的?

As I know so far, the output of cv2.waitKey(number) for all every int number is -1 , and 0xff is a hexadecimal number that is equals to 255 in decimal numbers. 到目前为止,我知道所有所有int数的cv2.waitKey(number)的输出都是-1 ,而0xff是一个十六进制数,等于十进制255

-1 & 0xff is equals to 255 in decimal numbers. -1 & 0xff等于255 (十进制数字)。

Also, ord('q') is equal to 113 . 而且, ord('q')等于113

But now, I don't know why 255 == 113 ? 但是现在,我不知道为什么255 == 113

cv2.waitKey(1) returns the character code of the currently pressed key and -1 if no key is pressed. cv2.waitKey(1)返回当前按下的键的字符代码,如果未按下任何键,则返回-1。 the & 0xFF is a binary AND operation to ensure only the single byte (ASCII) representation of the key remains as for some operating systems cv2.waitKey(1) will return a code that is not a single byte. & 0xFF是二进制AND操作,以确保仅保留键的单字节(ASCII)表示,对于某些操作系统, cv2.waitKey(1)将返回不是单字节的代码。 ord('q') always returns the ASCII representation of 'q' which is 113 (0x71 in hex). ord('q')始终返回ord('q')的ASCII表示形式,为113(十六进制为0x71)。

therefore if the user is pressing the q key when cv2.waitKey(1) is evaluated the following will be determined: 因此,如果在评估cv2.waitKey(1)时用户按下q键,将确定以下内容:

cv2.waitKey(1) & 0xFF == cv2.ord('q')
0xXX71 & 0xFF == 0x71
0x71 == 0x71
True

I just finished some OpenCV code and cv2.waitKey(1) & 0xff == ord('q') was one of the pieces that I toyed with multiple times. 我刚刚完成了一些OpenCV代码,而cv2.waitKey(1)和0xff == ord('q')是我多次戏弄的作品之一。

First: 第一:

cv2.waitKey([delay]) cv2.waitKey([delay])

The function waitKey waits for a key event infinitely and the delay is in milliseconds. 函数waitKey无限期地等待键事件,并且延迟以毫秒为单位。 waitKey(0) means forever. waitKey(0)表示永远。

Second: 第二:

The ord() method returns an integer representing a Unicode code point for the given Unicode character. ord()方法返回一个整数,该整数表示给定Unicode字符的Unicode代码点。 In your code you want the user to select the letter 'q' which is translated to the Unicode value of '113.' 在您的代码中,您希望用户选择字母“ q”,该字母被转换为Unicode值“ 113”。

Third: 第三:

0xFF is a hexadecimal constant which is 11111111 in binary. 0xFF是十六进制常量,二进制值为11111111。 It is used to mask off the last 8bits of the sequence and the ord() of any keyboard character will not be greater than 255. 它用于屏蔽序列的最后8位,并且任何键盘字符的ord()都不得大于255。

Here is the code that I'm using, which does not use ord() or & 0xff . 这是我正在使用的代码,它不使用ord()&0xff

def display_facial_prediction_results(image):
  # Display image with bounding rectangles
  # and title in a window. The window
  # automatically fits to the image size.
  cv2.imshow('Facial Prediction', image)

  while (True):
    # Displays the window infinitely
    key = cv2.waitKey(0)

    # Shuts down the display window and terminates
    # the Python process when a specific key is
    # pressed on the window.
    # 27 is the esc key
    # 113 is the letter 'q'
    if key == 27 or key == 113:
        break
  cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM