adaptiveDenoise static method

DenoiseResult adaptiveDenoise(
  1. Uint8List gray,
  2. int width,
  3. int height, {
  4. double? luminosity,
  5. double? noiseEstimate,
})

Adaptive denoising that automatically selects the best strategy based on image luminosity and noise level estimation.

  • Low-light images: Gamma correction + bilateral filter
  • Normal images: Light Gaussian blur + unsharp mask
  • High-noise images: Median filter + morphological opening

luminosity — Normalized average brightness (0.0–1.0). noiseEstimate — Estimated noise level (0.0–1.0). If null, auto-computed.

Returns the denoised image and the strategy that was applied.

Implementation

static DenoiseResult adaptiveDenoise(
  Uint8List gray,
  int width,
  int height, {
  double? luminosity,
  double? noiseEstimate,
}) {
  // Auto-compute luminosity if not provided
  final luma = luminosity ?? _estimateLuminosity(gray);
  final noise = noiseEstimate ?? _estimateNoise(gray, width, height);

  Uint8List result;
  String strategy;

  if (luma < 0.22) {
    // Low-light: brighten first, then smooth
    final brightened = gammaCorrection(gray, width, height, gamma: 0.55);
    result = bilateralFilter(
      brightened,
      width,
      height,
      spatialSigma: 2.5,
      rangeSigma: 30.0,
    );
    strategy = 'low_light_bilateral';
  } else if (noise > 0.15) {
    // High noise: aggressive denoising
    final median = medianFilter(gray, width, height, kernelSize: 3);
    result = morphOpen(median, width, height, kernelSize: 3);
    strategy = 'high_noise_median_morph';
  } else if (noise > 0.08) {
    // Moderate noise: bilateral filter only
    result = bilateralFilter(
      gray,
      width,
      height,
      spatialSigma: 1.5,
      rangeSigma: 20.0,
    );
    strategy = 'moderate_noise_bilateral';
  } else {
    // Low noise: light Gaussian + sharpening
    final blurred = gaussianBlur(gray, width, height, kernelSize: 3, sigma: 0.8);
    result = unsharpMask(blurred, width, height, amount: 0.5);
    strategy = 'low_noise_sharpen';
  }

  return DenoiseResult(
    bytes: result,
    width: width,
    height: height,
    strategy: strategy,
    estimatedLuminosity: luma,
    estimatedNoise: noise,
  );
}