liveBook · Manning

Gaussian blur

As explained in Deep Learning with JAX

An image-smoothing filter that uses Gaussian-shaped weights to reduce noise while blurring detail.

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

How Deep Learning with JAX covers it

How deep it goes
Explains kernel creation, convolution, channelwise application, and clipping, but does not develop filter theory beyond the practical example.
Examples
Demonstrates five-by-five kernels, noisy and denoised images, and a subsequent sharpening filter.
What you can do after
Apply it via a generated kernel and two-dimensional convolution to denoise a multichannel image.
How it connects
Connects Gaussian blur with finite impulse response filters, convolution, image denoising, and convolutional neural networks.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with JAX. The section links point to the parts of the book it draws on.