Python OpenCV cv.WaitKey正确地在Ubuntu模256映射上吐出奇怪的输出 [英] Python OpenCV cv.WaitKey spits back weird output on Ubuntu modulo 256 maps correctly

查看:139
本文介绍了Python OpenCV cv.WaitKey正确地在Ubuntu模256映射上吐出奇怪的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行带有OpenCV 2.2的Ubuntu 11.10(Lenovo T400)(我相信,导入工作就像导入cv2.cv一样,是cv).如果我只是导入简历",也会发生此问题.

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.

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

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

这是我的OpenCV代码:

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 10688688 886816886881868688186868818681868688 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

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模并得出一些合理的结果(只需尝试一下,它就可以正确识别我的所有键),但是,为什么我需要这样做呢?以前它确实没有做任何工作(打印chr(k)会给我一封信).有人有什么想法吗?

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?

推荐答案

模数有效,因为有关键的信息存储在返回值的后8位中. k & 255还将选择最后8位:

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'

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

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

这篇关于Python OpenCV cv.WaitKey正确地在Ubuntu模256映射上吐出奇怪的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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