Table of Contents

Class Mat<TElem>

Namespace
OpenCvSharp
Assembly
OpenCvSharp.dll

Type-specific abstract matrix

public class Mat<TElem> : Mat, IDisposable, ICvPtrHolder where TElem : unmanaged

Type Parameters

TElem

Element Type

Inheritance
Mat<TElem>
Implements
Inherited Members

Constructors

Mat()

Creates empty Mat

public Mat()

Mat(Mat)

Initializes by Mat object

public Mat(Mat mat)

Parameters

mat Mat

Managed Mat object

Mat(Mat<TElem>, Range, Range?)

creates a matrix header for a part of the bigger matrix

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

Parameters

m Mat<TElem>

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<TElem>, params Range[])

creates a matrix header for a part of the bigger matrix

protected Mat(Mat<TElem> m, params Range[] ranges)

Parameters

m Mat<TElem>

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<TElem>, Rect)

creates a matrix header for a part of the bigger matrix

public Mat(Mat<TElem> m, Rect roi)

Parameters

m Mat<TElem>

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(Size)

constructs 2D matrix of the specified size and type

public Mat(Size size)

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.

Mat(Size, Scalar)

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

public Mat(Size size, 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.

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(IEnumerable<int>)

constructs n-dimensional matrix

public Mat(IEnumerable<int> sizes)

Parameters

sizes IEnumerable<int>

Array of integers specifying an n-dimensional array shape.

Mat(IEnumerable<int>, Scalar)

constructs n-dimensional matrix

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

Parameters

sizes IEnumerable<int>

Array of integers specifying an n-dimensional array shape.

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(IEnumerable<int>, Array, IEnumerable<long>?)

constructor for matrix headers pointing to user-allocated data

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

Parameters

sizes IEnumerable<int>

Array of integers specifying an n-dimensional array shape.

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(int, int)

constructs 2D matrix of the specified size and type

public Mat(int rows, int cols)

Parameters

rows int

Number of rows in a 2D array.

cols int

Number of columns in a 2D array.

Mat(int, int, Scalar)

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

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

Parameters

rows int

Number of rows in a 2D array.

cols int

Number of columns in a 2D array.

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(int, int, Array, long)

constructor for matrix headers pointing to user-allocated data

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

Parameters

rows int

Number of rows in a 2D array.

cols int

Number of columns in a 2D array.

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() .

Properties

this[Range, Range]

Extracts a rectangular submatrix.

public Mat<TElem> this[Range rowRange, Range colRange] { get; set; }

Parameters

rowRange Range

Start and end row of the extracted submatrix. The upper boundary is not included. To select all the rows, use Range.All().

colRange Range

Start and end column of the extracted submatrix. The upper boundary is not included. To select all the columns, use Range.All().

Property Value

Mat<TElem>

this[Range[]]

Extracts a rectangular submatrix.

public Mat<TElem> this[params Range[] ranges] { get; set; }

Parameters

ranges Range[]

Array of selected ranges along each array dimension.

Property Value

Mat<TElem>

this[Rect]

Extracts a rectangular submatrix.

[SuppressMessage("Microsoft.Design", "CA1043: Use integral or string argument for indexers")]
public Mat<TElem> this[Rect roi] { get; set; }

Parameters

roi Rect

Extracted submatrix specified as a rectangle.

Property Value

Mat<TElem>

this[int, int, int, int]

Extracts a rectangular submatrix.

public Mat<TElem> this[int rowStart, int rowEnd, int colStart, int colEnd] { get; set; }

Parameters

rowStart int

Start row of the extracted submatrix. The upper boundary is not included.

rowEnd int

End row of the extracted submatrix. The upper boundary is not included.

colStart int

Start column of the extracted submatrix. The upper boundary is not included.

colEnd int

End column of the extracted submatrix. The upper boundary is not included.

Property Value

Mat<TElem>

Methods

Clone()

Creates a full copy of the matrix.

public Mat<TElem> Clone()

Returns

Mat<TElem>

FromNativePointer(nint)

Creates from native cv::Mat* pointer

public static Mat<TElem> FromNativePointer(nint ptr)

Parameters

ptr nint

Returns

Mat<TElem>

FromPixelData(IEnumerable<int>, nint, IEnumerable<long>?)

constructor for matrix headers pointing to user-allocated data

public static Mat<TElem> FromPixelData(IEnumerable<int> sizes, nint data, IEnumerable<long>? steps = null)

Parameters

sizes IEnumerable<int>

Array of integers specifying an n-dimensional array shape.

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.

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.

Returns

Mat<TElem>

FromPixelData(int, int, Array, long)

constructor for matrix headers pointing to user-allocated data

public static Mat<TElem> FromPixelData(int rows, int cols, Array data, long step = 0)

Parameters

rows int

Number of rows in a 2D array.

cols int

Number of columns in a 2D array.

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() .

Returns

Mat<TElem>

FromPixelData(int, int, nint, long)

constructor for matrix headers pointing to user-allocated data

public static Mat<TElem> FromPixelData(int rows, int cols, nint data, long step = 0)

Parameters

rows int

Number of rows in a 2D array.

cols int

Number of columns in a 2D array.

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() .

Returns

Mat<TElem>

GetEnumerator()

Gets read-only enumerator

public IEnumerator<TElem> GetEnumerator()

Returns

IEnumerator<TElem>

GetIndexer()

Gets a type-specific indexer. The indexer has getters/setters to access each matrix element.

public MatIndexer<TElem> GetIndexer()

Returns

MatIndexer<TElem>

Reshape(int)

Changes the shape of channels of a 2D matrix without copying the data.

public Mat<TElem> Reshape(int rows)

Parameters

rows int

New number of rows. If the parameter is 0, the number of rows remains the same.

Returns

Mat<TElem>

Reshape(params int[])

Changes the shape of a 2D matrix without copying the data.

public Mat<TElem> Reshape(params int[] newDims)

Parameters

newDims int[]

New number of rows. If the parameter is 0, the number of rows remains the same.

Returns

Mat<TElem>

SubMat(Range, Range)

Extracts a rectangular submatrix.

public Mat<TElem> SubMat(Range rowRange, Range colRange)

Parameters

rowRange Range

Start and end row of the extracted submatrix. The upper boundary is not included. To select all the rows, use Range.All().

colRange Range

Start and end column of the extracted submatrix. The upper boundary is not included. To select all the columns, use Range.All().

Returns

Mat<TElem>

SubMat(params Range[])

Extracts a rectangular submatrix.

public Mat<TElem> SubMat(params Range[] ranges)

Parameters

ranges Range[]

Array of selected ranges along each array dimension.

Returns

Mat<TElem>

SubMat(Rect)

Extracts a rectangular submatrix.

public Mat<TElem> SubMat(Rect roi)

Parameters

roi Rect

Extracted submatrix specified as a rectangle.

Returns

Mat<TElem>

SubMat(int, int, int, int)

Extracts a rectangular submatrix.

public Mat<TElem> SubMat(int rowStart, int rowEnd, int colStart, int colEnd)

Parameters

rowStart int

Start row of the extracted submatrix. The upper boundary is not included.

rowEnd int

End row of the extracted submatrix. The upper boundary is not included.

colStart int

Start column of the extracted submatrix. The upper boundary is not included.

colEnd int

End column of the extracted submatrix. The upper boundary is not included.

Returns

Mat<TElem>

T()

Transposes a matrix.

public Mat<TElem> T()

Returns

Mat<TElem>

ToArray()

Convert this mat to managed array

public TElem[] ToArray()

Returns

TElem[]

ToRectangularArray()

Convert this mat to managed rectangular array

public TElem[,] ToRectangularArray()

Returns

TElem[,]

Wrap(Mat)

protected Mat<TElem> Wrap(Mat mat)

Parameters

mat Mat

Returns

Mat<TElem>