Function: ABE - setiastro/setiastrosuitepro GitHub Wiki
Function: Automatic Background Extraction (ABE)
Polynomial background modeling with optional RBF refinement, smart sampling, exclusion polygons, non-destructive preview, and apply-with-undo. Works with mono or RGB float images in ([0,1]).
What ABE Does
- Estimates a smooth background (B) (gradients, vignetting, LP glow).
- Subtracts (B) while preserving the global median of the working domain.
- Optionally refines with RBF for complex/curvy gradients.
- Respects masks and user-drawn exclusion polygons in image space.
Model (math)
1) Polynomial stage (degree (d\in{0,\dots,6}))
We fit a 2-D polynomial to per-sample patch medians by least squares. For sample locations ((x_i,y_i)) with measured values (z_i), build a design matrix (A) of monomials up to degree (d) and solve:
\min_{\boldsymbol{\beta}}\;\|A\boldsymbol{\beta}-\mathbf{z}\|_2^2
\quad\Rightarrow\quad
\boldsymbol{\beta}=(A^{\top}A)^{-1}A^{\top}\mathbf{z}.
Evaluate the fitted surface over the (downsampled) image to get (B_{\mathrm{poly}}).
2) RBF refinement (optional)
Multiquadric RBF on the polynomial-corrected image (same sampling), with smoothness (\lambda):
\phi(r)=\sqrt{1 + (\epsilon r)^2},\quad
B_{\mathrm{rbf}}(x,y)=\sum_{i} w_i\,\phi\!\left(\sqrt{(x-x_i)^2+(y-y_i)^2}\right).
Weights (w_i) come from solving the regularized system ((\Phi+\lambda I)\mathbf{w}=\mathbf{z}). The final background is (B = B_{\mathrm{poly}} + B_{\mathrm{rbf}}) (if RBF is enabled; otherwise (B=B_{\mathrm{poly}})).
3) Median re-centering
After subtraction we restore the original working-domain median (m_0) so overall brightness stays stable:
I_{\mathrm{corr}} = \text{clip}\!\Big( I - B + (m_0 - \text{median}(I-B)) ,\, 0,\, 1 \Big).
(Here (\text{clip}(x,0,1)) clamps to ([0,1]) and (\text{median}(\cdot)) is the scalar median.)
Sampling & Exclusions
- Smart points: corners, borders, and quartile tiles.
- Bright-region avoidance: ignore top quantiles in luminance; prefer dim/neutral areas.
- Local patch median: each sample’s value is the median of a ((\text{patch size})) neighborhood.
- User polygons: draw exclusion polygons (right-click to close); excluded pixels are never sampled.
- Masks (if present): processing is blended as (m\cdot \text{ABE} + (1-m)\cdot \text{source}).
Parameters (UI)
- Polynomial degree: (0) (skip poly, RBF-only) … (6).
- # Sample points: count of background probes.
- Downsample: integer factor for fitting speed; full-res background is reconstructed by high-quality resize.
- Patch size (px): spatial window for per-sample medians.
- RBF refinement: enable/disable; Smooth controls (\lambda) (higher = smoother).
- Preview background: toggle between corrected image and background view.
- Create background doc: export (B) as a separate image.
Defaults (suggested): degree (=2), samples (=120), downsample (=4), patch (=15), RBF on, smooth (\approx 0.10).
Preview & Navigation
Because images are linear, use the built-in display helpers (preview only):
- Autostretch (Siril-style MAD) toggle for visualization.
- Zoom In/Out buttons; Ctrl+Wheel to zoom at cursor.
- Pan: middle-drag (or Ctrl+left-drag).
- Fit: scale to fit the preview viewport.
- Polygons: left-click to add vertices, right-click to close; Clear Exclusions removes all.
Workflow
- Add exclusions around nebulosity/galaxies and bright stars (optional).
- Pick a degree (start at 2) and enable RBF for complex gradients.
- Set samples, patch size, and smooth to taste.
- Preview → inspect corrected image (or background) with Autostretch as needed.
- Apply to commit (with undo). Optionally Create background doc.
Color vs Mono
- Internally ABE runs per-channel for RGB (on float ([0,1])).
- Mono inputs are treated as single-channel throughout; outputs match the input shape.
- Median re-centering is computed in the working domain (after any optional modeling pre-stretch—see below).
Notes on Parity with SASv2 (optional)
For 1:1 compatibility with legacy projects, ABE can run in the same modeling domain as SASv2:
- A lightweight pre-stretch (per-channel min shift + rational midtone lift to target median) is applied only for modeling, then analytically inverted after ABE so results return to the source domain.
Performance Tips
- Degree 0 + RBF: fast, good for mild gradients with local wiggles.
- Higher degree: captures broad vignetting/tilt; avoid overfitting—use RBF for the rest.
- Downsample aggressively for large frames; increase samples if the field is uneven.
- Increase patch size in noisy data to stabilize medians.
Output
- Corrected image (applied to the active document, undoable).
- Background image (optional extra document).
- Metadata: degree, samples, downsample, patch size, RBF on/off, smooth value, and mask usage.
Troubleshooting
- Residual gradient: raise samples, enable RBF, or bump degree by 1.
- Over-correction in bright structures: expand exclusion polygons and/or increase smoothness.
- Banding after subtraction: lower degree and rely more on RBF; ensure downsample isn’t too coarse.