def center_crop(img, length):
if img.shape[0] < length or img.shape[1] < length:
top = max(0, int(np.ceil((length - img.shape[0]) / 2.)))
left = max(0, int(np.ceil((length - img.shape[1]) / 2.)))
img = cv2.copyMakeBorder(
img, top, top, left, left, borderType=cv2.BORDER_REFLECT_101
)
crop_y = int(np.floor((img.shape[0] - length) / 2.))
crop_x = int(np.floor((img.shape[1] - length) / 2.))
crop = img[crop_y:crop_y + length, crop_x:crop_x + length]
return crop
评论列表
文章目录