stay python3 Of scipy ndimage The module provides a module called percentile_filter() Function of , It is a general version of median filter .
Impulse noise diagram
from skimage.io import imread
import matplotlib.pylab as pylab
import numpy as np
pylab.rcParams['font.sans-serif'] = ['KaiTi']
pylab.rcParams['axes.unicode_minus'] = False
def plot_image(image, title=''):
pylab.title(title, size=15)
pylab.imshow(image)
pylab.axis('off')
lena = imread(r'D:\image_processing\image4\f.jpg')
noise = np.random.random(lena.shape)
lena[noise > 0.9] = 255
lena[noise < 0.1] = 0
plot_image(lena, ' Noise map ')
pylab.show()
Median filter
from skimage.io import imread
import matplotlib.pylab as pylab
import numpy as np
from scipy import signal, misc, ndimage
pylab.rcParams['font.sans-serif'] = ['KaiTi']
pylab.rcParams['axes.unicode_minus'] = False
def plot_image(image, title=''):
pylab.title(title, size=15)
pylab.imshow(image)
pylab.axis('off')
lena = imread(r'D:\image_processing\image4\f.jpg')
noise = np.random.random(lena.shape)
lena[noise > 0.9] = 255
lena[noise < 0.1] = 0
plot_image(lena, ' Noise map ')
pylab.show()
fig = pylab.figure(figsize=(20, 15))
i = 1
for p in range(25, 100, 25):
for k in range(5, 25, 5):
pylab.subplot(3, 4, i)
filtered = ndimage.percentile_filter(lena, percentile=p, size=(k, k, 1))
plot_image(filtered, str(p) + ' The nuclear size is , ' + str(k) + 'x' + str(k))
i += 1
pylab.show()
After running the code above , We can see , In all percentile filters , Median filter with smaller core size ( Corresponding to the first 50 Percentile ) It has the best effect in removing impulse noise , At the same time, there are very few missing image details .