All problems
Bilateral Filter
Problem
Apply a bilateral filter with a fixed 3x3 window.
For each pixel, weight each in-bounds neighbor by exp(-(di^2 + dj^2) / (2 * spatial_sigma^2)) * exp(-(diff^2) / (2 * range_sigma^2)), where diff is the intensity difference from the center pixel, then output the weighted mean rounded to 4 decimal places.
Hint
Multiply a spatial Gaussian weight by a range Gaussian weight for each neighbor.
Test cases
case 1
in: [[[7,7,7],[7,7,7],[7,7,7]],1,1]
expected: [[7,7,7],[7,7,7],[7,7,7]]
case 2
in: [[[0,0,0],[0,9,0],[0,0,0]],1,1]
expected: [[0,0,0],[0,9,0],[0,0,0]]
case 3
in: [[[0,0],[0,9]],1,1]
expected: [[0,0],[0,9]]
case 4
in: [[[0,0,0],[0,10,0],[0,0,0]],1,5]
expected: [[0.22,0.2708,0.22],[0.2708,6.5467,0.2708],[0.22,0.2708,0.22]]
Starter codePython
def bilateral_filter(image, spatial_sigma, range_sigma):
# Your code here
pass4 test cases · run your code against them in the editor