toList3D method

List<List<List<num>>> toList3D({
  1. bool copy = true,
})

Returns a 3D list of the mat, only for multi-channel mats. The list is ordered as colchannels.

When copy is true (default), the returned lists are ordinary Dart lists (deep copy) and are safe to use after this Mat is disposed. When copy is false, each pixel list is a zero-copy view over the Mat's native memory: writing to it writes back to the Mat, and the view becomes invalid once the Mat is disposed or its buffer is reallocated.

Example:

final mat = Mat.fromList(
  2, 2, MatType.CV_8UC2,
  [1, 2, 3, 4, 5, 6, 7, 8],
);
final list = mat.toList3D();
print(list); // [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

Implementation

List<List<List<num>>> toList3D({bool copy = true}) {
  cvAssert(channels >= 2, "toList3D() only for channels >= 2, but this.channels=$channels");
  final rows = this.rows, cols = this.cols;
  return List.generate(
    rows,
    (r) => List.generate(cols, (c) {
      final pixel = atPixel(r, c);
      return copy ? pixel.toList() : pixel;
    }),
  );
}