python 产生高斯噪声
时间: 2023-11-07 12:55:23 浏览: 122
Python画图高斯分布的示例
要使用Python生成高斯噪声,可以使用NumPy库中的randn函数。下面是一个示例代码,用于向图像中添加高斯噪声:
import cv2
import numpy as np
def add_gaussian_noise(image_in, noise_sigma):
temp_image = np.float64(np.copy(image_in))
h = temp_image.shape
w = temp_image.shape
noise = np.random.randn(h, w) * noise_sigma
noisy_image = np.zeros(temp_image.shape, np.float64)
if len(temp_image.shape) == 2:
noisy_image = temp_image + noise
else:
noisy_image[:,:,0] = temp_image[:,:,0] + noise
noisy_image[:,:,1] = temp_image[:,:,1] + noise
noisy_image[:,:,2] = temp_image[:,:,2] + noise
return noisy_image
img_path = '/data/depart/label/1/0.png'
img = cv2.imread(img_path, 0)
noise_sigma = 25
noise_img = add_gaussian_noise(img, noise_sigma=noise_sigma)
cv2.imwrite('noise_{}.png'.format(noise_sigma), noise_img)
请注意,此代码假设输入图像是灰度图像。如果输入图像是彩色图像,请将代码中的通道数目适当调整为3。
阅读全文