Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading video from file opencv

Tags:

c++

c

opencv

Hi So I have written this code to capture a video from file

#include <stdio.h>
#include <cv.h>
#include "highgui.h"
#include <iostream>

//using namespace cv

int main(int argc, char** argv)
{
    CvCapture* capture=0;
    IplImage* frame=0;
    capture = cvCaptureFromAVI(char const* filename); // read AVI video    
    if( !capture )
        throw "Error when reading steam_avi";

    cvNamedWindow( "w", 1);
    for( ; ; )
    {
        frame = cvQueryFrame( capture );
        if(!frame)
            break;
        cvShowImage("w", frame);
    }
    cvWaitKey(0); // key press to close window
    cvDestroyWindow("w");
    cvReleaseImage(&frame);
}

Everytime I run it, I get the following error:

CaptureVideo.cpp: In function ‘int main(int, char**)’:

CaptureVideo.cpp:13:28: error: expected primary-expression before ‘char’

Any help will be much appreciated.

like image 504
Ikemesit Ansa Avatar asked Dec 04 '12 18:12

Ikemesit Ansa


1 Answers

This is C++ question, so you should use the C++ interface.

The errors in your original code:

  • You forgot to remove char const* in cvCaptureFromAVI.
  • You don't wait for the frame to be displayed. ShowImage only works if it is followed by WaitKey.
  • I'm not sure if capture=NULL would mean that your file was not opened. Use isOpened instead.

I have corrected your code and put it into the C++ interface, so it is a proper C++ code now. My rewrite does line-by-line the same as your program did.

//#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
//#include <iostream>

using namespace cv;
using std::string;

int main(int argc, char** argv)
{
    string filename = "yourfile.avi";
    VideoCapture capture(filename);
    Mat frame;

    if( !capture.isOpened() )
        throw "Error when reading steam_avi";

    namedWindow( "w", 1);
    for( ; ; )
    {
        capture >> frame;
        if(frame.empty())
            break;
        imshow("w", frame);
        waitKey(20); // waits to display frame
    }
    waitKey(0); // key press to close window
    // releases and window destroy are automatic in C++ interface
}
like image 151
Barney Szabolcs Avatar answered Oct 06 '22 07:10

Barney Szabolcs