OPENCV waitKey()方法返回类型 [英] OPENCV waitKey() method return type

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

问题描述

我正在尝试使用在线教程来学习针对图像处理项目的OPENCV.

I am trying to learn OPENCV for an image processing project using online tutorials.

opencv文档说waitKey()返回一个int.这应该是所按下键的ASCII值.但是大多数在线教程使用下面的代码进行编译和运行.

The opencv documentation says that waitKey() returns an int. This is supposed to be the ASCII value of the key pressed. But most tutorials online use the following code which compiles and runs fine.

if ( (char)27==waitKey(1) ) break;

这表明waitKey返回一个char而不是一个int. 有人可以解释吗?

This suggests that waitKey returns a char and not an int. Can someone please explain?

推荐答案

cv::waitKey()返回int. char key = cv::waitKey(1)起作用的原因是由于C ++中的隐式转换.在这种情况下,cv::waitKey()的类型为int的返回值隐式转换为char,然后分配给key.请参阅此链接以供参考.以下语句是等效的:

cv::waitKey() returns an int. The reason char key = cv::waitKey(1) works is due to implicit conversions in C++. In this case the return value of type int for cv::waitKey() is implicitely converted to char and then assigned to key. See this link for reference. The following statements are equivalent:

char key = (char) cv::waitKey(30); // explicit cast
char key = cv::waitKey(30);        // implicit cast

对于if ((char)27 == waitKey(1)) break;waitKey(1)的输出可能会隐式转换为char,然后与esc字符(ASCII代码27)进行比较.我将使用显式转换将其重写,以避免产生歧义.

In the case of if ((char)27 == waitKey(1)) break;, the output of waitKey(1) is probably implicitly converted to char and then compared to esc character (ASCII code 27). I would re-write it with explicit conversion to avoid ambiguity.

if ( (char)27 == (char) waitKey(1) ) break;

我在OpenCV示例cpp文件中看到它的方式:

The way I see how it's often done in OpenCV sample cpp files:

char key = (char) cv::waitKey(30);   // explicit cast
if (key == 27) break;                // break if `esc' key was pressed. 
if (key == ' ') do_something();      // do_something() when space key is pressed

以下也是可行的,但是第一种方法更加简洁:

The following is also possible but the first approach is much cleaner:

int key = cv::waitKey(30) & 255; // key is an integer here
if (key == 27) break;            // break when `esc' key is pressed

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

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