Python OpenCV cv.WaitKey 在 Ubuntu 模 256 映射上正确返回奇怪的输出

发布于 2025-01-03 07:55:21 字数 1590 浏览 0 评论 0原文

我正在使用 OpenCV 2.2 运行 Ubuntu 11.10 (Lenovo T400)(我相信导入是通过 import cv2.cv as cv 完成的)。如果我只是“导入简历”,也会发生这个问题。

我最近开始遇到这个问题,这有点奇怪。我不知道我做了什么重要的事情,自从它开始发生以来我就重新开始了。我安装了几个程序,但我认为这些不会影响这一点。

当我运行显示的人造图像(只是黑色图像)时,我尝试轮询 cv.WaitKey(10)。它会吐出垃圾。

这是我的OpenCV代码:

import cv2.cv as cv
import time

cv.NamedWindow("camera", 1)
img = cv.CreateImage((400,400), 8, 3)
valkeys = range(1,255)
f = open('/home/andrew/webuploads/keyboardtest', 'wb')
while True:
    cv.ShowImage("camera", img)
    k = cv.WaitKey(10)
    if k is -1:
        pass
    else:
        print 'writing %s' %str(k)
        f.write((str(k)+' '))

f.close()

这是我从程序中得到的输出:

1048678 1048676 1048673 1048691 1048676 1048678 1048689 1048695 1048677 1048688 1048687 1048681 1048677 1048677 1048695 1048624 1048633 1048690 1048633 1048624 1048695 1048677 1048690 1048624 1048633 1048681 1048677 1048681 1048688 1048687 1048677 1048681 1048692 1048688 1048681 1048688 1048687 1048681 1048681 1048688 1048687 1048585 1048687 1048681 1048688 1048687 1048681 1114085 1179728 1179727 1179721 1179728 1179721 1245153 1245289 1179727 1179721 1179727 1179721 1179728 1179727 1245155 1441865 1179728 1179727 1179721 1179728 1179727 1179721 1179728 1179727 1179718 1179721 1179716 1179728 1179727 1179731 1179721 1179713 1179728 1179727 1179687 1179723 1179716 1179736 1179724 1179715 1179734 1179725 1179692 1179736 1179738 1179725 1179715 1179734 1179692 1245155 1441859

现在我可以对这些数字取模 256 并得到一些合理的结果(刚刚尝试过,它正确地识别了我的所有密钥),但是,为什么我需要这样做?它以前确实工作过,没有做任何事情( print chr(k) 会给我一封信)。有人有什么想法吗?

I am running Ubuntu 11.10 (Lenovo T400) with OpenCV 2.2 (I believe as imports are done as import cv2.cv as cv). This problem also happens if i just 'import cv' instead.

I recently started having this problem, and it's kind of a weird one. I don't know anything significant I did, I have restarted since it started happening. I installed a couple programs, but I don't think those would affect this.

When I run with an artificial image showing (just a black image), I try to poll cv.WaitKey(10). It spits back garbage.

Here's my OpenCV code:

import cv2.cv as cv
import time

cv.NamedWindow("camera", 1)
img = cv.CreateImage((400,400), 8, 3)
valkeys = range(1,255)
f = open('/home/andrew/webuploads/keyboardtest', 'wb')
while True:
    cv.ShowImage("camera", img)
    k = cv.WaitKey(10)
    if k is -1:
        pass
    else:
        print 'writing %s' %str(k)
        f.write((str(k)+' '))

f.close()

Here's the output I get from the program:

1048678 1048676 1048673 1048691 1048676 1048678 1048689 1048695 1048677 1048688 1048687 1048681 1048677 1048677 1048695 1048624 1048633 1048690 1048633 1048624 1048695 1048677 1048690 1048624 1048633 1048681 1048677 1048681 1048688 1048687 1048677 1048681 1048692 1048688 1048681 1048688 1048687 1048681 1048681 1048688 1048687 1048585 1048687 1048681 1048688 1048687 1048681 1114085 1179728 1179727 1179721 1179728 1179721 1245153 1245289 1179727 1179721 1179727 1179721 1179728 1179727 1245155 1441865 1179728 1179727 1179721 1179728 1179727 1179721 1179728 1179727 1179718 1179721 1179716 1179728 1179727 1179731 1179721 1179713 1179728 1179727 1179687 1179723 1179716 1179736 1179724 1179715 1179734 1179725 1179692 1179736 1179738 1179725 1179715 1179734 1179692 1245155 1441859

Now I can modulo 256 these numbers and get somewhat sensible results out (just tried it, it correctly identified all my keys), however, why would I need to do this? It did work previously without doing anything (print chr(k) would give me a letter). Anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执妄 2025-01-10 07:55:21

模数起作用是因为有关密钥的信息存储在返回值的最后 8 位中。 A k & 255 还将选择最后 8 位:

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

在 Python 中,chr(n) 将返回与 n 对应的字符。不幸的是,OpenCV 文档没有提供有关此问题的信息< /a>.

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.

居里长安 2025-01-10 07:55:21

由于 Ubuntu 13.04 的当前 OpenCV 软件包 v2.4.2 仍然存在此问题:

k % 256k & 255 会将 -1 以及 1048831 映射到 255。为了区分这两种情况,需要进行额外的检查,例如 key < 0 是必要的。

如果减去 0x100000,则情况并非如此,它将 1048831 映射到 255,将 -1 映射到 -1048577< /code>,意味着“无键”仍然映射到唯一的负值。

k = cv2.waitKey(delay)
k -= 0x100000
if (k == 27):
    print("<Esc>")

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>")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文