使用 OpenCV 从 .avi 视频获取帧

发布于 2025-01-04 08:31:09 字数 812 浏览 2 评论 0原文

#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 帧获取一次。上面是用于获取视频一帧的代码。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任性一次 2025-01-11 08:31:09

您应该能够跳过 4 帧,然后保留第 5 帧。下面是我写的一个小例子来演示这一点:

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

希望有帮助:)

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

Hope that helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文