From 2f133a452848c045e796eff9b6bcff35cd1f35f3 Mon Sep 17 00:00:00 2001
From: Guillaume Vergnolle <guillaume.vergnolle@gmail.com>
Date: Wed, 26 Feb 2020 20:01:58 -0500
Subject: [PATCH] superposition solved. scale and position randomized

---
 robots-at-runes/research/constants.py     |  2 +
 robots-at-runes/research/superposition.py | 59 +++++++++++++++--------
 2 files changed, 41 insertions(+), 20 deletions(-)

diff --git a/robots-at-runes/research/constants.py b/robots-at-runes/research/constants.py
index e69de29..e670e58 100644
--- a/robots-at-runes/research/constants.py
+++ b/robots-at-runes/research/constants.py
@@ -0,0 +1,2 @@
+FACT_RESIZE = 1     # Facteur de redimensionnement. La taille de l'image sera
+                    # divisée ou multipliée par ce chiffre au maximum
\ No newline at end of file
diff --git a/robots-at-runes/research/superposition.py b/robots-at-runes/research/superposition.py
index 7cd5e00..949fd4a 100644
--- a/robots-at-runes/research/superposition.py
+++ b/robots-at-runes/research/superposition.py
@@ -2,26 +2,47 @@ import numpy as np
 import pandas as pd
 import matplotlib.pyplot as plt
 import cv2
+import random as rd
+import constants as cst
 
-threshold = 100
+percent_scale = np.round((2*rd.random())-1, 3)
 
-
-def superimpose(img1, img2, mask) :
+def superimpose(img1, img2, mask):
     """
     Superimpose two pictures based on a mask. The two pictures
     must have the same shape.
     :param img1: first picture
     :param img2: second picture
-    :param mask: mask applied on both pictures.
-    'True' values will be applied on first picture
+    :param mask: mask applied on both pictures. Values between 0 and 255
     :return: Composition
     """
     dst_shape = img1.shape
-    composition = np.\
-        zeros(dst_shape, dtype=np.uint8)
-    for i in range(dst_shape[2]) :
-        composition[:,:,i] = mask * img1[:,:,i] + ~mask * img2[:,:,i]
-    return composition
+    img1f = img1.astype(np.float)
+    img2f = img2.astype(np.float)
+    mix = np.zeros(dst_shape, dtype=np.uint8)
+    for i in range(dst_shape[2]):
+        mix[:, :, i] = ((~mask * img1f[:, :, i] + mask * img2f[:, :, i]) / 255).astype(np.uint8)
+    return mix
+
+
+def get_subset_shapes(img_extract, hs, ws):
+    he, we, _ = img_extract.shape
+    delta_h = he - hs
+    delta_w = we - ws
+    h_start = int(rd.random() * delta_h)
+    w_start = int(rd.random() * delta_w)
+    return img_extract[h_start:h_start + hs, w_start:w_start + ws, :], h_start, w_start
+
+
+def reshape_percentage(img_base, percent):
+    intensity = 1 + abs(percent) * cst.FACT_RESIZE
+    h, w, _ = img_base.shape
+    if percent < 0:
+        h_dest, w_dest = int(h / intensity), int(w / intensity)
+    else:
+        h_dest, w_dest = h * int(intensity), w * int(intensity)
+
+    return cv2.resize(img_base, (w_dest, h_dest), interpolation=cv2.INTER_AREA)
 
 
 background = cv2.imread("images_sup/back1.jpg")
@@ -29,22 +50,20 @@ background = cv2.cvtColor(background, cv2.COLOR_BGR2RGB)
 
 logo = cv2.imread("images_sup/logo.png", cv2.IMREAD_UNCHANGED)
 logo = cv2.cvtColor(logo, cv2.COLOR_BGRA2RGBA)
-
-h,w,_ = background.shape
-logo = cv2.resize(logo, (w,h), interpolation = cv2.INTER_AREA)
-
-mask_alpha = (logo[:,:,3] < threshold)
-
+logo = reshape_percentage(logo, percent_scale)
+mask_alpha = logo[:, :, 3]
 logo = cv2.cvtColor(logo, cv2.COLOR_RGBA2RGB)
+hs, ws, _ = logo.shape
 
-# logo = cv2.imread("images_sup/logo.png")
-# logo = cv2.cvtColor(logo, cv2.COLOR_BGR2RGB)
+background_subset, h_start, w_start = get_subset_shapes(background, hs, ws)
+composition_subset = superimpose(background_subset, logo, mask_alpha)
 
-composition = superimpose(background, logo, mask_alpha)
+composition = background.copy()
+composition[h_start:h_start + hs, w_start:w_start + ws, :] = composition_subset
 
 plt.imshow(background)
 plt.show()
 plt.imshow(logo)
 plt.show()
 plt.imshow(composition)
-plt.show()
\ No newline at end of file
+plt.show()
-- 
GitLab