我从我的网络摄像头(内置于HP dv5)中捕获图像时遇到问题。唯一的结果是灰色屏幕。代码如下:使用OpenCV从相机捕获灰色屏幕
#include "StdAfx.h"
#include "cv.h"
#include "highgui.h"
#include <stdio.h> // A Simple Camera Capture Framework
int main() {
CvCapture* capture;
for (int i = -1;i < 100;i++) {
capture = cvCaptureFromCAM(i);
if(!capture) {
fprintf(stderr, "ERROR: capture is NULL \n");
} else {
break;
}
}
//cvSetCaptureProperty(capture, CV_CAP_PROP_FPS,15);
//cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 160);
//cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 120);
// Create a window in which the captured images will be presented
cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE); // Show the image captured from the camera in the window and repeat
while(1) { // Get one frame
IplImage* frame = cvQueryFrame(capture);
cvGrabFrame(capture);
frame = cvRetrieveFrame(capture);
if(!frame) {
fprintf(stderr, "ERROR: frame is null...\n");
getchar();
break;
} else {
fprintf(stderr, "OK\n");
}
cvShowImage("mywindow", frame); // Do not release the frame!
//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), //remove higher bits using AND operator
int c = cvWaitKey(100);
} // Release the capture device housekeeping
cvReleaseCapture(&capture);
cvDestroyWindow("mywindow");
return 0;
}
它是来自OpenCV wiki的修改代码。我知道以这种方式发现相机很疯狂,但它不适用于-1或0.我添加了一些额外的属性(已评论),但它无法正常工作。在此先感谢:) 问候, 克里斯
我试图camera_id隔离,但其很奇怪......我用正确的id添加了我的循环cout,但是如果我把它放在cvCaptureFromCAM中,我就不起作用了。它只有当我把线翻倍(它没有第一次找到相机)时才起作用。当我第一次运行该程序时,它发现设备没有任何问题。 使用您的代码时,输出显示“错误:cvQueryFrame失败”相机与其他应用程序正常工作。我使用Win 7. – 2011-03-22 17:50:11
@SyntaxError您正在使用哪种OpenCV版本? – karlphillip 2011-03-22 18:02:04
2.2 - 我认为最新的。随着Visual Studio 2010. – 2011-03-22 18:20:40