File size: 19,057 Bytes
62cc23b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
#!/usr/bin/env python3
"""
CDL (Color Decision List) based edge smoothing for SegMatch
"""
import numpy as np
from typing import Tuple, Optional
from PIL import Image
import cv2
def calculate_cdl_params_face_only(source: np.ndarray, target: np.ndarray,
source_face_mask: np.ndarray, target_face_mask: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Calculate CDL parameters using only face pixels for focused accuracy.
Args:
source (np.ndarray): Source image as numpy array (0-1 range)
target (np.ndarray): Target image as numpy array (0-1 range)
source_face_mask (np.ndarray): Binary mask of face in source image
target_face_mask (np.ndarray): Binary mask of face in target image
Returns:
Tuple[np.ndarray, np.ndarray, np.ndarray]: (slope, offset, power)
"""
epsilon = 1e-6
# Extract face pixels only
source_face_pixels = source[source_face_mask > 0.5]
target_face_pixels = target[target_face_mask > 0.5]
# Ensure we have enough face pixels
if len(source_face_pixels) < 100 or len(target_face_pixels) < 100:
# Fallback to simple calculation if not enough face pixels
return calculate_cdl_params_simple(source, target)
slopes = []
offsets = []
powers = []
for channel in range(3):
src_channel = source_face_pixels[:, channel]
tgt_channel = target_face_pixels[:, channel]
# Use robust percentiles for face pixels
percentiles = [10, 25, 50, 75, 90]
src_percentiles = np.percentile(src_channel, percentiles)
tgt_percentiles = np.percentile(tgt_channel, percentiles)
# Calculate slope from face pixel range
src_range = src_percentiles[4] - src_percentiles[0] # 90th - 10th
tgt_range = tgt_percentiles[4] - tgt_percentiles[0]
slope = tgt_range / (src_range + epsilon)
# Calculate offset using face median
src_median = src_percentiles[2]
tgt_median = tgt_percentiles[2]
offset = tgt_median - (src_median * slope)
# Calculate gamma from face brightness relationship
src_mean = np.mean(src_channel)
tgt_mean = np.mean(tgt_channel)
if src_mean > epsilon:
power = np.log(tgt_mean + epsilon) / np.log(src_mean + epsilon)
power = np.clip(power, 0.3, 3.0)
else:
power = 1.0
slopes.append(slope)
offsets.append(offset)
powers.append(power)
return np.array(slopes), np.array(offsets), np.array(powers)
def calculate_cdl_params_simple(source: np.ndarray, target: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Simple CDL calculation as fallback method.
Args:
source (np.ndarray): Source image as numpy array (0-1 range)
target (np.ndarray): Target image as numpy array (0-1 range)
Returns:
Tuple[np.ndarray, np.ndarray, np.ndarray]: (slope, offset, power)
"""
epsilon = 1e-6
# Calculate mean and standard deviation for each RGB channel
source_mean = np.mean(source, axis=(0, 1))
source_std = np.std(source, axis=(0, 1))
target_mean = np.mean(target, axis=(0, 1))
target_std = np.std(target, axis=(0, 1))
# Calculate slope (gain)
slope = target_std / (source_std + epsilon)
# Calculate offset
offset = target_mean - (source_mean * slope)
# Set power to neutral
power = np.ones(3)
return slope, offset, power
def calculate_cdl_params_histogram(source: np.ndarray, target: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Calculate CDL parameters using histogram matching approach.
Args:
source (np.ndarray): Source image as numpy array (0-1 range)
target (np.ndarray): Target image as numpy array (0-1 range)
Returns:
Tuple[np.ndarray, np.ndarray, np.ndarray]: (slope, offset, power)
"""
epsilon = 1e-6
# Convert to 0-255 range for histogram calculation
source_255 = (source * 255).astype(np.uint8)
target_255 = (target * 255).astype(np.uint8)
slopes = []
offsets = []
powers = []
for channel in range(3):
# Calculate histograms
hist_source = cv2.calcHist([source_255], [channel], None, [256], [0, 256])
hist_target = cv2.calcHist([target_255], [channel], None, [256], [0, 256])
# Calculate cumulative distributions
cdf_source = np.cumsum(hist_source) / np.sum(hist_source)
cdf_target = np.cumsum(hist_target) / np.sum(hist_target)
# Find percentile mappings
p25_src = np.percentile(source[:, :, channel], 25)
p75_src = np.percentile(source[:, :, channel], 75)
p25_tgt = np.percentile(target[:, :, channel], 25)
p75_tgt = np.percentile(target[:, :, channel], 75)
# Calculate slope from percentile mapping
slope = (p75_tgt - p25_tgt) / (p75_src - p25_src + epsilon)
# Calculate offset
median_src = np.percentile(source[:, :, channel], 50)
median_tgt = np.percentile(target[:, :, channel], 50)
offset = median_tgt - (median_src * slope)
# Estimate power/gamma from the histogram shape
mean_src = np.mean(source[:, :, channel])
mean_tgt = np.mean(target[:, :, channel])
if mean_src > epsilon:
power = np.log(mean_tgt + epsilon) / np.log(mean_src + epsilon)
power = np.clip(power, 0.1, 10.0) # Reasonable gamma range
else:
power = 1.0
slopes.append(slope)
offsets.append(offset)
powers.append(power)
return np.array(slopes), np.array(offsets), np.array(powers)
def calculate_cdl_params_mask_aware(source: np.ndarray, target: np.ndarray,
changed_mask: Optional[np.ndarray] = None) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Calculate CDL parameters focusing only on changed regions.
Args:
source (np.ndarray): Source image as numpy array (0-1 range)
target (np.ndarray): Target image as numpy array (0-1 range)
changed_mask (np.ndarray, optional): Binary mask of changed regions
Returns:
Tuple[np.ndarray, np.ndarray, np.ndarray]: (slope, offset, power)
"""
if changed_mask is not None:
# Only use pixels where changes occurred
mask_bool = changed_mask > 0.5
if np.sum(mask_bool) > 100: # Ensure enough pixels
source_masked = source[mask_bool]
target_masked = target[mask_bool]
# Reshape back to have channel dimension
source_masked = source_masked.reshape(-1, 3)
target_masked = target_masked.reshape(-1, 3)
# Calculate statistics on masked regions
epsilon = 1e-6
source_mean = np.mean(source_masked, axis=0)
source_std = np.std(source_masked, axis=0)
target_mean = np.mean(target_masked, axis=0)
target_std = np.std(target_masked, axis=0)
slope = target_std / (source_std + epsilon)
offset = target_mean - (source_mean * slope)
power = np.ones(3)
return slope, offset, power
# Fallback to simple method if mask is not useful
return calculate_cdl_params_simple(source, target)
def calculate_cdl_params_lab(source: np.ndarray, target: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Calculate CDL parameters in LAB color space for better perceptual matching.
Args:
source (np.ndarray): Source image as numpy array (0-1 range)
target (np.ndarray): Target image as numpy array (0-1 range)
Returns:
Tuple[np.ndarray, np.ndarray, np.ndarray]: (slope, offset, power)
"""
# Convert to LAB color space
source_lab = cv2.cvtColor((source * 255).astype(np.uint8), cv2.COLOR_RGB2LAB).astype(np.float32)
target_lab = cv2.cvtColor((target * 255).astype(np.uint8), cv2.COLOR_RGB2LAB).astype(np.float32)
# Normalize LAB values
source_lab[:, :, 0] /= 100.0 # L: 0-100 -> 0-1
source_lab[:, :, 1] = (source_lab[:, :, 1] + 128) / 255.0 # A: -128-127 -> 0-1
source_lab[:, :, 2] = (source_lab[:, :, 2] + 128) / 255.0 # B: -128-127 -> 0-1
target_lab[:, :, 0] /= 100.0
target_lab[:, :, 1] = (target_lab[:, :, 1] + 128) / 255.0
target_lab[:, :, 2] = (target_lab[:, :, 2] + 128) / 255.0
# Calculate CDL in LAB space
epsilon = 1e-6
source_mean = np.mean(source_lab, axis=(0, 1))
source_std = np.std(source_lab, axis=(0, 1))
target_mean = np.mean(target_lab, axis=(0, 1))
target_std = np.std(target_lab, axis=(0, 1))
slope_lab = target_std / (source_std + epsilon)
offset_lab = target_mean - (source_mean * slope_lab)
# Convert back to RGB approximation
# This is a simplified conversion - for full accuracy we'd need to convert each pixel
slope = np.array([slope_lab[0], slope_lab[1], slope_lab[2]]) # Rough mapping
offset = np.array([offset_lab[0], offset_lab[1], offset_lab[2]])
power = np.ones(3)
return slope, offset, power
def calculate_cdl_params(source: np.ndarray, target: np.ndarray,
source_path: str = None, target_path: str = None) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Calculate CDL parameters using simple mean/std matching - the most basic approach.
Args:
source (np.ndarray): Source image as numpy array (0-1 range)
target (np.ndarray): Target image as numpy array (0-1 range)
source_path (str, optional): Ignored - kept for compatibility
target_path (str, optional): Ignored - kept for compatibility
Returns:
Tuple[np.ndarray, np.ndarray, np.ndarray]: (slope, offset, power)
"""
epsilon = 1e-6
# Calculate simple mean and standard deviation for each RGB channel
source_mean = np.mean(source, axis=(0, 1))
source_std = np.std(source, axis=(0, 1))
target_mean = np.mean(target, axis=(0, 1))
target_std = np.std(target, axis=(0, 1))
# Calculate slope (gain) from std ratio
slope = target_std / (source_std + epsilon)
# Calculate offset from mean difference
offset = target_mean - (source_mean * slope)
# Calculate simple gamma from brightness relationship
power = []
for channel in range(3):
if source_mean[channel] > epsilon:
gamma = np.log(target_mean[channel] + epsilon) / np.log(source_mean[channel] + epsilon)
gamma = np.clip(gamma, 0.1, 10.0) # Keep within reasonable bounds
else:
gamma = 1.0
power.append(gamma)
power = np.array(power)
return slope, offset, power
def calculate_change_mask(original: np.ndarray, composited: np.ndarray, threshold: float = 0.05) -> np.ndarray:
"""Calculate a mask of significantly changed regions between original and composited images.
Args:
original (np.ndarray): Original image (0-1 range)
composited (np.ndarray): Composited result (0-1 range)
threshold (float): Threshold for detecting significant changes
Returns:
np.ndarray: Binary mask of changed regions
"""
# Calculate per-pixel difference
diff = np.sqrt(np.sum((composited - original) ** 2, axis=2))
# Create binary mask where changes exceed threshold
change_mask = (diff > threshold).astype(np.float32)
# Apply morphological operations to clean up the mask
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
change_mask = cv2.morphologyEx(change_mask, cv2.MORPH_CLOSE, kernel)
return change_mask
def calculate_channel_stats(array: np.ndarray) -> dict:
"""Calculate per-channel statistics for an image array.
Args:
array: Image array of shape (H, W, 3)
Returns:
dict: Dictionary containing mean, std, min, max for each channel
"""
stats = {
'mean': np.mean(array, axis=(0, 1)),
'std': np.std(array, axis=(0, 1)),
'min': np.min(array, axis=(0, 1)),
'max': np.max(array, axis=(0, 1))
}
return stats
def apply_cdl_transform(image: np.ndarray, slope: np.ndarray, offset: np.ndarray, power: np.ndarray,
factor: float = 0.3) -> np.ndarray:
"""Apply CDL transformation to an image.
Args:
image (np.ndarray): Input image (0-1 range)
slope (np.ndarray): CDL slope parameters for each channel
offset (np.ndarray): CDL offset parameters for each channel
power (np.ndarray): CDL power parameters for each channel
factor (float): Blending factor (0.0 = no change, 1.0 = full transform)
Returns:
np.ndarray: Transformed image
"""
# Apply CDL transform: out = ((in * slope) + offset) ** power
transformed = np.power(np.maximum(image * slope + offset, 0), power)
# Clamp to valid range
transformed = np.clip(transformed, 0.0, 1.0)
# Blend with original based on factor
result = (1 - factor) * image + factor * transformed
return result
def cdl_edge_smoothing(composited_image_path: str, original_image_path: str, factor: float = 0.3) -> Image.Image:
"""Apply CDL-based edge smoothing between composited result and original image.
Args:
composited_image_path (str): Path to the composited result image
original_image_path (str): Path to the original target image
factor (float): Smoothing strength (0.0 = no smoothing, 1.0 = full smoothing)
Returns:
Image.Image: Smoothed result image
"""
# Load images
composited_img = Image.open(composited_image_path).convert("RGB")
original_img = Image.open(original_image_path).convert("RGB")
# Ensure same dimensions
if composited_img.size != original_img.size:
composited_img = composited_img.resize(original_img.size, Image.LANCZOS)
# Convert to numpy arrays (0-1 range)
composited_np = np.array(composited_img).astype(np.float32) / 255.0
original_np = np.array(original_img).astype(np.float32) / 255.0
# Calculate CDL parameters to transform composited to match original
slope, offset, power = calculate_cdl_params(composited_np, original_np)
# Apply CDL transformation with blending
smoothed_np = apply_cdl_transform(composited_np, slope, offset, power, factor)
# Convert back to PIL Image
smoothed_img = Image.fromarray((smoothed_np * 255).astype(np.uint8))
return smoothed_img
def get_smoothing_stats(original_image_path: str, composited_image_path: str) -> dict:
"""Get statistics about the CDL transformation for debugging.
Args:
original_image_path (str): Path to the original target image
composited_image_path (str): Path to the composited result image
Returns:
dict: Statistics about the transformation
"""
# Load images
composited_img = Image.open(composited_image_path).convert("RGB")
original_img = Image.open(original_image_path).convert("RGB")
# Ensure same dimensions
if composited_img.size != original_img.size:
composited_img = composited_img.resize(original_img.size, Image.LANCZOS)
# Convert to numpy arrays (0-1 range)
composited_np = np.array(composited_img).astype(np.float32) / 255.0
original_np = np.array(original_img).astype(np.float32) / 255.0
# Calculate statistics
composited_stats = calculate_channel_stats(composited_np)
original_stats = calculate_channel_stats(original_np)
# Calculate CDL parameters using face-based method when possible
slope, offset, power = calculate_cdl_params(original_np, composited_np,
original_image_path, composited_image_path)
return {
'composited_stats': composited_stats,
'original_stats': original_stats,
'cdl_slope': slope,
'cdl_offset': offset,
'cdl_power': power
}
def cdl_edge_smoothing_apply_to_source(source_image_path: str, target_image_path: str, factor: float = 1.0) -> Image.Image:
"""Apply CDL transformation to source image using face-based parameters when possible.
This function:
1. Calculates CDL parameters to transform source to match target (using face pixels when available)
2. Applies those CDL parameters to the entire source image
3. Returns the transformed source image
Args:
source_image_path (str): Path to the source image (to be transformed)
target_image_path (str): Path to the target image (reference for CDL calculation)
factor (float): Transform strength (0.0 = no change, 1.0 = full transform)
Returns:
Image.Image: Source image with CDL transformation applied
"""
# Load images
source_img = Image.open(source_image_path).convert("RGB")
target_img = Image.open(target_image_path).convert("RGB")
# Ensure same dimensions
if source_img.size != target_img.size:
target_img = target_img.resize(source_img.size, Image.LANCZOS)
# Convert to numpy arrays (0-1 range)
source_np = np.array(source_img).astype(np.float32) / 255.0
target_np = np.array(target_img).astype(np.float32) / 255.0
# Calculate CDL parameters using face-based method when possible
slope, offset, power = calculate_cdl_params(source_np, target_np,
source_image_path, target_image_path)
# Apply CDL transformation to the entire source image
transformed_np = apply_cdl_transform(source_np, slope, offset, power, factor)
# Convert back to PIL Image
transformed_img = Image.fromarray((transformed_np * 255).astype(np.uint8))
return transformed_img
def extract_face_mask(image_path: str) -> Optional[np.ndarray]:
"""Extract face mask from an image using human parts segmentation.
Args:
image_path (str): Path to the image
Returns:
np.ndarray or None: Binary face mask, or None if no face found
"""
try:
from human_parts_segmentation import HumanPartsSegmentation
segmenter = HumanPartsSegmentation()
masks_dict = segmenter.segment_parts(image_path, ['face'])
if 'face' in masks_dict and masks_dict['face'] is not None:
face_mask = masks_dict['face']
# Ensure it's a proper binary mask
if np.sum(face_mask > 0.5) > 100: # At least 100 face pixels
return face_mask
return None
except Exception as e:
print(f"Face extraction failed: {e}")
return None |