在OpenCV中播放视频 [英] Playing a video in OpenCV

查看:226
本文介绍了在OpenCV中播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OpenCV的初学者,我想在OpenCV中播放视频。我做了一个代码,但它只显示一个图像。
我使用OpenCV 2.1和Visual Studio 2008.
我真的很感激,如果有人指导我在哪里我会错了。
这是我的粘贴代码:

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;
}


推荐答案

c $ c> cvQueryFrame 而不是 cvRetrieveFrame 。正如@Chipmunk指出的,你必须在 cvShowImage 后添加延迟。

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

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

以下是使用OpenCV播放视频的完整方法:

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;
}

这篇关于在OpenCV中播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