histogramSimilarity method

double histogramSimilarity(
  1. List<int> inputColHist,
  2. List<int> inputRowHist
)

Computes histogram similarity between this template and an input artifact.

Returns a value between 0.0 and 1.0, where 1.0 is a perfect match. Uses L1 (Manhattan) distance normalized by the max possible distance.

Implementation

double histogramSimilarity(List<int> inputColHist, List<int> inputRowHist) {
  final List<double> templateCol = averageColHistogram;
  final List<double> templateRow = averageRowHistogram;

  if (templateCol.isEmpty || templateRow.isEmpty) {
    return 0.0;
  }

  double colDistance = 0;
  double colMax = 0;
  final int colLen = min(inputColHist.length, templateCol.length);
  for (int i = 0; i < colLen; i++) {
    colDistance += (inputColHist[i] - templateCol[i]).abs();
    colMax += max(inputColHist[i].toDouble(), templateCol[i]);
  }

  double rowDistance = 0;
  double rowMax = 0;
  final int rowLen = min(inputRowHist.length, templateRow.length);
  for (int i = 0; i < rowLen; i++) {
    rowDistance += (inputRowHist[i] - templateRow[i]).abs();
    rowMax += max(inputRowHist[i].toDouble(), templateRow[i]);
  }

  final double totalMax = colMax + rowMax;
  if (totalMax == 0) {
    return 0.0;
  }

  return 1.0 - ((colDistance + rowDistance) / totalMax);
}