Filtration is used to highlight an information and suppress the noise. It is dependent on type of the solved task.
Výsledek po průměrování
%pylab inline --no-import-all
import numpy as np
import scipy
from scipy import ndimage
import scipy.signal
import scipy.misc
import skimage.data
import skimage.io
import matplotlib.pyplot as plt
Populating the interactive namespace from numpy and matplotlib
# plt.imshow(scipy.misc.lena())
plt.imshow(skimage.data.astronaut())
<matplotlib.image.AxesImage at 0x1f43b7d1dd8>
np.convolve([3, 10, 10, 1, 2, 2], [1, -2, 1])
array([ 3, 4, -7, -9, 10, -1, -2, 2])
np.convolve([10, 10, 10, 10, 10, 10, 10, 10], [1, -2, 1], "valid")
array([0, 0, 0, 0, 0, 0])
np.convolve([10, 10, 10, 10, 10, 0, 0, 0, 0, 0], [1, -2, 1], "valid")
array([ 0, 0, 0, -10, 10, 0, 0, 0])
lena = skimage.io.imread("https://i.stack.imgur.com/3T6Gc.jpg")
plt.imshow(lena)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x21e4b55afd0>
# lena = scipy.misc.face()
# l = lena[250:340, 530:630,0]
lena = skimage.io.imread("https://i.stack.imgur.com/3T6Gc.jpg", as_grey=True)
# print(lena.max())
l = lena[230:290, 220:320]
noisy = l + 1.6*l.std()*np.random.random(l.shape)
print(noisy.std())
plt.imshow(noisy, cmap='gray', interpolation=None)
#plt.imshow(lena)
plt.colorbar()
plt.show()
0.20064397202412135
import scipy.signal
kernel = np.ones([15,15])
output1 = scipy.signal.convolve2d(noisy, kernel)
plt.imshow(output1, cmap="gray")
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x21e49ef5080>
kernel = np.ones([15,15])
kernel = kernel / np.sum(kernel)
output1 = scipy.signal.convolve2d(noisy, kernel)
plt.imshow(output1, cmap="gray")
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x21e49f8c0b8>
import scipy.signal
kernel = np.ones([15,15])
output1 = scipy.signal.convolve2d(noisy, kernel, boundary="fill", fillvalue=0)
output2 = scipy.signal.convolve2d(noisy, kernel, boundary="wrap")
output3 = scipy.signal.convolve2d(noisy, kernel, boundary="symm")
plt.figure(figsize=[10,15])
plt.subplot(131)
plt.imshow(output1, cmap="gray")
plt.subplot(132)
plt.imshow(output2, cmap="gray")
plt.subplot(133)
plt.imshow(output3, cmap="gray")
<matplotlib.image.AxesImage at 0x21e4a186f60>
import scipy.signal
kernel = np.ones([15,15])
output1 = scipy.signal.convolve2d(noisy, kernel, mode="full", boundary="fill")
output2 = scipy.signal.convolve2d(noisy, kernel, mode="same", boundary="fill")
output3 = scipy.signal.convolve2d(noisy, kernel, mode="valid", boundary="fill")
plt.figure(figsize=[10,15])
plt.subplot(131)
plt.imshow(output1, cmap="gray")
plt.subplot(132)
plt.imshow(output2, cmap="gray")
plt.subplot(133)
plt.imshow(output3, cmap="gray")
<matplotlib.image.AxesImage at 0x21e4a25be48>
local_mean = ndimage.uniform_filter(noisy, size=15)
plt.imshow(local_mean, cmap='gray', interpolation=None)
plt.show()
blurred_lena = ndimage.gaussian_filter(noisy, sigma=1)
very_blurred = ndimage.gaussian_filter(noisy, sigma=5)
plt.imshow(blurred_lena, cmap='gray')
plt.figure()
plt.imshow(very_blurred, cmap='gray')
<matplotlib.image.AxesImage at 0x21e4b54b518>