使用python实现图像的中值滤波
椒盐噪声处理的图片:
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
def mediafil(img,m):
img1=np.zeros((img.shape[0]+m-1,img.shape[1]+m-1))
img1[(m-1)//2:(img.shape[0]+(m-1)//2),(m-1)//2:(img.shape[1]+(m-1)//2)]=img
img2=np.zeros(img.shape)
for i in range((m-1)//2,img2.shape[0]-(m-1)//2):
for j in range((m-1)//2,img2.shape[1]-(m-1)//2):
img2[i,j]=np.median(img1[i - (m-1)//2:i + (m-1)//2 + 1, j - (m-1)//2:j + (m-1)//2 + 1])
return img2
img=io.imread("3-5-5.tif")[:,:,0]
m=3
img_new=mediafil(img,m)
plt.subplot(1,2,1)
plt.imshow(img,cmap='gray')
plt.subplot(1,2,2)
plt.imshow(img_new,cmap='gray')
plt.show()