From ee2c5165f8a794cc1181989abef43ed2dbc95946 Mon Sep 17 00:00:00 2001 From: Josselin Pennors Date: Wed, 20 Apr 2022 18:29:10 +0200 Subject: [PATCH 1/5] Add seamles clone method --- synthetic-dataset/image_blending.py | 20 ++++++++++++++++++++ synthetic-dataset/make_set.py | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/synthetic-dataset/image_blending.py b/synthetic-dataset/image_blending.py index 6eb89a7..34148c7 100644 --- a/synthetic-dataset/image_blending.py +++ b/synthetic-dataset/image_blending.py @@ -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_bleding(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 diff --git a/synthetic-dataset/make_set.py b/synthetic-dataset/make_set.py index 54c04c0..5e45e91 100644 --- a/synthetic-dataset/make_set.py +++ b/synthetic-dataset/make_set.py @@ -1,6 +1,6 @@ import cv2 from utils import read_video -from image_blending import basic_blending +from image_blending import basic_blending, seamless_clone_bleding import numpy as np import os import random From 68efb7dccea2c23a97fa1740eb9d7df2d14aac0f Mon Sep 17 00:00:00 2001 From: Josselin Pennors Date: Wed, 20 Apr 2022 18:37:21 +0200 Subject: [PATCH 2/5] Typo --- synthetic-dataset/image_blending.py | 2 +- synthetic-dataset/make_set.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/synthetic-dataset/image_blending.py b/synthetic-dataset/image_blending.py index 34148c7..b214f1f 100644 --- a/synthetic-dataset/image_blending.py +++ b/synthetic-dataset/image_blending.py @@ -30,7 +30,7 @@ def basic_blending(img, smoke, offset=(0, 0), opacity=0.8): return img, mask -def seamless_clone_bleding(img, smoke, offset=(0,0), clone_type = cv2.MIXED_CLONE): +def seamless_clone_blending(img, smoke, offset=(0,0), clone_type = cv2.MIXED_CLONE): """Add smoke on image using OpenCV with SeamlessClone method Args: diff --git a/synthetic-dataset/make_set.py b/synthetic-dataset/make_set.py index 5e45e91..4345f46 100644 --- a/synthetic-dataset/make_set.py +++ b/synthetic-dataset/make_set.py @@ -1,6 +1,6 @@ import cv2 from utils import read_video -from image_blending import basic_blending, seamless_clone_bleding +from image_blending import basic_blending, seamless_clone_blending import numpy as np import os import random From cfb41987cb6ab2142545eefa9edae067072d30fb Mon Sep 17 00:00:00 2001 From: Josselin Pennors Date: Wed, 20 Apr 2022 18:55:37 +0200 Subject: [PATCH 3/5] Add method to test different methods --- synthetic-dataset/make_set.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/synthetic-dataset/make_set.py b/synthetic-dataset/make_set.py index 4345f46..caa0d2b 100644 --- a/synthetic-dataset/make_set.py +++ b/synthetic-dataset/make_set.py @@ -4,7 +4,20 @@ 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): @@ -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 @@ -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) From 5c40a2b0927f887d010d1c41cebf06452f8bb1c4 Mon Sep 17 00:00:00 2001 From: Josselin Pennors Date: Wed, 18 May 2022 19:29:35 +0200 Subject: [PATCH 4/5] Move Blending Image method for new repo setup --- synthetic-dataset/image_blending.py | 50 ----------------------------- syntheticdataset/image_blending.py | 19 +++++++++++ 2 files changed, 19 insertions(+), 50 deletions(-) delete mode 100644 synthetic-dataset/image_blending.py diff --git a/synthetic-dataset/image_blending.py b/synthetic-dataset/image_blending.py deleted file mode 100644 index b214f1f..0000000 --- a/synthetic-dataset/image_blending.py +++ /dev/null @@ -1,50 +0,0 @@ -import cv2 -import numpy as np - - -def basic_blending(img, smoke, offset=(0, 0), opacity=0.8): - """Add smoke on image using basic image blending - - Args: - img (np.array): background image - smoke (np.array): smoke image - offset (tuple, optional): smoke location offset (dy, dx). Defaults to (0, 0). - opacity (float, optional): smoke image opacity in [0, 1]. Defaults to (0, 0). - - Returns: - _type_: _description_ - """ - ks = 7 - kernel = np.ones((ks, ks), np.float32)/(ks**2) - - dy, dx = offset - temp = img[dy:dy+smoke.shape[0], dx:dx+smoke.shape[1], :] - dst = cv2.filter2D(smoke, -1, kernel) - mask_dst = dst > 50 - alpha = 1 - opacity * dst/np.max(dst) - res = temp*alpha + smoke[:, :, ::-1]*(1-alpha) - img[dy:dy+smoke.shape[0], dx:dx+smoke.shape[1], :] = res - mask = img * 0 - 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 diff --git a/syntheticdataset/image_blending.py b/syntheticdataset/image_blending.py index 21cf190..9da44fd 100644 --- a/syntheticdataset/image_blending.py +++ b/syntheticdataset/image_blending.py @@ -63,3 +63,22 @@ def poisson_blending(img, smoke, offset=(0, 0)): mask[dy : dy + smoke_mask.shape[0], dx : dx + smoke_mask.shape[1]] = smoke_mask return result, 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 \ No newline at end of file From 7b37f524a91663564e5dd4f2010c7b16e9a139a2 Mon Sep 17 00:00:00 2001 From: Josselin Pennors Date: Wed, 18 May 2022 19:43:41 +0200 Subject: [PATCH 5/5] Linter --- syntheticdataset/image_blending.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/syntheticdataset/image_blending.py b/syntheticdataset/image_blending.py index 9da44fd..f058868 100644 --- a/syntheticdataset/image_blending.py +++ b/syntheticdataset/image_blending.py @@ -64,9 +64,10 @@ def poisson_blending(img, smoke, offset=(0, 0)): return result, mask -def seamless_clone_blending(img, smoke, offset=(0,0), clone_type = cv2.MIXED_CLONE): + +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 @@ -77,8 +78,8 @@ def seamless_clone_blending(img, smoke, offset=(0,0), clone_type = cv2.MIXED_CLO _type_: _description_ """ - mask = (( smoke > 0 ) * 255).astype('uint8') + mask = ((smoke > 0) * 255).astype("uint8") blended_img = cv2.seamlessClone(smoke, img, mask, offset, clone_type) - return blended_img, mask \ No newline at end of file + return blended_img, mask