extractSubGrid method

Artifact extractSubGrid({
  1. required IntRect rect,
})

Extracts a sub-grid from a larger binary image matrix.

This static method creates a new Matrix object representing a portion of the input binary image, as specified by the given rectangle.

Parameters:

  • matrix: The source Matrix from which to extract the sub-grid.
  • rect: A IntRect object specifying the region to extract. The rectangle's coordinates are relative to the top-left corner of the binaryImage.

Returns: A new Matrix object containing the extracted sub-grid.

Note:

  • If the specified rectangle extends beyond the boundaries of the source image, the out-of-bounds areas in the resulting sub-grid will be false.
  • The method uses integer coordinates, so any fractional values in the rect will be truncated.

Implementation

Artifact extractSubGrid({required final IntRect rect}) {
  final int startX = rect.left.toInt();
  final int startY = rect.top.toInt();
  final int subImageWidth = rect.width.toInt();
  final int subImageHeight = rect.height.toInt();

  final Artifact subImagePixels = Artifact(subImageWidth, subImageHeight);

  for (int x = 0; x < subImageWidth; x++) {
    for (int y = 0; y < subImageHeight; y++) {
      final int sourceX = startX + x;
      final int sourceY = startY + y;

      if (sourceX < cols && sourceY < rows) {
        subImagePixels.cellSet(x, y, cellGet(sourceX, sourceY));
      }
    }
  }

  subImagePixels.locationFound = rect.shift(rectFound.topLeft).topLeft;
  subImagePixels.locationAdjusted = rect.shift(rectAdjusted.topLeft).topLeft;

  return subImagePixels;
}