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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Salia Auto Align + Seam Blend GPT_1 -- ONE SELF-CONTAINED COMFYUI NODE
|
| 3 |
+
=================================================================
|
| 4 |
+
Install this ONE file in ComfyUI/custom_nodes/ and restart ComfyUI.
|
| 5 |
+
Add Salia/Image -> Salia Auto Align + Seam Blend GPT_1.
|
| 6 |
+
Requires NumPy, SciPy and PyTorch, the same dependencies as the working aligner.
|
| 7 |
+
No Salia_Auto_Align_Overlay.py, other custom node, OpenCV, model or network
|
| 8 |
+
access is needed. The working registration/EWA code is INCLUDED in this file.
|
| 9 |
+
|
| 10 |
+
SOURCE IMAGE INPUTS (same as the original working node)
|
| 11 |
+
background : original full image, RGB or straight RGBA.
|
| 12 |
+
overlay : ORIGINAL edited crop/image to align and blend.
|
| 13 |
+
Unedited_Rect : original crop BEFORE editing, used to locate its source area.
|
| 14 |
+
One image per input; batches must be split upstream.
|
| 15 |
+
|
| 16 |
+
The original alignment controls retain their names/defaults/behavior:
|
| 17 |
+
target_width=0, target_height=0, max_evaluations=100, pyramid_levels=3,
|
| 18 |
+
robust_threshold=0.05, scale_limit=2.0, min_tolerance=0.9.
|
| 19 |
+
Width/height 0 means use the unedited crop's corresponding initial dimension.
|
| 20 |
+
min_tolerance accepts the best original-crop match AFTER searching; 0.9=180/200.
|
| 21 |
+
|
| 22 |
+
OPTIONAL ORIGINAL INPUT MASKS
|
| 23 |
+
background_mask, overlay_mask, Unedited_Rect_mask accept matching standard
|
| 24 |
+
Load Image MASKs, unchanged (MASK=1-alpha). Conversion happens internally.
|
| 25 |
+
Native RGBA ALWAYS uses embedded alpha and ignores the optional matching MASK.
|
| 26 |
+
Do not invert masks for this node. RGB with no mask is opaque, exactly like
|
| 27 |
+
the original working node. Standard no-alpha 64x64 zero MASKs are supported.
|
| 28 |
+
|
| 29 |
+
INTERNAL PIPELINE -- no external aligned images or coordinates are inputs
|
| 30 |
+
1. Locate Unedited_Rect and jointly fit continuous X/Y/width/height.
|
| 31 |
+
2. Render the original overlay once with premultiplied linear-light EWA.
|
| 32 |
+
3. Produce the same sRGB source-over composite as the working aligner.
|
| 33 |
+
4. Choose a low-disagreement seam in a border band with compiled SciPy
|
| 34 |
+
max-flow, then feather border colors toward the original background.
|
| 35 |
+
The fitting coordinates, both aligned images, all alpha/mask preparation,
|
| 36 |
+
and their connections are handled inside this one node.
|
| 37 |
+
|
| 38 |
+
The final alpha is EXACTLY the internally aligned composite's alpha. Transparent
|
| 39 |
+
overlay pixels do not erase background. The protected center and pixels outside
|
| 40 |
+
the rectangle stay exact. No second overlay paste or alpha accumulation occurs.
|
| 41 |
+
|
| 42 |
+
ADDITIONAL USER CONTROLS
|
| 43 |
+
seam_band_px=24: adjustable border width; limited for small rectangles.
|
| 44 |
+
feather_px=3: softness of the selected seam, limited for small bands.
|
| 45 |
+
blend_strength=1: full border blend; 0 returns the unblended aligned result.
|
| 46 |
+
blend_method: Graph cut + feather / Border feather (diagnostic) /
|
| 47 |
+
Bypass (alignment result).
|
| 48 |
+
graph_max_side=512: graph resolution cap; final images remain full resolution.
|
| 49 |
+
No outputs from another node are required as inputs.
|
| 50 |
+
|
| 51 |
+
OUTPUTS
|
| 52 |
+
result_rgba: final full-size straight RGBA image.
|
| 53 |
+
status: crop match, fractional coordinates, optimizer trace, alpha diagnostics,
|
| 54 |
+
seam details, fallback/error messages and exact-preservation checks.
|
| 55 |
+
aligned_rgba: internal alignment composite BEFORE seam blending.
|
| 56 |
+
overlay_canvas_rgba: internally fitted overlay on a full-size transparent canvas.
|
| 57 |
+
debug_result_rgb, debug_alignment_rgb, debug_overlay_rgb: checkerboard previews.
|
| 58 |
+
debug_overlay_alpha_rgb: WHITE=opaque overlay, BLACK=transparent overlay.
|
| 59 |
+
debug_blend_weight_rgb: WHITE=aligned result, BLACK=original background color.
|
| 60 |
+
debug_seam_rgb: yellow=rectangle, cyan=protected core, green=chosen seam.
|
| 61 |
+
debug_difference_x8_rgb: visible final-minus-aligned difference amplified x8.
|
| 62 |
+
output_mask: standard Comfy MASK = 1-final alpha; not a seam mask.
|
| 63 |
+
All debug_* outputs are RGB IMAGEs; connect them directly to Preview Image.
|
| 64 |
+
|
| 65 |
+
If the original crop does not pass min_tolerance, no overlay is pasted; status
|
| 66 |
+
explains the failure and both alignment/final results retain the background.
|
| 67 |
+
If graph cut fails, a clearly reported border-feather fallback is used.
|
| 68 |
+
Invalid source tensors/masks raise the same clear input errors as the aligner.
|
| 69 |
+
"""
|
| 70 |
+
from __future__ import annotations
|
| 71 |
+
import math
|
| 72 |
+
from functools import lru_cache
|
| 73 |
+
from typing import Callable, Tuple
|
| 74 |
+
import numpy as np
|
| 75 |
+
from scipy import sparse
|
| 76 |
+
from scipy.special import j1, jn_zeros
|
| 77 |
+
_MODES = ("EWA LanczosSharp", "EWA LanczosRadius / Jinc-Jinc")
|
| 78 |
+
_PAIR_BUDGET = 262144
|
| 79 |
+
_LUT_INTERVALS = 65536
|
| 80 |
+
_ALPHA_EPSILON = 1.0e-8
|
| 81 |
+
|
| 82 |
+
def _to_linear(rgb: np.ndarray) -> np.ndarray:
|
| 83 |
+
return np.where(
|
| 84 |
+
rgb <= 0.04045,
|
| 85 |
+
rgb / 12.92,
|
| 86 |
+
np.power((rgb + 0.055) / 1.055, 2.4),
|
| 87 |
+
).astype(np.float32, copy=False)
|
| 88 |
+
|
| 89 |
+
def _to_srgb(rgb: np.ndarray) -> np.ndarray:
|
| 90 |
+
rgb = np.clip(rgb, 0.0, 1.0)
|
| 91 |
+
return np.where(
|
| 92 |
+
rgb <= 0.0031308,
|
| 93 |
+
rgb * 12.92,
|
| 94 |
+
1.055 * np.power(rgb, 1.0 / 2.4) - 0.055,
|
| 95 |
+
).astype(np.float32, copy=False)
|
| 96 |
+
|
| 97 |
+
def _prepare_image(source: np.ndarray) -> np.ndarray:
|
| 98 |
+
if not np.isfinite(source).all():
|
| 99 |
+
raise ValueError("Salia_Rescale_Image: image contains NaN or infinity.")
|
| 100 |
+
image = np.clip(source, 0.0, 1.0).astype(np.float32, copy=True)
|
| 101 |
+
image[..., :3] = _to_linear(image[..., :3])
|
| 102 |
+
if image.shape[-1] == 4:
|
| 103 |
+
image[..., :3] *= image[..., 3:4]
|
| 104 |
+
return image
|
| 105 |
+
|
| 106 |
+
def _finish_image(filtered: np.ndarray, channels: int) -> np.ndarray:
|
| 107 |
+
filtered = np.asarray(filtered, dtype=np.float32)
|
| 108 |
+
rgb = filtered[..., :3]
|
| 109 |
+
if channels == 4:
|
| 110 |
+
raw_alpha = filtered[..., 3:4]
|
| 111 |
+
visible = raw_alpha > _ALPHA_EPSILON
|
| 112 |
+
# Divide by the ORIGINAL filtered alpha, before clipping alpha.
|
| 113 |
+
# Negative-lobe filters can overshoot 1; clipping before division
|
| 114 |
+
# would alter the color of those pixels.
|
| 115 |
+
straight_rgb = np.zeros_like(rgb)
|
| 116 |
+
np.divide(rgb, raw_alpha, out=straight_rgb, where=visible)
|
| 117 |
+
rgb = _to_srgb(straight_rgb)
|
| 118 |
+
alpha = np.where(visible, np.clip(raw_alpha, 0.0, 1.0), 0.0)
|
| 119 |
+
result = np.concatenate((rgb, alpha), axis=-1)
|
| 120 |
+
else:
|
| 121 |
+
result = _to_srgb(rgb)
|
| 122 |
+
return np.ascontiguousarray(np.clip(result, 0.0, 1.0), dtype=np.float32)
|
| 123 |
+
|
| 124 |
+
def _jinc(x: np.ndarray) -> np.ndarray:
|
| 125 |
+
# Normalized Jinc = 2 * J1(pi*x) / (pi*x), with value 1 at zero.
|
| 126 |
+
z = math.pi * np.asarray(x, dtype=np.float64)
|
| 127 |
+
result = np.ones_like(z)
|
| 128 |
+
np.divide(2.0 * j1(z), z, out=result, where=np.abs(z) > 1.0e-12)
|
| 129 |
+
return result
|
| 130 |
+
|
| 131 |
+
@lru_cache(maxsize=2)
|
| 132 |
+
def _ewa_definition(mode: str) -> Tuple[float, np.ndarray, float]:
|
| 133 |
+
first_zero, _, third_zero = jn_zeros(1, 3) / math.pi
|
| 134 |
+
if mode == _MODES[0]:
|
| 135 |
+
blur = 0.9812505644269356
|
| 136 |
+
elif mode == _MODES[1]:
|
| 137 |
+
blur = 3.0 / third_zero
|
| 138 |
+
else:
|
| 139 |
+
raise ValueError("Unknown EWA mode: " + str(mode))
|
| 140 |
+
radius = float(third_zero * blur)
|
| 141 |
+
squared_radius = radius * radius
|
| 142 |
+
# A uniform table in squared distance avoids per-weight square roots.
|
| 143 |
+
squared_distances = np.linspace(0.0, squared_radius, _LUT_INTERVALS + 1)
|
| 144 |
+
r = np.sqrt(squared_distances) / blur
|
| 145 |
+
table = _jinc(r) * _jinc(r * first_zero / third_zero)
|
| 146 |
+
table[-1] = 0.0
|
| 147 |
+
# Duplicate endpoint makes interpolation at/outside support safe.
|
| 148 |
+
table = np.concatenate((table, np.zeros(1, dtype=np.float64)))
|
| 149 |
+
table.setflags(write=False)
|
| 150 |
+
return radius, table, _LUT_INTERVALS / squared_radius
|
| 151 |
+
|
| 152 |
+
def _lookup_ewa(squared_distance: np.ndarray, table: np.ndarray,
|
| 153 |
+
table_scale: float) -> np.ndarray:
|
| 154 |
+
position = np.minimum(squared_distance * table_scale, float(_LUT_INTERVALS))
|
| 155 |
+
index = position.astype(np.intp)
|
| 156 |
+
fraction = position - index
|
| 157 |
+
return table[index] + fraction * (table[index + 1] - table[index])
|
| 158 |
+
|
| 159 |
+
def _ewa_axis(source_size: int, target_size: int,
|
| 160 |
+
radius: float) -> Tuple[np.ndarray, np.ndarray]:
|
| 161 |
+
ratio = source_size / target_size
|
| 162 |
+
footprint_scale = max(1.0, ratio)
|
| 163 |
+
support = radius * footprint_scale
|
| 164 |
+
centers = (np.arange(target_size, dtype=np.float64) + 0.5) * ratio - 0.5
|
| 165 |
+
# All source indices with nonzero support fit in this fixed-width table.
|
| 166 |
+
count = min(source_size, int(math.ceil(2.0 * support)) + 1)
|
| 167 |
+
starts = np.maximum(0, np.ceil(centers - support).astype(np.int64))
|
| 168 |
+
raw_indices = starts[:, None] + np.arange(count, dtype=np.int64)[None, :]
|
| 169 |
+
squared_distance = ((raw_indices - centers[:, None]) / footprint_scale) ** 2
|
| 170 |
+
valid = (raw_indices < source_size) & (squared_distance < radius * radius)
|
| 171 |
+
# Infinity is deliberately mapped to the zero endpoint of the lookup table.
|
| 172 |
+
squared_distance[~valid] = np.inf
|
| 173 |
+
indices = np.minimum(raw_indices, source_size - 1).astype(np.int32)
|
| 174 |
+
return indices, squared_distance
|
| 175 |
+
|
| 176 |
+
def _resize_ewa(image: np.ndarray, out_width: int, out_height: int,
|
| 177 |
+
definition: Tuple[float, np.ndarray, float],
|
| 178 |
+
x_axis: Tuple[np.ndarray, np.ndarray],
|
| 179 |
+
y_axis: Tuple[np.ndarray, np.ndarray],
|
| 180 |
+
interrupt: Callable[[], None],
|
| 181 |
+
progress: Callable[[float], None]) -> np.ndarray:
|
| 182 |
+
_, table, table_scale = definition
|
| 183 |
+
in_height, in_width, channels = image.shape
|
| 184 |
+
x_indices, x_squared = x_axis
|
| 185 |
+
y_indices, y_squared = y_axis
|
| 186 |
+
nx, ny = x_indices.shape[1], y_indices.shape[1]
|
| 187 |
+
total_pixels = out_width * out_height
|
| 188 |
+
output = np.empty((total_pixels, channels), dtype=np.float32)
|
| 189 |
+
# Match the float64 weights once; otherwise SciPy may repeatedly upcast
|
| 190 |
+
# the complete source image for every output tile.
|
| 191 |
+
source_flat = np.ascontiguousarray(image, dtype=np.float64).reshape(-1, channels)
|
| 192 |
+
pixel_block = max(1, min(2048, _PAIR_BUDGET // max(1, nx * ny)))
|
| 193 |
+
index_dtype = np.int32 if in_width * in_height < 2**31 else np.int64
|
| 194 |
+
|
| 195 |
+
for begin in range(0, total_pixels, pixel_block):
|
| 196 |
+
interrupt()
|
| 197 |
+
end = min(begin + pixel_block, total_pixels)
|
| 198 |
+
output_pixels = np.arange(begin, end, dtype=np.int64)
|
| 199 |
+
oy, ox = np.divmod(output_pixels, out_width)
|
| 200 |
+
count = end - begin
|
| 201 |
+
weighted_sum = np.zeros((count, channels), dtype=np.float64)
|
| 202 |
+
weight_sum = np.zeros(count, dtype=np.float64)
|
| 203 |
+
# Also tile source taps, so even a huge reduction to 1x1 doesn't
|
| 204 |
+
# allocate one enormous output_pixels x source_pixels weight array.
|
| 205 |
+
x_chunk = min(nx, max(1, _PAIR_BUDGET // count))
|
| 206 |
+
y_chunk = min(ny, max(1, _PAIR_BUDGET // (count * x_chunk)))
|
| 207 |
+
|
| 208 |
+
for ys in range(0, ny, y_chunk):
|
| 209 |
+
ye = min(ys + y_chunk, ny)
|
| 210 |
+
yi = y_indices[oy, ys:ye]
|
| 211 |
+
dy = y_squared[oy, ys:ye]
|
| 212 |
+
for xs in range(0, nx, x_chunk):
|
| 213 |
+
interrupt()
|
| 214 |
+
xe = min(xs + x_chunk, nx)
|
| 215 |
+
xi = x_indices[ox, xs:xe]
|
| 216 |
+
dx = x_squared[ox, xs:xe]
|
| 217 |
+
distance = dy[:, :, None] + dx[:, None, :]
|
| 218 |
+
weights = _lookup_ewa(distance, table, table_scale)
|
| 219 |
+
taps_per_pixel = (ye - ys) * (xe - xs)
|
| 220 |
+
flat_weights = weights.reshape(count, taps_per_pixel)
|
| 221 |
+
weight_sum += flat_weights.sum(axis=1)
|
| 222 |
+
source_indices = (
|
| 223 |
+
yi.astype(np.int64)[:, :, None] * in_width
|
| 224 |
+
+ xi[:, None, :]
|
| 225 |
+
).astype(index_dtype, copy=False)
|
| 226 |
+
row_pointers = np.arange(count + 1, dtype=np.int64) * taps_per_pixel
|
| 227 |
+
# Sparse multiply avoids materializing a large [pixel,tap,RGBA]
|
| 228 |
+
# gather. Each row is one target pixel's full 2-D footprint.
|
| 229 |
+
operator = sparse.csr_matrix(
|
| 230 |
+
(weights.ravel(), source_indices.ravel(), row_pointers),
|
| 231 |
+
shape=(count, in_width * in_height),
|
| 232 |
+
)
|
| 233 |
+
weighted_sum += operator @ source_flat
|
| 234 |
+
if not np.isfinite(weight_sum).all() or np.any(np.abs(weight_sum) < 1.0e-12):
|
| 235 |
+
raise RuntimeError("Salia_Rescale_Image: degenerate EWA normalization.")
|
| 236 |
+
output[begin:end] = weighted_sum / weight_sum[:, None]
|
| 237 |
+
progress(end / total_pixels)
|
| 238 |
+
return output.reshape(out_height, out_width, channels)
|
| 239 |
+
|
| 240 |
+
class EwaLanczosSharpResizer:
|
| 241 |
+
"""Resample each requested size from the original prepared source.
|
| 242 |
+
|
| 243 |
+
Color preparation happens once. No candidate is resized from a previous
|
| 244 |
+
candidate. Axis tables are cheap and computed for each requested size.
|
| 245 |
+
The caller can cache finished candidate images when useful.
|
| 246 |
+
"""
|
| 247 |
+
def __init__(self, source: np.ndarray, interrupt: Callable[[], None] = lambda: None):
|
| 248 |
+
source = np.asarray(source)
|
| 249 |
+
if source.ndim != 3 or source.shape[2] not in (3, 4) or min(source.shape[:2]) < 1:
|
| 250 |
+
raise ValueError("EWA source must be a nonempty HWC RGB/RGBA array.")
|
| 251 |
+
if not np.isfinite(source).all():
|
| 252 |
+
raise ValueError("EWA source contains NaN or infinity.")
|
| 253 |
+
self.source = np.ascontiguousarray(source, dtype=np.float32)
|
| 254 |
+
self.prepared = _prepare_image(self.source)
|
| 255 |
+
self.definition = _ewa_definition(_MODES[0])
|
| 256 |
+
self.interrupt = interrupt
|
| 257 |
+
|
| 258 |
+
def resize(self, width: int, height: int) -> np.ndarray:
|
| 259 |
+
if width < 1 or height < 1:
|
| 260 |
+
raise ValueError("EWA dimensions must be positive.")
|
| 261 |
+
self.interrupt()
|
| 262 |
+
if (int(width), int(height)) == (self.source.shape[1], self.source.shape[0]):
|
| 263 |
+
return self.source.copy()
|
| 264 |
+
radius = self.definition[0]
|
| 265 |
+
x_axis = _ewa_axis(self.source.shape[1], int(width), radius)
|
| 266 |
+
y_axis = _ewa_axis(self.source.shape[0], int(height), radius)
|
| 267 |
+
filtered = _resize_ewa(
|
| 268 |
+
self.prepared, int(width), int(height), self.definition,
|
| 269 |
+
x_axis, y_axis, self.interrupt, lambda value: None,
|
| 270 |
+
)
|
| 271 |
+
self.interrupt()
|
| 272 |
+
return _finish_image(filtered, self.source.shape[2])
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
def resize_rgba_ewa(source: np.ndarray, width: int, height: int,
|
| 276 |
+
interrupt: Callable[[], None] = lambda: None) -> np.ndarray:
|
| 277 |
+
"""Standalone original-source EWA LanczosSharp resize (RGB or RGBA)."""
|
| 278 |
+
return EwaLanczosSharpResizer(source, interrupt).resize(width, height)
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
import logging
|
| 283 |
+
import time
|
| 284 |
+
from scipy.signal import correlate
|
| 285 |
+
__version__ = "2.2.0"
|
| 286 |
+
_LOG = logging.getLogger(__name__)
|
| 287 |
+
_MAX_DIMENSION = 32768
|
| 288 |
+
_RGB_COPY_ERROR_TOLERANCE = 1.0e-6
|
| 289 |
+
|
| 290 |
+
def _rgba_array(image, name="image"):
|
| 291 |
+
"""Private normalized float32 copy; hidden RGB is retained but never scored."""
|
| 292 |
+
result = np.asarray(image)
|
| 293 |
+
if result.ndim != 3 or result.shape[-1] not in (3, 4):
|
| 294 |
+
raise ValueError("%s must be HWC RGB or RGBA." % name)
|
| 295 |
+
if min(result.shape[:2]) < 1:
|
| 296 |
+
raise ValueError("%s is empty." % name)
|
| 297 |
+
if not np.issubdtype(result.dtype, np.floating):
|
| 298 |
+
raise ValueError("%s must contain floating-point values in [0, 1]." % name)
|
| 299 |
+
if not np.isfinite(result).all():
|
| 300 |
+
raise ValueError("%s contains NaN or infinity." % name)
|
| 301 |
+
result = np.clip(result, 0.0, 1.0).astype(np.float32, copy=True)
|
| 302 |
+
if result.shape[-1] == 3:
|
| 303 |
+
result = np.concatenate((result, np.ones((*result.shape[:2], 1), np.float32)), axis=-1)
|
| 304 |
+
return np.ascontiguousarray(result)
|
| 305 |
+
|
| 306 |
+
def _weights(template):
|
| 307 |
+
alpha = template[..., 3].astype(np.float64)
|
| 308 |
+
return np.where(alpha > _ALPHA_EPSILON, alpha, 0.0)
|
| 309 |
+
|
| 310 |
+
def _color_error(background, template):
|
| 311 |
+
# Straight sRGB, not premultiplied RGB: half-alpha green can match green.
|
| 312 |
+
delta = background[..., :3].astype(np.float64) - template[..., :3]
|
| 313 |
+
error = np.mean(delta * delta, axis=-1)
|
| 314 |
+
return np.where(background[..., 3] > _ALPHA_EPSILON, error, 1.0)
|
| 315 |
+
|
| 316 |
+
def GET_RECTS_SIMILARITY(rect1, rect2, template_mode=False):
|
| 317 |
+
"""Compare equal-size HWC float RGB/RGBA rectangles; return 0..200.
|
| 318 |
+
|
| 319 |
+
Default is symmetric: weight by max(alpha1, alpha2); RGB differences
|
| 320 |
+
count only when BOTH pixels are visible. A visible/invisible pair has
|
| 321 |
+
maximum error. If both pixels are invisible, hidden RGB is irrelevant.
|
| 322 |
+
Positive alpha magnitudes are confidence weights, not color darkness.
|
| 323 |
+
Thus solid green at alpha .5 and at alpha 1 have similarity 200.
|
| 324 |
+
|
| 325 |
+
template_mode=True is directional: rect1 is background; rect2 is the
|
| 326 |
+
template. Only rect2 alpha weights the score, so its transparent padding
|
| 327 |
+
does not punish a visible background. Used for locating and refining.
|
| 328 |
+
A fully transparent template raises ValueError (no matching evidence).
|
| 329 |
+
"""
|
| 330 |
+
a, b = _rgba_array(rect1, "rect1"), _rgba_array(rect2, "rect2")
|
| 331 |
+
if a.shape != b.shape:
|
| 332 |
+
raise ValueError("GET_RECTS_SIMILARITY requires equal rectangle dimensions.")
|
| 333 |
+
if template_mode:
|
| 334 |
+
weight = _weights(b)
|
| 335 |
+
total = float(weight.sum())
|
| 336 |
+
if total <= 0.0:
|
| 337 |
+
raise ValueError("A fully transparent template has no matching information.")
|
| 338 |
+
error = float(np.sum(weight * _color_error(a, b)) / total)
|
| 339 |
+
else:
|
| 340 |
+
aa, ba = _weights(a), _weights(b)
|
| 341 |
+
weight = np.maximum(aa, ba)
|
| 342 |
+
total = float(weight.sum())
|
| 343 |
+
if total <= 0.0:
|
| 344 |
+
return 200.0
|
| 345 |
+
visible_both = (aa > 0.0) & (ba > 0.0)
|
| 346 |
+
delta = a[..., :3].astype(np.float64) - b[..., :3]
|
| 347 |
+
pixel_error = np.where(visible_both, np.mean(delta * delta, axis=-1), 1.0)
|
| 348 |
+
error = float(np.sum(weight * pixel_error) / total)
|
| 349 |
+
return float(200.0 * (1.0 - np.clip(error, 0.0, 1.0)))
|
| 350 |
+
|
| 351 |
+
def _overlap(background, template, x, y):
|
| 352 |
+
bh, bw = background.shape[:2]
|
| 353 |
+
th, tw = template.shape[:2]
|
| 354 |
+
x0, y0 = max(0, x), max(0, y)
|
| 355 |
+
x1, y1 = min(bw, x + tw), min(bh, y + th)
|
| 356 |
+
if x1 <= x0 or y1 <= y0:
|
| 357 |
+
return None
|
| 358 |
+
return (slice(y0, y1), slice(x0, x1),
|
| 359 |
+
slice(y0 - y, y1 - y), slice(x0 - x, x1 - x))
|
| 360 |
+
|
| 361 |
+
def _error_at(background, template, x, y, respect_background_alpha=True):
|
| 362 |
+
"""Full template normalization: clipping outside the canvas cannot win."""
|
| 363 |
+
weight = _weights(template)
|
| 364 |
+
total = float(weight.sum())
|
| 365 |
+
if total <= 0.0:
|
| 366 |
+
return float("inf")
|
| 367 |
+
overlap = _overlap(background, template, x, y)
|
| 368 |
+
if overlap is None:
|
| 369 |
+
return 1.0
|
| 370 |
+
by, bx, ty, tx = overlap
|
| 371 |
+
inside_weight = weight[ty, tx]
|
| 372 |
+
# Off-canvas visible pixels are maximum mismatch, not discarded samples.
|
| 373 |
+
outside_error = max(0.0, total - float(inside_weight.sum()))
|
| 374 |
+
if respect_background_alpha:
|
| 375 |
+
pixel_error = _color_error(background[by, bx], template[ty, tx])
|
| 376 |
+
else:
|
| 377 |
+
delta = background[by, bx, :3].astype(np.float64) - template[ty, tx, :3]
|
| 378 |
+
pixel_error = np.mean(delta * delta, axis=-1)
|
| 379 |
+
inside_error = np.sum(inside_weight * pixel_error)
|
| 380 |
+
return float(np.clip((outside_error + inside_error) / total, 0.0, 1.0))
|
| 381 |
+
|
| 382 |
+
def _locate_unedited(background, template, interrupt=lambda: None, respect_background_alpha=True):
|
| 383 |
+
"""Evaluate every fully-contained placement using weighted RGB SSD.
|
| 384 |
+
|
| 385 |
+
FFT correlation is an algebraic acceleration of the SAME per-pixel
|
| 386 |
+
error. No image pyramid, approximate search, or reduced-resolution crop.
|
| 387 |
+
Re-evaluate near-best candidates directly to remove FFT roundoff.
|
| 388 |
+
"""
|
| 389 |
+
bh, bw = background.shape[:2]
|
| 390 |
+
th, tw = template.shape[:2]
|
| 391 |
+
if tw > bw or th > bh:
|
| 392 |
+
raise ValueError("Unedited_Rect is larger than background; it cannot be a crop of it.")
|
| 393 |
+
weight = _weights(template)
|
| 394 |
+
total = float(weight.sum())
|
| 395 |
+
if total <= 0.0:
|
| 396 |
+
raise ValueError("Unedited_Rect is fully transparent; its position cannot be located.")
|
| 397 |
+
visible = ((background[..., 3] > _ALPHA_EPSILON).astype(np.float64)
|
| 398 |
+
if respect_background_alpha else np.ones((bh, bw), dtype=np.float64))
|
| 399 |
+
if not np.any(visible):
|
| 400 |
+
# Every placement has the same maximum appearance error. Avoid
|
| 401 |
+
# directly rescoring every all-transparent location in a large image.
|
| 402 |
+
return 0, 0, 1.0, (bh - th + 1) * (bw - tw + 1)
|
| 403 |
+
rgb = background[..., :3].astype(np.float64)
|
| 404 |
+
template_rgb = template[..., :3].astype(np.float64)
|
| 405 |
+
|
| 406 |
+
def corr(a, b):
|
| 407 |
+
interrupt()
|
| 408 |
+
result = correlate(a, b, mode="valid", method="fft")
|
| 409 |
+
interrupt()
|
| 410 |
+
return result
|
| 411 |
+
|
| 412 |
+
background_square = np.mean(rgb * rgb, axis=-1)
|
| 413 |
+
template_square = np.mean(template_rgb * template_rgb, axis=-1)
|
| 414 |
+
if np.all(visible):
|
| 415 |
+
errors = corr(background_square, weight)
|
| 416 |
+
errors += float(np.sum(weight * template_square))
|
| 417 |
+
for channel in range(3):
|
| 418 |
+
errors -= (2.0 / 3.0) * corr(rgb[..., channel], weight * template_rgb[..., channel])
|
| 419 |
+
else:
|
| 420 |
+
errors = corr(1.0 - visible + visible * background_square, weight)
|
| 421 |
+
errors += corr(visible, weight * template_square)
|
| 422 |
+
for channel in range(3):
|
| 423 |
+
errors -= (2.0 / 3.0) * corr(visible * rgb[..., channel], weight * template_rgb[..., channel])
|
| 424 |
+
errors /= total
|
| 425 |
+
np.clip(errors, 0.0, 1.0, out=errors)
|
| 426 |
+
minimum = float(errors.min())
|
| 427 |
+
near_best = np.flatnonzero(errors.ravel() <= minimum + 1.0e-12)
|
| 428 |
+
# Never drop a possible best match because many low-contrast positions
|
| 429 |
+
# share a near-equal FFT score. Check the earliest candidate and the FFT
|
| 430 |
+
# argmin first, then every remaining near-best candidate if necessary.
|
| 431 |
+
# A directly verified EXACT zero is the mathematical lower bound, so
|
| 432 |
+
# only that allows early exit. Uniform duplicate crops stop immediately.
|
| 433 |
+
fft_best = int(np.argmin(errors))
|
| 434 |
+
first = int(near_best[0])
|
| 435 |
+
candidates = (first, fft_best)
|
| 436 |
+
best_error, best_x, best_y = float("inf"), 0, 0
|
| 437 |
+
from itertools import chain
|
| 438 |
+
for flat in chain(candidates, (int(k) for k in near_best if k != first and k != fft_best)):
|
| 439 |
+
interrupt()
|
| 440 |
+
y, x = divmod(int(flat), errors.shape[1])
|
| 441 |
+
error = _error_at(background, template, x, y, respect_background_alpha)
|
| 442 |
+
if error < best_error:
|
| 443 |
+
best_error, best_x, best_y = error, x, y
|
| 444 |
+
if best_error == 0.0:
|
| 445 |
+
break
|
| 446 |
+
return best_x, best_y, best_error, int(near_best.size)
|
| 447 |
+
|
| 448 |
+
def _source_over(background, template, x, y):
|
| 449 |
+
"""Straight-alpha source-over in sRGB; only the pasted region changes."""
|
| 450 |
+
composite = background.copy()
|
| 451 |
+
canvas = np.zeros_like(background)
|
| 452 |
+
overlap = _overlap(background, template, x, y)
|
| 453 |
+
if overlap is None:
|
| 454 |
+
return composite, canvas
|
| 455 |
+
by, bx, ty, tx = overlap
|
| 456 |
+
source, target = template[ty, tx], background[by, bx]
|
| 457 |
+
canvas[by, bx] = source
|
| 458 |
+
source_alpha, target_alpha = source[..., 3:4], target[..., 3:4]
|
| 459 |
+
alpha = source_alpha + target_alpha * (1.0 - source_alpha)
|
| 460 |
+
premultiplied = (source[..., :3] * source_alpha
|
| 461 |
+
+ target[..., :3] * target_alpha * (1.0 - source_alpha))
|
| 462 |
+
rgb = np.zeros_like(premultiplied)
|
| 463 |
+
np.divide(premultiplied, alpha, out=rgb, where=alpha > _ALPHA_EPSILON)
|
| 464 |
+
result = np.concatenate((np.clip(rgb, 0.0, 1.0), alpha), axis=-1)
|
| 465 |
+
# Preserve the background exactly under fully transparent overlay pixels.
|
| 466 |
+
composite[by, bx] = np.where(source_alpha > 0.0, result, target)
|
| 467 |
+
# Canonical invisible RGB on the otherwise empty overlay canvas.
|
| 468 |
+
canvas[..., :3][canvas[..., 3] <= 0.0] = 0.0
|
| 469 |
+
return composite, canvas
|
| 470 |
+
|
| 471 |
+
def _tensor_image(image, name, mask=None):
|
| 472 |
+
import torch
|
| 473 |
+
if not isinstance(image, torch.Tensor) or image.ndim != 4 or image.shape[0] != 1:
|
| 474 |
+
raise ValueError("%s must be one IMAGE shaped [1,H,W,3 or 4]; split batches first." % name)
|
| 475 |
+
if not image.is_floating_point():
|
| 476 |
+
raise ValueError("%s must be a floating-point IMAGE." % name)
|
| 477 |
+
result = _rgba_array(image[0].detach().to(device="cpu", dtype=torch.float32).numpy(), name)
|
| 478 |
+
# Embedded alpha is authoritative for RGBA. Do not replace, multiply or
|
| 479 |
+
# invert it through the external MASK, even if that mask is connected.
|
| 480 |
+
if int(image.shape[-1]) == 4:
|
| 481 |
+
return result
|
| 482 |
+
if mask is not None:
|
| 483 |
+
if not isinstance(mask, torch.Tensor):
|
| 484 |
+
raise ValueError("%s_mask must be a ComfyUI MASK tensor." % name)
|
| 485 |
+
value = mask.detach().to(device="cpu", dtype=torch.float32).numpy()
|
| 486 |
+
if value.ndim == 3 and value.shape[0] == 1:
|
| 487 |
+
value = value[0]
|
| 488 |
+
if value.shape != result.shape[:2]:
|
| 489 |
+
# Standard Load Image's no-alpha placeholder is a 64x64 all-zero
|
| 490 |
+
# MASK even when its RGB image has another resolution.
|
| 491 |
+
if value.shape == (64, 64) and not np.any(value):
|
| 492 |
+
value = np.zeros(result.shape[:2], dtype=np.float32)
|
| 493 |
+
else:
|
| 494 |
+
raise ValueError("%s_mask resolution must match %s. Masks are not resized." % (name, name))
|
| 495 |
+
if not np.isfinite(value).all():
|
| 496 |
+
raise ValueError("%s_mask contains NaN or infinity." % name)
|
| 497 |
+
# RGB has no alpha: reconstruct it from the Comfy Load Image MASK.
|
| 498 |
+
result[..., 3] = 1.0 - np.clip(value, 0.0, 1.0)
|
| 499 |
+
# Preserve the fact that RGB had NO alpha. Promoting it here used to erase
|
| 500 |
+
# that information before crop-location diagnostics and recovery.
|
| 501 |
+
if mask is None and int(image.shape[-1]) == 3:
|
| 502 |
+
return np.ascontiguousarray(result[..., :3])
|
| 503 |
+
return result
|
| 504 |
+
|
| 505 |
+
# ---------------------------------------------------------------------------
|
| 506 |
+
# Original-source EWA rendering with continuous rectangle geometry.
|
| 507 |
+
# The numerical filter/color helpers above are intentionally shared with the
|
| 508 |
+
# supplied Salia_Rescale_Image implementation. There is no second resize pass.
|
| 509 |
+
# ---------------------------------------------------------------------------
|
| 510 |
+
|
| 511 |
+
def _ewa_axis_at_centers(source_size, centers, source_per_output_pixel, radius):
|
| 512 |
+
"""EWA source taps for arbitrary source-coordinate pixel centers.
|
| 513 |
+
|
| 514 |
+
``centers`` are source pixel-center coordinates (first center is zero).
|
| 515 |
+
Footprint widening uses the continuous source/output scale, independent
|
| 516 |
+
of how many destination pixels happen to survive canvas clipping.
|
| 517 |
+
"""
|
| 518 |
+
centers = np.asarray(centers, dtype=np.float64)
|
| 519 |
+
footprint = max(1.0, float(source_per_output_pixel))
|
| 520 |
+
support = radius * footprint
|
| 521 |
+
count = min(int(source_size), int(math.ceil(2.0 * support)) + 1)
|
| 522 |
+
starts = np.maximum(0, np.ceil(centers - support).astype(np.int64))
|
| 523 |
+
raw_indices = starts[:, None] + np.arange(count, dtype=np.int64)[None, :]
|
| 524 |
+
squared_distance = ((raw_indices - centers[:, None]) / footprint) ** 2
|
| 525 |
+
valid = (raw_indices < source_size) & (squared_distance < radius * radius)
|
| 526 |
+
squared_distance[~valid] = np.inf
|
| 527 |
+
indices = np.minimum(raw_indices, source_size - 1).astype(np.int32)
|
| 528 |
+
return indices, squared_distance
|
| 529 |
+
|
| 530 |
+
|
| 531 |
+
def render_rgba_ewa(source, background_width, background_height,
|
| 532 |
+
center_x, center_y, width, height, interrupt=lambda: None):
|
| 533 |
+
"""Render a continuous RGBA rectangle from its original source, once.
|
| 534 |
+
|
| 535 |
+
Return ``(patch_rgba, x0, y0)``. ``x0/y0`` are integer pixel-edge canvas
|
| 536 |
+
coordinates; ``patch_rgba`` contains only the clipped destination bbox.
|
| 537 |
+
The requested rectangle has floating pixel-edge bounds
|
| 538 |
+
``[center_x-width/2, center_x+width/2]`` and corresponding Y bounds.
|
| 539 |
+
Destination pixel centers are inverse-mapped directly to the original
|
| 540 |
+
source. RGB filtering is linear-light and alpha-premultiplied. Source
|
| 541 |
+
edge taps are truncated and normalized just as in the original rescaler.
|
| 542 |
+
|
| 543 |
+
The fraction of a destination pixel covered by the rectangle multiplies
|
| 544 |
+
filtered alpha (not RGB). This antialiases fractional outer bounds without
|
| 545 |
+
darkening their straight RGB. Filtering and placement stay continuous;
|
| 546 |
+
width, height and position are never rounded to an integer transform.
|
| 547 |
+
|
| 548 |
+
For integer-aligned geometry the output is the original resizer's output
|
| 549 |
+
cropped to the background. Same-size integer-aligned requests are exact
|
| 550 |
+
no-ops on pixel values, including hidden RGB, matching that resizer.
|
| 551 |
+
Empty overlap returns float32 array ``(0,0,4)`` and a clipped origin.
|
| 552 |
+
"""
|
| 553 |
+
interrupt()
|
| 554 |
+
source = np.asarray(source)
|
| 555 |
+
if (source.ndim != 3 or source.shape[-1] != 4
|
| 556 |
+
or min(source.shape[:2]) < 1
|
| 557 |
+
or not np.issubdtype(source.dtype, np.floating)):
|
| 558 |
+
raise ValueError("render_rgba_ewa expects a nonempty floating HWC RGBA image.")
|
| 559 |
+
geometry = np.asarray((center_x, center_y, width, height), dtype=np.float64)
|
| 560 |
+
if not np.isfinite(geometry).all() or width < 1.0 or height < 1.0:
|
| 561 |
+
raise ValueError("EWA rectangle geometry must be finite with width/height >= 1.")
|
| 562 |
+
if (isinstance(background_width, bool) or isinstance(background_height, bool)
|
| 563 |
+
or int(background_width) != background_width
|
| 564 |
+
or int(background_height) != background_height
|
| 565 |
+
or background_width < 1 or background_height < 1):
|
| 566 |
+
raise ValueError("EWA background dimensions must be positive integers.")
|
| 567 |
+
if not np.isfinite(source).all():
|
| 568 |
+
raise ValueError("EWA source contains NaN or infinity.")
|
| 569 |
+
source = np.ascontiguousarray(np.clip(source, 0.0, 1.0), dtype=np.float32)
|
| 570 |
+
bw, bh = int(background_width), int(background_height)
|
| 571 |
+
left, top = float(center_x - width / 2.0), float(center_y - height / 2.0)
|
| 572 |
+
right, bottom = left + float(width), top + float(height)
|
| 573 |
+
x0 = max(0, min(bw, int(math.floor(left))))
|
| 574 |
+
y0 = max(0, min(bh, int(math.floor(top))))
|
| 575 |
+
x1 = max(0, min(bw, int(math.ceil(right))))
|
| 576 |
+
y1 = max(0, min(bh, int(math.ceil(bottom))))
|
| 577 |
+
if x1 <= x0 or y1 <= y0:
|
| 578 |
+
return np.empty((0, 0, 4), dtype=np.float32), x0, y0
|
| 579 |
+
source_height, source_width = source.shape[:2]
|
| 580 |
+
if (float(width) == source_width and float(height) == source_height
|
| 581 |
+
and left == math.floor(left) and top == math.floor(top)):
|
| 582 |
+
return (source[y0 - int(top):y1 - int(top),
|
| 583 |
+
x0 - int(left):x1 - int(left)].copy(), x0, y0)
|
| 584 |
+
|
| 585 |
+
definition = _ewa_definition(_MODES[0])
|
| 586 |
+
ratio_x, ratio_y = source_width / float(width), source_height / float(height)
|
| 587 |
+
output_x = np.arange(x0, x1, dtype=np.float64)
|
| 588 |
+
output_y = np.arange(y0, y1, dtype=np.float64)
|
| 589 |
+
source_x = (output_x + 0.5 - left) * ratio_x - 0.5
|
| 590 |
+
source_y = (output_y + 0.5 - top) * ratio_y - 0.5
|
| 591 |
+
x_axis = _ewa_axis_at_centers(source_width, source_x, ratio_x, definition[0])
|
| 592 |
+
y_axis = _ewa_axis_at_centers(source_height, source_y, ratio_y, definition[0])
|
| 593 |
+
filtered = _resize_ewa(_prepare_image(source), x1 - x0, y1 - y0,
|
| 594 |
+
definition, x_axis, y_axis, interrupt, lambda value: None)
|
| 595 |
+
result = _finish_image(filtered, 4)
|
| 596 |
+
coverage_x = np.maximum(0.0, np.minimum(output_x + 1.0, right)
|
| 597 |
+
- np.maximum(output_x, left))
|
| 598 |
+
coverage_y = np.maximum(0.0, np.minimum(output_y + 1.0, bottom)
|
| 599 |
+
- np.maximum(output_y, top))
|
| 600 |
+
result[..., 3] *= (coverage_y[:, None] * coverage_x[None, :]).astype(np.float32)
|
| 601 |
+
result[..., :3][result[..., 3] <= 0.0] = 0.0
|
| 602 |
+
interrupt()
|
| 603 |
+
return np.ascontiguousarray(result, dtype=np.float32), x0, y0
|
| 604 |
+
|
| 605 |
+
|
| 606 |
+
# ---------------------------------------------------------------------------
|
| 607 |
+
# Joint continuous alpha-aware registration (X, Y, width and height together).
|
| 608 |
+
# ---------------------------------------------------------------------------
|
| 609 |
+
|
| 610 |
+
from scipy.ndimage import gaussian_filter, map_coordinates, spline_filter
|
| 611 |
+
from scipy.optimize import least_squares
|
| 612 |
+
|
| 613 |
+
_MATCH_MAX_SIDE = 768
|
| 614 |
+
_MATCH_MAX_SAMPLES = 20000
|
| 615 |
+
|
| 616 |
+
|
| 617 |
+
def _premultiplied_fields(image, sigma_y=0.0, sigma_x=0.0, with_valid=False):
|
| 618 |
+
"""Float64 fields prevent hidden RGB leakage during blur/interpolation."""
|
| 619 |
+
alpha = image[..., 3].astype(np.float64)
|
| 620 |
+
fields = np.concatenate((image[..., :3].astype(np.float64) * alpha[..., None],
|
| 621 |
+
alpha[..., None]), axis=-1)
|
| 622 |
+
if with_valid:
|
| 623 |
+
fields = np.concatenate((fields, (alpha > _ALPHA_EPSILON)[..., None].astype(np.float64)), axis=-1)
|
| 624 |
+
if sigma_y > 0.0 or sigma_x > 0.0:
|
| 625 |
+
fields = gaussian_filter(fields, sigma=(sigma_y, sigma_x, 0.0), mode="nearest")
|
| 626 |
+
return fields
|
| 627 |
+
|
| 628 |
+
|
| 629 |
+
def _straight_fields(fields):
|
| 630 |
+
alpha = np.clip(fields[..., 3], 0.0, 1.0)
|
| 631 |
+
rgb = np.zeros_like(fields[..., :3])
|
| 632 |
+
np.divide(fields[..., :3], alpha[..., None], out=rgb,
|
| 633 |
+
where=alpha[..., None] > _ALPHA_EPSILON)
|
| 634 |
+
return np.clip(rgb, 0.0, 1.0), alpha
|
| 635 |
+
|
| 636 |
+
|
| 637 |
+
class _RegistrationLevel:
|
| 638 |
+
"""Fixed source samples and confidence; moving support cannot erase error.
|
| 639 |
+
|
| 640 |
+
Parameter vector: normalized center offsets and log width/height ratios.
|
| 641 |
+
A custom SciPy soft-L1 loss applies alpha AFTER robustification, so alpha
|
| 642 |
+
is a confidence weight rather than an RGB channel or a brightness change.
|
| 643 |
+
"""
|
| 644 |
+
def __init__(self, background, template, center_x, center_y, initial_width,
|
| 645 |
+
initial_height, factor, robust_threshold, interrupt, on_call):
|
| 646 |
+
self.center_x, self.center_y = float(center_x), float(center_y)
|
| 647 |
+
self.initial_width, self.initial_height = float(initial_width), float(initial_height)
|
| 648 |
+
self.background_height, self.background_width = background.shape[:2]
|
| 649 |
+
self.robust_threshold = float(robust_threshold)
|
| 650 |
+
self.interrupt, self.on_call = interrupt, on_call
|
| 651 |
+
self.calls = 0
|
| 652 |
+
sigma = 0.5 * factor if factor > 1 else 0.0
|
| 653 |
+
source_fields = _premultiplied_fields(template, sigma, sigma)
|
| 654 |
+
source_rgb, source_alpha = _straight_fields(source_fields)
|
| 655 |
+
valid = np.flatnonzero(source_alpha.ravel() > _ALPHA_EPSILON)
|
| 656 |
+
if not valid.size:
|
| 657 |
+
raise ValueError("The overlay has no visible samples at this matching resolution.")
|
| 658 |
+
budget = max(1024, _MATCH_MAX_SAMPLES // factor)
|
| 659 |
+
if valid.size > budget:
|
| 660 |
+
# A fixed seed/subset for this level: never change the samples as
|
| 661 |
+
# a function of the transform being tested.
|
| 662 |
+
valid = np.sort(np.random.default_rng(0).choice(valid, budget, replace=False))
|
| 663 |
+
sy, sx = np.divmod(valid, template.shape[1])
|
| 664 |
+
self.u = (sx.astype(np.float64) + 0.5) / template.shape[1] - 0.5
|
| 665 |
+
self.v = (sy.astype(np.float64) + 0.5) / template.shape[0] - 0.5
|
| 666 |
+
self.source_rgb = source_rgb.reshape(-1, 3)[valid]
|
| 667 |
+
confidence = source_alpha.ravel()[valid]
|
| 668 |
+
self.confidence = confidence / confidence.sum()
|
| 669 |
+
self.count = len(valid)
|
| 670 |
+
self.loss_weights = np.concatenate((np.repeat(self.confidence / 3.0, 3),
|
| 671 |
+
self.confidence, np.full(4, 1.0e-7)))
|
| 672 |
+
|
| 673 |
+
# Match antialiasing to the initial physical size, including a source
|
| 674 |
+
# registration-resolution cap. This pyramid is for fitting only;
|
| 675 |
+
# final output is rendered directly from the original overlay.
|
| 676 |
+
ratio_x = initial_width / template.shape[1]
|
| 677 |
+
ratio_y = initial_height / template.shape[0]
|
| 678 |
+
sigma_x = max(sigma * ratio_x, 0.35 * max(0.0, ratio_x - 1.0))
|
| 679 |
+
sigma_y = max(sigma * ratio_y, 0.35 * max(0.0, ratio_y - 1.0))
|
| 680 |
+
interrupt()
|
| 681 |
+
background_fields = _premultiplied_fields(background, sigma_y, sigma_x, with_valid=True)
|
| 682 |
+
# Cubic interpolation needs coefficients computed just once per level.
|
| 683 |
+
self.coefficients = [spline_filter(background_fields[..., c], order=3, mode="mirror")
|
| 684 |
+
for c in range(5)]
|
| 685 |
+
interrupt()
|
| 686 |
+
|
| 687 |
+
def geometry(self, p):
|
| 688 |
+
return (self.center_x + p[0] * self.initial_width,
|
| 689 |
+
self.center_y + p[1] * self.initial_height,
|
| 690 |
+
self.initial_width * math.exp(float(p[2])),
|
| 691 |
+
self.initial_height * math.exp(float(p[3])))
|
| 692 |
+
|
| 693 |
+
def differences(self, p):
|
| 694 |
+
self.interrupt()
|
| 695 |
+
center_x, center_y, width, height = self.geometry(p)
|
| 696 |
+
x = center_x + self.u * width - 0.5
|
| 697 |
+
y = center_y + self.v * height - 0.5
|
| 698 |
+
coordinates = np.vstack((y, x))
|
| 699 |
+
sampled = np.stack([map_coordinates(c, coordinates, order=3, mode="constant",
|
| 700 |
+
cval=0.0, prefilter=False)
|
| 701 |
+
for c in self.coefficients], axis=-1)
|
| 702 |
+
rgb, alpha = _straight_fields(sampled[..., :4])
|
| 703 |
+
visible = np.clip(sampled[..., 4], 0.0, 1.0)
|
| 704 |
+
visible = np.where(alpha > _ALPHA_EPSILON, visible, 0.0)
|
| 705 |
+
delta = (rgb - self.source_rgb) * visible[:, None]
|
| 706 |
+
# Retain every fixed source sample. Off-canvas/transparent reference
|
| 707 |
+
# samples carry a penalty instead of dropping out of the objective.
|
| 708 |
+
# Outside distance supplies a gradient back toward the canvas.
|
| 709 |
+
outside_x = np.maximum(-x, 0.0) + np.maximum(x - (self.background_width - 1), 0.0)
|
| 710 |
+
outside_y = np.maximum(-y, 0.0) + np.maximum(y - (self.background_height - 1), 0.0)
|
| 711 |
+
outside = (outside_x / max(1.0, self.initial_width * 0.1)
|
| 712 |
+
+ outside_y / max(1.0, self.initial_height * 0.1))
|
| 713 |
+
missing = 1.0 - visible + outside
|
| 714 |
+
return delta, missing
|
| 715 |
+
|
| 716 |
+
def residual(self, p):
|
| 717 |
+
self.calls += 1
|
| 718 |
+
self.on_call(self.calls)
|
| 719 |
+
delta, missing = self.differences(p)
|
| 720 |
+
return np.concatenate((delta.ravel(), missing, np.asarray(p, dtype=np.float64)))
|
| 721 |
+
|
| 722 |
+
def loss(self, z):
|
| 723 |
+
root = np.sqrt(1.0 + z)
|
| 724 |
+
rho = np.vstack((2.0 * (root - 1.0), 1.0 / root, -0.5 / (root ** 3)))
|
| 725 |
+
rho *= self.loss_weights[None, :]
|
| 726 |
+
# Tiny quadratic preference for the original geometry breaks exact
|
| 727 |
+
# appearance ties; it is not part of the reported image similarity.
|
| 728 |
+
rho[0, -4:] = self.loss_weights[-4:] * z[-4:]
|
| 729 |
+
rho[1, -4:] = self.loss_weights[-4:]
|
| 730 |
+
rho[2, -4:] = 0.0
|
| 731 |
+
return rho
|
| 732 |
+
|
| 733 |
+
def cost(self, p):
|
| 734 |
+
residual = self.residual(p)
|
| 735 |
+
scale = self.robust_threshold
|
| 736 |
+
return float(0.5 * scale * scale * self.loss((residual / scale) ** 2)[0].sum())
|
| 737 |
+
|
| 738 |
+
def similarity(self, p):
|
| 739 |
+
delta, missing = self.differences(p)
|
| 740 |
+
error = np.sum(self.confidence * (np.mean(delta * delta, axis=-1) + missing * missing))
|
| 741 |
+
return float(200.0 * (1.0 - np.clip(error, 0.0, 1.0)))
|
| 742 |
+
|
| 743 |
+
|
| 744 |
+
def _validate_registration_options(target_width, target_height, max_evaluations,
|
| 745 |
+
pyramid_levels, robust_threshold, scale_limit, min_tolerance=0.9):
|
| 746 |
+
for name, value, lo, hi in (
|
| 747 |
+
("target_width", target_width, 0, _MAX_DIMENSION),
|
| 748 |
+
("target_height", target_height, 0, _MAX_DIMENSION),
|
| 749 |
+
("max_evaluations", max_evaluations, 5, 1000),
|
| 750 |
+
("pyramid_levels", pyramid_levels, 1, 5),
|
| 751 |
+
):
|
| 752 |
+
if isinstance(value, bool) or not isinstance(value, (int, np.integer)) or not lo <= value <= hi:
|
| 753 |
+
raise ValueError("%s must be an integer in [%d,%d]." % (name, lo, hi))
|
| 754 |
+
for name, value, lo, hi in (("robust_threshold", robust_threshold, 0.001, 1.0),
|
| 755 |
+
("scale_limit", scale_limit, 1.01, 8.0),
|
| 756 |
+
("min_tolerance", min_tolerance, 0.0, 1.0)):
|
| 757 |
+
if isinstance(value, bool) or not isinstance(value, (int, float, np.integer, np.floating)):
|
| 758 |
+
raise ValueError("%s must be a finite number in [%g,%g]." % (name, lo, hi))
|
| 759 |
+
if not math.isfinite(float(value)) or not lo <= value <= hi:
|
| 760 |
+
raise ValueError("%s must be a finite number in [%g,%g]." % (name, lo, hi))
|
| 761 |
+
|
| 762 |
+
|
| 763 |
+
def _input_alpha_description(name, rgba, input_channels):
|
| 764 |
+
alpha = rgba[..., 3]
|
| 765 |
+
transparent = float(np.mean(alpha <= _ALPHA_EPSILON) * 100.0)
|
| 766 |
+
opaque = float(np.mean(alpha >= 1.0 - _ALPHA_EPSILON) * 100.0)
|
| 767 |
+
return ("%s=%dx%d, input_channels=%d, alpha_zero=%.2f%%, alpha_opaque=%.2f%%"
|
| 768 |
+
% (name, rgba.shape[1], rgba.shape[0], input_channels, transparent, opaque))
|
| 769 |
+
|
| 770 |
+
|
| 771 |
+
def align_arrays(background, overlay, unedited_rect, target_width=0, target_height=0,
|
| 772 |
+
max_evaluations=100, pyramid_levels=3, robust_threshold=0.05,
|
| 773 |
+
scale_limit=2.0, interrupt=lambda: None, progress=lambda value: None,
|
| 774 |
+
min_tolerance=0.9):
|
| 775 |
+
"""Locate the crop, jointly optimize four continuous values, then render.
|
| 776 |
+
|
| 777 |
+
No pixel-step grid, alternating move/scale phase, rotation, shear, or
|
| 778 |
+
optical flow. SciPy's bounded robust least-squares optimizer works on an
|
| 779 |
+
alpha-safe antialiased matching pyramid. EWA renders the original once.
|
| 780 |
+
"""
|
| 781 |
+
started = time.monotonic()
|
| 782 |
+
_validate_registration_options(target_width, target_height, max_evaluations,
|
| 783 |
+
pyramid_levels, robust_threshold, scale_limit, min_tolerance)
|
| 784 |
+
min_crop_similarity = float(min_tolerance) * 200.0
|
| 785 |
+
raw_inputs = (background, overlay, unedited_rect)
|
| 786 |
+
background = _rgba_array(background, "background")
|
| 787 |
+
overlay = _rgba_array(overlay, "overlay")
|
| 788 |
+
unedited_rect = _rgba_array(unedited_rect, "Unedited_Rect")
|
| 789 |
+
descriptions = [
|
| 790 |
+
_input_alpha_description(name, rgba, int(np.asarray(raw).shape[-1]))
|
| 791 |
+
for name, raw, rgba in zip(("background", "overlay", "Unedited_Rect"),
|
| 792 |
+
raw_inputs, (background, overlay, unedited_rect))
|
| 793 |
+
]
|
| 794 |
+
input_diagnostics = "Inputs:\n" + "\n".join(descriptions)
|
| 795 |
+
if float(_weights(overlay).sum()) <= 0.0:
|
| 796 |
+
raise ValueError("overlay is fully transparent; there is nothing to align or paste.")
|
| 797 |
+
interrupt()
|
| 798 |
+
crop_x, crop_y, crop_error, ties = _locate_unedited(background, unedited_rect, interrupt)
|
| 799 |
+
crop_match_mode = "alpha-aware crop appearance"
|
| 800 |
+
crop_notes = []
|
| 801 |
+
# A Load Image IMAGE output is RGB. If its MASK is missing while the
|
| 802 |
+
# background retains alpha, treating that RGB crop as genuinely opaque
|
| 803 |
+
# turns matching transparent padding into a maximum-error penalty.
|
| 804 |
+
# Recover ONLY when the actual RGB values provide a near-exact copy
|
| 805 |
+
# match. Do not cherry-pick arbitrary low-scoring fallback locations.
|
| 806 |
+
if (crop_error > 1.0e-8
|
| 807 |
+
and np.all(unedited_rect[..., 3] >= 1.0 - _ALPHA_EPSILON)
|
| 808 |
+
and np.any(background[..., 3] <= _ALPHA_EPSILON)):
|
| 809 |
+
rgb_x, rgb_y, rgb_error, rgb_ties = _locate_unedited(
|
| 810 |
+
background, unedited_rect, interrupt, respect_background_alpha=False)
|
| 811 |
+
rgb_region = background[rgb_y:rgb_y + unedited_rect.shape[0],
|
| 812 |
+
rgb_x:rgb_x + unedited_rect.shape[1]]
|
| 813 |
+
visible_evidence = int(np.count_nonzero(rgb_region[..., 3] > _ALPHA_EPSILON))
|
| 814 |
+
minimum_evidence = min(16, unedited_rect.shape[0] * unedited_rect.shape[1])
|
| 815 |
+
rgb_texture = float(np.mean(np.var(unedited_rect[..., :3].astype(np.float64), axis=(0, 1))))
|
| 816 |
+
trustworthy_rgb = visible_evidence >= minimum_evidence and rgb_texture > 1.0e-10
|
| 817 |
+
if (rgb_error <= _RGB_COPY_ERROR_TOLERANCE and rgb_error < crop_error
|
| 818 |
+
and trustworthy_rgb):
|
| 819 |
+
crop_notes.append(
|
| 820 |
+
"Alpha-aware candidate (%d,%d) scored %.8f/200. A near-exact RGB copy instead matched (%d,%d)."
|
| 821 |
+
% (crop_x, crop_y, 200.0 * (1.0 - crop_error), rgb_x, rgb_y))
|
| 822 |
+
crop_notes.append(
|
| 823 |
+
"Crop alpha may be missing: connect the crop's matching Load Image MASK. "
|
| 824 |
+
"RGB fallback applies to crop location only; overlay output still needs its own real alpha/MASK.")
|
| 825 |
+
crop_x, crop_y, crop_error, ties = rgb_x, rgb_y, rgb_error, rgb_ties
|
| 826 |
+
crop_match_mode = "verified near-exact RGB-copy fallback"
|
| 827 |
+
elif rgb_error <= _RGB_COPY_ERROR_TOLERANCE and not trustworthy_rgb:
|
| 828 |
+
crop_notes.append(
|
| 829 |
+
"An RGB-only candidate had insufficient visible/texture evidence and was not accepted. "
|
| 830 |
+
"Hidden RGB in transparent pixels cannot establish a reliable visible match.")
|
| 831 |
+
crop_similarity = 200.0 * (1.0 - crop_error)
|
| 832 |
+
# The search above evaluates all placements and selects the highest
|
| 833 |
+
# score. Tolerance is an acceptance check ONLY, never an early exit.
|
| 834 |
+
if crop_similarity < min_crop_similarity:
|
| 835 |
+
# Solver convergence is not evidence of correct crop localization.
|
| 836 |
+
# Return an explicit failure through the existing status socket and
|
| 837 |
+
# preserve background instead of pasting a wrong result somewhere.
|
| 838 |
+
status = "\n".join([
|
| 839 |
+
"LOCATION FAILED: original crop was not located reliably. No overlay was pasted.",
|
| 840 |
+
"Closest candidate: top_left=(%d,%d), size=%dx%d, similarity=%.8f/200; required >= %.4f/200."
|
| 841 |
+
% (crop_x, crop_y, unedited_rect.shape[1], unedited_rect.shape[0], crop_similarity,
|
| 842 |
+
min_crop_similarity),
|
| 843 |
+
"min_tolerance=%.6f. The highest-scoring location was selected before checking this minimum."
|
| 844 |
+
% float(min_tolerance),
|
| 845 |
+
"The best available crop match did not meet the configured minimum. "
|
| 846 |
+
"Check image wiring, crop resizing/flipping, and corresponding MASK/alpha inputs.",
|
| 847 |
+
"Output coordinates=-1 and dimensions=0 indicate no accepted placement.",
|
| 848 |
+
input_diagnostics, *crop_notes,
|
| 849 |
+
])
|
| 850 |
+
progress(100)
|
| 851 |
+
_LOG.warning("Salia: rejected unverified crop at (%d,%d), similarity %.8f/200", crop_x, crop_y, crop_similarity)
|
| 852 |
+
return {"composite": background.copy(), "overlay_canvas": np.zeros_like(background),
|
| 853 |
+
"status": status, "x": -1.0, "y": -1.0, "width": 0.0, "height": 0.0,
|
| 854 |
+
"center_x": -1.0, "center_y": -1.0, "similarity": 0.0,
|
| 855 |
+
"registration_similarity": 0.0, "robust_cost": None, "initial_robust_cost": None,
|
| 856 |
+
"converged": False, "accepted": False, "location_verified": False,
|
| 857 |
+
"trace": [], "parameters": np.zeros(4), "crop_x": crop_x, "crop_y": crop_y,
|
| 858 |
+
"crop_similarity": float(crop_similarity), "crop_match_mode": crop_match_mode}
|
| 859 |
+
crop_height, crop_width = unedited_rect.shape[:2]
|
| 860 |
+
center_x, center_y = crop_x + crop_width / 2.0, crop_y + crop_height / 2.0
|
| 861 |
+
width0, height0 = float(target_width or crop_width), float(target_height or crop_height)
|
| 862 |
+
# Keep work bounded for very large patches without changing final output
|
| 863 |
+
# size or resolution. The original remains the only final render source.
|
| 864 |
+
matching_ratio = min(1.0, _MATCH_MAX_SIDE / max(width0, height0))
|
| 865 |
+
match_width = max(1, int(math.floor(width0 * matching_ratio + 0.5)))
|
| 866 |
+
match_height = max(1, int(math.floor(height0 * matching_ratio + 0.5)))
|
| 867 |
+
template = resize_rgba_ewa(overlay, match_width, match_height, interrupt)
|
| 868 |
+
confidence = _weights(template)
|
| 869 |
+
if confidence.sum() <= 0.0:
|
| 870 |
+
raise ValueError("The overlay became fully transparent at registration resolution.")
|
| 871 |
+
mean = np.sum(template[..., :3] * confidence[..., None], axis=(0, 1)) / confidence.sum()
|
| 872 |
+
variance = float(np.sum(confidence[..., None] * (template[..., :3] - mean) ** 2)
|
| 873 |
+
/ (3.0 * confidence.sum()))
|
| 874 |
+
progress(8)
|
| 875 |
+
# Bounds are guardrails, not pixel search steps. Center can move by one
|
| 876 |
+
# initial rectangle width/height in either direction. Scale can vary by
|
| 877 |
+
# scale_limit or its reciprocal; actual dimensions always stay >=1.
|
| 878 |
+
low_width = max(1.0, width0 / float(scale_limit))
|
| 879 |
+
low_height = max(1.0, height0 / float(scale_limit))
|
| 880 |
+
high_width = min(float(_MAX_DIMENSION), width0 * float(scale_limit))
|
| 881 |
+
high_height = min(float(_MAX_DIMENSION), height0 * float(scale_limit))
|
| 882 |
+
# Positive bounds even for a 1px starting dimension at the upper limit.
|
| 883 |
+
lower = np.array((-1.0, -1.0, math.log(low_width / width0), math.log(low_height / height0)))
|
| 884 |
+
upper = np.array((1.0, 1.0, math.log(high_width / width0), math.log(high_height / height0)))
|
| 885 |
+
lower[2:] = np.minimum(lower[2:], -1.0e-12)
|
| 886 |
+
upper[2:] = np.maximum(upper[2:], 1.0e-12)
|
| 887 |
+
parameters = np.zeros(4, dtype=np.float64)
|
| 888 |
+
trace, notices = [], list(crop_notes)
|
| 889 |
+
final_problem = None
|
| 890 |
+
last_result = None
|
| 891 |
+
total_calls = 0
|
| 892 |
+
levels = [2 ** k for k in reversed(range(int(pyramid_levels)))]
|
| 893 |
+
if variance < 1.0e-10:
|
| 894 |
+
# A uniform-color patch cannot identify a unique width/height from
|
| 895 |
+
# color matching. Preserve the supplied crop geometry in that case.
|
| 896 |
+
notices.append("LOW TEXTURE: overlay is effectively uniform color; initial geometry retained.")
|
| 897 |
+
levels = [1]
|
| 898 |
+
for level_index, factor in enumerate(levels):
|
| 899 |
+
interrupt()
|
| 900 |
+
def on_call(calls, index=level_index):
|
| 901 |
+
fractional = min(0.98, calls / max(1, max_evaluations * 6))
|
| 902 |
+
progress(8 + int(78 * (index + fractional) / len(levels)))
|
| 903 |
+
problem = _RegistrationLevel(background, template, center_x, center_y,
|
| 904 |
+
width0, height0, factor, robust_threshold, interrupt, on_call)
|
| 905 |
+
initial_cost = problem.cost(parameters)
|
| 906 |
+
if variance >= 1.0e-10:
|
| 907 |
+
result = least_squares(
|
| 908 |
+
problem.residual, parameters, bounds=(lower, upper), method="trf", jac="2-point",
|
| 909 |
+
loss=problem.loss, f_scale=float(robust_threshold), x_scale=np.full(4, 0.1),
|
| 910 |
+
max_nfev=int(max_evaluations), ftol=1.0e-8, xtol=1.0e-7, gtol=1.0e-10,
|
| 911 |
+
)
|
| 912 |
+
candidate_cost = problem.cost(result.x)
|
| 913 |
+
if candidate_cost < initial_cost - 1.0e-14:
|
| 914 |
+
parameters = result.x.copy()
|
| 915 |
+
last_result = result
|
| 916 |
+
trace.append("factor=%d; samples=%d; cost %.10g -> %.10g; nfev=%d; solver_status=%d (%s)"
|
| 917 |
+
% (factor, problem.count, initial_cost, problem.cost(parameters), result.nfev,
|
| 918 |
+
result.status, str(result.message).strip()))
|
| 919 |
+
if result.status == 0:
|
| 920 |
+
notices.append("Evaluation limit reached at pyramid factor %d; best accepted fit retained." % factor)
|
| 921 |
+
else:
|
| 922 |
+
trace.append("factor=1; samples=%d; optimizer skipped for uniform color" % problem.count)
|
| 923 |
+
total_calls += problem.calls
|
| 924 |
+
final_problem = problem
|
| 925 |
+
# Validate every accumulated pyramid update on the SAME finest objective
|
| 926 |
+
# against the original transform. A coarse-only improvement is not enough.
|
| 927 |
+
base_cost = final_problem.cost(np.zeros(4))
|
| 928 |
+
fit_cost = final_problem.cost(parameters)
|
| 929 |
+
accepted = bool(fit_cost < base_cost - 1.0e-14)
|
| 930 |
+
if not accepted:
|
| 931 |
+
parameters[:] = 0.0
|
| 932 |
+
fit_cost = base_cost
|
| 933 |
+
center_x, center_y, width, height = final_problem.geometry(parameters)
|
| 934 |
+
width, height = max(1.0, min(_MAX_DIMENSION, width)), max(1.0, min(_MAX_DIMENSION, height))
|
| 935 |
+
x, y = center_x - width / 2.0, center_y - height / 2.0
|
| 936 |
+
registration_similarity = final_problem.similarity(parameters)
|
| 937 |
+
if accepted and np.any(np.minimum(parameters - lower, upper - parameters) < 1.0e-5):
|
| 938 |
+
notices.append("A fit parameter reached its allowed bound; inspect alignment or increase scale_limit if appropriate.")
|
| 939 |
+
if last_result is not None and accepted:
|
| 940 |
+
singular = np.linalg.svd(last_result.jac[:-4], compute_uv=False)
|
| 941 |
+
if singular.size and (singular[0] <= 1.0e-10 or singular[-1] < singular[0] * 1.0e-5):
|
| 942 |
+
notices.append("Weak geometry constraint: image detail does not reliably determine all four parameters.")
|
| 943 |
+
progress(88)
|
| 944 |
+
# The transform stays fractional: no rounding width, height, or top-left.
|
| 945 |
+
patch, paste_x, paste_y = render_rgba_ewa(
|
| 946 |
+
overlay, background.shape[1], background.shape[0], center_x, center_y, width, height, interrupt)
|
| 947 |
+
composite, canvas = _source_over(background, patch, paste_x, paste_y)
|
| 948 |
+
rendered_error = _error_at(background, patch, paste_x, paste_y) if patch.size else 1.0
|
| 949 |
+
similarity = float(200.0 * (1.0 - rendered_error))
|
| 950 |
+
if ties > 1:
|
| 951 |
+
notices.append("Crop location ambiguous: %d numerically tied locations; first confirmed best used." % ties)
|
| 952 |
+
if crop_error > 1.0e-8:
|
| 953 |
+
notices.append("Unedited_Rect was not an exact appearance match; closest crop location used.")
|
| 954 |
+
if x < 0.0 or y < 0.0 or x + width > background.shape[1] or y + height > background.shape[0]:
|
| 955 |
+
notices.append("Overlay extends outside canvas. Fitting penalizes missing samples; rendered similarity covers the visible paste only.")
|
| 956 |
+
converged = bool(last_result is not None and last_result.status > 0)
|
| 957 |
+
state = ("ALIGNED" if accepted else "INITIAL GEOMETRY KEPT")
|
| 958 |
+
status = "\n".join([
|
| 959 |
+
"%s; joint continuous X/Y/width/height registration; %s."
|
| 960 |
+
% (state, "solver converged numerically" if converged else "inspect solver notes"),
|
| 961 |
+
"Crop: top_left=(%d,%d), size=%dx%d, similarity=%.8f/200"
|
| 962 |
+
% (crop_x, crop_y, crop_width, crop_height, 200.0 * (1.0 - crop_error)),
|
| 963 |
+
"Crop match mode: " + crop_match_mode,
|
| 964 |
+
"Crop acceptance: min_tolerance=%.6f (%.6f/200); highest score selected before threshold check."
|
| 965 |
+
% (float(min_tolerance), min_crop_similarity),
|
| 966 |
+
input_diagnostics,
|
| 967 |
+
"Overlay: top_left=(%.8f,%.8f), size=(%.8f,%.8f), center=(%.8f,%.8f)"
|
| 968 |
+
% (x, y, width, height, center_x, center_y),
|
| 969 |
+
"Rendered similarity=%.8f/200; registration similarity=%.8f/200; robust cost %.10g -> %.10g"
|
| 970 |
+
% (similarity, registration_similarity, base_cost, fit_cost),
|
| 971 |
+
"Registration uses alpha-weighted soft-L1, fixed source samples, and a coarse-to-fine pyramid."
|
| 972 |
+
" Robust cost is the fitted objective; raw similarity may decrease when changed pixels are rejected as outliers.",
|
| 973 |
+
"Matching image=%dx%d; residual calls=%d; render_bbox=(%d,%d,%d,%d); seconds=%.3f"
|
| 974 |
+
% (match_width, match_height, total_calls, paste_x, paste_y, patch.shape[1], patch.shape[0], time.monotonic() - started),
|
| 975 |
+
"EWA LanczosSharp: direct original-source rendering with fractional geometry; no rotation/shear/optical flow.",
|
| 976 |
+
*notices, "Pyramid trace:", *trace,
|
| 977 |
+
])
|
| 978 |
+
progress(100)
|
| 979 |
+
_LOG.info("Salia Joint Align: center=(%.5f,%.5f), size=(%.5f,%.5f), accepted=%s, cost=%g -> %g",
|
| 980 |
+
center_x, center_y, width, height, accepted, base_cost, fit_cost)
|
| 981 |
+
return {"composite": composite, "overlay_canvas": canvas, "status": status,
|
| 982 |
+
"x": float(x), "y": float(y), "width": float(width), "height": float(height),
|
| 983 |
+
"center_x": float(center_x), "center_y": float(center_y), "similarity": similarity,
|
| 984 |
+
"registration_similarity": registration_similarity, "robust_cost": fit_cost,
|
| 985 |
+
"initial_robust_cost": base_cost, "converged": converged, "accepted": accepted,
|
| 986 |
+
"trace": trace, "parameters": parameters.copy(), "crop_x": crop_x, "crop_y": crop_y,
|
| 987 |
+
"location_verified": True, "crop_similarity": float(crop_similarity),
|
| 988 |
+
"crop_match_mode": crop_match_mode}
|
| 989 |
+
|
| 990 |
+
|
| 991 |
+
|
| 992 |
+
# Internal seam engine (included, no sibling imports).
|
| 993 |
+
|
| 994 |
+
import time
|
| 995 |
+
|
| 996 |
+
import numpy as np
|
| 997 |
+
from scipy.ndimage import binary_erosion, distance_transform_edt, zoom
|
| 998 |
+
|
| 999 |
+
_EPS = 1.0e-8
|
| 1000 |
+
_TOL = 2.0e-4
|
| 1001 |
+
_METHODS = ("Graph cut + feather", "Border feather (diagnostic)", "Bypass (alignment result)")
|
| 1002 |
+
|
| 1003 |
+
|
| 1004 |
+
def _array(value):
|
| 1005 |
+
if hasattr(value, "detach"):
|
| 1006 |
+
return value.detach().cpu().float().numpy()
|
| 1007 |
+
return np.asarray(value)
|
| 1008 |
+
|
| 1009 |
+
|
| 1010 |
+
def _image(value, name):
|
| 1011 |
+
image = _array(value)
|
| 1012 |
+
if image.ndim == 4 and image.shape[0] == 1:
|
| 1013 |
+
image = image[0]
|
| 1014 |
+
if image.ndim != 3 or image.shape[-1] not in (3, 4) or min(image.shape[:2]) < 1:
|
| 1015 |
+
raise ValueError(name + " must be one IMAGE [1,H,W,3 or 4]; split batches upstream.")
|
| 1016 |
+
if not np.issubdtype(image.dtype, np.floating) or not np.isfinite(image).all():
|
| 1017 |
+
raise ValueError(name + " must contain finite floating point image pixels.")
|
| 1018 |
+
return np.ascontiguousarray(np.clip(image, 0.0, 1.0), dtype=np.float32)
|
| 1019 |
+
|
| 1020 |
+
|
| 1021 |
+
def _rgba(image):
|
| 1022 |
+
if image.shape[-1] == 4:
|
| 1023 |
+
return image.copy()
|
| 1024 |
+
return np.concatenate((image, np.ones((*image.shape[:2], 1), np.float32)), axis=-1)
|
| 1025 |
+
|
| 1026 |
+
|
| 1027 |
+
def _load_mask(value, shape):
|
| 1028 |
+
mask = _array(value)
|
| 1029 |
+
if mask.ndim == 3 and mask.shape[0] == 1:
|
| 1030 |
+
mask = mask[0]
|
| 1031 |
+
if mask.ndim != 2 or not np.isfinite(mask).all():
|
| 1032 |
+
raise ValueError("background_mask must be one finite Load Image MASK [1,H,W].")
|
| 1033 |
+
if mask.shape != shape:
|
| 1034 |
+
if mask.shape == (64, 64) and not np.any(mask):
|
| 1035 |
+
return np.zeros(shape, np.float32)
|
| 1036 |
+
raise ValueError("background_mask dimensions must match the original background; do not resize/invert it.")
|
| 1037 |
+
return np.clip(mask, 0.0, 1.0).astype(np.float32)
|
| 1038 |
+
|
| 1039 |
+
|
| 1040 |
+
def _blend_source_over(background, overlay):
|
| 1041 |
+
# Match the ATTACHED working node: straight-alpha source-over in sRGB.
|
| 1042 |
+
sa, ba = overlay[..., 3:4], background[..., 3:4]
|
| 1043 |
+
alpha = sa + ba * (1.0 - sa)
|
| 1044 |
+
p = overlay[..., :3] * sa + background[..., :3] * ba * (1.0 - sa)
|
| 1045 |
+
rgb = np.zeros_like(p)
|
| 1046 |
+
np.divide(p, alpha, out=rgb, where=alpha > _EPS)
|
| 1047 |
+
result = np.concatenate((np.clip(rgb, 0.0, 1.0), alpha), axis=-1)
|
| 1048 |
+
return np.where(sa > 0.0, result, background).astype(np.float32)
|
| 1049 |
+
|
| 1050 |
+
|
| 1051 |
+
def _fields(rgba):
|
| 1052 |
+
return np.concatenate((rgba[..., :3] * rgba[..., 3:4], rgba[..., 3:4]), axis=-1)
|
| 1053 |
+
|
| 1054 |
+
|
| 1055 |
+
def _visible_error(first, second):
|
| 1056 |
+
# Hidden RGB in fully transparent pixels cannot invalidate good wiring.
|
| 1057 |
+
return float(np.max(np.abs(_fields(first) - _fields(second)), initial=0.0))
|
| 1058 |
+
|
| 1059 |
+
|
| 1060 |
+
def _linear(rgb):
|
| 1061 |
+
return np.where(rgb <= 0.04045, rgb / 12.92, ((rgb + 0.055) / 1.055) ** 2.4).astype(np.float32)
|
| 1062 |
+
|
| 1063 |
+
|
| 1064 |
+
def _srgb(rgb):
|
| 1065 |
+
rgb = np.clip(rgb, 0.0, 1.0)
|
| 1066 |
+
return np.where(rgb <= 0.0031308, rgb * 12.92, 1.055 * rgb ** (1.0 / 2.4) - 0.055).astype(np.float32)
|
| 1067 |
+
|
| 1068 |
+
|
| 1069 |
+
def _smooth(value):
|
| 1070 |
+
value = np.clip(value, 0.0, 1.0)
|
| 1071 |
+
return value * value * (3.0 - 2.0 * value)
|
| 1072 |
+
|
| 1073 |
+
|
| 1074 |
+
def _geometry(shape, x, y, width, height, x0=0, y0=0):
|
| 1075 |
+
h, w = shape
|
| 1076 |
+
xx = x0 + np.arange(w, dtype=np.float64)
|
| 1077 |
+
yy = y0 + np.arange(h, dtype=np.float64)
|
| 1078 |
+
cx = np.maximum(0.0, np.minimum(xx + 1.0, x + width) - np.maximum(xx, x))
|
| 1079 |
+
cy = np.maximum(0.0, np.minimum(yy + 1.0, y + height) - np.maximum(yy, y))
|
| 1080 |
+
support = (cy[:, None] > 0.0) & (cx[None, :] > 0.0)
|
| 1081 |
+
dx = np.minimum(xx + 0.5 - x, x + width - xx - 0.5)
|
| 1082 |
+
dy = np.minimum(yy + 0.5 - y, y + height - yy - 0.5)
|
| 1083 |
+
return support, np.minimum(dy[:, None], dx[None, :])
|
| 1084 |
+
|
| 1085 |
+
|
| 1086 |
+
def _resize_nearest(image, shape):
|
| 1087 |
+
iy = np.minimum(image.shape[0] - 1, (np.arange(shape[0]) * image.shape[0] / shape[0]).astype(np.intp))
|
| 1088 |
+
ix = np.minimum(image.shape[1] - 1, (np.arange(shape[1]) * image.shape[1] / shape[1]).astype(np.intp))
|
| 1089 |
+
return image[iy[:, None], ix[None, :]]
|
| 1090 |
+
|
| 1091 |
+
|
| 1092 |
+
def _graph_mask(error, core, frame, interrupt):
|
| 1093 |
+
"""Exact binary minimum cut via SciPy's compiled sparse max-flow solver."""
|
| 1094 |
+
from scipy.sparse import csr_matrix
|
| 1095 |
+
from scipy.sparse.csgraph import maximum_flow, breadth_first_order
|
| 1096 |
+
error = np.asarray(error, dtype=np.float64)
|
| 1097 |
+
core, frame = np.asarray(core, dtype=bool), np.asarray(frame, dtype=bool)
|
| 1098 |
+
if (error.ndim != 2 or min(error.shape) < 1
|
| 1099 |
+
or core.shape != error.shape or frame.shape != error.shape
|
| 1100 |
+
or not np.isfinite(error).all() or np.any(error < 0.0)):
|
| 1101 |
+
raise ValueError("Graph cost/core/frame must be matching finite nonnegative HxW arrays.")
|
| 1102 |
+
if np.any(core & frame) or not np.any(core) or not np.any(frame):
|
| 1103 |
+
raise ValueError("Graph needs nonoverlapping mandatory composite and background seeds.")
|
| 1104 |
+
interrupt()
|
| 1105 |
+
h, w = error.shape
|
| 1106 |
+
n = h * w
|
| 1107 |
+
error = np.clip(error, 0.0, 1.0)
|
| 1108 |
+
ids = np.arange(n, dtype=np.int32).reshape(h, w)
|
| 1109 |
+
horizontal, vertical = ids[:, :-1].reshape(-1), ids[:-1, :].reshape(-1)
|
| 1110 |
+
u = np.concatenate((horizontal, vertical))
|
| 1111 |
+
v = np.concatenate((horizontal + 1, vertical + w))
|
| 1112 |
+
scale = 4096
|
| 1113 |
+
cost = np.concatenate((
|
| 1114 |
+
1 + np.rint(scale * (error[:, :-1] + error[:, 1:])).astype(np.int32).reshape(-1),
|
| 1115 |
+
1 + np.rint(scale * (error[:-1, :] + error[1:, :])).astype(np.int32).reshape(-1)))
|
| 1116 |
+
core_ids, frame_ids = np.flatnonzero(core).astype(np.int32), np.flatnonzero(frame).astype(np.int32)
|
| 1117 |
+
# A seed can save at most four incident grid costs by changing labels.
|
| 1118 |
+
# Strictly greater terminals force the required label without huge
|
| 1119 |
+
# near-INT_MAX capacities. Aggregate flow may exceed 32-bit safely.
|
| 1120 |
+
terminal, source, sink = 4 * (1 + 2 * scale) + 1, n, n + 1
|
| 1121 |
+
rows = np.concatenate((u, v, np.full(core_ids.size, source, np.int32), frame_ids))
|
| 1122 |
+
cols = np.concatenate((v, u, core_ids, np.full(frame_ids.size, sink, np.int32)))
|
| 1123 |
+
data = np.concatenate((cost, cost, np.full(core_ids.size + frame_ids.size, terminal, np.int32)))
|
| 1124 |
+
capacity = csr_matrix((data, (rows, cols)), shape=(n + 2, n + 2), dtype=np.int32)
|
| 1125 |
+
del rows, cols, data, u, v, cost, ids, horizontal, vertical
|
| 1126 |
+
interrupt()
|
| 1127 |
+
flow = maximum_flow(capacity, source, sink, method="dinic")
|
| 1128 |
+
interrupt()
|
| 1129 |
+
residual = capacity - flow.flow
|
| 1130 |
+
residual.data[residual.data <= 0] = 0
|
| 1131 |
+
residual.eliminate_zeros()
|
| 1132 |
+
reachable = breadth_first_order(residual, source, directed=True, return_predecessors=False)
|
| 1133 |
+
selected = np.zeros(n + 2, dtype=bool)
|
| 1134 |
+
selected[reachable] = True
|
| 1135 |
+
selected = selected[:n].reshape(h, w)
|
| 1136 |
+
if not np.all(selected[core]) or np.any(selected[frame]):
|
| 1137 |
+
raise RuntimeError("SciPy graph-cut violated a mandatory label.")
|
| 1138 |
+
interrupt()
|
| 1139 |
+
return np.ascontiguousarray(selected)
|
| 1140 |
+
|
| 1141 |
+
|
| 1142 |
+
def _bounded_graph(error, core, frame, max_side, interrupt):
|
| 1143 |
+
h, w = error.shape
|
| 1144 |
+
scale = min(1.0, max_side / max(h, w))
|
| 1145 |
+
gh, gw = max(1, int(round(h * scale))), max(1, int(round(w * scale)))
|
| 1146 |
+
if (gh, gw) == (h, w):
|
| 1147 |
+
return _graph_mask(error, core, frame, interrupt), (gw, gh)
|
| 1148 |
+
# Evaluate mask constraints at cell centers, then restore original hard
|
| 1149 |
+
# constraints at full resolution; tiny mandatory seed sets must survive.
|
| 1150 |
+
small_error = zoom(error, (gh / h, gw / w), order=1, prefilter=False)
|
| 1151 |
+
small_core = _resize_nearest(core, (gh, gw))
|
| 1152 |
+
small_frame = _resize_nearest(frame, (gh, gw)) & ~small_core
|
| 1153 |
+
for original, reduced, other in ((core, small_core, small_frame), (frame, small_frame, small_core)):
|
| 1154 |
+
if np.any(original) and not np.any(reduced):
|
| 1155 |
+
points = np.argwhere(original)
|
| 1156 |
+
py, px = points[len(points) // 2]
|
| 1157 |
+
gy, gx = min(gh - 1, int(py * gh / h)), min(gw - 1, int(px * gw / w))
|
| 1158 |
+
if not other[gy, gx]:
|
| 1159 |
+
reduced[gy, gx] = True
|
| 1160 |
+
if not np.any(small_core) or not np.any(small_frame):
|
| 1161 |
+
raise RuntimeError("Graph reduction removed a mandatory seed; using border feather.")
|
| 1162 |
+
selected = _graph_mask(small_error, small_core, small_frame, interrupt)
|
| 1163 |
+
selected = _resize_nearest(selected, (h, w)).astype(bool)
|
| 1164 |
+
selected[core], selected[frame] = True, False
|
| 1165 |
+
return selected, (gw, gh)
|
| 1166 |
+
|
| 1167 |
+
|
| 1168 |
+
def _cost(background, reference):
|
| 1169 |
+
# Visible disagreement over black AND white; alpha and hidden RGB never
|
| 1170 |
+
# masquerade as a matching color. Add outline disagreement to avoid cuts
|
| 1171 |
+
# through detail when a nearby low-error route exists.
|
| 1172 |
+
a, b = _fields(background), _fields(reference)
|
| 1173 |
+
dark = a[..., :3] - b[..., :3]
|
| 1174 |
+
light = dark - (a[..., 3:4] - b[..., 3:4])
|
| 1175 |
+
error = np.sqrt(np.mean(dark * dark + light * light, axis=-1) * 0.5)
|
| 1176 |
+
if min(error.shape) >= 2:
|
| 1177 |
+
ay, ax = np.gradient(a, axis=(0, 1))
|
| 1178 |
+
by, bx = np.gradient(b, axis=(0, 1))
|
| 1179 |
+
error += 0.35 * np.sqrt(np.mean((ay - by) ** 2 + (ax - bx) ** 2, axis=-1))
|
| 1180 |
+
return np.clip(error, 0.0, 1.0).astype(np.float32)
|
| 1181 |
+
|
| 1182 |
+
|
| 1183 |
+
def _soft_seam(hard, core, frame, distance, band, radius, use_graph):
|
| 1184 |
+
if use_graph:
|
| 1185 |
+
if radius > 0.0 and np.any(hard) and np.any(~hard):
|
| 1186 |
+
signed = distance_transform_edt(hard) - distance_transform_edt(~hard)
|
| 1187 |
+
signed = np.sign(signed) * np.maximum(np.abs(signed) - 0.5, 0.0)
|
| 1188 |
+
weight = _smooth(0.5 + signed / (2.0 * radius))
|
| 1189 |
+
# Smoothly meet fixed endpoints even when the seam runs beside
|
| 1190 |
+
# the protected core; no sudden restamping jump at its boundary.
|
| 1191 |
+
if np.any(core) and np.any(~core):
|
| 1192 |
+
weight = np.maximum(weight, _smooth(1.0 - distance_transform_edt(~core) / radius))
|
| 1193 |
+
if np.any(frame) and np.any(~frame):
|
| 1194 |
+
weight = np.minimum(weight, _smooth(distance_transform_edt(~frame) / radius))
|
| 1195 |
+
else:
|
| 1196 |
+
weight = hard.astype(np.float32)
|
| 1197 |
+
else:
|
| 1198 |
+
weight = _smooth((distance - 0.75) / max(band - 0.75, 0.25))
|
| 1199 |
+
weight[core], weight[frame] = 1.0, 0.0
|
| 1200 |
+
return np.clip(weight, 0.0, 1.0).astype(np.float32)
|
| 1201 |
+
|
| 1202 |
+
|
| 1203 |
+
def _mix_color(background, reference, weight):
|
| 1204 |
+
# Blend visible colors with consistent premultiplied normalization. Keep
|
| 1205 |
+
# the KNOWN-GOOD reference alpha, without applying canvas alpha again.
|
| 1206 |
+
t = weight[..., None]
|
| 1207 |
+
ba, ra = background[..., 3:4], reference[..., 3:4]
|
| 1208 |
+
alpha = (1.0 - t) * ba + t * ra
|
| 1209 |
+
p = (1.0 - t) * _linear(background[..., :3]) * ba + t * _linear(reference[..., :3]) * ra
|
| 1210 |
+
color = np.zeros_like(p)
|
| 1211 |
+
np.divide(p, alpha, out=color, where=alpha > _EPS)
|
| 1212 |
+
out = reference.copy()
|
| 1213 |
+
out[..., :3] = _srgb(color)
|
| 1214 |
+
out[weight <= 0.0, :3] = background[weight <= 0.0, :3]
|
| 1215 |
+
out[weight >= 1.0] = reference[weight >= 1.0]
|
| 1216 |
+
identical = np.all(background[..., :3] == reference[..., :3], axis=-1)
|
| 1217 |
+
out[identical] = reference[identical]
|
| 1218 |
+
out[reference[..., 3] <= _EPS] = reference[reference[..., 3] <= _EPS]
|
| 1219 |
+
out[..., 3] = reference[..., 3]
|
| 1220 |
+
return np.ascontiguousarray(out)
|
| 1221 |
+
|
| 1222 |
+
|
| 1223 |
+
def _checker(rgba):
|
| 1224 |
+
h, w = rgba.shape[:2]
|
| 1225 |
+
yy, xx = np.ogrid[:h, :w]
|
| 1226 |
+
gray = np.where(((xx // 16 + yy // 16) % 2) == 0, 0.32, 0.58).astype(np.float32)
|
| 1227 |
+
a = rgba[..., 3:4]
|
| 1228 |
+
return np.ascontiguousarray(rgba[..., :3] * a + gray[..., None] * (1.0 - a), dtype=np.float32)
|
| 1229 |
+
|
| 1230 |
+
|
| 1231 |
+
def _gray(value):
|
| 1232 |
+
return np.repeat(np.clip(value, 0.0, 1.0)[..., None], 3, axis=-1).astype(np.float32)
|
| 1233 |
+
|
| 1234 |
+
|
| 1235 |
+
def _edge(mask):
|
| 1236 |
+
# Outside-canvas is not a new rectangle border. Geometry handles clipping.
|
| 1237 |
+
return mask & ~binary_erosion(mask, border_value=1)
|
| 1238 |
+
|
| 1239 |
+
|
| 1240 |
+
def _make_result(reference, overlay, result, weight, support, core, hard, invalid, lines):
|
| 1241 |
+
seam = _checker(result)
|
| 1242 |
+
seam[_edge(support)] = (1.0, 0.8, 0.0)
|
| 1243 |
+
seam[_edge(core)] = (0.0, 0.8, 1.0)
|
| 1244 |
+
seam[_edge(hard) & support] = (0.0, 1.0, 0.0)
|
| 1245 |
+
seam[invalid] = (1.0, 0.0, 1.0)
|
| 1246 |
+
# Difference on a FIXED matte so checker tiles do not hide subtle errors.
|
| 1247 |
+
ra, oa = reference[..., 3:4], result[..., 3:4]
|
| 1248 |
+
before = reference[..., :3] * ra + 0.5 * (1.0 - ra)
|
| 1249 |
+
after = result[..., :3] * oa + 0.5 * (1.0 - oa)
|
| 1250 |
+
return {
|
| 1251 |
+
"result_rgba": result, "status": "\n".join(lines),
|
| 1252 |
+
"debug_result_rgb": _checker(result), "debug_alignment_rgb": _checker(reference),
|
| 1253 |
+
"debug_overlay_rgb": _checker(overlay), "debug_overlay_alpha_rgb": _gray(overlay[..., 3]),
|
| 1254 |
+
"debug_blend_weight_rgb": _gray(weight), "debug_seam_rgb": seam,
|
| 1255 |
+
"debug_difference_x8_rgb": np.clip(np.abs(after - before) * 8.0, 0.0, 1.0).astype(np.float32),
|
| 1256 |
+
"output_mask": (1.0 - result[..., 3]).astype(np.float32),
|
| 1257 |
+
"weight": weight, "core": core, "support": support,
|
| 1258 |
+
}
|
| 1259 |
+
|
| 1260 |
+
|
| 1261 |
+
def blend_aligned_arrays(background, composite_rgba, overlay_canvas_rgba, x, y, width, height,
|
| 1262 |
+
seam_band_px=24.0, feather_px=3.0, strength=1.0,
|
| 1263 |
+
method=_METHODS[0], graph_max_side=512, background_mask=None,
|
| 1264 |
+
alignment_status=None, interrupt=lambda: None, progress=lambda value: None):
|
| 1265 |
+
"""Pure NumPy core; same alpha semantics as the supplied working aligner."""
|
| 1266 |
+
started = time.monotonic()
|
| 1267 |
+
raw_bg = _image(background, "background")
|
| 1268 |
+
raw_ref = _image(composite_rgba, "composite_rgba")
|
| 1269 |
+
raw_ov = _image(overlay_canvas_rgba, "overlay_canvas_rgba")
|
| 1270 |
+
reference, overlay = _rgba(raw_ref), _rgba(raw_ov)
|
| 1271 |
+
h, w = reference.shape[:2]
|
| 1272 |
+
empty = np.zeros((h, w), dtype=bool)
|
| 1273 |
+
support, core, hard, invalid = empty.copy(), empty.copy(), empty.copy(), empty.copy()
|
| 1274 |
+
weight = np.zeros((h, w), dtype=np.float32)
|
| 1275 |
+
errors, notes = [], []
|
| 1276 |
+
lines = ["Internal seam stage", "Alpha policy: preserve composite_rgba alpha EXACTLY; transparent overlay pixels never erase background."]
|
| 1277 |
+
for name, array in (("background", raw_bg), ("composite_rgba", raw_ref), ("overlay_canvas_rgba", raw_ov)):
|
| 1278 |
+
alpha = array[..., 3] if array.shape[-1] == 4 else np.ones(array.shape[:2])
|
| 1279 |
+
lines.append("%s: %dx%dx%d; alpha min=%.6f max=%.6f zero=%.2f%%."
|
| 1280 |
+
% (name, array.shape[1], array.shape[0], array.shape[2], alpha.min(), alpha.max(), 100.0 * np.mean(alpha <= _EPS)))
|
| 1281 |
+
def finish(headline, result=None):
|
| 1282 |
+
progress(100)
|
| 1283 |
+
shown_overlay = overlay if overlay.shape == reference.shape else np.zeros_like(reference)
|
| 1284 |
+
out = reference.copy() if result is None else result
|
| 1285 |
+
if result is None:
|
| 1286 |
+
# An early/error bypass selects the supplied aligned result,
|
| 1287 |
+
# not the background. Make its debug weight tell the truth.
|
| 1288 |
+
weight[:] = 1.0
|
| 1289 |
+
text = [headline, *lines, *["ERROR: " + s for s in errors], *["NOTE: " + s for s in notes],
|
| 1290 |
+
"seconds=%.3f" % (time.monotonic() - started)]
|
| 1291 |
+
if alignment_status:
|
| 1292 |
+
text += ["UPSTREAM ALIGNMENT STATUS:", str(alignment_status)[:16000]]
|
| 1293 |
+
return _make_result(reference, shown_overlay, out, weight, support, core, hard, invalid, text)
|
| 1294 |
+
if raw_ref.shape[-1] != 4 or raw_ov.shape[-1] != 4:
|
| 1295 |
+
errors.append("Both aligned image outputs must retain 4 embedded RGBA channels. Connect them directly from the working alignment node, without RGB conversion/reloading/mask edits.")
|
| 1296 |
+
if raw_bg.shape[:2] != (h, w) or overlay.shape[:2] != (h, w):
|
| 1297 |
+
errors.append("All three images must have the same full-canvas dimensions; use overlay_canvas_rgba, not the small original overlay.")
|
| 1298 |
+
if errors:
|
| 1299 |
+
return finish("ERROR / BYPASS: invalid image connections; displaying the supplied alignment result.")
|
| 1300 |
+
values = np.asarray((x, y, width, height, seam_band_px, feather_px, strength), np.float64)
|
| 1301 |
+
if not np.isfinite(values).all() or min(width, height, seam_band_px, feather_px) < 0.0 or not 0.0 <= strength <= 1.0:
|
| 1302 |
+
errors.append("Coordinates must be finite; sizes/band/feather must be nonnegative; strength must be 0..1.")
|
| 1303 |
+
return finish("ERROR / BYPASS: invalid coordinates or controls.")
|
| 1304 |
+
x, y, width, height, seam_band_px, feather_px, strength = map(float, values)
|
| 1305 |
+
if method not in _METHODS or int(graph_max_side) != graph_max_side or not 64 <= graph_max_side <= 1024:
|
| 1306 |
+
errors.append("Unknown method or graph_max_side outside integer range 64..1024.")
|
| 1307 |
+
return finish("ERROR / BYPASS: invalid blending controls.")
|
| 1308 |
+
lines.append("Geometry: x=%.8f y=%.8f width=%.8f height=%.8f (TOP LEFT pixel edges)." % (x, y, width, height))
|
| 1309 |
+
if width == 0.0 or height == 0.0:
|
| 1310 |
+
return finish("BYPASS: upstream zero-sized alignment/failure coordinates; alignment result retained.")
|
| 1311 |
+
support, distance = _geometry((h, w), x, y, width, height)
|
| 1312 |
+
invalid = ~support & (overlay[..., 3] > 2.0e-6)
|
| 1313 |
+
if np.any(invalid):
|
| 1314 |
+
errors.append("Overlay has %d visible pixels outside x/y/width/height. Coordinates and overlay must come from the SAME alignment run; debug_seam marks them magenta." % np.count_nonzero(invalid))
|
| 1315 |
+
bg = _rgba(raw_bg)
|
| 1316 |
+
ambiguous = np.zeros((h, w), dtype=bool)
|
| 1317 |
+
if raw_bg.shape[-1] == 4:
|
| 1318 |
+
notes.append("Background alpha: embedded RGBA, authoritative; optional background_mask ignored.")
|
| 1319 |
+
elif background_mask is not None:
|
| 1320 |
+
try:
|
| 1321 |
+
bg[..., 3] = 1.0 - _load_mask(background_mask, (h, w))
|
| 1322 |
+
notes.append("Background alpha: SAME standard Load Image MASK; converted internally as 1-mask. No external inversion.")
|
| 1323 |
+
except ValueError as exc:
|
| 1324 |
+
errors.append(str(exc))
|
| 1325 |
+
else:
|
| 1326 |
+
sa, ca = overlay[..., 3], reference[..., 3]
|
| 1327 |
+
known = sa < 1.0 - 1.0e-6
|
| 1328 |
+
recovered = np.zeros((h, w), np.float32)
|
| 1329 |
+
np.divide(ca - sa, 1.0 - sa, out=recovered, where=known)
|
| 1330 |
+
if np.any(known & ((recovered < -_TOL) | (recovered > 1.0 + _TOL))):
|
| 1331 |
+
errors.append("Composite/canvas alpha cannot come from source-over of the same background; check aligned image connections.")
|
| 1332 |
+
bg[..., 3] = np.where(known, np.clip(recovered, 0.0, 1.0), 1.0)
|
| 1333 |
+
unknown = ~known & support
|
| 1334 |
+
transparent_evidence = np.any(known & (recovered < 0.999))
|
| 1335 |
+
ambiguous = unknown if transparent_evidence else np.zeros_like(unknown)
|
| 1336 |
+
notes.append("Background RGB alpha reconstructed from the aligned outputs where possible; fully covered unknown pixels=%d." % np.count_nonzero(unknown))
|
| 1337 |
+
if np.any(ambiguous):
|
| 1338 |
+
notes.append("Transparent background detected; %d ambiguous fully covered pixels keep the working composite. For complete border blending there, connect the SAME background Load Image MASK unchanged." % np.count_nonzero(ambiguous))
|
| 1339 |
+
expected = _blend_source_over(bg, overlay)
|
| 1340 |
+
recomposition_error = _visible_error(expected, reference)
|
| 1341 |
+
lines.append("Source-over connection check: max visible RGBA error=%.9g; tolerance=%.1g." % (recomposition_error, _TOL))
|
| 1342 |
+
if recomposition_error > _TOL:
|
| 1343 |
+
errors.append("Composite is inconsistent with background + overlay canvas. Common causes: wrong background, swapped aligned images, differing alignment runs, or an inverted/different background MASK.")
|
| 1344 |
+
if errors:
|
| 1345 |
+
return finish("ERROR / BYPASS: inconsistent alignment connections; alignment result retained.")
|
| 1346 |
+
progress(15)
|
| 1347 |
+
if not np.any(support):
|
| 1348 |
+
return finish("BYPASS: rectangle is outside the canvas; alignment result retained.")
|
| 1349 |
+
if method == _METHODS[2] or strength == 0.0 or seam_band_px == 0.0:
|
| 1350 |
+
weight[support] = 1.0
|
| 1351 |
+
core, hard = support.copy(), support.copy()
|
| 1352 |
+
return finish("BYPASS: explicitly keeping the working alignment result.")
|
| 1353 |
+
# Work on a small ROI, preserving full-resolution output/debug canvases.
|
| 1354 |
+
yy, xx = np.nonzero(support)
|
| 1355 |
+
x0, y0 = max(0, int(xx.min()) - 1), max(0, int(yy.min()) - 1)
|
| 1356 |
+
x1, y1 = min(w, int(xx.max()) + 2), min(h, int(yy.max()) + 2)
|
| 1357 |
+
sl = np.s_[y0:y1, x0:x1]
|
| 1358 |
+
valid, d = support[sl], distance[sl]
|
| 1359 |
+
band = min(seam_band_px, 0.45 * min(width, height))
|
| 1360 |
+
radius = min(feather_px, band / 3.0)
|
| 1361 |
+
keep = valid & ((d >= band) | ambiguous[sl])
|
| 1362 |
+
# New visible parts over genuinely transparent background have no old
|
| 1363 |
+
# color to blend with; keep their known-good aligned appearance.
|
| 1364 |
+
keep |= valid & (bg[sl][..., 3] <= _EPS) & (reference[sl][..., 3] > _EPS)
|
| 1365 |
+
if not np.any(keep):
|
| 1366 |
+
index = np.argmax(np.where(valid, d, -np.inf))
|
| 1367 |
+
keep.flat[index] = True
|
| 1368 |
+
frame = ((~valid) | (d <= 0.75)) & ~keep
|
| 1369 |
+
chosen = keep.copy()
|
| 1370 |
+
use_graph = method == _METHODS[0]
|
| 1371 |
+
graph_shape = (0, 0)
|
| 1372 |
+
if not np.any(frame):
|
| 1373 |
+
chosen = valid.copy()
|
| 1374 |
+
notes.append("No original rectangle border is visible on this clipped canvas; retained the working composite.")
|
| 1375 |
+
elif use_graph:
|
| 1376 |
+
interrupt()
|
| 1377 |
+
try:
|
| 1378 |
+
chosen, graph_shape = _bounded_graph(_cost(bg[sl], reference[sl]), keep, frame, int(graph_max_side), interrupt)
|
| 1379 |
+
except (RuntimeError, ValueError, MemoryError, OverflowError) as exc:
|
| 1380 |
+
use_graph = False
|
| 1381 |
+
notes.append("GRAPH FALLBACK to border feather: %s: %s" % (type(exc).__name__, str(exc)))
|
| 1382 |
+
progress(65)
|
| 1383 |
+
chosen &= valid
|
| 1384 |
+
chosen[keep], chosen[frame] = True, False
|
| 1385 |
+
soft = (_soft_seam(chosen, keep, frame, d, band, radius, use_graph)
|
| 1386 |
+
if np.any(frame) else valid.astype(np.float32))
|
| 1387 |
+
soft[~valid] = 0.0
|
| 1388 |
+
soft[valid] = 1.0 - strength * (1.0 - soft[valid])
|
| 1389 |
+
weight[sl] = soft
|
| 1390 |
+
core[sl], hard[sl] = keep, chosen
|
| 1391 |
+
result = reference.copy()
|
| 1392 |
+
mixed = _mix_color(bg[sl], reference[sl], soft)
|
| 1393 |
+
result[sl] = np.where(valid[..., None], mixed, reference[sl])
|
| 1394 |
+
interrupt()
|
| 1395 |
+
alpha_error = float(np.max(np.abs(result[..., 3] - reference[..., 3])))
|
| 1396 |
+
center_error = float(np.max(np.abs(result[core] - reference[core]), initial=0.0))
|
| 1397 |
+
outside_error = float(np.max(np.abs(result[~support] - reference[~support]), initial=0.0))
|
| 1398 |
+
difference = np.max(np.abs(_fields(result) - _fields(reference)), axis=-1)
|
| 1399 |
+
changed = int(np.count_nonzero(difference > 1.0e-6))
|
| 1400 |
+
lines += [
|
| 1401 |
+
"Engine: %s; SciPy max-flow graph=%dx%d; ROI=(%d,%d)-(%d,%d)." % (method, *graph_shape, x0, y0, x1, y1),
|
| 1402 |
+
"Effective band=%.3fpx feather=%.3fpx strength=%.3f; protected=%d; visibly changed=%d pixels." % (band, radius, strength, np.count_nonzero(core), changed),
|
| 1403 |
+
"Invariants: alpha error=%.9g; protected-center error=%.9g; outside-rectangle error=%.9g (all must be 0)." % (alpha_error, center_error, outside_error),
|
| 1404 |
+
"Debug seam: yellow=rect, cyan=protected core, green=chosen seam, magenta=invalid canvas support.",
|
| 1405 |
+
]
|
| 1406 |
+
if changed == 0:
|
| 1407 |
+
notes.append("NO VISIBLE CHANGE: inspect alignment, overlay alpha, and blend weight previews; the border may already match or be fully protected.")
|
| 1408 |
+
if band != seam_band_px or radius != feather_px:
|
| 1409 |
+
notes.append("Band/feather reduced automatically to preserve a center in this rectangle.")
|
| 1410 |
+
if x < 0.0 or y < 0.0 or x + width > w or y + height > h:
|
| 1411 |
+
notes.append("Clipped placement: distances use the ORIGINAL fractional rect, not a fabricated canvas-edge border.")
|
| 1412 |
+
return finish("OK: blended aligned border colors; working composite alpha and center preserved.", result)
|
| 1413 |
+
|
| 1414 |
+
|
| 1415 |
+
|
| 1416 |
+
def _alignment_input_types():
|
| 1417 |
+
return {"required": {
|
| 1418 |
+
"background": ("IMAGE", {"tooltip": "Single full-size RGB/RGBA background."}),
|
| 1419 |
+
"overlay": ("IMAGE", {"tooltip": "Original full-resolution edited overlay. Final EWA render always uses this source."}),
|
| 1420 |
+
"Unedited_Rect": ("IMAGE", {"tooltip": "Original crop from background; locates initial center and default size."}),
|
| 1421 |
+
"target_width": ("INT", {"default": 0, "min": 0, "max": _MAX_DIMENSION, "step": 1,
|
| 1422 |
+
"tooltip": "INITIAL width; 0 uses Unedited_Rect width. Optimizer can refine it."}),
|
| 1423 |
+
"target_height": ("INT", {"default": 0, "min": 0, "max": _MAX_DIMENSION, "step": 1,
|
| 1424 |
+
"tooltip": "INITIAL height; 0 uses Unedited_Rect height. Optimized independently of width."}),
|
| 1425 |
+
"max_evaluations": ("INT", {"default": 100, "min": 5, "max": 1000, "step": 1,
|
| 1426 |
+
"tooltip": "Maximum solver function evaluations PER pyramid level. Status reports limits."}),
|
| 1427 |
+
"pyramid_levels": ("INT", {"default": 3, "min": 1, "max": 5, "step": 1,
|
| 1428 |
+
"tooltip": "Coarse-to-fine registration levels. 3 uses smoothing factors 4,2,1."}),
|
| 1429 |
+
"robust_threshold": ("FLOAT", {"default": 0.05, "min": 0.001, "max": 1.0, "step": 0.005,
|
| 1430 |
+
"tooltip": "Soft-L1 RGB threshold: smaller values reduce influence of large appearance edits."}),
|
| 1431 |
+
"scale_limit": ("FLOAT", {"default": 2.0, "min": 1.01, "max": 8.0, "step": 0.1,
|
| 1432 |
+
"tooltip": "Independent width/height ratio bounds: 2 permits 0.5x..2x initial dimensions. Not a pixel increment."}),
|
| 1433 |
+
"min_tolerance": ("FLOAT", {"default": 0.9, "min": 0.0, "max": 1.0, "step": 0.01,
|
| 1434 |
+
"tooltip": "Minimum crop similarity / 200. 0.9 = 180/200. Always searches for the highest score first; never stops at the first passing location."}),
|
| 1435 |
+
}, "optional": {
|
| 1436 |
+
"background_mask": ("MASK", {"tooltip": "RGB only: optional Load Image MASK (1-alpha). RGBA always uses embedded alpha and ignores this input."}),
|
| 1437 |
+
"overlay_mask": ("MASK", {"tooltip": "RGB only: optional Load Image MASK (1-alpha). RGBA always uses embedded alpha and ignores this input."}),
|
| 1438 |
+
"Unedited_Rect_mask": ("MASK", {"tooltip": "RGB only: optional Load Image MASK (1-alpha). RGBA always uses embedded alpha and ignores this input."}),
|
| 1439 |
+
}}
|
| 1440 |
+
|
| 1441 |
+
|
| 1442 |
+
class Salia_Auto_Align_Seam_Blend_GPT_2:
|
| 1443 |
+
@classmethod
|
| 1444 |
+
def INPUT_TYPES(cls):
|
| 1445 |
+
schema = _alignment_input_types()
|
| 1446 |
+
schema["required"].update({
|
| 1447 |
+
"seam_band_px": ("FLOAT", {"default": 24.0, "min": 0.0, "max": 512.0, "step": 1.0,
|
| 1448 |
+
"tooltip": "Width of border available for seam finding. Center protected. 0 keeps the internally aligned result."}),
|
| 1449 |
+
"feather_px": ("FLOAT", {"default": 3.0, "min": 0.0, "max": 64.0, "step": 0.5,
|
| 1450 |
+
"tooltip": "Softness of the chosen border seam; automatically capped for small overlays."}),
|
| 1451 |
+
"blend_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.05,
|
| 1452 |
+
"tooltip": "1=blend the border fully; 0=exact original alignment output for comparison."}),
|
| 1453 |
+
"blend_method": (list(_METHODS), {"default": _METHODS[0],
|
| 1454 |
+
"tooltip": "Graph cut + feather selects a content-aware seam. Diagnostic feather skips graph cut. Bypass shows alignment only."}),
|
| 1455 |
+
"graph_max_side": ("INT", {"default": 512, "min": 64, "max": 1024, "step": 64,
|
| 1456 |
+
"tooltip": "Graph resolution limit only; final image/debug outputs retain full resolution."}),
|
| 1457 |
+
})
|
| 1458 |
+
return schema
|
| 1459 |
+
|
| 1460 |
+
RETURN_TYPES = ("IMAGE", "STRING", "IMAGE", "IMAGE", "IMAGE", "IMAGE", "IMAGE", "IMAGE", "IMAGE", "IMAGE", "IMAGE", "MASK")
|
| 1461 |
+
RETURN_NAMES = ("result_rgba", "status", "aligned_rgba", "overlay_canvas_rgba", "debug_result_rgb",
|
| 1462 |
+
"debug_alignment_rgb", "debug_overlay_rgb", "debug_overlay_alpha_rgb",
|
| 1463 |
+
"debug_blend_weight_rgb", "debug_seam_rgb", "debug_difference_x8_rgb", "output_mask")
|
| 1464 |
+
FUNCTION = "process"
|
| 1465 |
+
CATEGORY = "Salia/Image"
|
| 1466 |
+
DESCRIPTION = (
|
| 1467 |
+
"ONE standalone node: original background + edited overlay + Unedited_Rect, with the same "
|
| 1468 |
+
"alignment controls/masks as the working aligner. Includes joint registration, EWA placement, "
|
| 1469 |
+
"automatic internal wiring, graph-cut seam blending, status and RGB debug previews. "
|
| 1470 |
+
"No separate alignment node or Python file is needed."
|
| 1471 |
+
)
|
| 1472 |
+
|
| 1473 |
+
def process(self, background, overlay, Unedited_Rect, target_width=0, target_height=0,
|
| 1474 |
+
max_evaluations=100, pyramid_levels=3, robust_threshold=0.05, scale_limit=2.0,
|
| 1475 |
+
min_tolerance=0.9, seam_band_px=24.0, feather_px=3.0, blend_strength=1.0,
|
| 1476 |
+
blend_method=_METHODS[0], graph_max_side=512,
|
| 1477 |
+
background_mask=None, overlay_mask=None, Unedited_Rect_mask=None):
|
| 1478 |
+
import torch
|
| 1479 |
+
try:
|
| 1480 |
+
from comfy.model_management import throw_exception_if_processing_interrupted as interrupt
|
| 1481 |
+
except ImportError:
|
| 1482 |
+
interrupt = lambda: None
|
| 1483 |
+
try:
|
| 1484 |
+
from comfy.utils import ProgressBar
|
| 1485 |
+
bar = ProgressBar(100)
|
| 1486 |
+
previous = [-1]
|
| 1487 |
+
def progress(value):
|
| 1488 |
+
value = max(previous[0], min(100, int(value)))
|
| 1489 |
+
if value != previous[0]:
|
| 1490 |
+
bar.update_absolute(value, 100)
|
| 1491 |
+
previous[0] = value
|
| 1492 |
+
except ImportError:
|
| 1493 |
+
progress = lambda value: None
|
| 1494 |
+
started = time.monotonic()
|
| 1495 |
+
with torch.inference_mode():
|
| 1496 |
+
interrupt()
|
| 1497 |
+
# Apply exactly the working node's input/alpha rules ONCE. Native
|
| 1498 |
+
# RGBA wins; RGB uses its matching standard Load Image MASK.
|
| 1499 |
+
prepared_background = _tensor_image(background, "background", background_mask)
|
| 1500 |
+
prepared_overlay = _tensor_image(overlay, "overlay", overlay_mask)
|
| 1501 |
+
prepared_unedited = _tensor_image(Unedited_Rect, "Unedited_Rect", Unedited_Rect_mask)
|
| 1502 |
+
registration = align_arrays(
|
| 1503 |
+
prepared_background, prepared_overlay, prepared_unedited,
|
| 1504 |
+
target_width=target_width, target_height=target_height,
|
| 1505 |
+
max_evaluations=max_evaluations, pyramid_levels=pyramid_levels,
|
| 1506 |
+
robust_threshold=robust_threshold, scale_limit=scale_limit,
|
| 1507 |
+
min_tolerance=min_tolerance, interrupt=interrupt,
|
| 1508 |
+
progress=lambda value: progress(0.75 * value),
|
| 1509 |
+
)
|
| 1510 |
+
interrupt()
|
| 1511 |
+
# Every item below is produced INTERNALLY by the embedded aligner.
|
| 1512 |
+
# The prepared background already has the correct alpha; no mask
|
| 1513 |
+
# reconstruction, inversion choice or external output is needed.
|
| 1514 |
+
result = blend_aligned_arrays(
|
| 1515 |
+
_rgba_array(prepared_background, "background"),
|
| 1516 |
+
registration["composite"], registration["overlay_canvas"],
|
| 1517 |
+
registration["x"], registration["y"], registration["width"], registration["height"],
|
| 1518 |
+
seam_band_px=seam_band_px, feather_px=feather_px, strength=blend_strength,
|
| 1519 |
+
method=blend_method, graph_max_side=graph_max_side,
|
| 1520 |
+
alignment_status=registration["status"], interrupt=interrupt,
|
| 1521 |
+
progress=lambda value: progress(75.0 + 0.25 * value),
|
| 1522 |
+
)
|
| 1523 |
+
result["aligned_rgba"] = registration["composite"]
|
| 1524 |
+
result["overlay_canvas_rgba"] = registration["overlay_canvas"]
|
| 1525 |
+
result["status"] = "\n".join([
|
| 1526 |
+
"Salia Auto Align + Seam Blend GPT_1 -- SELF-CONTAINED",
|
| 1527 |
+
"Source inputs: background, overlay, Unedited_Rect; alignment/coordinates/alpha/seam wiring handled internally.",
|
| 1528 |
+
"Total seconds=%.3f." % (time.monotonic() - started),
|
| 1529 |
+
result["status"],
|
| 1530 |
+
])
|
| 1531 |
+
output = []
|
| 1532 |
+
for name in self.RETURN_NAMES:
|
| 1533 |
+
if name == "status":
|
| 1534 |
+
output.append(result[name])
|
| 1535 |
+
else:
|
| 1536 |
+
output.append(torch.from_numpy(np.ascontiguousarray(result[name], dtype=np.float32)).unsqueeze(0))
|
| 1537 |
+
progress(100)
|
| 1538 |
+
return tuple(output)
|
| 1539 |
+
|
| 1540 |
+
|
| 1541 |
+
NODE_CLASS_MAPPINGS = {"Salia_Auto_Align_Seam_Blend_GPT_2": Salia_Auto_Align_Seam_Blend_GPT_2}
|
| 1542 |
+
NODE_DISPLAY_NAME_MAPPINGS = {"Salia_Auto_Align_Seam_Blend_GPT_2": "Salia_Auto_Align_Seam_Blend_GPT_2"}
|