Skip to content
DeepForgeby svx
All problems
MediumComputer Visioncv-017

Nearest-Neighbor Resize

Problem

Resize a 2D image using nearest-neighbor interpolation.

For output pixel (y, x), sample the input at row y * H // out_h and column x * W // out_w, where H and W are the input height and width. out_h and out_w are positive.

Hint

Integer-divide the output coordinate scaled by the input size.

Test cases

case 1
in: [[[1,2],[3,4]],4,4]
expected: [[1,1,2,2],[1,1,2,2],[3,3,4,4],[3,3,4,4]]
case 2
in: [[[1,2,3],[4,5,6]],3,2]
expected: [[1,2],[1,2],[4,5]]
case 3
in: [[[5]],2,2]
expected: [[5,5],[5,5]]
case 4
in: [[[1,2,3]],2,6]
expected: [[1,1,2,2,3,3],[1,1,2,2,3,3]]
Starter codePython
def resize_nearest(image, out_h, out_w):
    # Your code here
    pass

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