python实现图像添加泊松噪声和高斯噪声
时间: 2024-02-13 12:32:11 浏览: 240
添加泊松噪声:
```python
import numpy as np
import cv2
img = cv2.imread('image.jpg', 0)
lam = 10 # 控制噪声强度的参数
noise = np.random.poisson(lam, img.shape)
img_noise = np.uint8(np.clip(img + noise, 0, 255))
cv2.imshow('image', img)
cv2.imshow('noise', img_noise)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
添加高斯噪声:
```python
import numpy as np
import cv2
img = cv2.imread('image.jpg', 0)
mean = 0 # 噪声的均值
var = 100 # 噪声的方差
sigma = var ** 0.5 # 噪声的标准差
noise = np.random.normal(mean, sigma, img.shape)
img_noise = np.uint8(np.clip(img + noise, 0, 255))
cv2.imshow('image', img)
cv2.imshow('noise', img_noise)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
相关问题
编写Python程序,给图像添加高斯噪声、椒盐噪声、泊松噪声、瑞利噪声、指数分布噪声和均匀分布噪声
可以使用OpenCV库来对图像添加噪声。下面是一个示例程序,展示如何给图像添加高斯噪声、椒盐噪声、泊松噪声、瑞利噪声、指数分布噪声和均匀分布噪声。
```python
import cv2
import numpy as np
# 添加高斯噪声
def add_gaussian_noise(image):
row, col, ch = image.shape
mean = 0
var = 0.1
sigma = var ** 0.5
gauss = np.random.normal(mean, sigma, (row, col, ch))
gauss = gauss.reshape(row, col, ch)
noisy_image = image + gauss
return noisy_image
# 添加椒盐噪声
def add_salt_and_pepper_noise(image, ratio=0.05):
row, col, ch = image.shape
num_pixels = row * col
num_salt = int(num_pixels * ratio)
coords = [np.random.randint(0, i - 1, num_salt) for i in image.shape]
image[coords] = 1
num_pepper = int(num_pixels * ratio)
coords = [np.random.randint(0, i - 1, num_pepper) for i in image.shape]
image[coords] = 0
return image
# 添加泊松噪声
def add_poisson_noise(image):
vals = len(np.unique(image))
vals = 2 ** np.ceil(np.log2(vals))
noisy_image = np.random.poisson(image * vals) / float(vals)
return noisy_image
# 添加瑞利噪声
def add_rayleigh_noise(image):
row, col, ch = image.shape
sigma = 0.1
rayleigh = np.random.rayleigh(sigma, (row, col, ch))
noisy_image = image + rayleigh
return noisy_image
# 添加指数分布噪声
def add_exponential_noise(image):
row, col, ch = image.shape
scale = 0.1
exponential = np.random.exponential(scale, (row, col, ch))
noisy_image = image + exponential
return noisy_image
# 添加均匀分布噪声
def add_uniform_noise(image):
row, col, ch = image.shape
low = -0.5
high = 0.5
uniform = np.random.uniform(low, high, (row, col, ch))
noisy_image = image + uniform
return noisy_image
# 读取图像
image_path = 'image.jpg'
image = cv2.imread(image_path)
# 添加高斯噪声
gaussian_noisy_image = add_gaussian_noise(image.copy())
# 添加椒盐噪声
salt_and_pepper_noisy_image = add_salt_and_pepper_noise(image.copy())
# 添加泊松噪声
poisson_noisy_image = add_poisson_noise(image.copy())
# 添加瑞利噪声
rayleigh_noisy_image = add_rayleigh_noise(image.copy())
# 添加指数分布噪声
exponential_noisy_image = add_exponential_noise(image.copy())
# 添加均匀分布噪声
uniform_noisy_image = add_uniform_noise(image.copy())
# 显示原图和添加噪声后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Gaussian Noisy Image', gaussian_noisy_image)
cv2.imshow('Salt and Pepper Noisy Image', salt_and_pepper_noisy_image)
cv2.imshow('Poisson Noisy Image', poisson_noisy_image)
cv2.imshow('Rayleigh Noisy Image', rayleigh_noisy_image)
cv2.imshow('Exponential Noisy Image', exponential_noisy_image)
cv2.imshow('Uniform Noisy Image', uniform_noisy_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
请注意,这只是一个示例程序,你可以根据自己的需求进行修改和扩展。
编写程序,给图像添加高斯噪声、椒盐噪声、泊松噪声、瑞利噪声、指数分布噪声和均匀分布噪声。
您可以使用Python中的OpenCV库来给图像添加不同类型的噪声。下面是一个示例代码,演示如何添加高斯噪声、椒盐噪声、泊松噪声、瑞利噪声、指数分布噪声和均匀分布噪声。
```python
import numpy as np
import cv2
def add_gaussian_noise(image, mean=0, stddev=10):
noise = np.random.normal(mean, stddev, image.shape)
noisy_image = np.clip(image + noise, 0, 255).astype(np.uint8)
return noisy_image
def add_salt_pepper_noise(image, salt_prob=0.05, pepper_prob=0.05):
noisy_image = np.copy(image)
salt_coords = np.random.rand(*image.shape) < salt_prob
pepper_coords = np.random.rand(*image.shape) < pepper_prob
noisy_image[salt_coords] = 255
noisy_image[pepper_coords] = 0
return noisy_image
def add_poisson_noise(image):
noise = np.random.poisson(image.astype(np.float32))
noisy_image = np.clip(image + noise, 0, 255).astype(np.uint8)
return noisy_image
def add_rayleigh_noise(image, scale=50):
noise = np.random.rayleigh(scale, image.shape)
noisy_image = np.clip(image + noise, 0, 255).astype(np.uint8)
return noisy_image
def add_exponential_noise(image, scale=50):
noise = np.random.exponential(scale, image.shape)
noisy_image = np.clip(image + noise, 0, 255).astype(np.uint8)
return noisy_image
def add_uniform_noise(image, low=0, high=50):
noise = np.random.uniform(low, high, image.shape)
noisy_image = np.clip(image + noise, 0, 255).astype(np.uint8)
return noisy_image
# 读取图像
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# 添加高斯噪声
gaussian_noise_image = add_gaussian_noise(image)
# 添加椒盐噪声
salt_pepper_noise_image = add_salt_pepper_noise(image)
# 添加泊松噪声
poisson_noise_image = add_poisson_noise(image)
# 添加瑞利噪声
rayleigh_noise_image = add_rayleigh_noise(image)
# 添加指数分布噪声
exponential_noise_image = add_exponential_noise(image)
# 添加均匀分布噪声
uniform_noise_image = add_uniform_noise(image)
# 显示原始图像和添加噪声后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Gaussian Noise Image', gaussian_noise_image)
cv2.imshow('Salt and Pepper Noise Image', salt_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'替换为您想要添加噪声的图像的路径。此代码将在窗口中显示原始图像和添加噪声后的图像。您可以根据需要调整噪声参数。
阅读全文