利用python和opencv批量给图像加噪声(椒盐噪声、高斯噪声、随机噪声)
导入头文件
import os
import cv2
import numpy as np
import random
添加椒盐噪声
def sp_noise(noise_img, proportion):
'''
添加椒盐噪声
proportion的值表示加入噪声的量,可根据需要自行调整
return: img_noise
'''
height, width = noise_img.shape[0], noise_img.shape[1]#获取高度宽度像素值
num = int(height * width * proportion) #一个准备加入多少噪声小点
for i in range(num):
w = random.randint(0, width - 1