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 , 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 and , 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 neighborhood of each pixel, using the kernel values as weights, resulting in a single output value.
- Since the kernel size is and the image size is , only the central pixel has a full neighborhood, and its blurred value is calculated as the weighted sum of all pixels in the image, yielding a value of after rounding to 4 decimal places.
- The final output is the blurred image with only the central pixel value, which is .
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