加高斯噪声
# 加高斯噪声
def add_gaussian_noise(image, mean, var):
# 图像数组加上一个标准正态分布的随机数数组
noise = np.random.normal(mean, var ** 0.5, image.shape)
# 把随机数数组加到原始图像上
noisy_image = image + noise
# 把图像像素值限制在0~255之间
noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
# 返回添加高斯噪声后的图像
return noisy_image
加椒盐噪声
# 加椒盐噪声
def add_salt_pepper_noise(image, pa, pb):
# 复制原始图像
noisy_image = np.copy(image)
# pa表示盐噪声的概率,pb表示椒噪声的概率
# 每个像素随机生成一个0~1的值,如果小于pa,就把像素值设为255,表示盐噪声
# 如果大于1-pb,就把像素值设为0,表示椒噪声
# 剩下的值不变,表示没有噪声
threshold1 = pa
threshold2 = 1 - pb
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = np.random.rand()
if rdn < threshold1:
noisy_image[i][j] = 255
elif rdn > threshold2:
noisy_image[i][j] = 0
# 返回添加椒盐噪声后的图像
return noisy_image
均值滤波
# 定义空间均值滤波器
def mean_filter(image, kernel_size):
# 构造kernel_size×kernel_size的全1矩阵,表示卷积核
kernel = np.ones((kernel_size, kernel_size), np.float32) / (kernel_size ** 2)
# 使用cv2.filter2D函数进行卷积运算
dst = cv2.filter2D(image, -1, kernel)
# 返回滤波后的图像
return dst
中值滤波
# 定义空间中值滤波器
def median_filter(image, kernel_size):
# 使用cv2.medianBlur函数进行中值滤波
dst = cv2.medianBlur(image, kernel_size)
# 返回滤波后的图像
return dst
计算信噪比
def psnr(img1, img2):
mse = np.mean((img1 - img2) ** 2)
if mse == 0:
return 100
PIXEL_MAX = 255.0
return 20 * np.log10(PIXEL_MAX / np.sqrt(mse))
合集
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 读入原始lena图像
img = cv2.imread('lena.png', cv2.IMREAD_GRAYSCALE)
#cv2.imshow('Original Image', img)
#cv2.waitKey(0)
plt.subplot(131)
plt.imshow(img,cmap='gray')
plt.title('Original Image')
# 加高斯噪声
def add_gaussian_noise(image, mean, var):
# 图像数组加上一个标准正态分布的随机数数组
noise = np.random.normal(mean, var ** 0.5, image.shape)
# 把随机数数组加到原始图像上
noisy_image = image + noise
# 把图像像素值限制在0~255之间
noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
# 返回添加高斯噪声后的图像
return noisy_image
# 加椒盐噪声
def add_salt_pepper_noise(image, pa, pb):
# 复制原始图像
noisy_image = np.copy(image)
# pa表示盐噪声的概率,pb表示椒噪声的概率
# 每个像素随机生成一个0~1的值,如果小于pa,就把像素值设为255,表示盐噪声
# 如果大于1-pb,就把像素值设为0,表示椒噪声
# 剩下的值不变,表示没有噪声
threshold1 = pa
threshold2 = 1 - pb
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = np.random.rand()
if rdn < threshold1:
noisy_image[i][j] = 255
elif rdn > threshold2:
noisy_image[i][j] = 0
# 返回添加椒盐噪声后的图像
return noisy_image
# 定义空间均值滤波器
def mean_filter(image, kernel_size):
# 构造kernel_size×kernel_size的全1矩阵,表示卷积核
kernel = np.ones((kernel_size, kernel_size), np.float32) / (kernel_size ** 2)
# 使用cv2.filter2D函数进行卷积运算
dst = cv2.filter2D(image, -1, kernel)
# 返回滤波后的图像
return dst
# 定义空间中值滤波器
def median_filter(image, kernel_size):
# 使用cv2.medianBlur函数进行中值滤波
dst = cv2.medianBlur(image, kernel_size)
# 返回滤波后的图像
return dst
def psnr(img1, img2):
mse = np.mean((img1 - img2) ** 2)
if mse == 0:
return 100
PIXEL_MAX = 255.0
return 20 * np.log10(PIXEL_MAX / np.sqrt(mse))
# 添加高斯噪声
gauss_mean = 0
gauss_var = 0.05
noisy_img_gaussian = add_gaussian_noise(img, gauss_mean, gauss_var)
#cv2.imshow('Image with Gaussian Noise', noisy_img_gaussian)
#cv2.waitKey(0)
plt.figure(1)
plt.subplot(132)
plt.imshow(noisy_img_gaussian,cmap='gray')
plt.title('Gaussian')
# 添加椒盐噪声
salt = 0.01
pepper = 0.01
noisy_img_salt_pepper = add_salt_pepper_noise(img, salt, pepper)
#cv2.imshow('Image with Salt Pepper Noise', noisy_img_salt_pepper)
#cv2.waitKey(0)
plt.subplot(133)
plt.imshow(noisy_img_salt_pepper,cmap='gray')
plt.title('Salt & Pepper')
plt.show()
# 对添加高斯噪声的图像进行滤波
filtered_img_gaussian_mean3 = mean_filter(noisy_img_gaussian, 3)
filtered_img_gaussian_median3 = median_filter(noisy_img_gaussian, 3)
plt.figure(2)
plt.subplot(131)
plt.imshow(noisy_img_gaussian,cmap='gray')
plt.title('Gaussian')
plt.subplot(132)
plt.imshow(filtered_img_gaussian_mean3,cmap='gray')
plt.title('Mean Filter')
plt.subplot(133)
plt.imshow(filtered_img_gaussian_median3,cmap='gray')
plt.title('Median Filter')
plt.show()
# 对添加椒盐噪声的图像进行滤波
filtered_img_salt_pepper_mean3 = mean_filter(noisy_img_salt_pepper, 3)
filtered_img_salt_pepper_median3 = median_filter(noisy_img_salt_pepper, 3)
plt.figure(3)
plt.subplot(131)
plt.imshow(noisy_img_salt_pepper,cmap='gray')
plt.title('Salt & Pepper')
plt.subplot(132)
plt.imshow(filtered_img_salt_pepper_mean3,cmap='gray')
plt.title('Mean Filter')
plt.subplot(133)
plt.imshow(filtered_img_salt_pepper_median3,cmap='gray')
plt.title('Median Filter')
plt.show()
# 计算信噪比
psnr_gaussian = psnr(img, noisy_img_gaussian)
psnr_salt_pepper = psnr(img, noisy_img_salt_pepper)
psnr_gaussian_mean = psnr(img, filtered_img_gaussian_mean3)
psnr_gaussian_median = psnr(img, filtered_img_gaussian_median3)
psnr_salt_pepper_mean = psnr(img, filtered_img_salt_pepper_mean3)
psnr_salt_pepper_median = psnr(img, filtered_img_salt_pepper_median3)
print('PSNR_Gaussian:',psnr_gaussian)
print('PSNR_Salt_Pepper:',psnr_salt_pepper)
print('PSNR_Gaussian_mean:',psnr_gaussian_mean)
print('PSNR_Gaussian_median:',psnr_gaussian_median)
print('PSNR_Salt_Pepper_mean:',psnr_salt_pepper_mean)
print('PSNR_Salt_Pepper_median:',psnr_salt_pepper_median)