gpu版letterbox


def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), scaleup=True):
    """
    :param img:
    :param new_shape: (h,w)
    :param color: pad填充颜色
    :param scaleup:
    :return:
    """
    shape = img.shape[:2]
    ratio = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
    if not scaleup:
        ratio = min(ratio, 1.)
    new_w, new_h = int(round(shape[1] * ratio)
                    ), int(round(shape[0] * ratio))  # wxh
    dw, dh = new_shape[1] - new_w, new_shape[0] - new_h
    dw /= 2
    dh /= 2
    resized_img = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_LINEAR)
    top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
    left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
    # new_img = np.full((new_shape[0],new_shape[1],3),114, dtype=img.dtype)
    # new_img[top:top+new_h,left:left+new_w] = resized_img
    # cpu上copyborder更快
    new_img = cv2.copyMakeBorder(
        resized_img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # add border
    return new_img, ratio, (left, top)

def letterbox_gpu(img, new_shape=(640, 640), color=(114, 114, 114), scaleup=True):
    """
    :param img:
    :param new_shape: (h,w)
    :param color: pad填充颜色
    :param scaleup:
    :return:
    """
    shape = img.shape[:2]
    ratio = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
    if not scaleup:
        ratio = min(ratio, 1.)
    new_w, new_h = int(round(shape[1] * ratio)
                    ), int(round(shape[0] * ratio))  # wxh
    dw, dh = new_shape[1] - new_w, new_shape[0] - new_h
    dw /= 2
    dh /= 2
    top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
    left, right = int(round(dw - 0.1)), int(round(dw + 0.1))

    resized_img = zoom(img,(ratio,ratio,1),order=1)
    new_img = cp.full((new_shape[0],new_shape[1],3),114, dtype=resized_img.dtype)
    new_img[top:top+new_h,left:left+new_w] = resized_img
    # gpu上pad更慢
    # new_img = cp.pad(resized_img, ((top, bottom), (left, right), (0, 0)), mode='constant', constant_values=((114,114),(114,114),(114,114)))

    return new_img, ratio, (left, top)

测试图片1280x720,单纯测试letterbox,gpu版本要快0.24ms,但是gpu版本需要先将1280x720的原图拷贝到gpu,而cpu版本只需要拷贝640x640的结果图片到gpu,gpu拷贝这一步的耗时源超cpu版本,整体耗时基本相同。
结论:
在cpu上进行letterbox操作,在gpu上做归一化

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值