erodeSoft method

Artifact erodeSoft()

Creates a softly eroded version of this artifact to reduce stroke thickness.

A pixel remains "on" only if it has at least _erosionNeighborThreshold neighbors (including itself) in a 3x3 window. This is used to improve matching when templates are thinner than the input glyphs.

Implementation

Artifact erodeSoft() {
  final Artifact result = Artifact(cols, rows);
  if (isEmpty) {
    return result;
  }

  for (int y = 0; y < rows; y++) {
    for (int x = 0; x < cols; x++) {
      if (!cellGet(x, y)) {
        continue;
      }

      int neighbors = 0;
      for (int dy = -1; dy <= 1; dy++) {
        final int ny = y + dy;
        if (ny < 0 || ny >= rows) {
          continue;
        }
        for (int dx = -1; dx <= 1; dx++) {
          final int nx = x + dx;
          if (nx < 0 || nx >= cols) {
            continue;
          }
          if (cellGet(nx, ny)) {
            neighbors++;
          }
        }
      }

      if (neighbors >= _erosionNeighborThreshold) {
        result.cellSet(x, y, true);
      }
    }
  }

  return result;
}