根据Net输入,对图片进行保持纵横比resize,并填充

方式一: 以比例较大边进行resize后去覆盖zeros图片

def resize2netsize(img, input_size=(224, 224)):
    im_ratio = float(img.shape[0]) / img.shape[1]  # 图片的高/宽
    model_ratio = float(input_size[1]) / input_size[0]  # 图片与网络输入的 高/宽
    if im_ratio > model_ratio:
        new_height = input_size[1]  # 原图改高=设置高
        new_width = int(new_height / im_ratio)  # 宽进行缩小
    else:
        new_width = input_size[0]
        new_height = int(new_width * im_ratio)
    det_scale = float(new_height) / img.shape[0]  # 缩放比例
    resized_img = cv2.resize(img, (new_width, new_height))
    img_ret=np.zeros(shape=(input_size[1],input_size[0],3),dtype=np.uint8)
    img_ret[:new_height,:new_width,:]=resized_img
    return img_ret

方法二:tesorflow.image

tf.image.resize_with_pad(img,高,宽,method='bilinear')
  • 通过保持纵横比相同而不会失真,将图像调整为目标宽度和高度。如果目标尺寸与图像尺寸不匹配,则将调整图像的大小,然后用零填充以匹配请求的尺寸
  • method='nearest'时,图像与原图较相似
  • 此方法得到数据为tensor格式,可以直接imshow,如果其他操作,需要np.array()转numpy格式
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容