2

Please help me in achieving above task.I'm newbie to openCV. I have OpenCV 2.2 installed in my system and using VC++ 2010 Express as IDE. I don't have inbuilt webcam in my laptop... just i learnt how to load image. I'm very eager to load a video file from my disk(preferably mp4 , flv format) and wish to play it using openCV.

Easyboy
  • 23
  • 1
  • 5
  • If you can edit this question to indicate any attempt on your part, feel free to flag it for moderator attention to be reviewed. – Tim Post Aug 08 '11 at 18:18

2 Answers2

1

Using the C interface of OpenCV (which have worked better for me on Windows boxes), the function to load the video file is cvCaptureFromAVI(). After that, you need to use the traditional loop to retrieve frames throughcvQueryFrame() and then cvShowImage() to display them on a window created with cvNamedWindow().

CvCapture *capture = cvCaptureFromAVI("video.avi");
if(!capture)
{   
    printf("!!! cvCaptureFromAVI failed (file not found?)\n");
    return -1; 
}   

int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
printf("* FPS: %d\n", fps);

cvNamedWindow("display_video", CV_WINDOW_AUTOSIZE);

IplImage* frame = NULL;
char key = 0;

while (key != 'q')
{   
    frame = cvQueryFrame(capture);
    if (!frame)
    {   
        printf("!!! cvQueryFrame failed: no frame\n");
        break;
    }

    cvShowImage("display_video", frame);

    key = cvWaitKey(1000 / fps);
}

cvReleaseCapture(&capture);
cvDestroyWindow("display_video");

This blog post brings a little extra info on the task you are trying to accomplish.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Thanks alot KarlPhilip,,,, the above code works fine.... I'm new to openCV but would like to know more. Please guide me from where should i start ?? can i get some help files as we use get for MATLAB.. Thanks once again ... – Easyboy Aug 08 '11 at 19:36
  • @mahesh http://stackoverflow.com/questions/5679909/looking-for-opencv-tutorial – karlphillip Aug 08 '11 at 20:06
0

(Hummm... you don't seem to be trying to do something by yourself but anyway)

From the docs:

#include "opencv2/opencv.hpp"

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("frames",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        //ignore below sample, since you only want to play
        //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("frames", frame);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

This is the old way using opencv 1.x apis.

nacho4d
  • 43,720
  • 45
  • 157
  • 240