Problems
Loading...
1 / 1

Gaussian Blur Kernel

Computer Vision
Medium

Gaussian blur is the most widely used smoothing filter in image processing and computer vision. The filter is defined by a 2D kernel whose weights follow a Gaussian distribution, giving more influence to nearby pixels and less to distant ones.

Given an odd kernel size and a standard deviation sigma, generate the normalized 2D Gaussian kernel.

Algorithm

  1. Find the center of the kernel: center = size // 2.
  2. For each position (i, j), compute offsets x = j - center and y = i - center.
  3. Compute the unnormalized weight using the Gaussian function:
G(x,y)=ex2+y22σ2G(x, y) = e^{-\frac{x^2 + y^2}{2\sigma^2}}
  1. Divide every entry by the sum of all entries so the kernel sums to 1.
Loading visualization...

Examples

Input:

size = 3, sigma = 1.0

Output (rounded to 4 decimals):

[[0.0751, 0.1238, 0.0751], [0.1238, 0.2042, 0.1238], [0.0751, 0.1238, 0.0751]]

The center has the largest weight. The kernel is symmetric and sums to 1.0.

Input:

size = 1, sigma = 1.0

Output:

[[1.0]]

A single-element kernel always normalizes to 1.0 regardless of sigma.

Hint 1

The center of a kernel of size n is at index n // 2. Offsets x and y measure distance from this center.

Hint 2

After computing all raw Gaussian values, divide each by the total sum. This ensures the kernel sums to 1.0 without needing the full Gaussian normalization constant.

Requirements

  • Compute Gaussian weights using the formula with correct offsets from center
  • Normalize the kernel so all entries sum to exactly 1.0
  • Return a 2D list of floats with dimensions size x size

Constraints

  • size is an odd positive integer (1, 3, 5, ...)
  • sigma is a positive float
  • Return a 2D list of floats that sums to 1.0
  • Time limit: 300 ms
Try Similar Problems