图像加pad

tensorflow:

def np_scale_pad(img, label, output_shape):
    """
    将图片缩放至指定大小,加pad,如果有标注也对标注进行缩放
    :param img:图像数据
    :param label: 标记数据,能乘以比例即可。
    :param output_shape: 输出形状
    :return:image, label
    """
    output_height, output_width = output_shape[0:2]
    target_scale = output_width / output_height
    img_scale = img.shape[1] / img.shape[0]
    if img_scale > target_scale:
        # 按宽度缩放
        new_size = output_width, int(round(output_width / img_scale))
        scale = output_width / img.shape[1]
    else:
        new_size = int(round(output_height * img_scale)), output_height
        scale = output_height / img.shape[0]
    resized_img = cv2.resize(img, new_size, interpolation=cv2.INTER_LINEAR)

    padded_img = np.pad(
        resized_img, [[0, output_height - new_size[1]],
                      [0, output_width - new_size[0]], [0, 0]],
        mode='constant')

    if label is None:
        return padded_img
    else:
        label = np.round(label * scale).astype(np.float32)
        return padded_img, label

pytorch:

import torch.nn.functional as F

def pad_to_square(img, pad_value):
    c, h, w = img.shape
    dim_diff = np.abs(h - w)
    # (upper / left) padding and (lower / right) padding
    pad1, pad2 = dim_diff // 2, dim_diff - dim_diff // 2
    # Determine padding
    pad = (0, 0, pad1, pad2) if h <= w else (pad1, pad2, 0, 0)
    # Add padding
    img = F.pad(img, pad, "constant", value=pad_value)

    return img, pad

个人感觉还是pytorch好呀,啥都给弄好了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值