2012-10-02 51 views
1

我想通過讀取openCV中的攝像頭流來做一些計算機視頻。我已經試過同時與內部網絡攝像頭和外部的USB攝像頭與兩者有類似camorama程序工作的偉大,流光等我的代碼VideoCapture dosnt從網絡攝像頭讀取openCV2.4.0 linux

部分:

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <iostream> 
#include "pic_manipulation.hpp" 
#include "colorDetector.h" 
#include <time.h> 
using namespace std; 


int main (int argc, char **argv){ 
    string file="../test.avi"; 
    cv::VideoCapture capture(0);//les video 
    if(!capture.isOpened()){ 
     cout <<"could not read file"<<endl; 
     return -1; 
    } 
    double rate=capture.get(CV_CAP_PROP_FPS); 
    bool stop(false); 
    cv::Mat frame; 
    cv::namedWindow("Extract frame"); 
    int delay=1000/rate; 
    while(!stop){ 
     if(!capture.read(frame))break; 
     cv::imshow("Extract frame", frame); 
     sleep(delay/1000); 
     if(cv::waitKey(delay)>=0){ 
      cout<<"stop"<<endl; 
      stop=true; 
      capture.release(); 
     } 
     cout<< "one loop finished"<<endl; 
    } 
    return 0; 
}  

當我編譯並運行程序我沒有得到任何錯誤或警告,它只是返回在if(!capture.isOpened())(或者如果我跳過isOpened它返回在下一個if(...))。 它讀取沒有問題的視頻文件。任何人都知道,如果這是我安裝opencv的一些錯誤,或者如果它是導致問題的Linux攝像頭設置? 我使用Linux Mint的建設有cmake的/ G ++

回答

0

項目再看看你的代碼:

cv::VideoCapture capture(0);  // open the default camera 
if(!capture.isOpened())   // check if it succeeded 
{ 
    //... 
} 

isOpened()失敗的指數0的事實告訴你,它不能打開默認相機。既然你已經連接到電腦上的其他相機,你也可以嘗試通過123,...

這是一件好事,check the docs並瞭解每種方法做和回報。

Here is a list of supported webcams,您的某些相機可能不被OpenCV支持。這可以解釋爲什麼默認相機不起作用。

+0

我可能忘了說,我已經嘗試了VideoCapture構造函數(0,1,2,3 ...)的不同輸入,但沒有運氣。內部和USB攝像頭在/ dev /中顯示爲video0和video1。難道兩臺相機都不支持? (我使用的是內置攝像頭的acer timeline3030T和羅技的USB攝像頭) –

+0

它可能是。嘗試傳遞'-1'作爲參數。 – karlphillip

+0

-1也沒有幫助。有趣的是,我可以用python綁定opencv來讀取兩臺攝像機。所以我猜我在做我的C++程序錯誤 –