opencv读取avi视频并抽取帧

来源:互联网 发布:淘宝刚买就降价补差价 编辑:程序博客网 时间:2024/06/10 06:10

opencv读取avi视频并抽取帧

             /
#include "stdafx.h"
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    //cvNamedWindow("test", CV_WINDOW_AUTOSIZE);
    CvCapture* capture = cvCreateFileCapture("G://test.avi");//cvcapture 和 vediocapture 区别:一个是c一个是c++
    //获取视频总帧数
    int numFrames = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
    cout << "视频总帧数为:" << numFrames << endl;
    //获取视频fps
    int fps = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    cout << "fps:" << fps << endl;

    IplImage* frame;
    int pos = 0;
    while (1)
    {
        //抽取帧
        cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, pos);
        cout << pos << "/" << numFrames << endl;
        frame = cvQueryFrame(capture);
        
        //将IplImage转化为mat,方便以后进行处理
        Mat mat = cvarrToMat(frame);
        imshow("test", mat);

        char c = cvWaitKey(33);
        if (c == 27)
            break;

        //调整抽取帧的位置
        pos+=24;

    }
    cvReleaseCapture(&capture);
    cvDestroyWindow("test");
}

阅读全文
0 0