Skip to content
DeepForgeby svx
All problems
EasyComputer Visioncv-041

Image Translation

Problem

Shift a 2D image by (dy, dx) using zero fill.

Positive dy moves content down and positive dx moves it right; pixels shifted outside the image are dropped and the exposed area is filled with 0. Return an image with the same shape; dy and dx may be negative.

Hint

Read from (i - dy, j - dx) and write 0 when the source falls outside.

Test cases

case 1
in: [[[1,2],[3,4]],1,1]
expected: [[0,0],[0,1]]
case 2
in: [[[1,2],[3,4]],-1,0]
expected: [[3,4],[0,0]]
case 3
in: [[[1,2],[3,4]],0,-3]
expected: [[0,0],[0,0]]
case 4
in: [[[1,2,3],[4,5,6],[7,8,9]],1,-1]
expected: [[0,0,0],[2,3,0],[5,6,0]]
Starter codePython
def translate(image, dy, dx):
    # Your code here
    pass

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