每个处理过程都是用代码跟注释进行解释。
使用图片:
代码:
import numpy as np
import cv2 as cv
def img_add():
"""
图像的加法
1、+
超过255时,
a+b = mod(a+b, 256):除以256取余
取模运算,能够使可能相对较大的数值变小,从而变暗
2、cv2.add()
a+b > 255, 直接取值 255
直接取值运算,能够使相对较大的值依然还是最大的值,相对来说,还是明亮的感觉
3、加权和
计算图像像素之和,图像的权重考虑进来
!!! 两个图片必须是大小、类型相同
:return: None
"""
# 1、+
img1 = np.random.randint(0, 256, size=[3, 3], dtype=np.uint8)
img2 = np.random.randint(0, 256, size=[3, 3], dtype=np.uint8)
print("img1 = \n", img1)
print("img2 = \n", img2)
print("img1 + img2 = \n", img1+img2)
# 2、cv2.add()函数
img1 = np.random.randint(0, 256, size=[3, 3], dtype=np.uint8)
img2 = np.random.randint(0, 256, size=[3, 3], dtype=np.uint8)
print("img1 = \n", img1)
print("img2 = \n", img2)
img3 = cv.add(img1, img2)
print("img3 = ", img3)
img4 = cv.imread("lena.jpg")
cv.imshow("img4", img4)
img5 = img4 + img4
cv.imshow("img5", img5)
img6 = cv.add(img4, img4)
cv.imshow("img6", img6)
# 3、 权重和
img7 = np.ones((3, 4), dtype=np.uint8)*100
img8 = np.ones((3, 4), dtype=np.uint8)*10
gamme = 3
img9 = cv.addWeighted(img7, 0.6, img8, 4, gamme)
print("img9 = ", img9)
img10 = cv.imread("111.jpg")
img11 = cv.imread("111.jpg&