设计自适应中值滤波器, 通过添加高斯、盐和胡椒噪声生成噪声图像具有不同的参数,如标准差、极性概率和等等。 使用所设计的自适应中值滤波器对图像进行去噪,在相同的滤波器大小下并使用PSNR和SSIM度量,自适应中值滤波器与传统中值滤波器的去噪性能比较标准中值滤波器。的代码
时间: 2024-03-14 22:42:50 浏览: 58
【图像去噪】基于自适应中值滤波图像去噪含Matlab代码.zip
5星 · 资源好评率100%
以下是 Python 代码示例,实现了自适应中值滤波器及其与传统中值滤波器的比:
```python
import cv2
import numpy as np
from skimage.metrics import peak_signal_noise_ratio as psnr
from skimage.metrics import structural_similarity as ssim
def adaptive_median_filter(img, window_size, max_window_size):
height, width = img.shape
out_img = np.zeros((height, width), dtype=np.uint8)
for i in range(height):
for j in range(width):
window = img[max(i-window_size,0):min(i+window_size+1,height), max(j-window_size,0):min(j+window_size+1,width)]
window_min = np.min(window)
window_max = np.max(window)
window_median = np.median(window)
if window_min < window_median < window_max:
out_img[i,j] = img[i,j]
else:
window_size += 1
if window_size <= max_window_size:
adaptive_median_filter(img, window_size, max_window_size)
else:
out_img[i,j] = window_median
window_size = 3
return out_img
def main():
img = cv2.imread('lena.png', cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (512, 512))
# Add Gaussian noise
gaussian_img = img + np.random.normal(0, 20, img.shape)
gaussian_img = np.clip(gaussian_img, 0, 255).astype(np.uint8)
# Add salt and pepper noise
sp_img = img.copy()
prob = 0.1
sp_img[np.random.rand(*img.shape) < prob/2] = 0
sp_img[np.random.rand(*img.shape) < prob/2] = 255
# Apply median filter
median_gaussian = cv2.medianBlur(gaussian_img, 3)
median_sp = cv2.medianBlur(sp_img, 3)
# Apply adaptive median filter
adaptive_median_gaussian = adaptive_median_filter(gaussian_img, 3, 7)
adaptive_median_sp = adaptive_median_filter(sp_img, 3, 7)
# Calculate PSNR and SSIM
psnr_median_gaussian = psnr(img, median_gaussian)
psnr_median_sp = psnr(img, median_sp)
psnr_adaptive_median_gaussian = psnr(img, adaptive_median_gaussian)
psnr_adaptive_median_sp = psnr(img, adaptive_median_sp)
ssim_median_gaussian = ssim(img, median_gaussian)
ssim_median_sp = ssim(img, median_sp)
ssim_adaptive_median_gaussian = ssim(img, adaptive_median_gaussian)
ssim_adaptive_median_sp = ssim(img, adaptive_median_sp)
# Show results
cv2.imshow('Original', img)
cv2.imshow('Gaussian noise', gaussian_img)
cv2.imshow('Salt and pepper noise', sp_img)
cv2.imshow('Median filter (Gaussian noise)', median_gaussian)
cv2.imshow('Median filter (Salt and pepper noise)', median_sp)
cv2.imshow('Adaptive median filter (Gaussian noise)', adaptive_median_gaussian)
cv2.imshow('Adaptive median filter (Salt and pepper noise)', adaptive_median_sp)
print('PSNR (Median filter, Gaussian noise):', psnr_median_gaussian)
print('PSNR (Median filter, Salt and pepper noise):', psnr_median_sp)
print('PSNR (Adaptive median filter, Gaussian noise):', psnr_adaptive_median_gaussian)
print('PSNR (Adaptive median filter, Salt and pepper noise):', psnr_adaptive_median_sp)
print('SSIM (Median filter, Gaussian noise):', ssim_median_gaussian)
print('SSIM (Median filter, Salt and pepper noise):', ssim_median_sp)
print('SSIM (Adaptive median filter, Gaussian noise):', ssim_adaptive_median_gaussian)
print('SSIM (Adaptive median filter, Salt and pepper noise):', ssim_adaptive_median_sp)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
```
其中 `adaptive_median_filter` 函数实现了自适应中值滤波器,`main` 函数生成了带有高斯和盐和胡椒噪声的图像,分别使用传统中值滤波器和自适应中值滤波器进行去噪,并计算了 PSNR 和 SSIM 评估指标以进行比较。
阅读全文