Feng Qinping
Feng Qinping

Reputation: 49

Reading video file with OpenCV frame by frame

I have wrote these code:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace std;

int main()
{
    cv::namedWindow( "Example 2-3", cv::WINDOW_AUTOSIZE );
    cv::VideoCapture cap;
    cap.open("/Testing/test.avi");
    if(cap.isOpened()==0)
    {
        cout<<"The video file cannot be opened."<<endl;
        return -1;
    }
    cv::Mat frame;
    cap >> frame;
    std::cout<<frame.empty()<<std::endl;  // I have a problem here: Why the result is 1?
    for(;;)
    {
        cap >> frame;
        if( frame.empty() ) break; // Ran out of film
        cv::imshow( "Example 2-3", frame );
        if( (char)cv::waitKey(33) >= 0 ) break;
    }
    return 0;
}

I'm trying to show the video frame by frame, when I open the video by cap.open, it is successfully opened, but when the program executing the code std::cout<<frame.empty()<<std::endl;, it supposed to be 0, because the video is successfully opened, so the frame shouldn't be empty, but the result is 1, which mean the frame is empty, so the video cannot be showed, why? Do you have any solution?

Upvotes: 0

Views: 2422

Answers (1)

Loi Ly
Loi Ly

Reputation: 154

In this case, as far as I know, you should check your video file. Also, try debugging to view what your cap contains after opening the file, for example the frame count, or fps, etc.

Open your video file successfully does not mean your file is good. Sometimes your video file is corrupted (when download, or when decode, etc) will lead to some frames may be empty.

Try another video files to check whether it get different result or not.

Upvotes: 1

Related Questions