功能,将数组中的数据在(a_min, a_max)范围之外的数据切割在,这个范围直接,小于a_min的值修改为a_min ,大于 a_max修改为 a_max,其他值不变
import numpy as np
a= np.array([[[-1,3,5],[3,4,5]],[[1,3,5],[3,4,5]]])
b=np.array([[[1,3,5],[1,1,1]],[[1,3,5],[1,1,1]]])
c = a+b
print(c.shape)
c = np.clip(a,0,3)
print(c.shape)
print(c)
参考:https://blog.csdn.net/sunmingyang1987/article/details/111591712
可以先用clip将数据放在一定的范围内,然后再用astype修改数据类型
import numpy as np
a= np.array([[[-1,3,5],[3,4,5]],[[1,3,5],[3,4,5]]])
print(a.astype(np.uint8))
import numpy as np
import cv2
img = cv2.imread('test_img/0.jpg')
cv2.imshow('img',img)
noise = np.random.randint(0,100,(img.shape[0],img.shape[1],3),dtype=np.uint8)
cv2.imshow('noise',noise)
img_noise = np.clip(img+noise,0,255).astype(np.uint8)
cv2.imshow('img_noise',img_noise)
cv2.waitKey(0)
原图
噪声图片
合并