crop center portion of a numpy image

后端 未结 5 1438
醉梦人生
醉梦人生 2020-12-08 13:52

Let\'s say I have a numpy image of some width x and height y. I have to crop the center portion of the image to width cropx and height cropy. Let\'s assume that cropx and cr

5条回答
  •  不知归路
    2020-12-08 14:03

    A simple modification from @Divakar 's answer that preserves the image channel:

        def crop_center(self, img, cropx, cropy):
           _, y, x = img.shape
           startx = x // 2 - (cropx // 2)
           starty = y // 2 - (cropy // 2)
           return img[:, starty:starty + cropy, startx:startx + cropx]
    

提交回复
热议问题