Table of Contents

Constructor Mat

Namespace
OpenCvSharp
Assembly
OpenCvSharp.dll

Mat()

Creates empty Mat

public Mat()

Mat(string, ImreadModes)

Loads an image from a file. (cv::imread)

public Mat(string fileName, ImreadModes flags = ImreadModes.Color)

Parameters

fileName string

Name of file to be loaded.

flags ImreadModes

Specifies color type of the loaded image

Mat(int, int, MatType)

constructs 2D matrix of the specified size and type

public Mat(int rows, int cols, MatType type)

Parameters

rows int

Number of rows in a 2D array.

cols int

Number of columns in a 2D array.

type MatType

Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.

Mat(Size, MatType)

constructs 2D matrix of the specified size and type

public Mat(Size size, MatType type)

Parameters

size Size

2D array size: Size(cols, rows) . In the Size() constructor, the number of rows and the number of columns go in the reverse order.

type MatType

Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or MatType.CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.

Mat(int, int, MatType, Scalar)

constructs 2D matrix and fills it with the specified Scalar value.

public Mat(int rows, int cols, MatType type, Scalar s)

Parameters

rows int

Number of rows in a 2D array.

cols int

Number of columns in a 2D array.

type MatType

Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.

s Scalar

An optional value to initialize each matrix element with. To set all the matrix elements to the particular value after the construction, use SetTo(Scalar s) method .

Mat(Size, MatType, Scalar)

constructs 2D matrix and fills it with the specified Scalar value.

public Mat(Size size, MatType type, Scalar s)

Parameters

size Size

2D array size: Size(cols, rows) . In the Size() constructor, the number of rows and the number of columns go in the reverse order.

type MatType

Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices.

s Scalar

An optional value to initialize each matrix element with. To set all the matrix elements to the particular value after the construction, use SetTo(Scalar s) method .

Mat(Mat, Range, Range?)

creates a matrix header for a part of the bigger matrix

public Mat(Mat m, Range rowRange, Range? colRange = null)

Parameters

m Mat

Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it. The reference counter, if any, is incremented. So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m . If you want to have an independent copy of the sub-array, use Mat::clone() .

rowRange Range

Range of the m rows to take. As usual, the range start is inclusive and the range end is exclusive. Use Range.All to take all the rows.

colRange Range?

Range of the m columns to take. Use Range.All to take all the columns.

Mat(Mat, Range[])

creates a matrix header for a part of the bigger matrix

public Mat(Mat m, Range[] ranges)

Parameters

m Mat

Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it. The reference counter, if any, is incremented. So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m . If you want to have an independent copy of the sub-array, use Mat.Clone() .

ranges Range[]

Array of selected ranges of m along each dimensionality.

Mat(Mat, Rect)

creates a matrix header for a part of the bigger matrix

public Mat(Mat m, Rect roi)

Parameters

m Mat

Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it. The reference counter, if any, is incremented. So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m . If you want to have an independent copy of the sub-array, use Mat.Clone() .

roi Rect

Region of interest.

Mat(int, int, MatType, nint, long)

constructor for matrix headers pointing to user-allocated data

[Obsolete("Use Mat.FromPixelData instead. This constructor has been deprecated because the introduction of 'nint' made overload resolution confusing.", true)]
public Mat(int rows, int cols, MatType type, nint data, long step = 0)

Parameters

rows int

Number of rows in a 2D array.

cols int

Number of columns in a 2D array.

type MatType

Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.

data nint

Pointer to the user data. Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically de-allocated, so you should take care of it.

step long

Number of bytes each matrix row occupies. The value should include the padding bytes at the end of each row, if any. If the parameter is missing (set to AUTO_STEP ), no padding is assumed and the actual step is calculated as cols*elemSize() .

Mat(int, int, MatType, Array, long)

Constructor for matrix headers pointing to user-allocated data. Do not use this constructor directly. Please use FromPixelData(int, int, MatType, nint, long) instead. This constructor was removed from the public API because the introduction of nint in .NET caused overload resolution confusion.

protected Mat(int rows, int cols, MatType type, Array data, long step = 0)

Parameters

rows int

Number of rows in a 2D array.

cols int

Number of columns in a 2D array.

type MatType

Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.

data Array

Pointer to the user data. Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically de-allocated, so you should take care of it.

step long

Number of bytes each matrix row occupies. The value should include the padding bytes at the end of each row, if any. If the parameter is missing (set to AUTO_STEP ), no padding is assumed and the actual step is calculated as cols*elemSize() .

Mat(IEnumerable<int>, MatType, Array, IEnumerable<long>?)

constructor for matrix headers pointing to user-allocated data

protected Mat(IEnumerable<int> sizes, MatType type, Array data, IEnumerable<long>? steps = null)

Parameters

sizes IEnumerable<int>

Array of integers specifying an n-dimensional array shape.

type MatType

Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.

data Array

Pointer to the user data. Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically de-allocated, so you should take care of it.

steps IEnumerable<long>

Array of ndims-1 steps in case of a multi-dimensional array (the last step is always set to the element size). If not specified, the matrix is assumed to be continuous.

Mat(IEnumerable<int>, MatType)

constructs n-dimensional matrix

public Mat(IEnumerable<int> sizes, MatType type)

Parameters

sizes IEnumerable<int>

Array of integers specifying an n-dimensional array shape.

type MatType

Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.

Mat(IEnumerable<int>, MatType, Scalar)

constructs n-dimensional matrix

public Mat(IEnumerable<int> sizes, MatType type, Scalar s)

Parameters

sizes IEnumerable<int>

Array of integers specifying an n-dimensional array shape.

type MatType

Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.

s Scalar

An optional value to initialize each matrix element with. To set all the matrix elements to the particular value after the construction, use SetTo(Scalar s) method .

Mat(MatShape, MatType)

Constructs a matrix of the given shape (OpenCV 5). The shape may be N-D, a 0-D scalar or empty, and can carry a data layout and channel count.

public Mat(MatShape shape, MatType type)

Parameters

shape MatShape

Matrix shape.

type MatType

Array type.

Mat(MatShape, MatType, Scalar)

Constructs a matrix of the given shape (OpenCV 5), initialized with the given value.

public Mat(MatShape shape, MatType type, Scalar s)

Parameters

shape MatShape

Matrix shape.

type MatType

Array type.

s Scalar

Value to initialize each element with.