簡體   English   中英

OpenCV C ++視頻捕獲似乎不起作用

[英]OpenCV C++ Video Capture does not seem to work

我使用的是Mac OS X 10.6機器。 我使用Xcode及其GCC編譯器從源代碼編譯OpenCV 2.1 x64。

我在使用OpenCV的C ++視頻閱讀功能時遇到了麻煩。 這是我正在使用的簡單測試代碼(直接來自OpenCV文檔):

#include "cv.h"
#include "highgui.h"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, CV_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(200) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

程序編譯得很好,但是當我嘗試運行它時,我看到網絡攝像頭上的綠燈亮了幾秒鍾,然后程序退出並顯示錯誤消息:

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /Users/mark/Downloads/OpenCV-2.1.0/src/cxcore/cxarray.cpp, line 2476
terminate called after throwing an instance of 'cv::Exception'
  what():  /Users/mark/Downloads/OpenCV-2.1.0/src/cxcore/cxarray.cpp:2476: error: (-206) Unrecognized or unsupported array type in function cvGetMat

在調試模式下,在cap >> frame line之后,矩陣似乎仍然是空的。

當我嘗試從視頻文件或圖像中捕獲時,我得到類似的行為,因此它不是相機。 有什么不對,你覺得呢? 我能做些什么來使這項工作?

編輯:我想補充說,如果我使用C功能,一切正常。 但是如果可以的話,我想堅持使用C ++。

謝謝

我見過同樣的問題。 當我使用C功能時,有時也會出現類似的問題。 從C代碼的錯誤消息,我認為這是因為相機得到一個NULL幀。 所以我認為可以通過這種方式解決:

do
{
    capture>>frame;
}while(frame.empty());

這樣它就可以在我的機器上運行。

我遇到了同樣的問題,似乎前兩次嘗試獲取視頻不會返回任何信號,所以如果你嘗試使用它你會得到一個錯誤,這就是我如何解決這個問題,只需添加一個計數器和檢查視頻的大小。

int cameraNumber = 0;
if ( argc > 1 )
    cameraNumber = atoi(argv[1]);

cv::VideoCapture camera;
camera.open(cameraNumber);
if ( !camera.isOpened() ) {
    cerr << "ERROR: Could not access the camera or video!" << endl;
    exit(1);
}

//give the camera 40 frames attempt to get the camera object, 
//if it fails after X (40) attemts the app will terminatet, 
//till then it will display 'Accessing camera' note;

int CAMERA_CHECK_ITERATIONS = 40;
while (true) {

    Mat cameraFrame;
    camera >> cameraFrame;
    if ( cameraFrame.total() > 0 ) {
        Mat displayFrame( cameraFrame.size(), CV_8UC3 );
        doSomething( cameraFrame, displayFrame );
        imshow("Image", displayFrame );
    } else {
        cout << "::: Accessing camera :::" << endl;
        if ( CAMERA_CHECK_ITERATIONS > 0 ) CAMERA_CHECK_ITERATIONS--;
        if ( CAMERA_CHECK_ITERATIONS < 0 ) break;
    }


    int key = waitKey(200);
    if (key == 27) break;

}

嗨,我為你找到了解決方案:)

VideoCapture san_cap(0);
if (san_cap.isOpened()) {
    while (1) {



        san_cap.read(san);

        imshow("Video", san);

        Mat frame;
        san_cap.read(frame);      // get a new frame from camera
        cvtColor(frame, edges, CV_BGR2GRAY);

        imshow("Video2", edges);



        int key = cv::waitKey(waitKeyValue);

        if (key == 27 ) {
            break;
        }
    }
} 

嘗試簡化程序,以便確定問題的確切位置,例如更改循環,使其如下所示:

for(;;)
{
    Mat frame;
    cap >> frame; // get a new frame from camera
//  cvtColor(frame, edges, CV_BGR2GRAY);
//  GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
//  Canny(edges, edges, 0, 30, 3);
//  imshow("edges", edges);
    imshow("edges", frame);
    if(waitKey(200) >= 0) break;
}

如果可以正常工作,那么嘗試一次添加一個處理調用,例如

for(;;)
{
    Mat frame;
    cap >> frame; // get a new frame from camera
    cvtColor(frame, edges, CV_BGR2GRAY);
//  GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
//  Canny(edges, edges, 0, 30, 3);
    imshow("edges", edges);
    if(waitKey(200) >= 0) break;
}

等等...

一旦確定了有問題的行,您就可以專注於該行並進一步調查。

轉到project->project properties->configuration properties->linker->input

在附加依賴項中粘貼cv210.lib cvaux210.lib cxcore210.lib highgui210.lib

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM