gpu版letterbox
迪丽瓦拉
2025-05-29 08:43:06
0次

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))  # wxhdw, dh = new_shape[1] - new_w, new_shape[0] - new_hdw /= 2dh /= 2resized_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 borderreturn 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))  # wxhdw, dh = new_shape[1] - new_w, new_shape[0] - new_hdw /= 2dh /= 2top, 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上做归一化

相关内容

热门资讯

AI做攻略、“双向奔赴”……国... 本文转自:人民网人民网记者 许维娜今年国庆假期,AI(人工智能)成了不少年轻人的“旅行搭子”。行前做...
合肥的这个“梗”, 翻篇了 社交平台的评论区,曾长期流传一句专属于合肥的黑色幽默。有人问合肥哪里好玩,本地网友半开玩笑地回复:合...
7万+旅游中高层都在的群,今天... 做旅游这行,资源卡位、渠道互通、项目落地——往往就差一个“对的机会”。我们的行业社群已聚集70,00...
一场帐篷音乐节,如何让井研从“... 帐篷音乐节现场封面新闻记者 周洪攀10月的研溪湿地,晚风里裹着柑橘的清甜与民谣的回响。夜幕降临,10...
白鹤翩翩落星愿,上海迪士尼度假... 10月9日,世界候鸟日前夕,上海迪士尼度假区星愿公园的湖风里多了一分白鹤的灵动。三座白鹤雕塑正式揭幕...
成都锦江边赏初秋枫叶飘飘 目前,成都锦江边国华街枫叶树红了。近年来,国华街附近的几条街都栽种了红枫树,是城区较集中看枫叶的点位...
GBTA:商务旅行正在复苏,但... 亚太正在成为全球商务旅行增长的重要引擎。在经历疫情后的快速反弹之后,全球商务旅行正在进入一个新的增长...
从景区焕新到节赛引流 邹城双节... 齐鲁网·闪电新闻10月9日讯 中秋、国庆“双节”假期,邹城市文化和旅游局提前谋划、周密部署,立足自身...
江山如此多娇 且看石楼今朝 金秋十月,黄河岸畔秋意盎然,丰收盛会喜迎国庆。本次石楼国庆文旅活动以“江山如此多娇,且看石楼今朝”为...
海南国庆假期接待游客475.4... 2026年国庆假期,海南接待游客475.40万人次,较2025年增长13.1%,较2024年增长15...