The scipy ndimage module provides a function named percentile_filter(), which is a generic version of the median filter. The following code block demonstrates how to use this filter:
lena = misc.imread('../images/lena.jpg')# add salt-and-pepper noise to the input imagenoise = np.random.random(lena.shape)lena[noise > 0.9] = 255lena[noise < 0.1] = 0plot_image(lena, 'noisy image')pylab.show()fig = pylab.figure(figsize=(20,15))i = 1for 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) + ' percentile, ' + str(k) + 'x' + str(k) + ' kernel') i += 1pylab.show()
The following screenshot shows the output ...