c/c++_渐变动画制作_合成视频

1、渐变动画制作原理:

两幅图像加权相加,img = a*img1 + (1-a)img2,效果从img1变到img2,取值从1到0,取值间隔可以随意,间隔越小,视觉上越自然。

2、代码 (c/c++_渐变动画制作_合成视频)

    opencv版本是:3.4.5

#include <opencv2/opencv.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;


void gradient(cv::Mat& im1_src, cv::Mat& im2_src, VideoWriter writer, const char* save_pth_video)
{
cv::resize(im1_src, im1_src, cv::Size(im2_src.cols, im2_src.rows), 0, 0, cv::INTER_CUBIC);

cv::Mat im_src_blur1, im_src_blur2, im_src_blur3, im_src_blur4, im_src_blur5, im_src_blur6, im_src_blur7, im_src_blur8, im_src_blur9;
cv::Mat im_src_blur10, im_src_blur0;

im_src_blur0 = 1 * im1_src + 0 * im2_src;
im_src_blur1 = 0.9*im1_src + 0.1*im2_src;
im_src_blur2 = 0.8*im1_src + 0.2*im2_src;
im_src_blur3 = 0.7*im1_src + 0.3*im2_src;
im_src_blur4 = 0.6*im1_src + 0.4*im2_src;
im_src_blur5 = 0.5*im1_src + 0.5*im2_src;
im_src_blur6 = 0.4*im1_src + 0.6*im2_src;
im_src_blur7 = 0.3*im1_src + 0.7*im2_src;
im_src_blur8 = 0.2*im1_src + 0.8*im2_src;
im_src_blur9 = 0.1*im1_src + 0.9*im2_src;
im_src_blur10 = 0 * im1_src + 1 * im2_src;

cv::Size size = im_src_blur10.size();
writer.open(save_pth_video, CV_FOURCC(‘M‘, ‘J‘, ‘P‘, ‘G‘), 2, size, true);
writer.write(im_src_blur0);
writer.write(im_src_blur1);
writer.write(im_src_blur2);
writer.write(im_src_blur3);
writer.write(im_src_blur4);
writer.write(im_src_blur5);
writer.write(im_src_blur6);
writer.write(im_src_blur7);
writer.write(im_src_blur8);
writer.write(im_src_blur9);
writer.write(im_src_blur10);
}

int main() {
const char* save_pth_video = "a_result.avi";
const char* im1_pth = "./pic/1.png";
const char* im2_pth = "./pic/2.png";

cv::Mat im1_src = cv::imread(im1_pth);
cv::Mat im2_src = cv::imread(im2_pth);
VideoWriter writer;
gradient(im1_src, im2_src, writer, save_pth_video);

return 0;
}

直接运行,就可以查看效果。

我采用的图像为:

c/c++_渐变动画制作_合成视频c/c++_渐变动画制作_合成视频

相关推荐