无法使用 cvWaitKey() 捕获键盘敲击
我这里有问题,需要你的帮助。我一直在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 0xff 掩码在 ESC 按键上返回 27:
或者,您可以转换为字符:
try using a 0xff mask to return 27 on an ESC key press:
Alternatively, you can cast to a character:
问题的原因是 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';
orcout.flush();
after your currentcout
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 :))