Mat.from2DList constructor

Mat.from2DList(
  1. Iterable<Iterable<num>> data,
  2. MatType type, {
  3. int? cols,
  4. int? rows,
})

Create a Mat from a 2D list

data should be a 2D list of numbers with a shape of (rows, cols). type specifies the Mat type.

if rows or cols is not specified, it will be inferred from the data. if rows and cols are specified, they must be equal to the shape of the data.

Implementation

factory Mat.from2DList(Iterable<Iterable<num>> data, MatType type, {int? cols, int? rows}) {
  // here we support given [cols] and [rows] since sometimes Mat's channel is specified by
  // [type], e.g., 64FC3 for Point3d, in such occasions, [cols] should be 1 but not real [cols]
  // of [data]
  rows ??= data.length;
  cols ??= data.first.length;
  cvAssert(rows > 0 && cols > 0, "The input data must have the same number of elements.");

  final flatData = <num>[for (final row in data) ...row];
  return Mat.fromList(rows, cols, type, flatData);
}