Skip to content
DeepForgeby svx
All problems
EasyComputer Visioncv-048

Center Crop

Problem

Crop a 2D image around its center.

Use top = (H - out_h) // 2 and left = (W - out_w) // 2, then take out_h rows and out_w columns. You may assume out_h <= H and out_w <= W.

Test cases

case 1
in: [[[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]],2,2]
expected: [[5,6],[9,10]]
case 2
in: [[[1,2,3],[4,5,6],[7,8,9]],2,2]
expected: [[1,2],[4,5]]
case 3
in: [[[1,2,3],[4,5,6]],2,2]
expected: [[1,2],[4,5]]
Starter codePython
def center_crop(image, out_h, out_w):
    # Your code here
    pass

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