computeBlurScore static method

double computeBlurScore(
  1. Uint8List gray,
  2. int width,
  3. int height
)

Calculates Laplacian variance sharpness score for blur detection. Inspired by OpenCV Laplacian variance algorithm.

Implementation

static double computeBlurScore(Uint8List gray, int width, int height) {
  if (gray.length < width * height || width < 3 || height < 3) return 0.0;

  double sum = 0.0;
  double sumSq = 0.0;
  int count = 0;

  // 3x3 Discrete Laplacian Kernel:
  // [  0,  1,  0 ]
  // [  1, -4,  1 ]
  // [  0,  1,  0 ]
  final stride = width;
  for (int y = 1; y < height - 1; y += 2) {
    final row = y * stride;
    for (int x = 1; x < width - 1; x += 2) {
      final idx = row + x;
      final lap = gray[idx - stride] +
          gray[idx - 1] +
          gray[idx + 1] +
          gray[idx + stride] -
          (4 * gray[idx]);
      final val = lap.toDouble();
      sum += val;
      sumSq += val * val;
      count++;
    }
  }

  if (count == 0) return 0.0;
  final mean = sum / count;
  final variance = (sumSq / count) - (mean * mean);
  return math.max(0.0, variance);
}