forEachPixel method

void forEachPixel(
  1. void callback(
    1. int row,
    2. int col,
    3. List<num> pixel
    )
)

Iterate over all pixels in the Mat.

callback is called for each pixel in the Mat, the parameter pixel of callback is a view of the pixel at every (row, col), which means it can be modified and will be reflected in the Mat.

Example:

final mat = cv.Mat.ones(3, 3, cv.MatType.CV_8UC3);
mat.forEachPixel((row, col, pixel) {
  print(pixel); // [1, 1, 1]
  pixel[0] = 2;
});
print(mat.atPixel(0, 0)); // [2, 1, 1]

Implementation

void forEachPixel(void Function(int row, int col, List<num> pixel) callback) {
  // cache necessary props, they will be only get once
  final depth = type.depth, pdata = dataPtr, step = this.step, channels = this.channels;
  final rows = this.rows, cols = this.cols;

  for (int row = 0; row < rows; row++) {
    for (int col = 0; col < cols; col++) {
      final pp = pdata + row * step.$1 + col * step.$2;
      callback(row, col, _ptrAsTypedList(pp, channels, depth));
    }
  }
}