2014-05-04 48 views
1

捕捉視頻我在OSX小牛一臺MacBook Air 2013從相機

#include <iostream> 

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/video/tracking.hpp> 

int main() 
{ 
    cv::VideoCapture cap; 
    cap.open(0); 

    if(!cap.isOpened()) 
    { 
     std::cerr << "***Could not initialize capturing...***\n"; 
     return -1; 
    } 

    cv::Mat frame; 

    while(1){ 
     cap >> frame; 

     if(frame.empty()){ 
      std::cerr<<"frame is empty"<<std::endl; 
      break; 
     } 

     cv::imshow("", frame); 
     cv::waitKey(10); 
    } 

    return 1; 
} 

相機正確初始化(isOpened返回true),但是它一直返回空幀。但是,從文件而不是相機檢索幀可以正常工作。

此外,使用C API的cvQueryFrame似乎工作正常!

關於如何調試我的問題的任何想法?

編輯:下面的代碼似乎讓相機工作正常。有人知道爲什麼

#include <iostream> 

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/video/tracking.hpp> 

using namespace cv; 
using namespace std; 

int main() 
{ 
    VideoCapture cap; 
    cap.open(0); 
    namedWindow("Window"); 

    if(!cap.isOpened()) 
    { 
     std::cerr << "***Could not initialize capturing...***\n"; 
     return -1; 
    } 

    cv::Mat frame; 

    while(1){ 
     cap >> frame; 

     if(!(frame.empty())){ 
      imshow("Window", frame); 
     } 

     waitKey(10); 
    } 

    return 1; 
} 
+0

適合我。運行時您的相機是否成功打開? – herohuyongtao

+0

@herohuyongtao嗯,我不確定。 cap.isOpened()返回true,但是我的相機指示燈未打開。這是什麼意思? – dfg

+0

你使用了哪個OpenCV版本?當你有視頻通話時,你的相機能打開嗎? – herohuyongtao

回答

0

這是我在筆記本電腦上使用OpenCv時遇到的問題。

只需在while之前加上cvWaitKey(6700);循環。

代碼:

#include <iostream> 

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/video/tracking.hpp> 

using namespace cv; 
using namespace std; 

int main() 
{ 
    VideoCapture cap; 
    cap.open(0); 
    namedWindow("Window"); 

    if(!cap.isOpened()) 
    { 
     std::cerr << "***Could not initialize capturing...***\n"; 
     return -1; 
    } 

    cv::Mat frame; 

    waitKey(6700); 

    while(1){ 
     cap >> frame; 

     if(!(frame.empty())){ 
      imshow("Window", frame); 
     } 

     waitKey(25); 
    } 

    return 1; 
} 

希望它應該工作。

+0

嗯。 waitKey(6700)沒有區別。無論是否存在,我的代碼只有在「namedWindow()」調用時纔有效。 – dfg

0

嘗試

while(cap.grab()){ 

     cap.retrieve(frame); 
     waitKey(25); 
    } 

它不會給你空架。