编写程序,给图像添加高斯噪声、椒盐噪庐、泊松噪声、瑞利噪声、指数分布噪声和均匀分布噪声。
时间: 2023-10-09 20:11:25 浏览: 130
MATLAB实现向图像中添加高斯噪声和椒盐噪声
5星 · 资源好评率100%
很高兴为您解答。要给图像添加不同类型的噪声,您可以使用Python编程语言和OpenCV库来实现。下面是一个示例代码,展示了如何给图像添加高斯噪声、椒盐噪声、泊松噪声、瑞利噪声、指数分布噪声和均匀分布噪声。
```python
import cv2
import numpy as np
def add_gaussian_noise(image, mean, std_dev):
noise = np.random.normal(mean, std_dev, image.shape)
noisy_image = image + noise
noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
return noisy_image
def add_salt_and_pepper_noise(image, salt_prob, pepper_prob):
noisy_image = np.copy(image)
salt_pixels = np.random.rand(*image.shape) < salt_prob
pepper_pixels = np.random.rand(*image.shape) < pepper_prob
noisy_image\[salt_pixels\] = 255
noisy_image\[pepper_pixels\] = 0
return noisy_image
def add_poisson_noise(image):
noisy_image = np.random.poisson(image).astype(np.uint8)
return noisy_image
def add_rayleigh_noise(image, scale):
noise = np.random.rayleigh(scale, image.shape)
noisy_image = image + noise
noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
return noisy_image
def add_exponential_noise(image, scale):
noise = np.random.exponential(scale, image.shape)
noisy_image = image + noise
noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
return noisy_image
def add_uniform_noise(image, low, high):
noise = np.random.uniform(low, high, image.shape)
noisy_image = image + noise
noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
return noisy_image
# 读取图像
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# 添加高斯噪声
gaussian_noise_image = add_gaussian_noise(image, mean=0, std_dev=20)
# 添加椒盐噪声
salt_and_pepper_noise_image = add_salt_and_pepper_noise(image, salt_prob=0.01, pepper_prob=0.01)
# 添加泊松噪声
poisson_noise_image = add_poisson_noise(image)
# 添加瑞利噪声
rayleigh_noise_image = add_rayleigh_noise(image, scale=50)
# 添加指数分布噪声
exponential_noise_image = add_exponential_noise(image, scale=50)
# 添加均匀分布噪声
uniform_noise_image = add_uniform_noise(image, low=-50, high=50)
# 显示添加噪声后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Gaussian Noise Image', gaussian_noise_image)
cv2.imshow('Salt and Pepper Noise Image', salt_and_pepper_noise_image)
cv2.imshow('Poisson Noise Image', poisson_noise_image)
cv2.imshow('Rayleigh Noise Image', rayleigh_noise_image)
cv2.imshow('Exponential Noise Image', exponential_noise_image)
cv2.imshow('Uniform Noise Image', uniform_noise_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
请注意,上述代码中的`image.jpg`是您要添加噪声的图像文件名。您可以根据需要修改文件名和噪声参数。此代码将显示原始图像和添加噪声后的图像。
希望这可以帮助到您!
#### 引用[.reference_title]
- *1* *2* [图像处理之图像噪声和各种噪声的matlab实现](https://blog.csdn.net/qq_44111805/article/details/126346525)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [高斯噪声,椒盐噪声的思想及多种噪声的实现](https://blog.csdn.net/q923714892/article/details/117438922)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文