add_img = cv2.add(img1, img2)
substract_img = cv2.subtract(img1, img2)
multiply_img = cv2.multiply(img1, img2)
div_img = cv2.divide(img1, img2)
and_img = cv2.bitwise_and(img1, img2)
or_img = cv2.bitwise_or(img1, img2)
xor_img = cv2.bitwise_xor(img1, img2)
def addweighted(img1, img2, c):
addweighted_img = cv2.addWeighted(img1, c, img2, 1-c, 2)
show('addweighted_img', addweighted_img)
addWeighted(src1: Any, alpha: Any, src2: Any, beta: Any, gamma: Any, dst: Any = None, dtype: Any = None)
alpha: src1权重
beta: src2权重
gamma: 融合后添加的数值,调节亮度
mask = mask = np.zeros(img1.shape, dtype=np.uint8)
img_changed = cv2.addWeighted(img, c, mask, 1-c, b)
c决定对比度,c>1时,对比度增强,c<1时,对比度减弱
b决定整体亮度,b>0时,亮度增强
import cv2
import numpy as np
img1 = cv2.imread('E:/PycharmProjects/one.jpg')
img2 = cv2.imread('E:/PycharmProjects/two.jpg')
def show(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
show('img1', img1)
show('img2', img2)
# 1.两幅图像算数运算、逻辑运算
def logic_img(img1, img2):
add_img = cv2.add(img1, img2)
show('add', add_img)
substract_img = cv2.subtract(img1, img2)
show('substract', substract_img)
multiply_img = cv2.multiply(img1, img2)
show('multiply_img', multiply_img)
div_img = cv2.divide(img1, img2)
show('div_img', div_img)
and_img = cv2.bitwise_and(img1, img2)
show('and', and_img)
or_img = cv2.bitwise_or(img1, img2)
show('or', or_img)
xor_img = cv2.bitwise_xor(img1, img2)
show('xor', xor_img)
logic_img(img1, img2)
# 2.两幅图像融合
def addweighted(img1, img2, c):
addweighted_img = cv2.addWeighted(img1, c, img2, 1-c, 2)
show('addweighted_img', addweighted_img)
addweighted(img1, img2, 0.5)
# 3.调节亮度和对比度
def brightness_constract(img, c, b):
mask = np.zeros(img1.shape, dtype=np.uint8)*255
img_changed = cv2.addWeighted(img, c, mask, 1-c, b)
show('img_changed', img_changed)
brightness_constract(img1, 1.5, 5)
cv2.destroyAllWindows()