evaluateImageSharpness function

Future<bool> evaluateImageSharpness(
  1. File file, {
  2. int? index,
})

Implementation

Future<bool> evaluateImageSharpness(File file, {int? index}) async {
  // Load image from file
  final imageBytes = await file.readAsBytes();
  final image = img.decodeImage(Uint8List.fromList(imageBytes))!;

  //final lBPSharpnessMetric = LBPSharpnessMetric().compute(image);

  final varianceOfLaplacian = VarianceOfLaplacian().compute(image);
  if (varianceOfLaplacian < 150) return true;

  final contrastAdjustedVarianceMetric =
      ContrastAdjustedVarianceMetric().compute(image);
  if (contrastAdjustedVarianceMetric < 0.009) return true;

  final frequencyContentMetric = FrequencyContentMetric().compute(image);
  if (frequencyContentMetric < 150) return true;

  final autoCorrelationMetric = AutoCorrelationMetric().compute(image);

  final brightness = computeAverageBrightness(image);

  final tenengradFocusMeasureMetric =
      TenengradFocusMeasureMetric().compute(image);

  final enhancedBrennerFocusMeasureMetric =
      EnhancedBrennerFocusMeasureMetric().compute(image);

  final waveletTransformMetric = WaveletTransformMetric().compute(image);

  List<bool> cases = [
    // case 1:
    (() {
      if (autoCorrelationMetric < 500 && contrastAdjustedVarianceMetric < 2) {
        double contrastAdjustedPercentage =
            getPercentage(value: contrastAdjustedVarianceMetric, totalValue: 2);
        double autoCorrelationPercentage =
            getPercentage(value: autoCorrelationMetric, totalValue: 500);
        double tenengradFocusPercentage =
            getPercentage(value: tenengradFocusMeasureMetric, totalValue: 1);
        double enhancedBrennerFocusPercentage = getPercentage(
            value: enhancedBrennerFocusMeasureMetric, totalValue: 0.2);
        double waveletPercentage =
            getPercentage(value: waveletTransformMetric, totalValue: 0.006);

        double targetAdjustment = 5000 + brightness;

        double adjustedValue = getValueByPercentage(
            value: targetAdjustment,
            requiredPercentage: (contrastAdjustedPercentage +
                    autoCorrelationPercentage +
                    tenengradFocusPercentage +
                    enhancedBrennerFocusPercentage +
                    waveletPercentage) /
                5);

        double requirement = targetAdjustment - adjustedValue;

        double totalMetricSum = varianceOfLaplacian + frequencyContentMetric;

        if (requirement > totalMetricSum) return false;
      }

      double adjustedVarianceOfLaplacian = 1000;
      adjustedVarianceOfLaplacian += (5000 - autoCorrelationMetric) <= 0
          ? 0
          : (5000 - autoCorrelationMetric);

      double adjustedContrastVariance = 1;
      adjustedContrastVariance += (2 - (500 / autoCorrelationMetric)) <= 0
          ? 0
          : (2 - (500 / autoCorrelationMetric));

      double adjustedFrequencyContent = 1000;
      adjustedFrequencyContent += (5000 - autoCorrelationMetric) <= 0
          ? 0
          : (5000 - autoCorrelationMetric);

      double variancePercentage = getPercentage(
          value: varianceOfLaplacian, totalValue: adjustedVarianceOfLaplacian);
      double contrastPercentage = getPercentage(
          value: contrastAdjustedVarianceMetric,
          totalValue: adjustedContrastVariance);
      // double autoCorrelationP =
      //     getPercentage(value: autoCorrelationMetric, totalValue: 5000);
      double frequencyPercentage = getPercentage(
          value: frequencyContentMetric, totalValue: adjustedFrequencyContent);

      double tenengradPercentage =
          getPercentage(value: tenengradFocusMeasureMetric, totalValue: 1);
      double enhancedBrennerPercentage = getPercentage(
          value: enhancedBrennerFocusMeasureMetric, totalValue: 0.2);
      double waveletTransformPercentage =
          getPercentage(value: waveletTransformMetric, totalValue: 0.006);

      double combinedPercentage = variancePercentage +
          contrastPercentage +
          frequencyPercentage +
          ((tenengradPercentage +
                  enhancedBrennerPercentage +
                  waveletTransformPercentage) /
              2);

      double requiredCombinedThreshold = (100 + (brightness / 50));

      bool isAboveThreshold = combinedPercentage > requiredCombinedThreshold;

      return isAboveThreshold;
    })(),

    // case 2:
    (() {
      if (brightness < 200 ||
          contrastAdjustedVarianceMetric > 0.5 ||
          (varianceOfLaplacian >= 2000 &&
              autoCorrelationMetric >= 2000 &&
              frequencyContentMetric >= 2000) ||
          (varianceOfLaplacian +
                  autoCorrelationMetric +
                  frequencyContentMetric >=
              10000)) {
        return true;
      }
      return false;
    })()
  ];

  return cases.contains(false);
}