最近看电视,当主人公回忆其之前的的美好生活时,之前的美好生活画面会若影若现的显示出来,和当前画面重叠在一起。
本人准备两张照片,首先可以用ffmpeg将两张的尺寸调整成宽高一致。
然后用cximage分别提取相同位置的rgb值,做权重叠加,比如权重为1:1时,则第一张图片的rgb0.5+第二张图片的rgb0.5。
权重为9:1时,则第一张图片的rgb0.9+第二张图片的rgb0.1。
第一张图片如下:
第二张图片如下:
按照1:1合成,效果如下:
代码如下,说明下,本人现在还没真正理解两幅图这样的叠加原理,为何这种权重叠加就能显示出想要的画面;对于单张图片,其各个像素值乘以0.7这样一个小于1的数,生成的图像还能保持原有的轮廓是好理解的,两张图片,感觉难以理解。
// CxImageMerge.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <Windows.h>
#include "cximage/ximage.h"
std::wstring string2wstring(const std::string& str)
{
std::wstring result;
//获取缓冲区大小,并申请空间,缓冲区大小按字符计算
int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
TCHAR* buffer = new TCHAR[len + 1];
//多字节编码转换成宽字节编码
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
buffer[len] = '\0'; //添加字符串结尾
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return result;
}
int main()
{
CxImage image(CXIMAGE_FORMAT_JPG);
image.Load(L"E:\\learn\\c++\\cximage\\CxImageTest\\x64\\Release\\beauty.jpg", CXIMAGE_FORMAT_JPG);
CxImage imageBack(CXIMAGE_FORMAT_JPG);
imageBack.Load(L"E:\\learn\\c++\\cximage\\CxImageTest\\x64\\Release\\beauty_back.jpg", CXIMAGE_FORMAT_JPG);
int iJpgWidth = image.GetWidth();
int iJpgHeight = image.GetHeight();
int iJpgWidthBack = imageBack.GetWidth();
int iJpgHeightBack = imageBack.GetHeight();
if (iJpgWidth != iJpgWidthBack || iJpgHeight != iJpgHeightBack)
{
return -1;
}
long x1 = 0;
long y1 = 0;
image.GetOffset(&x1, &y1);
for (int i = 0; i < iJpgWidth; i++)
{
for (int j = 0; j < iJpgHeight; j++)
{
RGBQUAD rgb = image.GetPixelColor(i, (iJpgHeight - 1) - j, false);
RGBQUAD rgbBack = imageBack.GetPixelColor(i, (iJpgHeight - 1) - j, false);
int iRed = rgb.rgbRed * 0.5 + rgbBack.rgbRed * 0.5;
int iGreen = rgb.rgbGreen * 0.5 + rgbBack.rgbGreen * 0.5;
int iBlue = rgb.rgbBlue * 0.5 + rgbBack.rgbBlue * 0.5;
COLORREF color = COLORREF(RGB(iRed, iGreen, iBlue));
image.SetPixelColor(i, (iJpgHeight - 1) - j, color);
}
}
std::string strSavePath = "E:\\learn\\c++\\cximage\\CxImageTest\\x64\\Release\\";
char szPicName[100] = { 0 };
sprintf(szPicName, "beauty_0.7.jpg");
strSavePath = strSavePath + szPicName;
std::wstring strwSavePath = string2wstring(strSavePath);
image.Save(strwSavePath.c_str(), CXIMAGE_FORMAT_JPG);
return 0;
}