PIXELBANKv9.1.0
Menu

Implement a Gaussian Blur filter on a given 2D image using a specified kernel size and sigma value. This process involves applying a convolution operation to reduce image noise and detail.

The Gaussian distribution is a fundamental concept in statistics and signal processing, described by the equation f(x)=1σ2πe−x22σ2f(x) = \frac{1}{\sigma \sqrt{2\pi}} e^{-\frac{x^2}{2\sigma^2}}, where σ\sigma is the standard deviation. In the context of image processing, a 2D Gaussian kernel is used to blur an image by averaging neighboring pixel values.

To apply the Gaussian blur, follow these steps:

  1. Generate a 2D Gaussian kernel with the given kernel size and sigma value.
  2. Apply the 2D convolution operation in valid mode to the input image using the generated kernel.
f(x,y)=12πσ2e−x2+y22σ2f(x, y) = \frac{1}{2\pi\sigma^2} e^{-\frac{x^2 + y^2}{2\sigma^2}}

This technique is widely used in image preprocessing for object detection and recognition tasks.

Example:

Input:
image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
kernel_size = 3, sigma = 1.0
Output:
[[5.0]]
Reasoning:
  • The Gaussian kernel is generated with a size of 3×33 \times 3 and σ=1.0\sigma = 1.0, resulting in a kernel with values that follow a Gaussian distribution.
  • The 2D convolution is applied to the input image with the generated kernel in valid mode, meaning that only the pixels where the kernel fully overlaps with the image are considered.
  • The convolution operation calculates the weighted sum of the pixel values in the 3×33 \times 3 neighborhood of each pixel, using the kernel values as weights, resulting in a single output value.
  • Since the kernel size is 3×33 \times 3 and the image size is 3×33 \times 3, only the central pixel has a full 3×33 \times 3 neighborhood, and its blurred value is calculated as the weighted sum of all pixels in the image, yielding a value of 5.05.0 after rounding to 4 decimal places.
  • The final output is the blurred image with only the central pixel value, which is [[5.0]][[5.0]].

Constraints:

  • image is a 2D list of numbers
  • kernel_size is a positive odd integer
  • sigma is a positive float
  • Return 2D list rounded to 4 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Gaussian Blur - Medium | PixelBank