Table of Contents

Method TryDetectBorderBackgroundColor

Namespace
LMKit.Media.Image
Assembly
LM-Kit.NET.dll

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

color Color32

When the method returns true, contains the detected background color as an Color32 value. When the method returns false, this value is unspecified.

minAccuracy float

Minimum fraction (0..1) of border pixels that must support the detected color (per channel for RGB/ARGB32). Defaults to 0.85.

Returns

bool

true if a sufficiently uniform background color is found; otherwise false.

Remarks

Algorithm

  1. Accumulate histograms over the border pixels only (perimeter scan).
  2. For GRAY8: pick the most frequent gray level; accept if its frequency / perimeter ≥ minAccuracy.
  3. For RGB/ARGB32: 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 ARGB32 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 ARGB32.