Method TryDetectBorderBackgroundColor
TryDetectBorderBackgroundColor(out Color32, float)
Attempts to estimate a uniform background color by analyzing only the image border (top and bottom rows, and left/right columns).
public bool TryDetectBorderBackgroundColor(out Color32 color, float minAccuracy = 0.85)
Parameters
colorColor32When the method returns
true, contains the detected background color as an Color32 value. When the method returnsfalse, this value is unspecified.minAccuracyfloatMinimum fraction (0..1) of border pixels that must support the detected color (per channel for RGB/RGBA32). Defaults to
0.85.
Returns
- bool
trueif a sufficiently uniform background color is found; otherwisefalse.
Remarks
Algorithm
- Accumulate histograms over the border pixels only (perimeter scan).
-
For GRAY8: pick the most frequent gray level; accept if its frequency / perimeter ≥
minAccuracy. -
For RGB/RGBA32: build per-channel histograms (alpha ignored), apply a 3-sample box smoothing,
pick the max bin per channel, and require each channel’s frequency / perimeter ≥
minAccuracy.
Performance: O(perimeter) time; uses stack-allocated histograms to avoid GC pressure.
Notes: Works best when the background is roughly uniform near the border. For RGBA32 inputs, alpha is ignored (transparent borders are treated as their RGB values).
Exceptions
- NotSupportedException
Thrown if the pixel format is not one of GRAY8, RGB24, or RGBA32.