Opencv cv :: waitKey()返回值 [英] Opencv cv::waitKey() return value

查看:164
本文介绍了Opencv cv :: waitKey()返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试一些在Ubuntu 14上使用OpenCV的C ++代码,这些代码已知可以在Ubuntu 12上使用,并且可能与其他OpenCV库构建一起使用.

I'm debugging some C++ code that use OpenCV on Ubuntu 14, which is known to work On Ubuntu 12 and maybe with other OpenCV library build.

以前是什么

int key_pressed = waitKey(0);
cout << "key_pressed " << int(key_pressed) << endl;
switch( key_pressed )
{
    case 27: //esc
    {
        //close all windows and quit
        destroyAllWindows();
    }

    ...

但是此代码不起作用,在输出中我有key_pressed 1048603

But this code is not working and in output I have key_pressed 1048603

此代码有效:

char key_pressed = cv::waitKey();
cout << "key_pressed " << int(key_pressed) << endl;
switch( key_pressed )
{
    case 27: //esc
    {
        //close all windows and quit
        destroyAllWindows();
    }

    ...

此代码有效,在输出中我有key_pressed 27

This code is working and in output I have key_pressed 27

这种行为的原因是什么?

What can be reason of such behaviour?

P.S. 文档说 cv :: waitKey()返回int,那么为什么我们应该将其转换为char?

P.S. documentation says that cv::waitKey() return int, so why we should convert it to char?

推荐答案

此功能高度依赖于操作系统:/其中一些会对整数添加一点....

This function is highly dependent on the operating system :/ some of them add a bit to the integer....

它应该返回按键的ASCII码,例如,27ESC键的ASCII码...

it should return the ascii code of the key press, for example, 27 is the ascii code for the ESC key...

现在,问题是要知道将int强制转换为char时会发生什么.

Now, the problem would be to know what happens when you cast an int to a char.

在这种情况下:这是实现定义的....(这就是标准所说的),这里是

In this case: It is implementation-defined.... (That is what the standard says) here is a link to a similar case

一些解决方案:

1)将其放入char变量中...即使它是实现定义的,也似乎是最常见的工作解决方案之一(在某些opencv示例中,他们使用它)

1) put it into a char variable... even though it is implementation-defined it seems that is one of the most common working solutions (in some of the opencv samples they use it)

2)使用int key = cv::waitKey(1) & 255.它将消除多余的位...

2) use int key = cv::waitKey(1) & 255. It will eliminate the extra bits...

要进一步说明,请检查值:

To go a little bit further, lets check the values:

You obtained as an int: 1048603
in binary it will be: 00000000 00010000 00000000 00011011
27 in binary is:      00000000 00000000 00000000 00011011

如您所见,它们在唯一的地方有所不同....最安全,最可移植的方法是使用按位逻辑运算(例如我的2号解决方案)将其删除.其他人则使用一些十六进制值而不是255,例如0xEFFFFF,该值将以二进制形式显示

As you can see they differ in that lone bit.... The safetest and most portable way is to remove it with a bitwise logic operation, such as my number 2 solution. Other people use some hex value instead of 255, such as 0xEFFFFF that in binary it will be

00000000 11101111 11111111 11111111

为什么会这样?

我曾经搜索过一次,如果您再次激活numslockcapslockctrl键,似乎某些位会发生变化...这又取决于平台.

I searched for this once, it seems that some of the bits changes if you have numslock or capslock or ctrl key active... again, this is platform dependent.

这篇关于Opencv cv :: waitKey()返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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