Python OpenCV cv.WaitKey spits back weird output on Ubuntu modulo 256 maps correctly

白昼怎懂夜的黑 提交于 2019-12-04 03:08:09

The modulus works because the information about the key is stored in the last 8 bits of the return value. A k & 255 will also pick the last 8 bits:

>>> k = 1048678
>>> chr(k & 255)
'f'

In Python, chr(n) will return the character corresponding to n. Unfortunately, OpenCV documentation presents no information about this issue.

As this problem persists with the current OpenCV package v2.4.2 of Ubuntu 13.04:

Both k % 256 and k & 255 would map -1 as well as 1048831 to 255. To distinguish between these two cases an additional check like key < 0 would be necessary.

Not so if you subtract 0x100000, which maps 1048831 to 255 and -1 to -1048577, meaning that "no key" remains mapped to the only negative value.

k = cv2.waitKey(delay)
k -= 0x100000
if (k == 27):
    print("<Esc>")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!