All problems
Binary Erosion
Problem
Erode a binary mask with a full 3x3 structuring element and zero padding.
A pixel stays 1 only if all nine positions of its 3x3 window are 1; out-of-bounds positions count as 0, so border pixels are always eroded. The output has the same shape as the mask.
Hint
Treat any position outside the mask as a 0, which erodes the whole border.
Test cases
case 1
in: [[[1,1,1],[1,1,1],[1,1,1]]]
expected: [[0,0,0],[0,1,0],[0,0,0]]
case 2
in: [[[0,0,0],[0,1,0],[0,0,0]]]
expected: [[0,0,0],[0,0,0],[0,0,0]]
case 3
in: [[[1,1],[1,1]]]
expected: [[0,0],[0,0]]
case 4
in: [[[1,1,1,1],[1,1,1,1],[1,1,1,1]]]
expected: [[0,0,0,0],[0,1,1,0],[0,0,0,0]]
Starter codePython
def binary_erode(mask):
# Your code here
pass4 test cases · run your code against them in the editor