OpenCV入门:(四:混合两张图片)
1. 原理
对两张图片使用如下公式可以得到两张图片的混合图片,
其中f0(x),f1(x)分别是图片1和图片2同一位置的像素点。
2. OpenCV中的AddWeight函数
函数和参数说明: void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1) src1 – first input array. alpha – weight of the first array elements. src2 – second input array of the same size and channel number as src1. beta – weight of the second array elements. dst – output array that has the same size and number of channels as the input arrays. gamma – scalar added to each sum. dtype – optional depth of the output array; when both input arrays have the same depth, dtype can be set to -1, which will be equivalent to src1.depth().
实现的转换公式为:
3. 实现代码
void Blend(const Mat& Src1,const Mat& Src2,double alpha,Mat& Dst) { double beta = 1.0 - alpha; addWeighted(Src1,alpha,Src2,beta,0.0,Dst); namedWindow("原图1"); imshow("原图1",Src1); waitKey(0); namedWindow("原图2"); imshow("原图2",Src2); waitKey(0); namedWindow("混合图"); imshow("混合图",Dst); waitKey(0); }
4. 结果
下图为设置alpha为0.5的处理结果: