All problems
Median Filter
Problem
Apply a median filter with a k x k window to a grayscale image.
At each valid position, sort the k * k window values and take the element at index (k * k) // 2 (the lower median). k is odd and the image has at least k rows and columns.
Hint
Collect all k * k values, sort them and pick the middle index.
Test cases
case 1
in: [[[1,1,1],[1,100,1],[1,1,1]],3]
expected: [[1]]
case 2
in: [[[5]],1]
expected: [[5]]
case 3
in: [[[9,2,8],[4,7,1],[3,6,5]],3]
expected: [[5]]
case 4
in: [[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],3]
expected: [[6,7],[10,11]]
Starter codePython
def median_filter(image, k):
# Your code here
pass4 test cases · run your code against them in the editor