Skip to content
DeepForgeby svx
All problems
MediumComputer Visioncv-020

Gaussian Kernel Generation

Problem

Generate a normalized 2D Gaussian kernel.

kernel[i][j] = exp(-((i - c)^2 + (j - c)^2) / (2 * sigma^2)) with c = size // 2, then divide every entry by the sum of all entries. size is odd and sigma is positive; round each value to 6 decimal places.

Hint

Compute the squared distance from the center, exponentiate, then normalize by the total.

Test cases

case 1
in: [3,1]
expected: [[0.075114,0.123841,0.075114],[0.123841,0.20418,0.123841],[0.075114,0.123841,0.075114]]
case 2
in: [1,1]
expected: [[1]]
case 3
in: [5,2]
expected: [[0.023247,0.033824,0.038328,0.033824,0.023247],[0.033824,0.049214,0.055766,0.049214,0.033824],[0.038328,0.055766,0.063191,0.055766,0.038328],[0.033824,0.049214…
case 4
in: [3,0.5]
expected: [[0.011344,0.08382,0.011344],[0.08382,0.619347,0.08382],[0.011344,0.08382,0.011344]]
Starter codePython
def gaussian_kernel(size, sigma):
    # Your code here
    pass

4 test cases · run your code against them in the editor