Candle commited on
Commit
07ef396
·
1 Parent(s): 6ba27a6
Files changed (1) hide show
  1. correct_fgr.py +37 -24
correct_fgr.py CHANGED
@@ -3,8 +3,9 @@
3
  Foreground Correction Script
4
 
5
  This script processes images from data/expanded/*/*.png and matching pairs
6
- from data/automatte/*/*.png to generate corrected foreground images in
7
- data/fgr/*/*.png with the same names.
 
8
 
9
  The foreground correction process:
10
  1. Load alpha (from automatte folder)
@@ -13,7 +14,9 @@ The foreground correction process:
13
  4. Invert selection to create a mask
14
  5. Apply minimum filter with 4px radius to the RGB image using the inverted selection mask
15
  (equivalent to Filter > Other > Minimum in Photoshop)
16
- 6. Use the contracted alpha (before inversion) as the final alpha channel
 
 
17
  """
18
 
19
  import os
@@ -75,14 +78,15 @@ def apply_minimum_filter_to_rgb(rgb_image, mask, radius=4):
75
  return filtered_rgb
76
 
77
 
78
- def process_foreground_correction(expanded_path, automatte_path, output_path):
79
  """
80
  Process a single image pair for foreground correction.
81
 
82
  Args:
83
  expanded_path: Path to the expanded image
84
  automatte_path: Path to the automatte (alpha) image
85
- output_path: Path where the corrected image will be saved
 
86
  """
87
  # Load the expanded image (RGB)
88
  expanded_img = cv2.imread(expanded_path, cv2.IMREAD_COLOR)
@@ -109,8 +113,8 @@ def process_foreground_correction(expanded_path, automatte_path, output_path):
109
  thresholded_alpha = apply_threshold(alpha, threshold=235)
110
 
111
  # Step 3: Contract the alpha channel 1px
112
- # contracted_alpha = contract_alpha(thresholded_alpha, pixels=0)
113
- contracted_alpha = thresholded_alpha
114
 
115
  # Step 4: Invert selection
116
  selection_mask = invert_selection(contracted_alpha)
@@ -129,17 +133,24 @@ def process_foreground_correction(expanded_path, automatte_path, output_path):
129
  # import sys; sys.exit(0)
130
 
131
  # Apply the original contracted alpha (before inversion) to the filtered RGB image
132
- # Convert to RGBA
133
- expanded_rgba = cv2.cvtColor(filtered_rgb, cv2.COLOR_BGR2BGRA)
134
- expanded_rgba[:, :, 3] = alpha # Use the contracted alpha (before inversion)
135
-
136
- # Create output directory if it doesn't exist
137
- os.makedirs(os.path.dirname(output_path), exist_ok=True)
 
 
 
 
 
 
 
138
 
139
- # Save the result
140
- success = cv2.imwrite(output_path, filtered_rgb)
141
- if not success:
142
- print(f"Error: Could not save image to {output_path}")
143
  return False
144
 
145
  return True
@@ -168,9 +179,10 @@ def find_matching_pairs():
168
 
169
  # Check if the automatte file exists
170
  if os.path.exists(automatte_path):
171
- # Construct output path
172
- output_path = os.path.join("data/fgr", rel_path)
173
- pairs.append((expanded_path, automatte_path, output_path))
 
174
  else:
175
  print(f"Warning: No matching automatte file for {expanded_path}")
176
 
@@ -205,17 +217,18 @@ def main():
205
  else:
206
  print(f"Found {len(pairs)} matching pairs to process")
207
 
208
- # Create output base directory
209
  os.makedirs("data/fgr", exist_ok=True)
 
210
 
211
  # Process each pair
212
  successful = 0
213
  failed = 0
214
 
215
- for i, (expanded_path, automatte_path, output_path) in enumerate(pairs):
216
- print(f"Processing {i+1}/{len(pairs)}: {os.path.basename(output_path)}")
217
 
218
- if process_foreground_correction(expanded_path, automatte_path, output_path):
219
  successful += 1
220
  else:
221
  failed += 1
 
3
  Foreground Correction Script
4
 
5
  This script processes images from data/expanded/*/*.png and matching pairs
