letterbox实现

数据集中的图片一般为长方形,当模型输入为正方形时直接将长方形图片resize为正方形会使得图片失真,采用letterbox通过填充边界(通常是灰色填充)的方式来保持原始图片的长宽比例,同时又满足模型正方形输入的需要。

import cv2

def letterbox(im, new_shape=(640, 640), color=(114, 114, 114)):

    shape = im.shape[:2]

    r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])

    new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
    dw, dh = (new_shape[1] - new_unpad[0])/2, (new_shape[0] - new_unpad[1])/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))

    if shape[::-1] != new_unpad:
        im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
    im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
    return im

src = cv2.imread("1.jpg")
dst = letterbox(src, (768,768))
cv2.imwrite("letterbox.jpg",dst)

letterbox实现

Original: https://blog.csdn.net/taifyang/article/details/127820108
Author: 给算法爸爸上香
Title: letterbox实现

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/657157/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球