简体   繁体   中英

Playing a video in OpenCV

I am a beginner to OpenCV and I wish to play a video in OpenCV. I've made a code but it's displaying a single image only. I am using OpenCV 2.1 and Visual Studio 2008. I would really appreciate it if someone guided me where am I going wrong. Here is my pasted code:

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

int main()
{
CvCapture* capture = cvCaptureFromAVI("C:/OpenCV2.1/samples/c/tree.avi");
IplImage* img = 0; 
if(!cvGrabFrame(capture)){              // capture a frame 
printf("Could not grab a frame\n\7");
exit(0);}
cvQueryFrame(capture); // this call is necessary to get correct 
                   // capture properties
int frameH    = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
int frameW    = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
int fps       = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int numFrames = (int) cvGetCaptureProperty(capture,  CV_CAP_PROP_FRAME_COUNT);
///numFrames=total number of frames



printf("Number of rows %d\n",frameH);
printf("Number of columns %d\n",frameW,"\n");
printf("frames per second %d\n",fps,"\n");
printf("Number of frames %d\n",numFrames,"\n");

for(int i=0;i<numFrames;i++)
{
IplImage* img = 0;
img=cvRetrieveFrame(capture); 
cvNamedWindow( "img" );
cvShowImage("img", img);

}
cvWaitKey(0);
cvDestroyWindow( "img" );
cvReleaseImage( &img );
cvReleaseCapture(&capture);


return 0;
}

You have to use cvQueryFrame instead of cvRetrieveFrame . Also as pointed out by @Chipmunk, you have to add a delay after cvShowImage .

#include "stdafx.h" 
#include "cv.h"       
#include "highgui.h"
cvNamedWindow( "img" );
for(int i=0;i<numFrames;i++)
{
   IplImage* img = cvQueryFrame(capture); 
   cvShowImage("img", img);
   cvWaitKey(10);
}

Here is the complete method to play a video using OpenCV:

int main()
{
    CvCapture* capture = cvCreateFileCapture("C:/OpenCV2.1/samples/c/tree.avi");

    IplImage* frame = NULL;

    if(!capture)
    {
        printf("Video Not Opened\n");
        return -1;
    }

    int width = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH);
    int height = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT);
    double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    int frame_count = (int)cvGetCaptureProperty(capture,  CV_CAP_PROP_FRAME_COUNT);

    printf("Video Size = %d x %d\n",width,height);
    printf("FPS = %f\nTotal Frames = %d\n",fps,frame_count);

    while(1)
    {
        frame = cvQueryFrame(capture);

        if(!frame)
        {
            printf("Capture Finished\n");
            break;
        }

        cvShowImage("video",frame);
        cvWaitKey(10);
    }

    cvReleaseCapture(&capture);
    return 0;
}

After showing a image on the window, there has to be a delay or a wait before the next image can be show, I think you can guess why that is. Okay so for that delay we use cvWaitKey() . And thats what I have added in the code in the loop.

cvNamedWindow( "img" );
for(int i=0;i<numFrames;i++)
{
   IplImage* img = 0;
   img=cvRetrieveFrame(capture); 

   cvShowImage("img", img);
   cvWaitKey(10); 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM