Datasets:

ArXiv:
Tags:
art
License:
schirrmacher commited on
Commit
6b540a3
1 Parent(s): 2c95215

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. create_dataset.sh +4 -4
  2. util/merge_images.py +46 -66
create_dataset.sh CHANGED
@@ -26,10 +26,10 @@ main() {
26
  random_merge dataset/training/im dataset/training/gt &
27
  random_merge dataset/training/im dataset/training/gt &
28
  random_merge dataset/training/im dataset/training/gt &
29
- random_merge dataset/training/im dataset/training/gt &
30
- random_merge dataset/training/im dataset/training/gt &
31
- random_merge dataset/training/im dataset/training/gt &
32
- random_merge dataset/validation/im dataset/validation/g
33
  done
34
  }
35
 
 
26
  random_merge dataset/training/im dataset/training/gt &
27
  random_merge dataset/training/im dataset/training/gt &
28
  random_merge dataset/training/im dataset/training/gt &
29
+ random_merge dataset/training/im dataset/training/gt
30
+ random_merge dataset/training/im dataset/training/gt
31
+ random_merge dataset/training/im dataset/training/gt
32
+ random_merge dataset/validation/im dataset/validation/gt
33
  done
34
  }
35
 
util/merge_images.py CHANGED
@@ -12,8 +12,8 @@ def apply_scale_and_move(image):
12
  A.HorizontalFlip(p=0.5),
13
  A.ShiftScaleRotate(
14
  shift_limit_x=(-0.3, 0.3),
15
- shift_limit_y=(0.0, 0.2),
16
- scale_limit=(1.0, 1.5),
17
  border_mode=cv2.BORDER_CONSTANT,
18
  rotate_limit=(-3, 3),
19
  p=0.7,
@@ -23,7 +23,7 @@ def apply_scale_and_move(image):
23
  return transform(image=image)["image"]
24
 
25
 
26
- def apply_transform(image):
27
  has_alpha = image.shape[2] == 4
28
  if has_alpha:
29
  alpha_channel = image[:, :, 3]
@@ -59,7 +59,7 @@ def apply_transform(image):
59
  return final_image
60
 
61
 
62
- def apply_noise(image):
63
  transform = A.Compose(
64
  [
65
  A.MotionBlur(blur_limit=(5, 11), p=1.0),
@@ -97,85 +97,79 @@ def remove_alpha(image, alpha_threshold=200):
97
  return image
98
 
99
 
100
- def merge_images(
101
- background_path, overlay_path, output_path, groundtruth_path, width, height
102
- ):
103
  letters = string.ascii_lowercase
104
  random_string = "".join(random.choice(letters) for i in range(13))
105
  file_name = random_string + "_" + os.path.basename(overlay_path)
106
 
107
- # Read the background image and resize it to the specified dimensions
108
  background = cv2.imread(background_path, cv2.IMREAD_COLOR)
109
-
110
  height, width = background.shape[:2]
111
 
112
- height = int(1.5 * height)
113
- width = int(1.5 * width)
114
-
115
- resized_background = cv2.resize(
116
- background, (width, height), interpolation=cv2.INTER_AREA
117
- )
118
-
119
- # Read the overlay image with alpha channel
120
  overlay = cv2.imread(overlay_path, cv2.IMREAD_UNCHANGED)
121
 
122
- # Ensure overlay has an alpha channel
123
  if overlay.shape[2] < 4:
124
  raise Exception("Overlay image does not have an alpha channel.")
125
 
126
- # Apply transformations to the overlay
127
  overlay = expand_image_borders_rgba(overlay, width, height)
128
  overlay = apply_scale_and_move(overlay)
129
 
130
  # store ground truth
131
- extract_alpha_channel_as_bw(overlay, os.path.join(groundtruth_path, file_name))
132
 
133
- overlay = apply_transform(overlay)
134
 
135
- # Overlay placement on the resized background
136
- x_offset = (width - overlay.shape[1]) // 2
137
- y_offset = (height - overlay.shape[0]) // 2
138
 
139
- # Preventing overlay from exceeding the background dimensions
140
- x_offset = max(0, x_offset)
141
- y_offset = max(0, y_offset)
 
 
142
 
143
- # Calculate the normalized alpha mask
144
- alpha_overlay = overlay[..., 3] / 255.0
145
- region_of_interest = resized_background[
146
- y_offset : y_offset + overlay.shape[0],
147
- x_offset : x_offset + overlay.shape[1],
148
- :,
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  ]
 
150
 
151
- # Blend the images
152
- for c in range(0, 3):
153
- region_of_interest[..., c] = (
154
- alpha_overlay * overlay[..., c]
155
- + (1 - alpha_overlay) * region_of_interest[..., c]
156
- )
157
 
158
- resized_background[
159
- y_offset : y_offset + overlay.shape[0], x_offset : x_offset + overlay.shape[1]
160
- ] = region_of_interest
161
 
162
- resized_background = apply_noise(resized_background)
163
 
164
- cv2.imwrite(os.path.join(output_path, file_name), resized_background)
165
 
166
 
167
  def expand_image_borders_rgba(
168
  image, final_width, final_height, border_color=(0, 0, 0, 0)
169
  ):
170
- # Check if image has an alpha channel
171
- if image.shape[2] < 4:
172
- raise ValueError(
173
- "Loaded image does not contain an alpha channel. Make sure the input image is RGBA."
174
- )
175
-
176
- # Current dimensions
177
  height, width = image.shape[:2]
178
 
 
 
 
 
179
  # Calculate padding needed
180
  top = bottom = (final_height - height) // 2
181
  left = right = (final_width - width) // 2
@@ -194,7 +188,7 @@ def expand_image_borders_rgba(
194
  return new_image
195
 
196
 
197
- def extract_alpha_channel_as_bw(image, output_path):
198
  # Check if the image has an alpha channel
199
  if image.shape[2] < 4:
200
  raise ValueError(
@@ -225,18 +219,6 @@ def main():
225
  default="im",
226
  help="Path where the merged image will be saved",
227
  )
228
- parser.add_argument(
229
- "--width",
230
- type=int,
231
- default=1920,
232
- help="Width to which the background image will be resized",
233
- )
234
- parser.add_argument(
235
- "--height",
236
- type=int,
237
- default=1080,
238
- help="Height to which the background image will be resized",
239
- )
240
  parser.add_argument(
241
  "-gt",
242
  "--groundtruth-path",
@@ -256,8 +238,6 @@ def main():
256
  args.overlay,
257
  args.image_path,
258
  args.groundtruth_path,
259
- args.width,
260
- args.height,
261
  )
262
 
263
 
 
12
  A.HorizontalFlip(p=0.5),
13
  A.ShiftScaleRotate(
14
  shift_limit_x=(-0.3, 0.3),
15
+ shift_limit_y=(-0.1, 0.6),
16
+ scale_limit=(1.0, 1.2),
17
  border_mode=cv2.BORDER_CONSTANT,
18
  rotate_limit=(-3, 3),
19
  p=0.7,
 
23
  return transform(image=image)["image"]
24
 
25
 
26
+ def augment_overlay(image):
27
  has_alpha = image.shape[2] == 4
28
  if has_alpha:
29
  alpha_channel = image[:, :, 3]
 
59
  return final_image
60
 
61
 
62
+ def augment_result(image):
63
  transform = A.Compose(
64
  [
65
  A.MotionBlur(blur_limit=(5, 11), p=1.0),
 
97
  return image
98
 
99
 
100
+ def merge_images(background_path, overlay_path, output_path, groundtruth_path):
 
 
101
  letters = string.ascii_lowercase
102
  random_string = "".join(random.choice(letters) for i in range(13))
103
  file_name = random_string + "_" + os.path.basename(overlay_path)
104
 
 
105
  background = cv2.imread(background_path, cv2.IMREAD_COLOR)
 
106
  height, width = background.shape[:2]
107
 
 
 
 
 
 
 
 
 
108
  overlay = cv2.imread(overlay_path, cv2.IMREAD_UNCHANGED)
109
 
 
110
  if overlay.shape[2] < 4:
111
  raise Exception("Overlay image does not have an alpha channel.")
112
 
 
113
  overlay = expand_image_borders_rgba(overlay, width, height)
114
  overlay = apply_scale_and_move(overlay)
115
 
116
  # store ground truth
117
+ store_ground_truth(overlay, os.path.join(groundtruth_path, file_name))
118
 
119
+ overlay = augment_overlay(overlay)
120
 
121
+ # Calculate the aspect ratio of the overlay
122
+ overlay_height, overlay_width = overlay.shape[:2]
123
+ aspect_ratio = overlay_width / overlay_height
124
 
125
+ # Calculate scaling factors, maintaining the aspect ratio
126
+ max_height = background.shape[0]
127
+ max_width = background.shape[1]
128
+ scale_width = max_width
129
+ scale_height = int(scale_width / aspect_ratio)
130
 
131
+ # Check if the scaled overlay height is too large
132
+ if scale_height > max_height:
133
+ scale_height = max_height
134
+ scale_width = int(scale_height * aspect_ratio)
135
+
136
+ # Resize the overlay image
137
+ overlay_resized = cv2.resize(overlay, (scale_width, scale_height))
138
+
139
+ # Calculate position for overlay (centered)
140
+ x_pos = (background.shape[1] - scale_width) // 2
141
+ y_pos = (background.shape[0] - scale_height) // 2
142
+
143
+ # Extract the alpha mask and the color channels of the overlay
144
+ alpha_mask = overlay_resized[:, :, 3] / 255.0
145
+ overlay_color = overlay_resized[:, :, :3]
146
+
147
+ # Use the mask to create the transparent effect in the background
148
+ background_part = (1 - alpha_mask)[:, :, None] * background[
149
+ y_pos : y_pos + scale_height, x_pos : x_pos + scale_width, :
150
  ]
151
+ overlay_part = alpha_mask[:, :, None] * overlay_color
152
 
153
+ # Add the overlay part to the background part
154
+ merged = background_part + overlay_part
 
 
 
 
155
 
156
+ # Put the merged image back into the background
157
+ background[y_pos : y_pos + scale_height, x_pos : x_pos + scale_width] = merged
 
158
 
159
+ result = augment_result(background)
160
 
161
+ cv2.imwrite(os.path.join(output_path, file_name), result)
162
 
163
 
164
  def expand_image_borders_rgba(
165
  image, final_width, final_height, border_color=(0, 0, 0, 0)
166
  ):
 
 
 
 
 
 
 
167
  height, width = image.shape[:2]
168
 
169
+ # The background image might be smaller or bigger
170
+ final_height = max(height, final_height)
171
+ final_width = max(width, final_width)
172
+
173
  # Calculate padding needed
174
  top = bottom = (final_height - height) // 2
175
  left = right = (final_width - width) // 2
 
188
  return new_image
189
 
190
 
191
+ def store_ground_truth(image, output_path):
192
  # Check if the image has an alpha channel
193
  if image.shape[2] < 4:
194
  raise ValueError(
 
219
  default="im",
220
  help="Path where the merged image will be saved",
221
  )
 
 
 
 
 
 
 
 
 
 
 
 
222
  parser.add_argument(
223
  "-gt",
224
  "--groundtruth-path",
 
238
  args.overlay,
239
  args.image_path,
240
  args.groundtruth_path,
 
 
241
  )
242
 
243