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上做归一化