Skip to content
DeepForgeby svx
All problems
EasyComputer Visioncv-042

Edge Padding (Replicate)

Problem

Pad a 2D image by replicating its edge pixels.

Every padded row or column copies the nearest original border pixel, so the top-left corner of [[1, 2], [3, 4]] with pad 1 is 1. pad may be 0.

Hint

Clamp the source index into [0, h - 1] and [0, w - 1] before reading.

Test cases

case 1
in: [[[1,2],[3,4]],1]
expected: [[1,1,2,2],[1,1,2,2],[3,3,4,4],[3,3,4,4]]
case 2
in: [[[1,2,3]],1]
expected: [[1,1,2,3,3],[1,1,2,3,3],[1,1,2,3,3]]
case 3
in: [[[1,2],[3,4]],0]
expected: [[1,2],[3,4]]
case 4
in: [[[5]],2]
expected: [[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5]]
Starter codePython
def edge_pad(image, pad):
    # Your code here
    pass

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