图像相加
原理:相同大小的图片的对应像素值进行相加再与255进行取模运算
img = (img1 + img2) % 255 #像素值不超过255
- 直接加:
img = img1 + img2
或者img = cv.add(img1, img2)
import cv2 as cv
import numpy as np
img1 = cv.imread(r'C:\Users\74452\Pictures\opencv\test1.jpg')
img2 = cv.imread(r'C:\Users\74452\Pictures\opencv\test2.jpg')
cv.imshow('img1', img1)
cv.imshow('img2', img2)
img = img1 + img2
cv.imshow('img',img)
cv.waitKey()
cv.destroyAllWindows()
- 权重相加:
addWeighted(src1, alpha, src2, beta, gamma)
原理:
g(x,y) = src1*alpha + src2*beta + gamma
- alpha为第一张图src1的权重
- beta= 1 - alpha, 为第二张图src2的权重
- gamma控制所有像素值偏转度
import cv2 as cv
import numpy as np
img1 = cv.imread(r'C:\Users\74452\Pictures\opencv\test1.jpg')
img2 = cv.imread(r'C:\Users\74452\Pictures\opencv\test2.jpg')
cv.imshow('img1', img1)
cv.imshow('img2', img2)
img = cv.addWeighted(img1,0.8,img2,0.2, 0)
cv.imshow('img', img)
cv.waitKey()
cv.destroyAllWindows()