Gaussian blur smooths noisy images by weighting nearby pixels with a Gaussian-shaped kernel. The kernel slides across an image, multiplies each neighborhood by corresponding weights, and sums the results through convolution. section 3.1.4
A practical implementation follows two steps:
- Create a two-dimensional kernel from its size and sigma; larger weights concentrate near the center. section 3.1.4
- Apply the kernel independently to each color channel, clip resulting intensities, and stack the channels back into an image. section 3.1.4
- Use the filtered result to reduce noise, while recognizing that denoising also blurs image detail. section 3.1.4
A five-by-five Gaussian kernel
Generate `kernel_gauss = gaussian_kernel(5)`, then pass it to `color_convolution(img_noised, kernel_gauss)` to compare the denoised image with its noisy input. section 3.1.4
Listing 3.6 Gaussian blur kernel
def gaussian_kernel(kernel_size, sigma=1.0, mu=0.0):
""" A function to generate Gaussian 2D kernel """
center = kernel_size // 2 #1
x, y = np.mgrid[ #2
-center : kernel_size - center, #2
-center : kernel_size - center] #2
d = np.sqrt(np.square(x) + np.square(y))
koeff = 1 / (2 * np.pi * np.square(sigma))
kernel = koeff * np.exp(-np.square(d-mu) /
(2 * np.square(sigma))) #3
return kernel
kernel_gauss = gaussian_kernel(5)
kernel_gauss
>>> array([[0.00291502, 0.01306423, 0.02153928,
0.01306423, 0.00291502], #4
>>> [0.01306423, 0.05854983, 0.09653235,
0.05854983, 0.01306423], #4
>>> [0.02153928, 0.09653235, 0.15915494,
0.09653235, 0.02153928], #4
>>> [0.01306423, 0.05854983, 0.09653235,
0.05854983, 0.01306423], #4
>>> [0.00291502, 0.01306423, 0.02153928,
0.01306423, 0.00291502]]) #4
Inspect the runnable kernel generator and its resulting weight matrix. section 3.1.4
