findSubArtifacts method

List<Artifact> findSubArtifacts()

Finds the connected components (artifacts) in a binary image matrix.

This method identifies distinct connected regions in the binary image by using a flood fill algorithm. For each unvisited "on" pixel (value = true), it performs a flood fill to collect all connected points that form a single artifact.

The method tracks visited pixels to ensure each pixel is processed only once. Each connected component is converted to a separate Artifact object using the Artifact.fromPoints factory method, which creates a minimal bounding box containing just the connected region.

Parameters: None - operates on the current Artifact instance.

Returns: A list of Artifact objects, each representing a distinct connected component found in the binary image. The artifacts are sorted using Artifact.sortMatrices.

Implementation

List<Artifact> findSubArtifacts() {
  // Clear existing regions
  List<Artifact> regions = [];

  // Create a matrix to track visited pixels
  final Artifact visited = Artifact(cols, rows);

  // Scan through each pixel
  for (int y = 0; y < rows; y++) {
    for (int x = 0; x < cols; x++) {
      // If pixel is on and not visited, flood fill from this point
      if (!visited.cellGet(x, y) && cellGet(x, y)) {
        // Get connected points using flood fill
        final List<Point<int>> connectedPoints = floodFill(
          this,
          visited,
          x,
          y,
        );

        if (connectedPoints.isEmpty) {
          continue;
        }
        regions.add(Artifact.fromPoints(connectedPoints));
      }
    }
  }

  Artifact.sortMatrices(regions);
  return regions;
}