OPENCV视频处理,对视频帧进行复制并重新用帧复制和合成新视频

来源:互联网 发布:网络写手赚钱吗 编辑:程序博客网 时间:2024/06/10 01:48

用的是OPENCV249+VS2013平台

为了实现对视频的处理,同时又不是简单的将帧长进行采样,设计了这个程序,将相邻帧进行K因子复制后重新合成帧数据,实际上相当于对视频进行加长和稠密化处理

#include <stdlib.h>#include <stdio.h>#include <math.h>#include <cv.h>#include <highgui.h>void Video_to_Video(char* filename,int K){    CvCapture* capture = cvCaptureFromAVI(filename);    cvQueryFrame(capture);    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);    printf("视频正在分解。视频信息如下:\n \t视频高度 : %d\n\t视频宽度 : %d\n\tfps : %d\n\t视频帧数 : %d\n", frameH, frameW, fps, numFrames);    int i = 0;    IplImage* img = 0;    char image_name[13];    cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);    while (1)    {        img = cvQueryFrame(capture); //获取一帧图片        cvShowImage("mainWin", img); //将其显示        char key = cvWaitKey(20);        sprintf(image_name, "%s%d%s", "image", ++i, ".jpg");//保存的图片名        cvSaveImage(image_name, img); //保存一帧图片        //_mm_pause;        if (i == numFrames) break;    }    system("pause");//******开始合成    //初始化视频编写器,参数根据实际视频文件修改    CvVideoWriter *writer = 0;    int isColor = 1;        writer = cvCreateVideoWriter("out.avi", CV_FOURCC('X', 'V', 'I', 'D'), fps, cvSize(frameW, frameH), isColor);    printf("视频正在合成。视频信息如下:\n \t视频高度 : %d\n\t视频宽度 : %d\n\tfps : %d\n\t视频帧数 : %d\n", frameH, frameW, fps, K*numFrames);    i = 1;    while (i<numFrames)    {        sprintf(image_name, "%s%d%s", "image", i++, ".jpg");        img = cvLoadImage(image_name);        printf("Image_filename:%s", image_name);        if (!img)        {            printf("Could not load image file...\n");            exit(0);        }        char key = cvWaitKey(20);        for (int j = K; j >0;j--)        {            cvWriteFrame(writer, img);        }    }    system("pause");    cvReleaseVideoWriter(&writer);    cvDestroyWindow("mainWin");}int main(int argc, char *argv[]){    char filename[13] = "out.avi";    int K = 2;  //K代表倍数,每帧画面被复制为K倍,K=1表示不对视频进行操作    Video_to_Video(filename, K); //视频转视频,根据因子K决定将每帧画面复制几次    return 0;}
0 0
原创粉丝点击