Search code examples
c++opencv

Playing a video file in opencv c++


I'm trying to play a video file using the following code.

When run it only shows a black screen with the window name (Video), can anyone help me fix it.

#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2\core\core.hpp>
#include "opencv2/opencv.hpp"
using namespace cv;

int main( int argc, char** argv ) 
{
  CvCapture* capture = cvCreateFileCapture( "1.avi" );
  Mat frame= cvQueryFrame(capture);

  imshow("Video", frame);
  waitKey();
  cvReleaseCapture(&capture);
}

Solution

  • Actually the code you posted won't even compile.

    Just have a look at OpenCV documentation: Reading and Writing images and video

    #include "opencv2/opencv.hpp"
    
    using namespace cv;
    
    int main(int, char**)
    {
    VideoCapture cap(0); // open the default camera
    //Video Capture cap(path_to_video); // open the video file
    if(!cap.isOpened())  // check if we succeeded
        return -1;
    
    namedWindow("Video",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera        
        imshow("Video", frame);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
    }