使用OpenCV从.avi视频获取帧 [英] Getting frames from .avi video using OpenCV

查看:337
本文介绍了使用OpenCV从.avi视频获取帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv)
{
CvCapture* capture=0;
IplImage* frame=0;

capture = cvCaptureFromAVI("C:\\boy walking back.avi"); // read AVI video
if( !capture )
    throw "Error when reading steam_avi";

cvNamedWindow( "w", 1);

for( ; ; )
{
/*  int cvGrabFrame (CvCapture* capture);
    IplImage* cvRetrieveFrame (CvCapture* capture)*/
    frame = cvQueryFrame( capture );
if(!frame)
        break;
    cvShowImage("w", frame);

}
cvWaitKey(0); // key press to close window
cvDestroyWindow("w");
cvReleaseImage(&frame);
}

我正在将openCV与VS2008一起使用.我已经读了一个视频文件,并使用CV_CAP_PROP_FRAME_COUNT来获取帧数,​​对于4秒长的视频剪辑,该帧数约为130.我正在对行走进行运动识别,因此我需要每隔5帧获得一次,因为在5帧之间,身体的运动几乎没有变化.到目前为止,我有一个程序可以让我获取视频剪辑的一帧.但是,我无法获得不同的帧,而且我不确定如何获取其他5帧.上面是用于获取视频一帧的代码.

I am using openCV with VS2008. I have read in a video file and used CV_CAP_PROP_FRAME_COUNT to obtain the number of frames which was approximately 130 for a 4 second long video clip. I am doing a motion recognition of walking so I need to get every other 5 frames since between 5 frames, there is little change in the motion of the body. I have a program so far which allows me to obtain one frame of the video clip. However, I am unable to obtain different frames and also, I am not sure how to go about getting every other 5 frames. The above is the code used to get one frame of the video.

推荐答案

您应该可以跳过4帧,然后保持第5帧.下面是我写的一个小例子来演示这一点:

You should be able to skip 4 frames, and then keep the 5th frame. Below is a small example I wrote to demonstrate this:

IplImage* skipNFrames(CvCapture* capture, int n)
{
    for(int i = 0; i < n; ++i)
    {
        if(cvQueryFrame(capture) == NULL)
        {
            return NULL;
        }
    }

    return cvQueryFrame(capture);
}


int main(int argc, char* argv[])
{
    CvCapture* capture = cvCaptureFromFile("../opencv-root/samples/c/tree.avi");

    IplImage* frame = NULL;
    do
    {
        frame = skipNFrames(capture, 4);
        cvNamedWindow("frame", CV_WINDOW_AUTOSIZE);
        cvShowImage("frame", frame);
        cvWaitKey(100);
    } while( frame != NULL );

    cvReleaseCapture(&capture);
    cvDestroyWindow("frame");
    cvReleaseImage(&frame);

    return 0;
}

希望有帮助:)

这篇关于使用OpenCV从.avi视频获取帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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