c++图像处理之图像混合
1、图像混合原理
图像混合的原理如下:
以两张图像为例;
a
a
a 为权值参数,
f
0
(
x
)
f_0(x)
f0(x)表示第一张图像,
f
1
(
x
)
f_1(x)
f1(x)表示第二张图像。
g
(
x
)
g(x)
g(x)表示权重融合后两张图像的形式。
示例图:
结果图:
2、代码实现
图像混合:addWeight(src1, alpha, src2, alpha, beta, dst)
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** args)
{
Mat img1 = imread("G:/testpic/img1.png");
Mat img2 = imread("G:/testpic/img2.png");
Mat dst;
if(!img1.data)
{
printf("the image cannot load");
return -1;
}
if(!img2.data)
{
printf("the image cannot load");
return -1;
}
if(img1.rows == img2.rows && img1.cols == img2.cols && img1.type() == img2.type())
{
double alpha = 0.5;
addWeight(img1, alpha, img2, 1.0-alpha, 0.0, dst);
imshow("img1", img1);
imshow("img2", img2);
namedWindow("output", WINDOW_AUTOSIZE);
imshow("output", dst);
imwrite("G:/testpic/result/imgresult.png", dst);
}
else
{
printf("these images not the sanme");
return -1;
}
waitKey(0);
return 0;
}