Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RandomCropLetterbox cv2 is missing #90

Open
chenguanfu511 opened this issue Jul 13, 2019 · 0 comments
Open

RandomCropLetterbox cv2 is missing #90

chenguanfu511 opened this issue Jul 13, 2019 · 0 comments

Comments

@chenguanfu511
Copy link

def randomCropLetterboxPil(img):
    output_w, output_h = (1408, 768)
    jitter = 0.3
    fill_color = 127

    orig_w, orig_h = img.size
    img_np = np.array(img)
    channels = img_np.shape[2] if len(img_np.shape) > 2 else 1
    dw = int(jitter * orig_w)
    dh = int(jitter * orig_h)
    new_ar = float(orig_w + random.randint(-dw, dw)) / (orig_h + random.randint(-dh, dh))
    scale = random.random() * (2 - 0.25) + 0.25
    if new_ar < 1:
        nh = int(scale * orig_h)
        nw = int(nh * new_ar)
    else:
        nw = int(scale * orig_w)
        nh = int(nw / new_ar)

    if output_w > nw:
        dx = random.randint(0, output_w - nw)
    else:
        dx = random.randint(output_w - nw, 0)

    if output_h > nh:
        dy = random.randint(0, output_h - nh)
    else:
        dy = random.randint(output_h - nh, 0)

    nxmin = max(0, -dx)
    nymin = max(0, -dy)
    nxmax = min(nw, -dx + output_w - 1)
    nymax = min(nh, -dy + output_h - 1)
    sx, sy = float(orig_w) / nw, float(orig_h) / nh
    orig_xmin = int(nxmin * sx)
    orig_ymin = int(nymin * sy)
    orig_xmax = int(nxmax * sx)
    orig_ymax = int(nymax * sy)
    orig_crop = img.crop((orig_xmin, orig_ymin, orig_xmax, orig_ymax))
    orig_crop_resize = orig_crop.resize((nxmax - nxmin, nymax - nymin))
    output_img = Image.new(img.mode, (output_w, output_h), color=(fill_color,) * channels)
    output_img.paste(orig_crop_resize, (0, 0))
    return output_img


def randomCropLetterboxCv(img):
    output_w, output_h = (1408, 768)
    jitter = 0.3
    fill_color = 127

    # orig_w, orig_h = img.size
    orig_w, orig_h = img.shape[:2]
    channels = img.shape[2] if len(img.shape) > 2 else 1
    dw = int(jitter * orig_w)
    dh = int(jitter * orig_h)
    new_ar = float(orig_w + random.randint(-dw, dw)) / (orig_h + random.randint(-dh, dh))
    scale = random.random() * (2 - 0.25) + 0.25
    if new_ar < 1:
        nh = int(scale * orig_h)
        nw = int(nh * new_ar)
    else:
        nw = int(scale * orig_w)
        nh = int(nw / new_ar)

    if output_w > nw:
        dx = random.randint(0, output_w - nw)
    else:
        dx = random.randint(output_w - nw, 0)

    if output_h > nh:
        dy = random.randint(0, output_h - nh)
    else:
        dy = random.randint(output_h - nh, 0)

    nxmin = max(0, -dx)
    nymin = max(0, -dy)
    nxmax = min(nw, -dx + output_w - 1)
    nymax = min(nh, -dy + output_h - 1)
    sx, sy = float(orig_w) / nw, float(orig_h) / nh
    orig_xmin = int(nxmin * sx)
    orig_ymin = int(nymin * sy)
    orig_xmax = int(nxmax * sx)
    orig_ymax = int(nymax * sy)
    # orig_crop = img.crop((orig_xmin, orig_ymin, orig_xmax, orig_ymax))
    orig_crop = img[orig_xmin:orig_xmax, orig_ymin:orig_ymax, :]
    orig_crop_resize = cv2.resize(orig_crop, (nxmax - nxmin, nymax - nymin), interpolation=cv2.INTER_CUBIC)
    output_img = np.ones((output_h, output_w, channels), dtype=np.uint8) * fill_color

    y_lim = int(min(output_img.shape[0], orig_crop_resize.shape[0]))
    x_lim = int(min(output_img.shape[1], orig_crop_resize.shape[1]))
    output_img[:y_lim, :x_lim, :] = orig_crop_resize[:y_lim, :x_lim, :]
    # orig_crop_resize = orig_crop.resize((nxmax - nxmin, nymax - nymin))
    # output_img = Image.new(img.mode, (output_w, output_h), color=(fill_color,) * channels)
    # output_img.paste(orig_crop_resize, (0, 0))
    return output_img

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant