介绍
我们都知道在 RGB 色彩空间下,若三个值均为255,则该像素为白色,若三个值均为0,则为黑色,通过这个原理,我们可以随机设置几个点像素值为黑色或者白色,成为该图像的噪点。
代码
import numpy as np
import cv2
# 读取图片
img = cv2.imread(r"E:/tu/13.png", cv2.IMREAD_UNCHANGED)
rows, cols, chn = img.shape
# 加噪声
for i in range(5000):
x = np.random.randint(0, rows)
y = np.random.randint(0, cols)
img[x, y, :] = 255
cv2.imshow("noise", img)
cv2.imwrite("noise.jpg",img)
# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()