Skip to content
DeepForgeby svx
All problems
MediumComputer Visioncv-061

Adaptive Threshold (Mean minus C)

Problem

Threshold each pixel against the mean of its local neighborhood minus c.

For each pixel compute the mean of the in-bounds pixels in its k x k window (k odd, clipped at the borders) and output 255 if the pixel is strictly greater than mean - c, otherwise 0. The output has the same shape as the image.

Hint

Clip the window at the borders and only count the pixels that exist.

Test cases

case 1
in: [[[5,5],[5,5]],3,1]
expected: [[255,255],[255,255]]
case 2
in: [[[0,0,0],[0,100,0],[0,0,0]],3,5]
expected: [[0,0,0],[0,255,0],[0,0,0]]
case 3
in: [[[10,10,10],[10,10,10],[10,10,10]],3,0]
expected: [[0,0,0],[0,0,0],[0,0,0]]
case 4
in: [[[0,255,0],[0,255,0],[0,255,0]],3,10]
expected: [[0,255,0],[0,255,0],[0,255,0]]
Starter codePython
def adaptive_threshold(image, k, c):
    # Your code here
    pass

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