图像混合的计算公式如下:
g(x) = (1-α)f0(x) +αf1(x)
通过修改α的值(0 → 1),可以实现混合
函数cv2.addWeighted()可以按下面的公式对图片进行混合操作。
dst = α·img1 + β·img2 + γ
这里 γ取为零。
图像的混合:将两幅图像按照不同的比例进行混合
cv.addWeighted()
注意:要求两幅图像大小相同。
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1.读取图像
img1 = cv.imread("1.png")
img2 = cv.imread("2.png")
# 2.图像混合
img3 = cv.addWeighted(img1, 0.7, img2, 0.3, 0) # 图片img1所占比重0.7;图片img2所占比重0.3
# 3.图像显示
plt.figure(figsize=(8, 8))
plt.imshow(img3[:, :, ::-1])
plt.show()