Gaussian Blur
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)=σ2π​1​e−2σ2x2​, where σ 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:
- Generate a 2D Gaussian kernel with the given kernel size and sigma value.
- Apply the 2D convolution operation in valid mode to the input image using the generated kernel.
This technique is widely used in image preprocessing for object detection and recognition tasks.
Example:
image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] kernel_size = 3, sigma = 1.0
[[5.0]]
- The Gaussian kernel is generated with a size of 3×3 and σ=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×3 neighborhood of each pixel, using the kernel values as weights, resulting in a single output value.
- Since the kernel size is 3×3 and the image size is 3×3, only the central pixel has a full 3×3 neighborhood, and its blurred value is calculated as the weighted sum of all pixels in the image, yielding a value of 5.0 after rounding to 4 decimal places.
- The final output is the blurred image with only the central pixel value, which is [[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
Background Knowledge
The Gaussian blur is a widely used image processing technique that reduces the noise in an image by smoothing it. The key concept behind Gaussian blur is the Gaussian distribution, which is a continuous probability distribution with a bell-shaped curve. In the context of image processing, the Gaussian distribution is used to create a kernel that is applied to the image to blur it. The Gaussian kernel is a 2D array of values that are calculated using the Gaussian distribution formula: G(x,y)=\frac{1}{2\pi\sigma^2}e^{-\frac{x^2 + y^2}{2\sigma^2}}, where σ is the standard deviation of the distribution.
The Gaussian kernel is typically normalized to ensure that the sum of its values equals 1. This is important because it ensures that the overall brightness of the image is preserved after applying the blur. The size of the kernel is also an important parameter, as it determines the amount of blur that is applied to the image. A larger kernel size will result in a more blurred image, while a smaller kernel size will result in a less blurred image.
In addition to the Gaussian kernel, the other key concept in this problem is 2D convolution. Convolution is a mathematical operation that combines two functions by sliding one function over the other. In the context of image processing, convolution is used to apply a kernel to an image. The kernel is slid over the image, and at each position, the values of the kernel and the image are multiplied and summed to produce the output value. The mode of convolution (valid, same, or full) determines how the kernel is applied to the image. In this problem, we are using valid mode, which means that the kernel is only applied to the parts of the image where the kernel fully overlaps with the image.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.