|
import os |
|
import cv2 |
|
import argparse |
|
import random |
|
import string |
|
import albumentations as A |
|
|
|
|
|
def apply_scale_and_move(image): |
|
transform = A.Compose( |
|
[ |
|
A.HorizontalFlip(p=0.5), |
|
A.ShiftScaleRotate( |
|
shift_limit_x=(-0.3, 0.3), |
|
shift_limit_y=(-0.1, 0.6), |
|
scale_limit=(1.0, 1.2), |
|
border_mode=cv2.BORDER_CONSTANT, |
|
rotate_limit=(-3, 3), |
|
p=0.7, |
|
), |
|
] |
|
) |
|
return transform(image=image)["image"] |
|
|
|
|
|
def augment_overlay(image): |
|
has_alpha = image.shape[2] == 4 |
|
if has_alpha: |
|
alpha_channel = image[:, :, 3] |
|
color_channels = image[:, :, :3] |
|
else: |
|
color_channels = image |
|
|
|
|
|
transform = A.Compose( |
|
[ |
|
A.RandomBrightnessContrast( |
|
brightness_limit=(-0.1, 0.1), contrast_limit=(-0.4, 0), p=0.8 |
|
) |
|
] |
|
) |
|
|
|
|
|
transformed = transform(image=color_channels) |
|
transformed_image = transformed["image"] |
|
|
|
|
|
if has_alpha: |
|
final_image = cv2.merge( |
|
( |
|
transformed_image[:, :, 0], |
|
transformed_image[:, :, 1], |
|
transformed_image[:, :, 2], |
|
alpha_channel, |
|
) |
|
) |
|
else: |
|
final_image = transformed_image |
|
return final_image |
|
|
|
|
|
def augment_result(image): |
|
transform = A.Compose( |
|
[ |
|
A.MotionBlur(blur_limit=(5, 11), p=1.0), |
|
A.GaussNoise(var_limit=(10, 150), p=1.0), |
|
A.RandomBrightnessContrast( |
|
brightness_limit=(-0.1, 0.1), contrast_limit=(-0.1, 0.1), p=0.5 |
|
), |
|
A.RandomFog( |
|
fog_coef_lower=0.05, |
|
fog_coef_upper=0.2, |
|
alpha_coef=0.08, |
|
always_apply=False, |
|
p=0.5, |
|
), |
|
A.RandomShadow( |
|
shadow_roi=(0, 0.5, 1, 1), |
|
num_shadows_limit=(1, 2), |
|
num_shadows_lower=None, |
|
num_shadows_upper=None, |
|
shadow_dimension=5, |
|
always_apply=False, |
|
p=0.5, |
|
), |
|
A.RandomToneCurve(scale=0.1, always_apply=False, p=0.5), |
|
] |
|
) |
|
return transform(image=image)["image"] |
|
|
|
|
|
def remove_alpha(image, alpha_threshold=200): |
|
|
|
mask = image[:, :, 3] < alpha_threshold |
|
image[mask] = [0, 0, 0, 0] |
|
|
|
return image |
|
|
|
|
|
def merge_images(background_path, overlay_path, output_path, groundtruth_path): |
|
letters = string.ascii_lowercase |
|
random_string = "".join(random.choice(letters) for i in range(13)) |
|
file_name = random_string + "_" + os.path.basename(overlay_path) |
|
|
|
background = cv2.imread(background_path, cv2.IMREAD_COLOR) |
|
height, width = background.shape[:2] |
|
|
|
overlay = cv2.imread(overlay_path, cv2.IMREAD_UNCHANGED) |
|
|
|
if overlay.shape[2] < 4: |
|
raise Exception("Overlay image does not have an alpha channel.") |
|
|
|
overlay = expand_image_borders_rgba(overlay, width, height) |
|
overlay = apply_scale_and_move(overlay) |
|
|
|
|
|
store_ground_truth(overlay, os.path.join(groundtruth_path, file_name)) |
|
|
|
overlay = augment_overlay(overlay) |
|
|
|
|
|
overlay_height, overlay_width = overlay.shape[:2] |
|
aspect_ratio = overlay_width / overlay_height |
|
|
|
|
|
max_height = background.shape[0] |
|
max_width = background.shape[1] |
|
scale_width = max_width |
|
scale_height = int(scale_width / aspect_ratio) |
|
|
|
|
|
if scale_height > max_height: |
|
scale_height = max_height |
|
scale_width = int(scale_height * aspect_ratio) |
|
|
|
|
|
overlay_resized = cv2.resize(overlay, (scale_width, scale_height)) |
|
|
|
|
|
x_pos = (background.shape[1] - scale_width) // 2 |
|
y_pos = (background.shape[0] - scale_height) // 2 |
|
|
|
|
|
alpha_mask = overlay_resized[:, :, 3] / 255.0 |
|
overlay_color = overlay_resized[:, :, :3] |
|
|
|
|
|
background_part = (1 - alpha_mask)[:, :, None] * background[ |
|
y_pos : y_pos + scale_height, x_pos : x_pos + scale_width, : |
|
] |
|
overlay_part = alpha_mask[:, :, None] * overlay_color |
|
|
|
|
|
merged = background_part + overlay_part |
|
|
|
|
|
background[y_pos : y_pos + scale_height, x_pos : x_pos + scale_width] = merged |
|
|
|
result = augment_result(background) |
|
|
|
cv2.imwrite(os.path.join(output_path, file_name), result) |
|
|
|
|
|
def expand_image_borders_rgba( |
|
image, final_width, final_height, border_color=(0, 0, 0, 0) |
|
): |
|
height, width = image.shape[:2] |
|
|
|
|
|
final_height = max(height, final_height) |
|
final_width = max(width, final_width) |
|
|
|
|
|
top = bottom = (final_height - height) // 2 |
|
left = right = (final_width - width) // 2 |
|
|
|
|
|
if (final_height - height) % 2 != 0: |
|
bottom += 1 |
|
if (final_width - width) % 2 != 0: |
|
right += 1 |
|
|
|
|
|
new_image = cv2.copyMakeBorder( |
|
image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=border_color |
|
) |
|
|
|
return new_image |
|
|
|
|
|
def store_ground_truth(image, output_path): |
|
|
|
if image.shape[2] < 4: |
|
raise ValueError( |
|
"Loaded image does not contain an alpha channel. Make sure the input image is in PNG format with an alpha channel." |
|
) |
|
|
|
|
|
image = remove_alpha(image.copy()) |
|
alpha_channel = image[:, :, 3] |
|
|
|
cv2.imwrite(output_path, alpha_channel) |
|
|
|
|
|
def main(): |
|
parser = argparse.ArgumentParser( |
|
description="Merge two images with one image having transparency." |
|
) |
|
parser.add_argument( |
|
"-b", "--background", required=True, help="Path to the background image" |
|
) |
|
parser.add_argument( |
|
"-o", "--overlay", required=True, help="Path to the overlay image" |
|
) |
|
parser.add_argument( |
|
"-im", |
|
"--image-path", |
|
type=str, |
|
default="im", |
|
help="Path where the merged image will be saved", |
|
) |
|
parser.add_argument( |
|
"-gt", |
|
"--groundtruth-path", |
|
type=str, |
|
default="gt", |
|
help="Ground truth folder", |
|
) |
|
args = parser.parse_args() |
|
|
|
if not os.path.exists(args.image_path): |
|
os.makedirs(args.image_path) |
|
if not os.path.exists(args.groundtruth_path): |
|
os.makedirs(args.groundtruth_path) |
|
|
|
merge_images( |
|
args.background, |
|
args.overlay, |
|
args.image_path, |
|
args.groundtruth_path, |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|