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

用的是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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值