findSubRegions method

List<IntRect> findSubRegions()

Identifies distinct regions in a dilated binary image.

This function analyzes a dilated image to find connected components that likely represent characters or groups of characters.

The algorithm uses direct array access for performance optimization and employs a flood fill approach to identify connected regions.

This is the preprocessed binary image after dilation.

Returns: A list of IntRect objects representing the bounding boxes of identified regions. Each rectangle defines the boundaries of a potential character or character group. The returned list is sorted using Artifact.sortRectangles.

Implementation

List<IntRect> findSubRegions() {
  // Clear existing regions
  List<IntRect> regions = [];

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

  final int width = cols;
  final int height = rows;
  final Uint8List imageData = matrix;
  final Uint8List visitedData = visited.matrix;

  // Scan through each pixel - use direct array access
  for (int y = 0; y < height; y++) {
    final int rowOffset = y * width;
    for (int x = 0; x < width; x++) {
      final int index = rowOffset + x;
      // Check if pixel is on and not visited using direct array access
      if (visitedData[index] == 0 && imageData[index] == 1) {
        // Find region bounds directly without storing all points
        final IntRect rect = floodFillToRect(this, visited, x, y);

        if (rect.width > 0 && rect.height > 0) {
          regions.add(rect);
        }
      }
    }
  }

  Artifact.sortRectangles(regions);
  return regions;
}