6
+ from data/automatte/*/*.png to generate two types of corrected images:
7
+ 1. Processed RGB images in data/fgr/*/*.png (RGB without alpha)
8
+ 2. Masked RGBA images in data/masked/*/*.png (with alpha channel applied)
9
 
10
  The foreground correction process:
11
  1. Load alpha (from automatte folder)
 
14
  4. Invert selection to create a mask
15
  5. Apply minimum filter with 4px radius to the RGB image using the inverted selection mask
16
  (equivalent to Filter > Other > Minimum in Photoshop)
17
+ 6. Export both versions:
18
+ - FGR: RGB image with the processed regions
19
+ - Masked: RGBA image using the contracted alpha (before inversion) as the alpha channel
20
  """
21
 
22
  import os
 
78
  return filtered_rgb
79
 
80
 
81
+ def process_foreground_correction(expanded_path, automatte_path, fgr_output_path, masked_output_path):
82
  """
83
  Process a single image pair for foreground correction.
84
 
85
  Args:
86
  expanded_path: Path to the expanded image
87
  automatte_path: Path to the automatte (alpha) image
88
+ fgr_output_path: Path where the processed RGB image will be saved
89
+ masked_output_path: Path where the masked RGBA image will be saved
90
  """
91
  # Load the expanded image (RGB)
92
  expanded_img = cv2.imread(expanded_path, cv2.IMREAD_COLOR)
 
113
  thresholded_alpha = apply_threshold(alpha, threshold=235)
114
 
115
  # Step 3: Contract the alpha channel 1px
116
+ contracted_alpha = contract_alpha(thresholded_alpha, pixels=1)
117
+ # contracted_alpha = thresholded_alpha
118
 
119
  # Step 4: Invert selection
120
  selection_mask = invert_selection(contracted_alpha)
 
133
  # import sys; sys.exit(0)
134
 
135
  # Apply the original contracted alpha (before inversion) to the filtered RGB image
136
+ # Convert to RGBA for masked output
137
+ masked_rgba = cv2.cvtColor(filtered_rgb, cv2.COLOR_BGR2BGRA)
138
+ masked_rgba[:, :, 3] = alpha # Use the contracted alpha (before inversion)
139
+
140
+ # Create output directories if they don't exist
141
+ os.makedirs(os.path.dirname(fgr_output_path), exist_ok=True)
142
+ os.makedirs(os.path.dirname(masked_output_path), exist_ok=True)
143
+
144
+ # Save the processed RGB image (without alpha)
145
+ success_fgr = cv2.imwrite(fgr_output_path, filtered_rgb)
146
+ if not success_fgr:
147
+ print(f"Error: Could not save RGB image to {fgr_output_path}")
148
+ return False
149
 
150
+ # Save the masked RGBA image (with alpha channel)
151
+ success_masked = cv2.imwrite(masked_output_path, masked_rgba)
152
+ if not success_masked:
153
+ print(f"Error: Could not save masked image to {masked_output_path}")
154
  return False
155
 
156
  return True
 
179
 
180
  # Check if the automatte file exists
181
  if os.path.exists(automatte_path):
182
+ # Construct output paths for both fgr and masked
183
+ fgr_output_path = os.path.join("data/fgr", rel_path)
184
+ masked_output_path = os.path.join("data/masked", rel_path)
185
+ pairs.append((expanded_path, automatte_path, fgr_output_path, masked_output_path))
186
  else:
187
  print(f"Warning: No matching automatte file for {expanded_path}")
188
 
 
217
  else:
218
  print(f"Found {len(pairs)} matching pairs to process")
219
 
220
+ # Create output base directories
221
  os.makedirs("data/fgr", exist_ok=True)
222
+ os.makedirs("data/masked", exist_ok=True)
223
 
224
  # Process each pair
225
  successful = 0
226
  failed = 0
227
 
228
+ for i, (expanded_path, automatte_path, fgr_output_path, masked_output_path) in enumerate(pairs):
229
+ print(f"Processing {i+1}/{len(pairs)}: {os.path.basename(fgr_output_path)}")
230
 
231
+ if process_foreground_correction(expanded_path, automatte_path, fgr_output_path, masked_output_path):
232
  successful += 1
233
  else:
234
  failed += 1