Candle commited on
Commit
ca65e92
·
1 Parent(s): 38084f6
Files changed (1) hide show
  1. correct_fgr.py +16 -17
correct_fgr.py CHANGED
@@ -9,7 +9,7 @@ from data/automatte/*/*.png to generate two types of corrected images:
9
 
10
  The foreground correction process:
11
  1. Load alpha (from automatte folder)
12
- 2. Apply thresholding (white = anything >253, black otherwise)
13
  3. Contract the alpha channel 1px (equivalent to Select > Modify > Contract in Photoshop)
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
@@ -27,8 +27,7 @@ import numpy as np
27
  from pathlib import Path
28
  import argparse
29
 
30
-
31
- def apply_threshold(alpha_channel, threshold=253):
32
  """Apply thresholding: white for values > threshold, black otherwise."""
33
  _, thresholded = cv2.threshold(alpha_channel, threshold, 255, cv2.THRESH_BINARY)
34
  return thresholded
@@ -142,7 +141,7 @@ def apply_boundary_blur(rgb_image, boundary_mask, blur_sigma=0.5):
142
 
143
 
144
  def process_foreground_correction(expanded_path, automatte_path, fgr_output_path, masked_output_path,
145
- threshold=253, contract_pixels=1, minimum_radius=4, blur_sigma=0.5):
146
  """
147
  Process a single image pair for foreground correction.
148
 
@@ -194,19 +193,19 @@ def process_foreground_correction(expanded_path, automatte_path, fgr_output_path
194
  final_rgb = apply_boundary_blur(filtered_rgb, boundary_mask, blur_sigma=blur_sigma)
195
 
196
  # # DO NOT COMMIT: save alpha and exit
197
- # cv2.imwrite("debug_expanded.png", expanded_img)
198
- # cv2.imwrite("debug_automatte.png", automatte_img)
199
- # cv2.imwrite("debug_alpha.png", alpha)
200
- # cv2.imwrite("debug_thresholded_alpha.png", thresholded_alpha)
201
- # cv2.imwrite("debug_contracted_alpha.png", contracted_alpha)
202
- # cv2.imwrite("debug_selection_mask.png", selection_mask)
203
- # cv2.imwrite("debug_filtered_rgb.png", filtered_rgb)
204
- # import sys; sys.exit(0)
205
 
206
  # Apply the original contracted alpha (before inversion) to the final RGB image
207
  # Convert to RGBA for masked output
208
  masked_rgba = cv2.cvtColor(final_rgb, cv2.COLOR_BGR2BGRA)
209
- masked_rgba[:, :, 3] = contracted_alpha # Use the contracted alpha (before inversion)
210
 
211
  # Create output directories if they don't exist
212
  os.makedirs(os.path.dirname(fgr_output_path), exist_ok=True)
@@ -263,12 +262,12 @@ def find_matching_pairs():
263
  def main():
264
  """Main function to process all image pairs."""
265
  parser = argparse.ArgumentParser(description="Apply foreground correction to image pairs")
266
- parser.add_argument("--threshold", type=int, default=253,
267
- help="Threshold value for alpha binarization (default: 253)")
268
  parser.add_argument("--contract-pixels", type=int, default=1,
269
  help="Number of pixels to contract alpha channel (default: 1)")
270
- parser.add_argument("--minimum-radius", type=int, default=4,
271
- help="Radius for minimum filter operation (default: 4)")
272
  parser.add_argument("--blur-sigma", type=float, default=0.5,
273
  help="Gaussian blur sigma for boundary smoothing (default: 0.5)")
274
  parser.add_argument("--sample", type=str, default=None,
 
9
 
10
  The foreground correction process:
11
  1. Load alpha (from automatte folder)
12
+ 2. Apply thresholding (white = anything >230, black otherwise)
13
  3. Contract the alpha channel 1px (equivalent to Select > Modify > Contract in Photoshop)
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
 
27
  from pathlib import Path
28
  import argparse
29
 
30
+ def apply_threshold(alpha_channel, threshold=230):
 
31
  """Apply thresholding: white for values > threshold, black otherwise."""
32
  _, thresholded = cv2.threshold(alpha_channel, threshold, 255, cv2.THRESH_BINARY)
33
  return thresholded
 
141
 
142
 
143
  def process_foreground_correction(expanded_path, automatte_path, fgr_output_path, masked_output_path,
144
+ threshold=230, contract_pixels=1, minimum_radius=2, blur_sigma=0.5):
145
  """
146
  Process a single image pair for foreground correction.
147
 
 
193
  final_rgb = apply_boundary_blur(filtered_rgb, boundary_mask, blur_sigma=blur_sigma)
194
 
195
  # # DO NOT COMMIT: save alpha and exit
196
+ # cv2.imwrite("debug/debug_expanded.png", expanded_img)
197
+ # cv2.imwrite("debug/debug_automatte.png", automatte_img)
198
+ # cv2.imwrite("debug/debug_alpha.png", alpha)
199
+ # cv2.imwrite("debug/debug_thresholded_alpha.png", thresholded_alpha)
200
+ # cv2.imwrite("debug/debug_contracted_alpha.png", contracted_alpha)
201
+ # cv2.imwrite("debug/debug_selection_mask.png", selection_mask)
202
+ # cv2.imwrite("debug/debug_filtered_rgb.png", filtered_rgb)
203
+ # cv2.imwrite("debug/debug_final_rgb.png", final_rgb)
204
 
205
  # Apply the original contracted alpha (before inversion) to the final RGB image
206
  # Convert to RGBA for masked output
207
  masked_rgba = cv2.cvtColor(final_rgb, cv2.COLOR_BGR2BGRA)
208
+ masked_rgba[:, :, 3] = alpha # Use predicted alpha
209
 
210
  # Create output directories if they don't exist
211
  os.makedirs(os.path.dirname(fgr_output_path), exist_ok=True)
 
262
  def main():
263
  """Main function to process all image pairs."""
264
  parser = argparse.ArgumentParser(description="Apply foreground correction to image pairs")
265
+ parser.add_argument("--threshold", type=int, default=230,
266
+ help="Threshold value for alpha binarization (default: 230)")
267
  parser.add_argument("--contract-pixels", type=int, default=1,
268
  help="Number of pixels to contract alpha channel (default: 1)")
269
+ parser.add_argument("--minimum-radius", type=int, default=3,
270
+ help="Radius for minimum filter operation (default: 2)")
271
  parser.add_argument("--blur-sigma", type=float, default=0.5,
272
  help="Gaussian blur sigma for boundary smoothing (default: 0.5)")
273
  parser.add_argument("--sample", type=str, default=None,