无法使用 cvWaitKey() 捕获键盘敲击

发布于 2024-10-20 14:33:02 字数 1298 浏览 3 评论 0原文

我这里有问题,需要你的帮助。我一直在尝试使用 cvWaitKey() 函数捕获键盘敲击。 cvWaitKey(10) 应返回每 10 毫秒按下的键盘敲击次数。 但就我而言,我每 18 秒就会一次获取在这 18 秒期间按下的所有按键。 当我按“esc”(参见代码)时通常应该关闭的窗口仍然打开。

在控制台中我得到这个:

VIDIOC_QUERYMENU:Invalid argument
VIDIOC_QUERYMENU:参数无效
VIDIOC_QUERYMENU:参数无效
-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-11536870939-1-1-1-1-1-1 -1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-111536870939-1-1-1-1-1-1-1-1 -1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 -1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 -1-1-1-1-1-1-1-1-1-1

包含:
1536870939 对应于我按下“esc”时
-1 对应于没有按下任何键时

感谢您的帮助。


#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "iostream"

using namespace std;

int main()
{

   IplImage *src=cvCreateImage(cvSize(640,480), 8, 3);
   CvCapture* capture =cvCaptureFromCAM(CV_CAP_ANY);

   int key;
   while(1){

       src = cvRetrieveFrame( capture );

       cvNamedWindow( "out", CV_WINDOW_AUTOSIZE );
       cvShowImage( "out", src );

       key = cvWaitKey(10);
       cout<<key;

       if( key == 1536870939 ) break; //if 'esc' is pressed (in ubuntu 10.04)
       cvGrabFrame( capture );
    }

cvDestroyAllWindows();
cvReleaseCapture( &capture );
return 0;
}

我正在使用:Opencv 2.2.0,ubuntu 10.04 LTS,CodeBlocks 8.02

I have a problem here and need your help. I've been trying to capture keyboard strokes using cvWaitKey() function.
cvWaitKey(10) should return the keyboard stroke pressed every 10ms.
But in my case, I'm getting every 18 seconds, all the keys that I've pressed during this period of 18 seconds, at once.
And the window that should normally close when I press 'esc' (see the code) is still open.

In console I get this:

VIDIOC_QUERYMENU:Invalid argument
VIDIOC_QUERYMENU:Invalid argument
VIDIOC_QUERYMENU:Invalid argument
-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-11536870939-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-111536870939-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1

With:
1536870939 corresponds to when I pressed 'esc'
-1 correspond to when no key was pressed

Thanks for help.


#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "iostream"

using namespace std;

int main()
{

   IplImage *src=cvCreateImage(cvSize(640,480), 8, 3);
   CvCapture* capture =cvCaptureFromCAM(CV_CAP_ANY);

   int key;
   while(1){

       src = cvRetrieveFrame( capture );

       cvNamedWindow( "out", CV_WINDOW_AUTOSIZE );
       cvShowImage( "out", src );

       key = cvWaitKey(10);
       cout<<key;

       if( key == 1536870939 ) break; //if 'esc' is pressed (in ubuntu 10.04)
       cvGrabFrame( capture );
    }

cvDestroyAllWindows();
cvReleaseCapture( &capture );
return 0;
}

I'm using: Opencv 2.2.0 , ubuntu 10.04 LTS , CodeBlocks 8.02

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

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

发布评论

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

评论(2

烟凡古楼 2024-10-27 14:33:02

尝试使用 0xff 掩码在 ESC 按键上返回 27:

if( (cvWaitKey(10)&0xff) == 27 ) break;

或者,您可以转换为字符:

if( (char)cvWaitKey(10) == 27 ) break;

try using a 0xff mask to return 27 on an ESC key press:

if( (cvWaitKey(10)&0xff) == 27 ) break;

Alternatively, you can cast to a character:

if( (char)cvWaitKey(10) == 27 ) break;
£烟消云散 2024-10-27 14:33:02

问题的原因是 cout 的工作方式:它缓冲字符,并且仅在您发送换行符或明确要求它这样做时才将它们发送到控制台。 (第三种情况:缓冲区已满...但这就是您必须等待 18 秒的情况。)

因此,请将 cout <<; '\n';cout.flush(); 在当前 cout 语句之后。

(顺便说一下:VIDIOC_QUERYMENU:Invalid argument 来自 v4l(网络摄像头)驱动程序...我不知道 1536870939 来自哪里,ESC 应该是 27 :))

The cause of the problem is the way cout works: it buffers characters and only sends them to the console if you send a newline character or if you explicitly ask it to do so. (Third case: the buffer gets full... but that's what you have to wait 18 seconds for.)

So put either cout << '\n'; or cout.flush(); after your current cout statement.

(By the way: VIDIOC_QUERYMENU:Invalid argument is from the v4l (webcam) driver... and I have no idea where 1536870939 comes from, ESC should be 27 :))

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