computeTenengradScore static method

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

Computes Tenengrad sharpness score using Sobel gradient sum.

More robust than Laplacian variance for document scanning. Returns higher values for sharper images.

Implementation

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

  double sumGradientSq = 0.0;
  int count = 0;
  final step = math.max(2, (width * height) ~/ 5000);

  for (int y = 1; y < height - 1; y += step) {
    final row = y * width;
    for (int x = 1; x < width - 1; x += step) {
      final idx = row + x;

      // Sobel X: horizontal gradient
      final gx = gray[idx + 1] - gray[idx - 1];
      // Sobel Y: vertical gradient
      final gy = gray[idx + width] - gray[idx - width];

      sumGradientSq += gx * gx + gy * gy;
      count++;
    }
  }

  return count > 0 ? sumGradientSq / count : 0.0;
}