简体   繁体   中英

OpenCV C++ Video Capture does not seem to work

I am using a Mac OS X 10.6 machine. I have OpenCV 2.1 x64 compiled from source using Xcode and its GCC compiler.

I am having trouble using the C++ video reading features of OpenCV. Here is the simple test code I am using (came straight from OpenCV documentation):

#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;
}

The program compiles fine, but when I try to run it, I see the green light on my webcam come on for a few seconds, then the program exits with the error message:

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

Under debug mode, the matrix still seems to be empty after the cap >> frame line.

I get similar behavior when I try to capture from a video file or an image, so it's not the camera. What is wrong, do you think? Anything I can do to make this work?

EDIT: I'd like to add that if I use the C features, everything works fine. But I would like to stick with C++ if I can.

Thanks

I've seen the same problem. When I use the C features, sometimes the similar question also comes up. From the error message of the C code, I think it happened because the camera got a NULL frame. So I think it can be solved in this way:

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

That way it works on my machine.

I encountered the same problem, it seems that the first two attempts to get the video wont return any signal, so if you try to use it you'll get an error, here is how I got around this, simply by adding a counter and checking the size of the video.

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;

}

Hi I got the solution for you :)

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;
        }
    }
} 

Try simplifying the program so that you can identify the exact location of the problem, eg change your loop so that it looks like this:

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;
}

If that works OK then try adding the processing calls back in, one at a time, eg

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;
}

and so on...

Once you've identified the problematic line you can then focus on that and investigate further.

Go to project->project properties->configuration properties->linker->input

In the additional dependencies paste cv210.lib cvaux210.lib cxcore210.lib highgui210.lib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM