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

[Blending] OpenCV - SeamlessClone Method #2

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions synthetic-dataset/image_blending.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,23 @@ def basic_blending(img, smoke, offset=(0, 0), opacity=0.8):
mask[dy:dy+smoke.shape[0], dx:dx+smoke.shape[1], :] = mask_dst

return img, mask


def seamless_clone_blending(img, smoke, offset=(0,0), clone_type = cv2.MIXED_CLONE):
"""Add smoke on image using OpenCV with SeamlessClone method

Args:
img (np.array): background image
smoke (np.array): smoke image
offset (tuple, optional): smoke location offset (dy, dx). Defaults to (0, 0).
clone_type (any): type of clone : NORMAL_CLONE, MIXED_CLONE, MONOCHROME_TRANSFER.

Returns:
_type_: _description_
"""

mask = (( smoke > 0 ) * 255).astype('uint8')

blended_img = cv2.seamlessClone(smoke, img, mask, offset, clone_type)

return blended_img, mask
33 changes: 23 additions & 10 deletions synthetic-dataset/make_set.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import cv2
from utils import read_video
from image_blending import basic_blending
from image_blending import basic_blending, seamless_clone_blending
import numpy as np
import os
import random
from functools import partial

BLENDING_METHODS = {
'basic_blending' : basic_blending,
'seamless_clone_mixed' : partial(seamless_clone_blending, clone_type=cv2.MIXED_CLONE),
'seamless_clone_normal' : partial(seamless_clone_blending, clone_type=cv2.MIXED_CLONE),
'seamless_clone_monochrome' : partial(seamless_clone_blending, clone_type=cv2.MONOCHROME_TRANSFER)
}

DATASET_BASE_PATH = 'dataset'

def save_img(folder_path, filename, img):
os.makedirs(folder_path, exist_ok=True)
cv2.imwrite(f'{folder_path}/{filename}', img)

def make_one_set(smoke_video_file, background_file, set_idx, fx=0.3,
fy=0.2, opacity=0.8, smoke_speed=5, smoke_offset=20):
Expand All @@ -31,10 +44,6 @@ def make_one_set(smoke_video_file, background_file, set_idx, fx=0.3,
# Read background
imgs = read_video(background_file)

os.makedirs('dataset/img/', exist_ok=True)
os.makedirs('dataset/mask/', exist_ok=True)
os.makedirs('dataset/smoke/', exist_ok=True)

name = 'set_' + str(set_idx).zfill(3) + '_'

# Random offset
Expand All @@ -45,9 +54,13 @@ def make_one_set(smoke_video_file, background_file, set_idx, fx=0.3,
dy = random.randint(0, hbg - hs - 1)
dx = random.randint(0, wbg - ws - 1)

for i, (img, smoke) in enumerate(zip(imgs, smoke_imgs)):
result, mask = basic_blending(img, smoke, offset=(dy, dx))
for blending_type, blending_method in BLENDING_METHODS.items():

print(f'Starting {blending_type} ...')

cv2.imwrite('dataset/img/' + name + str(i).zfill(4) + '.png', result)
cv2.imwrite('dataset/mask/' + name + str(i).zfill(4) + '.jpg', mask*255)
cv2.imwrite('dataset/smoke/' + name + str(i).zfill(4) + '.jpg', smoke)
for i, (img, smoke) in enumerate(zip(imgs, smoke_imgs)):
result, mask = blending_method(img, smoke, offset=(dy, dx))

save_img(f'{DATASET_BASE_PATH}/{blending_type}/img', name + str(i).zfill(4) + '.png', result)
save_img(f'{DATASET_BASE_PATH}/{blending_type}/mask', name + str(i).zfill(4) + '.jpg', mask*255)
save_img(f'{DATASET_BASE_PATH}/{blending_type}/smoke', name + str(i).zfill(4) + '.jpg', smoke)