2012-05-03 80 views
0

我想使用相機並對輸入流執行圖像處理,但是當程序執行時會出現一個窗口,詢問'Capture Source',之後'OK'或'Apply'什麼也沒有發生。OpenCV 2.2無法打開相機

但是,如果我使用視頻文件而不是相機,程序運行得很好。

下面是代碼:

int MainWindow::on_CameraOpen_triggered() 
{ 
// programming done in Qt and using Opencv 2.2. 
// all the variables are defined & declared in private of header file 
// there is no compilation error 
// return type of the function is 'int' 
// Following program works fine if name of video file is mentioned 
// instead of '0' is the line below 
VideoCapture capture(0); 

if(!capture.isOpened()) 
    return 1; 

    bool stop(false); 
    double rate = capture.get(CV_CAP_PROP_FPS); 
    namedWindow("Extracted Frame"); 
    int delayVideo = 1000/rate; 

    while(!stop) 
    { 
     if(!capture.read(frame)) 
     { 
      break; 
     } 
     imshow("Extracted frame", frame); 
     if(waitKey(delayVideo)>=0) 
     { 
      stop = true; 
     } 
    } 

capture.release(); 
} 

我試圖以去除在下面的鏈接糾正錯誤: https://code.ros.org/trac/opencv/changeset/4400

https://code.ros.org/trac/opencv/browser/trunk/opencv/modules/highgui/src/precomp.hpp?rev=4400

拍照效果上的gtalk等拍照軟件精。

請建議/指導可以做些什麼。

非常感謝。

問候, DBS

回答

0

試試這個代碼:

# include <opencv2/highgui/highgui.hpp> 

CvCapture *_capture; 


_capture = cvCaptureFromCAM(-1); 
//_capture = cvCaptureFromFile("test1.mp4"); 
if (!_capture) 
{ 
    //"Unable capture video" 
    return 1; 
} 

double rate = cvGetCaptureProperty(_capture, CV_CAP_PROP_FPS); 

//... 

cv::Mat = cvQueryFrame(_capture); 

//... 
+0

嗨,你可以解釋一下/澄清在你提到的代碼中的'// ...'部分應該有什麼w.r.t.在我的代碼在後。我從未在2.2之前使用過任何版本的opencv – DBS

0

這裏是你可以用它來檢查,如果一切與OpenCV的正常工作的另一個小例子:

#include <opencv2/opencv.hpp> 
#include <opencv2/highgui/highgui.hpp> 

int main() { 
    cv::VideoCapture cam = cv::VideoCapture(0); 
    cv::Mat frame; 
    cv::namedWindow ("Demo", CV_WINDOW_AUTOSIZE); 

    while (1) { 
     cam >> frame; 
     imshow ("Demo", frame); 
    } 

    cam.release(); 
    return 0; 
} 

它嘗試看看問題出在OpenCV還是你的Qt程序。