Synthesis, filtering and filtering performance evaluation of noise interference images

Please follow the following process to complete the designated tasks.

 

1. Read the original 256-level grayscale image and map it to a floating point value between 0 and 1.

2. For the above input image (i.e., true value image), program to achieve image synthesis after two types of noise interference.

(1) Additive Gaussian noise interference (mean 0, variance 0.5; variance 1)

(2) Salt and pepper switch noise interference (noise density 0.2; noise density 0.6) Understand how to control the interference degree of each type of noise to the true image; and visualize the images before and after noise interference; estimate the noise interference image relative to the true image MSE value, PSNR value, SSIM value.

3. For the above-mentioned noise-interferenced images, implement image smoothing based on Gaussian filtering and median filtering based on templates of different sizes (such as 3*3, 7*7), display and save the filtering results; and estimate that these filtered images are relatively MSE value, PSNR value, and SSIM value based on the ground truth image.

Code:

from PIL import Image
from pylab import *
import cv2
import numpy as np
from skimage.metrics import structural_similarity as compare_ssim
from skimage.metrics import peak_signal_noise_ratio as compare_psnr
from skimage.metrics import mean_squared_error as compare_mse
# 高版本用法


def gauss_noise(img, mean=0, sigma=25):
    image = np.array(img / 255, dtype=float)  # 将原始图像的像素值进行归一化
    # 创建一个均值为mean,方差为sigma,呈高斯分布的图像矩阵
    noise = np.random.normal(mean, sigma / 255.0, image.shape)
    out = image + noise  # 将噪声和原始图像进行相加得到加噪后的图像
    res_img = np.clip(out, 0.0, 1.0)
    res_img = np.uint8(res_img * 255.0)

    return res_img


def noise(img,snr):
    h=img.shape[0]
    w=img.shape[1]
    img1=img.copy()
    sp=h*w   # 计算图像像素点个数
    NP=int(sp*(1-snr))   # 计算图像椒盐噪声点个数
    for i in range (NP):
        randx=np.random.randint(1,h-1)   # 生成一个 1 至 h-1 之间的随机整数
        randy=np.random.randint(1,w-1)   # 生成一个 1 至 w-1 之间的随机整数
        if np.random.random()<=0.5:   # np.random.random()生成一个 0 至 1 之间的浮点数
            img1[randx,randy]=0
        else:
            img1[randx,randy]=255
    return img1

im = array(Image.open('./lena.bmp'))
img = im
im[:,:,0] = im[:,:,1] = im[:,:,2] = (im[:,:,0] * 0.3 + im[:,:,1] * 0.59 + im[:,:,2] * 0.11)
#imshow(im)
#show()

gaussimg = gauss_noise(im)
#cv2.imshow('gauss',gaussimg)
#cv2.imwrite('./gauss.png'.format(len(gaussimg)),gaussimg)
psnr = compare_psnr(img, gaussimg)
ssim = compare_ssim(img, gaussimg, channel_axis=-1)
mse = compare_mse(img, gaussimg)
print('PSNR:{},SSIM:{},MSE:{}'.format(psnr, ssim, mse))

noiseimage_2 = noise(im,0.2)
noiseimage_6 = noise(im,0.6)
#cv2.imshow('noise 0.2',noiseimage_2)
#cv2.imwrite('./noiseimage_2.png'.format(len(noiseimage_2)),noiseimage_2)
psnr = compare_psnr(img, noiseimage_2)
ssim = compare_ssim(img, noiseimage_2, channel_axis=-1)
mse = compare_mse(img, noiseimage_2)
print('PSNR:{},SSIM:{},MSE:{}'.format(psnr, ssim, mse))

#cv2.imshow('noise 0.6',noiseimage_6)
#cv2.imwrite('./noiseimage_6.png'.format(len(noiseimage_6)),noiseimage_6)
psnr = compare_psnr(img, noiseimage_6)
ssim = compare_ssim(img, noiseimage_6, channel_axis=-1)
mse = compare_mse(img, noiseimage_6)
print('PSNR:{},SSIM:{},MSE:{}'.format(psnr, ssim, mse))

img1 = np.hstack((gaussimg,noiseimage_2,noiseimage_6))

print("基于高斯滤波_3*3")
tmp1 = cv2.GaussianBlur(gaussimg, (3,3), 1)
tmp2 = cv2.GaussianBlur(noiseimage_2, (3,3), 1)
tmp3 = cv2.GaussianBlur(noiseimage_6, (3,3), 1)
res1 = np.hstack((tmp1, tmp2, tmp3))
cv2.imshow("res1",res1)
cv2.imwrite('./gauss_3.png'.format(len(res1)),res1)
psnr = compare_psnr(img1, res1)
ssim = compare_ssim(img1, res1, channel_axis=-1)
mse = compare_mse(img1, res1)
print('PSNR:{},SSIM:{},MSE:{}'.format(psnr, ssim, mse))
cv2.waitKey(0)
cv2.destroyAllWindows()


print("基于高斯滤波_7*7")
tmp1 = cv2.GaussianBlur(gaussimg, (7,7), 1)
tmp2 = cv2.GaussianBlur(noiseimage_2, (7,7), 1)
tmp3 = cv2.GaussianBlur(noiseimage_6, (7,7), 1)
res2 = np.hstack((tmp1, tmp2, tmp3))
cv2.imshow("res2",res2)
cv2.imwrite('./gauss_7.png'.format(len(res2)),res2)
psnr = compare_psnr(img1, res2)
ssim = compare_ssim(img1, res2, channel_axis=-1)
mse = compare_mse(img1, res2)
print('PSNR:{},SSIM:{},MSE:{}'.format(psnr, ssim, mse))
cv2.waitKey(0)
cv2.destroyAllWindows()

print("中值滤波")
tmp1 = cv2.medianBlur(gaussimg, 5)
tmp2 = cv2.medianBlur(noiseimage_2, 5)
tmp3 = cv2.medianBlur(noiseimage_6, 5)
res3 = np.hstack((tmp1, tmp2, tmp3))
cv2.imshow("res3",res3)
cv2.imwrite('./Median.png'.format(len(res3)),res3)
psnr = compare_psnr(img1, res3)
ssim = compare_ssim(img1, res3, channel_axis=-1)
mse = compare_mse(img1, res3)
print('PSNR:{},SSIM:{},MSE:{}'.format(psnr, ssim, mse))
cv2.waitKey(0)
cv2.destroyAllWindows()

result:

Guess you like

Origin blog.csdn.net/m0_67629315/article/details/130357479
Recommended