Candle
commited on
Commit
·
79edece
1
Parent(s):
cfa713b
erosion max
Browse files- correct_fgr.py +73 -18
correct_fgr.py
CHANGED
|
@@ -52,28 +52,78 @@ def apply_minimum_filter(alpha_channel, radius=4):
|
|
| 52 |
return filtered
|
| 53 |
|
| 54 |
|
| 55 |
-
def apply_minimum_filter_to_rgb(rgb_image, mask, radius=4):
|
| 56 |
"""
|
| 57 |
Apply minimum filter to RGB image using a mask selection.
|
| 58 |
Only pixels where mask is white (255) will be affected.
|
| 59 |
-
"""
|
| 60 |
-
# Create kernel for minimum filter
|
| 61 |
-
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2*radius+1, 2*radius+1))
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
filtered_rgb = rgb_image.copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
mask_binary = (mask == 255).astype(np.uint8)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
filtered_rgb[:, :, channel] = np.where(mask_binary,
|
| 75 |
-
eroded_channel,
|
| 76 |
-
rgb_image[:, :, channel])
|
| 77 |
|
| 78 |
return filtered_rgb
|
| 79 |
|
|
@@ -141,7 +191,7 @@ def apply_boundary_blur(rgb_image, boundary_mask, blur_sigma=0.5):
|
|
| 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 |
|
|
@@ -154,6 +204,7 @@ def process_foreground_correction(expanded_path, automatte_path, fgr_output_path
|
|
| 154 |
contract_pixels: Number of pixels to contract alpha channel
|
| 155 |
minimum_radius: Radius for minimum filter operation
|
| 156 |
blur_sigma: Gaussian blur sigma for boundary smoothing
|
|
|
|
| 157 |
"""
|
| 158 |
# Load the expanded image (RGB)
|
| 159 |
expanded_img = cv2.imread(expanded_path, cv2.IMREAD_COLOR)
|
|
@@ -186,7 +237,7 @@ def process_foreground_correction(expanded_path, automatte_path, fgr_output_path
|
|
| 186 |
selection_mask = invert_selection(contracted_alpha)
|
| 187 |
|
| 188 |
# Step 5: Apply minimum filter to RGB image using the selection mask
|
| 189 |
-
filtered_rgb = apply_minimum_filter_to_rgb(expanded_img, selection_mask, radius=minimum_radius)
|
| 190 |
|
| 191 |
# Step 6: Apply Gaussian blur on ±0.5 region of selection mask boundaries
|
| 192 |
boundary_mask = create_boundary_mask(selection_mask, boundary_width=1)
|
|
@@ -266,10 +317,13 @@ def main():
|
|
| 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=
|
| 270 |
-
help="Radius for minimum filter operation (default:
|
| 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,
|
| 274 |
help="Process only specific sample (e.g., 'sample-000')")
|
| 275 |
|
|
@@ -304,7 +358,8 @@ def main():
|
|
| 304 |
threshold=args.threshold,
|
| 305 |
contract_pixels=args.contract_pixels,
|
| 306 |
minimum_radius=args.minimum_radius,
|
| 307 |
-
blur_sigma=args.blur_sigma
|
|
|
|
| 308 |
successful += 1
|
| 309 |
else:
|
| 310 |
failed += 1
|
|
|
|
| 52 |
return filtered
|
| 53 |
|
| 54 |
|
| 55 |
+
def apply_minimum_filter_to_rgb(rgb_image, mask, radius=4, method='erosion'):
|
| 56 |
"""
|
| 57 |
Apply minimum filter to RGB image using a mask selection.
|
| 58 |
Only pixels where mask is white (255) will be affected.
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
Args:
|
| 61 |
+
rgb_image: Input RGB image
|
| 62 |
+
mask: Binary mask (white = process, black = leave unchanged)
|
| 63 |
+
radius: Filter radius in pixels
|
| 64 |
+
method: 'erosion' (standard), 'radial' (distance-based), 'opening', 'closing'
|
| 65 |
+
"""
|
| 66 |
filtered_rgb = rgb_image.copy()
|
| 67 |
+
mask_binary = (mask == 255).astype(np.uint8)
|
| 68 |
+
|
| 69 |
+
if method == 'radial':
|
| 70 |
+
# Distance transform based radial approach
|
| 71 |
+
return apply_radial_filter_to_rgb(rgb_image, mask, radius)
|
| 72 |
+
elif method == 'opening':
|
| 73 |
+
# Morphological opening (erosion + dilation)
|
| 74 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2*radius+1, 2*radius+1))
|
| 75 |
+
for channel in range(3):
|
| 76 |
+
opened_channel = cv2.morphologyEx(rgb_image[:, :, channel], cv2.MORPH_OPEN, kernel)
|
| 77 |
+
filtered_rgb[:, :, channel] = np.where(mask_binary, opened_channel, rgb_image[:, :, channel])
|
| 78 |
+
elif method == 'closing':
|
| 79 |
+
# Morphological closing (dilation + erosion)
|
| 80 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2*radius+1, 2*radius+1))
|
| 81 |
+
for channel in range(3):
|
| 82 |
+
closed_channel = cv2.morphologyEx(rgb_image[:, :, channel], cv2.MORPH_CLOSE, kernel)
|
| 83 |
+
filtered_rgb[:, :, channel] = np.where(mask_binary, closed_channel, rgb_image[:, :, channel])
|
| 84 |
+
else: # 'erosion' (default)
|
| 85 |
+
# Standard erosion (minimum filter)
|
| 86 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2*radius+1, 2*radius+1))
|
| 87 |
+
for channel in range(3):
|
| 88 |
+
eroded_channel = cv2.erode(rgb_image[:, :, channel], kernel, iterations=1)
|
| 89 |
+
filtered_rgb[:, :, channel] = np.where(mask_binary, eroded_channel, rgb_image[:, :, channel])
|
| 90 |
|
| 91 |
+
return filtered_rgb
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def apply_radial_filter_to_rgb(rgb_image, mask, radius=4):
|
| 95 |
+
"""
|
| 96 |
+
Apply radial minimum filter using distance transform.
|
| 97 |
+
This method works more radially than standard morphological erosion.
|
| 98 |
+
"""
|
| 99 |
+
filtered_rgb = rgb_image.copy()
|
| 100 |
mask_binary = (mask == 255).astype(np.uint8)
|
| 101 |
|
| 102 |
+
# Create distance transform of the mask
|
| 103 |
+
dist_transform = cv2.distanceTransform(mask_binary, cv2.DIST_L2, 5)
|
| 104 |
+
|
| 105 |
+
# Create a radial kernel based on distance
|
| 106 |
+
height, width = rgb_image.shape[:2]
|
| 107 |
+
y, x = np.ogrid[:height, :width]
|
| 108 |
+
|
| 109 |
+
for channel in range(3):
|
| 110 |
+
channel_data = rgb_image[:, :, channel].astype(np.float32)
|
| 111 |
+
filtered_channel = channel_data.copy()
|
| 112 |
+
|
| 113 |
+
# For each pixel in the mask, find minimum in radial neighborhood
|
| 114 |
+
mask_coords = np.where(mask_binary > 0)
|
| 115 |
+
|
| 116 |
+
for my, mx in zip(mask_coords[0], mask_coords[1]):
|
| 117 |
+
# Create circular mask around current pixel
|
| 118 |
+
distances = np.sqrt((y - my)**2 + (x - mx)**2)
|
| 119 |
+
circle_mask = distances <= radius
|
| 120 |
+
|
| 121 |
+
# Find minimum value in the circular neighborhood
|
| 122 |
+
if np.any(circle_mask):
|
| 123 |
+
min_val = np.min(channel_data[circle_mask])
|
| 124 |
+
filtered_channel[my, mx] = min_val
|
| 125 |
|
| 126 |
+
filtered_rgb[:, :, channel] = filtered_channel.astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
return filtered_rgb
|
| 129 |
|
|
|
|
| 191 |
|
| 192 |
|
| 193 |
def process_foreground_correction(expanded_path, automatte_path, fgr_output_path, masked_output_path,
|
| 194 |
+
threshold=230, contract_pixels=1, minimum_radius=2, blur_sigma=0.5, filter_method='erosion'):
|
| 195 |
"""
|
| 196 |
Process a single image pair for foreground correction.
|
| 197 |
|
|
|
|
| 204 |
contract_pixels: Number of pixels to contract alpha channel
|
| 205 |
minimum_radius: Radius for minimum filter operation
|
| 206 |
blur_sigma: Gaussian blur sigma for boundary smoothing
|
| 207 |
+
filter_method: Method for minimum filter ('erosion', 'radial', 'opening', 'closing')
|
| 208 |
"""
|
| 209 |
# Load the expanded image (RGB)
|
| 210 |
expanded_img = cv2.imread(expanded_path, cv2.IMREAD_COLOR)
|
|
|
|
| 237 |
selection_mask = invert_selection(contracted_alpha)
|
| 238 |
|
| 239 |
# Step 5: Apply minimum filter to RGB image using the selection mask
|
| 240 |
+
filtered_rgb = apply_minimum_filter_to_rgb(expanded_img, selection_mask, radius=minimum_radius, method=filter_method)
|
| 241 |
|
| 242 |
# Step 6: Apply Gaussian blur on ±0.5 region of selection mask boundaries
|
| 243 |
boundary_mask = create_boundary_mask(selection_mask, boundary_width=1)
|
|
|
|
| 317 |
help="Threshold value for alpha binarization (default: 230)")
|
| 318 |
parser.add_argument("--contract-pixels", type=int, default=1,
|
| 319 |
help="Number of pixels to contract alpha channel (default: 1)")
|
| 320 |
+
parser.add_argument("--minimum-radius", type=int, default=50,
|
| 321 |
+
help="Radius for minimum filter operation (default: 3)")
|
| 322 |
parser.add_argument("--blur-sigma", type=float, default=0.5,
|
| 323 |
help="Gaussian blur sigma for boundary smoothing (default: 0.5)")
|
| 324 |
+
parser.add_argument("--filter-method", type=str, default="erosion",
|
| 325 |
+
choices=["erosion", "radial", "opening", "closing"],
|
| 326 |
+
help="Method for minimum filter (default: erosion). 'radial' works more radially.")
|
| 327 |
parser.add_argument("--sample", type=str, default=None,
|
| 328 |
help="Process only specific sample (e.g., 'sample-000')")
|
| 329 |
|
|
|
|
| 358 |
threshold=args.threshold,
|
| 359 |
contract_pixels=args.contract_pixels,
|
| 360 |
minimum_radius=args.minimum_radius,
|
| 361 |
+
blur_sigma=args.blur_sigma,
|
| 362 |
+
filter_method=args.filter_method):
|
| 363 |
successful += 1
|
| 364 |
else:
|
| 365 |
failed += 1
|