从摄像头捕捉并显示图像 - OpenCV的 - Eclipse的 - 视窗 [英] Capture image from webcam and display it - OpenCV - Eclipse - Windows
问题描述
我是新来的OpenCV,我想显示我的摄像头看到跟OpenCV的东西。我使用C语言编码。
I'm new to OpenCV and I want to display what my webcam sees with OpenCV. I'm using the C Coding Language.
我试过这个code:
#include <stdio.h>
#include <cv.h> // Include the OpenCV library
#include <highgui.h> // Include interfaces for video capturing
int main()
{
cvNamedWindow("Window", CV_WINDOW_AUTOSIZE);
CvCapture* capture =cvCreateCameraCapture(-1);
if (!capture){
printf("Error. Cannot capture.");
}
else{
cvNamedWindow("Window", CV_WINDOW_AUTOSIZE);
while (1){
IplImage* frame = cvQueryFrame(capture);
if(!frame){
printf("Error. Cannot get the frame.");
break;
}
cvShowImage("Window",frame);
}
cvReleaseCapture(&capture);
cvDestroyWindow("Window");
}
return 0;
}
我的网络摄像头的指示灯亮起,但结果完全是一种灰色的窗口,没有图像。
My webcam's light turns on, but the result is a completely grey window, with no image.
你能帮助我吗?
推荐答案
您需要添加
cvWaitKey(30);
到,而
-loop。
cvWaitKey(X)/ CV :: waitKey(X)
做了两件事:
- 它等待的 X 毫秒的关键preSS。如果一个关键是在这段时间pssed $ P $,它返回键的ASCII code。否则,它返回
1
。 - 它可以处理任何窗口的事件,如
cvNamedWindow创建窗口()
,或显示与图像cvShowImage()
。
- It waits for x milliseconds for a key press. If a key was pressed during that time, it returns the key's ASCII code. Otherwise, it returns
-1
. - It handles any windowing events, such as creating windows with
cvNamedWindow()
, or showing images withcvShowImage()
.
有关OpenCV的新手常犯的错误是调用 cvShowImage()
通过视频帧的循环,而不遵循了每个战平 cvWaitKey(30 )
。在这种情况下,没有出现在屏幕上,因为highgui从未给定的时间来处理来自 cvShowImage平局请求()
。
A common mistake for opencv newcomers is to call cvShowImage()
in a loop through video frames, without following up each draw with cvWaitKey(30)
. In this case, nothing appears on screen, because highgui is never given time to process the draw requests from cvShowImage()
.
请参阅 OpenCV的 - cvWaitKey()以获得更多信息。
See OpenCV - cvWaitKey( ) for more info.
这篇关于从摄像头捕捉并显示图像 - OpenCV的 - Eclipse的 - 视窗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!