MediumComputer Vision

Gaussian Blur Kernel

Computer Vision

Medium

Problem

Generate a normalized square Gaussian blur kernel. For every position, measure offsets x and y from the center and compute:

G(x,y)=\exp\left(-\frac{x^2+y^2}{2\sigma^2}\right)

Here, \sigma is the supplied standard deviation. Divide every weight by the sum of all unnormalized weights so the final kernel sums to one. Return the kernel as a two-dimensional list of floats.

Theory

Gaussian blur is the most widely used smoothing filter in image processing. It removes high-frequency noise while preserving edges better than simple averaging. The blur effect is controlled by a parameter called sigma (\sigma).


The Gaussian Function

The 2D Gaussian function is:

G(x, y) = \frac{1}{2\pi\sigma^2} e^{-\frac{x^2 + y^2}{2\sigma^2}}

Where:


Building the Kernel

To create a Gaussian kernel:

Step 1: Choose kernel size

Step 2: Compute offsets

Step 3: Apply Gaussian formula

Step 4: Normalize


Step-by-Step Example

Parameters: size = 3, sigma = 1.0

Offsets:

(-1,-1) (-1,0) (-1,1) (0,-1) (0,0) (0,1) (1,-1) (1,0) (1,1)

Unnormalized weights (computing G(x,y)):

Kernel before normalization:

0.368 0.607 0.368 0.607 1.000 0.607 0.368 0.607 0.368

Sum: 0.3684 + 0.6074 + 1.0 = 4.9

Normalized kernel:

0.075 0.124 0.075 0.124 0.204 0.124 0.075 0.124 0.075

This sums to 1.0.


The Effect of Sigma

Small sigma (e.g., 0.5):

Medium sigma (e.g., 1.0):

Large sigma (e.g., 3.0):


Kernel Size vs. Sigma

The kernel should be large enough to capture most of the Gaussian:

Rule of thumb: kernel_size = ceil(6 * sigma) + 1 (round up to odd)

For sigma = 1: size >= 7 (but 5 or 3 often works) For sigma = 2: size >= 13 (typically use 9 or 11)

If kernel is too small:


Properties of Gaussian Blur

Separability: The 2D Gaussian is separable:

G(x, y) = G(x) \cdot G(y)

This means a 2D convolution can be done as two 1D convolutions:

  1. Blur horizontally with 1D kernel
  2. Blur vertically with 1D kernel

This is much faster: O(nk) instead of O(nk^2) per pixel.

Linearity: Blurring twice with sigma1 and sigma2 equals blurring once with:

\sigma_{combined} = \sqrt{\sigma_1^2 + \sigma_2^2}


Gaussian vs. Box Blur

Box blur (simple averaging):

Gaussian blur:

Gaussian blur is preferred for most applications.


Applications

Noise reduction:

Scale-space:

Pre-processing:

Artistic effects:


Implementation Tips

Avoid computing exp() repeatedly:

Use symmetry:

Handle normalization carefully:

Examples

Example 1

Input
size = 3, sigma = 1
Output
[[0.075114, 0.123841, 0.075114], [0.123841, 0.20418, 0.123841], [0.075114, 0.123841, 0.075114]]
Explanation
The center receives the largest weight, the kernel is symmetric, and all weights sum to one.

Example 2

Input
size = 1, sigma = 1
Output
[[1]]

Hints

  1. Use size // 2 as the center coordinate.
  2. Accumulate all unnormalized weights before dividing each value by their total.

Requirements

Constraints

Starter Code

import math

def gaussian_kernel(size: int, sigma: float) -> list:
    """
    Returns a square two-dimensional list.
    """
    # Write code here
    pass

Test Cases

CaseMatches
3x3 sigma=1public
1x1 kernelpublic