首页 > 其他 > 详细

How to pad an image in numpy? - 如何在numpy中填充图像

时间:2019-05-08 00:20:10      阅读:151      评论:0      收藏:0      [点我收藏+]

I need 2d padding for 3 channel images with numpy. I know that there is  np.pad  function, but it pads channels as well and as a result I have modified channels count, but I just need to pad only the image. How to do It?

def pad(img, h, w):
    #  in case when you have odd number
    top_pad = np.floor((h - img.shape[0]) / 2).astype(np.uint16)
    bottom_pad = np.ceil((h - img.shape[0]) / 2).astype(np.uint16)
    right_pad = np.ceil((w - img.shape[1]) / 2).astype(np.uint16)
    left_pad = np.floor((w - img.shape[1]) / 2).astype(np.uint16)
    return np.copy(np.pad(img, ((top_pad, bottom_pad), (left_pad, right_pad), (0, 0)), mode=‘constant‘, constant_values=0))

AI Pool Discussion: https://ai-pool.com/d/padding-images-with-numpy

How to pad an image in numpy? - 如何在numpy中填充图像

原文:https://www.cnblogs.com/hazarapet/p/10829048.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!