OpenCV4.0学习笔记(4)图像混合
原理:
对于一些图像(一般是一张主要图像和张背景图像)我们经常想让他们重叠,即显示背景和较透明的人物轮廓以得到更好的艺术效果。
对于图像中的任意一点像素g(x,y),其值满足
g(x,y)=(1-a)f1(x,y)+af2(x,y)
其中a,(1-a)分别为两幅图像在结果图像上的占比权重。
API
addWeight(src1, alpha, src2,(1.0 - alpha), 0.0, dst)
//图像1,图像1在处理后图像的权重,图像二,图像二权重,gamma值(一般设置0.0),输出图像1
只有一个API而且原理较为简单
但是需要注意的是:
1.alpha为双精度浮点型变量
2.混合图像1,2的格式必须完全一致,大小,色彩空间。
3.gamma值设置时一般为0.0
练习代码
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc, char**argv)
{
/*Mat M(100, 100, CV_8UC3,Scalar(0, 0, 0));
if (M.empty())
{
printf("can't show the image...");
return -1;
}
imshow("output", M);
*/
Mat src1,src2,dst;
src1 = imread("E://text//text.jpg");
src2 = imread("E://text/.jpg");
if (src1.empty())
{
printf("can't show the image1...");
return -1;
}
if (!scr1.data)
{
printf("can't show the image2...");
return -1;
}
double alpha = 0.7;
if (src1.rows == src2.rows && src1.cols == src2.cols && src1.type() == src2.type())
{
addWeighted(src1, alpha, src2, (1.0 - alpha), 0.0, dst);
//add(src1, src2, dst, Mat());
//multiply(src1, src2, dst, 1.0);
imshow("the first image", src1);
imshow("the second image", src2);
imshow("the mixed image", dst);
}
else
{
printf("could not blend images, the size of images is not same...\n");
return -1;
}
waitKey(0);
return 0;
}
运行结果: