text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png image_output_2.png image_output_1.png
from scipy import ndimage

from skimage import color

import imageio

import matplotlib.pyplot as plt

import numpy as np

import numpy as np # linear algebra

images = ['peppers.png', 'cameraman.tif', 'coins.png']
path = '/kaggle/input/lab-python/Immagini/'
I = imageio.imread(path + images[0])
if len(I.shape) == 3:
    I = color.rgb2gray(I)
(plt.subplot(1, 2, 1), plt.imshow(I), plt.title('original image'))
(plt.subplot(1, 2, 2), plt.imshow(I, cmap='gray'), plt.title('gray version'))

side_box = 3
h_box = np.ones((side_box, side_box)) / side_box ** 2
sigma = 3
side_gauss = 33
[x, y] = np.meshgrid(np.arange(-side_gauss // 2, side_gauss // 2 + 1), np.arange(-side_gauss // 2, side_gauss // 2 + 1))
h_gauss = np.exp(-0.5 * (x ** 2 + y ** 2) / sigma ** 2) / (2 * np.pi * sigma ** 2)
h_gauss = h_gauss / np.sum(h_gauss)
temp = np.zeros((side_box, side_box))
temp[side_box // 2, side_box // 2] = 1
H_box = ndimage.uniform_filter(temp, side_box)
temp = np.zeros((side_gauss, side_gauss))
temp[side_gauss // 2, side_gauss // 2] = 1
H_gauss = ndimage.gaussian_filter(temp, sigma)
plt.figure()
(plt.subplot(121), plt.imshow(h_box, cmap='gray', vmin=-1 / side_box ** 2, vmax=2 / side_box ** 2))
(plt.subplot(122), plt.imshow(H_box, cmap='gray', vmin=-1 / side_box ** 2, vmax=2 / side_box ** 2))
plt.figure()
(plt.subplot(121), plt.imshow(h_gauss, cmap='gray'))
(plt.subplot(122), plt.imshow(H_gauss, cmap='gray'))