目的
使用AddWeighted对两幅图像求和
dst=α⋅src1+β⋅src2+γ
图像1
图像2
效果
AddWeighted权重相加
函数说明:使用权重叠加两个数组(Array)。输出类型为CV_32S时,没有进行饱和操作(Saturation),可能得到一个符号不正确的溢出结果。
dst(I)=saturate(src1(I)∗alpha+src2(I)∗beta+gamma)
//函数原型
void AddWeighted(InputArray src1,
double alpha,
InputArray src2,
double beta,
double gamma,
OutputArray dst,
int dtype = -1)
参数 | 说明 |
InputArray src1 | 输入数组1 |
double alpha | 数组1的权重 |
InputArray src2 | 输入数组2 |
double beta | 数组2的权重 |
double gamma | 加到各和的标量(如调暗或调亮) |
OutputArray dst | 输出数组:大小与通道数组与输入一致 |
int dtype | 输出数组的深度,当src1与src2的深度一样时,该值为-1时,结果与src1相同。 |
源码示例
Mat src1;
Mat src2;
string winName = "Linear Blend";
int alpha = 50;
int gamma = 0;
public void Run() {
src1 = Cv2.ImRead(ImagePath.WindowsLogo);
if (src1.Empty()) throw new Exception($"图像打开有误:{ImagePath.WindowsLogo}");
src2 = Cv2.ImRead(ImagePath.LinuxLogo);
if (src2.Empty()) throw new Exception($"图像打开有误:{ImagePath.LinuxLogo}");
var alphaTrackBarName = "alpha%";
var gammaTrackBarName = "gamma";
Cv2.NamedWindow(winName, WindowFlags.AutoSize);
Cv2.CreateTrackbar(alphaTrackBarName, winName, ref alpha, 100, OnChange);
Cv2.CreateTrackbar(gammaTrackBarName, winName, ref gamma, 255, OnChange);
Cv2.WaitKey();
Cv2.DestroyAllWindows();
}
private void OnChange(int pos, IntPtr intPtr) {
double alphaD = alpha / 100D;
double beta = 1 - alphaD;
using var dst = new Mat();
Cv2.AddWeighted(src1, alphaD, src2, beta, gamma, dst, -1);
Cv2.PutText(dst, $"alpha={alphaD.ToString("0.00")},beta={beta.ToString("0.00")},gamma={gamma}", new Point(10, 15), HersheyFonts.HersheySimplex, 0.5, Scalar.Red);
Cv2.ImShow(winName, dst);
}
参考
https://docs.opencv.org/4.7.0/d5/dc4/tutorial_adding_images.html