OpenCV C++ Video Capture does not seem to work

前端 未结 5 763
北荒
北荒 2020-12-18 14:34

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 feat

相关标签:
5条回答
  • 2020-12-18 15:15

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

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

    0 讨论(0)
  • 2020-12-18 15:18

    Try simplifying the program so that you can identify the exact location of the problem, e.g. 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, e.g

    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.

    0 讨论(0)
  • 2020-12-18 15:24

    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;
            }
        }
    } 
    
    0 讨论(0)
  • 2020-12-18 15:28

    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.

    0 讨论(0)
  • 2020-12-18 15:30

    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;
    
    }
    
    0 讨论(0)
提交回复
热议问题