我從我的網絡攝像頭(內置於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