python实现图像添加泊松噪声和高斯噪声
时间: 2024-02-13 11:32:11 浏览: 328
添加泊松噪声:
```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编写程序,给图像添加高斯噪声、椒盐噪声、泊松噪声、瑞利噪声、指数分布噪声和均匀分布噪声
在Python中,我们可以使用一些库来进行图像处理和添加噪声操作。这里简单介绍如何给图像添加常见类型的噪声:
1. **高斯噪声**(Gaussian Noise):`scikit-image`库中的`random_noise`函数可以生成高斯噪声。例如:
```python
from skimage.util import random_noise
image = ... # 你的图像数据
noisy_image = random_noise(image, var=0.05) # 设置噪声的标准差
```
2. **椒盐噪声**(Salt and Pepper Noise):可以使用`skimage`的`salt_and_pepper`函数:
```python
from skimage.exposure import additive_noise
noisy_image = additive_noise(image, amount=0.05)
```
3. **泊松噪声**(Poisson Noise):`poisson_noise`函数适用于像素值为整数的情况:
```python
import scipy.stats as stats
noisy_image = stats.poisson(image).rvs()
```
4. **瑞利噪声**(Rayleigh Noise):较少直接提供现成函数,需要自定义或用其他库如`numpy`生成随机复数数组然后取模:
```python
import numpy as np
noisy_image = np.abs(np.random.normal(0, image.std(), image.shape)) + image
```
5. **指数分布噪声**(Exponential Noise):同样不是直接提供的,你需要生成符合指数分布的随机值再调整到合适范围:
```python
import scipy.stats as stats
expon = stats.expon(scale=image.std())
noisy_image = image + expon.rvs(image.size).reshape(image.shape)
```
6. **均匀分布噪声**(Uniform Noise):类似指数噪声,可以生成均匀分布随机数:
```python
uniform = np.random.uniform(-image.std(), image.std(), image.shape)
noisy_image = image + uniform
```
在每种噪声之后,别忘了检查结果并可能对图像进行归一化或保存。
编写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()
```
请注意,这只是一个示例程序,你可以根据自己的需求进行修改和扩展。
阅读全文