使用OpenCV函数读入、播放视频文件并获取视频相应属性

来源:互联网 发布:mac如何打包文件 编辑:程序博客网 时间:2024/05/18 06:04
 

#include "stdafx.h"

#include <opencv2/opencv.hpp>

#include <string>

 

int _tmain(int argc, _TCHAR* argv[])

{

    cvNamedWindow("ShowAvi", CV_WINDOW_AUTOSIZE);

 

    string strAviName = "F:\\aaa.avi";

 

    CvCapture *capture = cvCreateFileCapture(strAviName.c_str());

 

    if (capture == NULL)

    {

        return 0;

    }

 

    IplImage *frame = NULL;

 

    int iNum=0, iFrameH=0, iFrameW=0, iFps=0, iNumFrames=0;

 

    char ch[10];

    string strImageName;

 

    while (1)

    {

        frame = cvQueryFrame(capture);

 

        if (!frame)

        {

            break;

        }

 

        cvShowImage("ShowAvi", frame);

 

        iNum ++;

 

        if (iNum == 1)

        {

            iFrameH    = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);

            iFrameW    = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);

            iFps       = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);

            iNumFrames = (int) cvGetCaptureProperty(capture,  CV_CAP_PROP_FRAME_COUNT);

        }

 

        itoa(iNum, ch, 10);

 

        strImageName = ch;

 

        strImageName += ".jpg";

 

        //cvSaveImage(strImageName.c_str(), frame);//将每帧图像保存下来

 

        char c = cvWaitKey(33);

 

        if (c == 27)

        {

            break;

        }

    }

 

    cvReleaseCapture(&capture);

    cvDestroyWindow("ShowAvi");

 

    cout<<"一共的帧数为:"<<iNum<<endl;

    cout<<"宽度、高度、每秒录入的帧数(帧率)、总帧数:"<<iFrameW<<"  "<<iFrameH<<"  "<<iFps<<"  "<<iNumFrames<<endl;

    return 0;

}

 

1、cvCreateFileCapture:通过参数设置确定要读入的视频文件,返回一个指向CvCapture结构的指针。这个结构包括了所有关于读入视频文件的信息,其中包含状态信息。在调用这个函数后,返回指针所指向的CvCapture结构被初始化到所对应视频文件的开头。

2、cvQueryFrame:参数为CvCapture结构的指针。用来将下一帧视频文件载入内存(实际是填充或更新CvCapture结构中)。返回一个对应当前帧的指针。与cvLoadImage不同的是,cvLoadeImage为图像分配内存空间,而cvQueryFrame使用已经在cvCapture结构中分配好的内存。这样的话,就没有必要通过cvReleaseImage()对这个返回的图像指针进行释放,当CvCapture结构被释放后,每一帧图像所对应的内存空间即会被释放。

3、cvGetCaptureProperty:查询CvCapture对象的各种属性。

4、cvCreateTrackbar:创建一个滚动条,可以方便地从视频的一帧跳到另外一帧。

 

注:要想能够正确的读入视频文件,需要有相应的视频编码解码的DLL文件!!

 

原创粉丝点击