extractSubGrid method

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

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

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;
}