gokaygokay commited on
Commit
37327f6
1 Parent(s): b5ca733

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -32
app.py CHANGED
@@ -160,44 +160,29 @@ def _blend(img1: np.ndarray, img2: np.ndarray, mask: np.ndarray) -> np.ndarray:
160
  return img1 * mask + img2 * (1.0 - mask)
161
 
162
  def laplacian_blend(img1: np.ndarray, img2: np.ndarray, mask: np.ndarray, depth: int, sigma: int) -> np.ndarray:
163
- # Ensure both images have the same number of channels (3 for RGB)
164
- if len(img1.shape) == 2:
165
- img1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2RGB)
166
- if len(img2.shape) == 2:
167
- img2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2RGB)
168
-
169
- # Ensure mask has 3 channels
170
- if len(mask.shape) == 2:
171
- mask = np.stack((mask,) * 3, axis=-1)
172
-
173
- # Resize all images to the same size (use the size of img1)
174
- h, w = img1.shape[:2]
175
- img2 = cv2.resize(img2, (w, h))
176
- mask = cv2.resize(mask, (w, h))
177
-
178
  mask_gaus_pyramid = _gaus_pyramid(mask, depth, sigma)
179
  img1_lap_pyramid, img2_lap_pyramid = _lap_pyramid(img1, depth, sigma), _lap_pyramid(img2, depth, sigma)
180
 
181
  blended = [_blend(obj, bg, mask) for obj, bg, mask in zip(img1_lap_pyramid, img2_lap_pyramid, mask_gaus_pyramid)][::-1]
182
 
183
- blended_img = _blend(img1, img2, mask)
184
 
185
- for d in range(depth-1):
186
- gaussian_img = _low_pass_filter(blended_img, sigma)
187
- gaussian_img = cv2.resize(gaussian_img, (blended[d].shape[1], blended[d].shape[0]))
188
-
189
- # Ensure both images have 3 channels
190
- if len(blended[d].shape) == 2:
191
- blended[d] = cv2.cvtColor(blended[d], cv2.COLOR_GRAY2RGB)
192
- if len(gaussian_img.shape) == 2:
193
- gaussian_img = cv2.cvtColor(gaussian_img, cv2.COLOR_GRAY2RGB)
194
-
195
  reconstructed_img = cv2.add(blended[d], gaussian_img)
196
 
197
- blended_img = cv2.pyrUp(reconstructed_img)
198
- blended_img = cv2.resize(blended_img, (w, h))
199
-
200
- return np.clip(blended_img, 0, 1)
201
 
202
  def get_image(img_path: str, mask: bool=False, scale: bool=True) -> np.array:
203
  """
@@ -251,7 +236,7 @@ with gr.Blocks(theme='bethecloud/storj_theme') as iface:
251
  mask_img = gr.Image(label="Mask Image", type="numpy", height=300)
252
  with gr.Row():
253
  with gr.Column():
254
- method = gr.Radio(["Laplacian", "Mixed Gradient"], label="Blending Method", value="Laplacian")
255
  with gr.Column():
256
  blend_button = gr.Button("Blend Images")
257
 
@@ -265,7 +250,7 @@ with gr.Blocks(theme='bethecloud/storj_theme') as iface:
265
 
266
  gr.Examples(
267
  examples=[
268
- ["img1.jpg", "img2.jpg", "mask1.jpg", "Mixed Gradient"],
269
  ["img3.jpg", "img4.jpg", "mask2.jpg", "Mixed Gradient"],
270
  ["img6.jpg", "img9.jpg", "mask3.jpg", "Laplacian"]
271
  ],
 
160
  return img1 * mask + img2 * (1.0 - mask)
161
 
162
  def laplacian_blend(img1: np.ndarray, img2: np.ndarray, mask: np.ndarray, depth: int, sigma: int) -> np.ndarray:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  mask_gaus_pyramid = _gaus_pyramid(mask, depth, sigma)
164
  img1_lap_pyramid, img2_lap_pyramid = _lap_pyramid(img1, depth, sigma), _lap_pyramid(img2, depth, sigma)
165
 
166
  blended = [_blend(obj, bg, mask) for obj, bg, mask in zip(img1_lap_pyramid, img2_lap_pyramid, mask_gaus_pyramid)][::-1]
167
 
168
+ h, w = blended[0].shape[:2]
169
 
170
+ img1 = cv2.resize(img1, (w, h))
171
+ img2 = cv2.resize(img2, (w, h))
172
+ mask = cv2.resize(mask, (w, h))
173
+
174
+ blanded_img = _blend(img1, img2, mask)
175
+ blanded_img = cv2.resize(blanded_img, blended[0].shape[:2])
176
+
177
+ imgs = []
178
+ for d in range(0, depth-1):
179
+ gaussian_img = _low_pass_filter(blanded_img.copy(), sigma)
180
  reconstructed_img = cv2.add(blended[d], gaussian_img)
181
 
182
+ imgs.append(reconstructed_img)
183
+ blanded_img = cv2.pyrUp(reconstructed_img)
184
+
185
+ return np.clip(imgs[-1], 0, 1)
186
 
187
  def get_image(img_path: str, mask: bool=False, scale: bool=True) -> np.array:
188
  """
 
236
  mask_img = gr.Image(label="Mask Image", type="numpy", height=300)
237
  with gr.Row():
238
  with gr.Column():
239
+ method = gr.Radio(["Laplacian", "Poisson", "Mixed Gradient"], label="Blending Method", value="Laplacian")
240
  with gr.Column():
241
  blend_button = gr.Button("Blend Images")
242
 
 
250
 
251
  gr.Examples(
252
  examples=[
253
+ ["img1.jpg", "img2.jpg", "mask1.jpg", "Poisson"],
254
  ["img3.jpg", "img4.jpg", "mask2.jpg", "Mixed Gradient"],
255
  ["img6.jpg", "img9.jpg", "mask3.jpg", "Laplacian"]
256
  ],