multicolorfits.deblend_background#
- multicolorfits.deblend_background(rgb, background='white', gamma=1.0, tol=0.0058823529411764705, alpha_smooth=0.0, alpha_lo=None, alpha_hi=None, alpha_gamma=1.0, fill='background', matte='none', soft_edge=0.0, crop='none', pad=0.05, size=None)[source]#
Recover an RGBA foreground from a composite flattened onto a known solid background color (un-premultiply) — the inverse of
flatten_rgba()and the principled sibling ofmake_transparent_cutout().Solves
C = alpha * F + (1 - alpha) * Bper pixel for the minimum alpha whose foregroundFstays inside the RGB gamut (closed form; see_deblend_alpha()). Works for any background color: black reduces tomax(C), white tomax(1 - C), and dark features on a light background survive.With the default settings the reconstruction is exact: flattening the returned RGBA back onto background (same gamma) reproduces the input. The matte-shaping knobs below share the vocabulary — and the implementation — of
make_transparent_cutout(); using them trades exactness for presentation control.- Parameters:
rgb (array (ny, nx, 3)) – Flattened composite, float [0, 1] (e.g. a JPEG/PNG loaded and scaled).
background (color) – The known solid background it was flattened onto (any matplotlib color spec).
gamma (float) – Encoding gamma assumed for the blend.
1.0(default) treats stored values as blend-space; use e.g.2.2if the composite was blended in linear light.tol (float) – Snap alpha to exactly 0 where the pixel is within tol of the background in every channel (default 1.5/255). This absorbs 8-bit quantization / JPEG noise that would otherwise leave a faint near-transparent haze over the whole frame.
alpha_smooth (float) – Gaussian sigma (pixels) applied to the recovered alpha map before the foreground is re-solved (gamut-clipped) — same trick as in
make_transparent_cutout(): mild smoothing trades exact reconstruction near edges for a less noisy matte. Requires scipy when > 0.alpha_lo (float or None) – Optional percentile ramp re-applied to the recovered alpha, with the same meaning as in
make_transparent_cutout():alpha_hiis the full-opacity threshold, fading to transparent atalpha_lo.None(default) keeps the physical alpha.alpha_hi (float or None) – Optional percentile ramp re-applied to the recovered alpha, with the same meaning as in
make_transparent_cutout():alpha_hiis the full-opacity threshold, fading to transparent atalpha_lo.None(default) keeps the physical alpha.alpha_gamma (float) – Fade shape (> 0). With a ramp, shapes it exactly as in
make_transparent_cutout(); without one, applied as a plain power on the physical alpha (< 1bolder body,> 1tighter matte).fill ({'background', 'black', 'white'} or color) – Foreground color to store where alpha == 0 (invisible; affects only later edits). Default keeps the background color there.
matte ({'none', 'circle', 'ellipse'}) – Optional geometric window (same as
make_transparent_cutout()).soft_edge (float) – Gaussian feather (pixels) on the finished alpha (same as
make_transparent_cutout()).crop ({'none', 'auto'} or False) –
'auto'trims to the non-transparent bbox (+ pad). Default'none'— deblending is a recovery operation, so the frame is preserved unless cropping is requested.pad (float) – Fractional padding around the auto-crop bbox.
size (int or None) – Downsample so the longest side is at most this many pixels.
- Returns:
array (ny, nx, 4) float in [0, 1]
Notes
The problem is underdetermined (4 unknowns, 3 equations); minimum alpha is the standard, maximally-transparent convention (as used by color-keying / un-premultiply tools). The recovered F touches the gamut boundary in at least one channel wherever
0 < alpha < 1— that is inherent, not a bug.