Gaussian Kernel Generation
Implement a function to generate a normalized 2D Gaussian kernel, a fundamental component in image processing and computer vision. Given a kernel size, an odd integer, and standard deviation , create a 2D kernel where each element is computed based on its distance from the center.
The Gaussian distribution is a continuous probability distribution, commonly used to model noise in images. In the context of a 2D Gaussian kernel, the value at each position is determined by its distance from the center , where .
- Compute the distance of each position from the center.
- Calculate the Gaussian value at each position using the formula. The main equation for the Gaussian value is
This technique is widely used in image filtering.
Example
kernel_size = 3, sigma = 1.0
[[0.075114, 0.123841, 0.075114], [0.123841, 0.20418, 0.123841], [0.075114, 0.123841, 0.075114]]
- Inputs:
sizemeans a grid, andsigmasets how fast weights fall off from the middle. The center is , so position sits in the middle. - Squared distances: the center has , the four edge cells have , and the four corners have .
- Unnormalized weights: with , the center gets , the edges get , and the corners get .
- Normalize: the total is . Dividing every weight by it gives in the middle, on the edges and in the corners, each rounded to 6 decimal places.
- Output: a nested list whose values add up to 1. Notice that it is symmetric under flips and rotations, because only the distance from the center matters. A smaller piles more weight onto the middle: with the center rises to .
Constraints
- kernel_size is a positive odd integer
- sigma is a positive float
- Return 2D list normalized to sum to 1, rounded to 6 decimal places