Class Mat<TElem>
- Namespace
- OpenCvSharp
- Assembly
- OpenCvSharp.dll
Type-specific matrix. TElem fixes the element type, so elements can be
read and written as TElem (e.g. via mat[row, col]) and the matrix
can be enumerated.
public class Mat<TElem> : Mat, IDisposable, IEnumerable<TElem>, IEnumerable where TElem : unmanaged
Type Parameters
TElemElement Type
- Inheritance
-
Mat<TElem>
- Implements
-
IEnumerable<TElem>
- Inherited Members
- Extension Methods
Constructors
Mat()
Creates empty Mat
public Mat()
Mat(Mat)
Initializes by Mat object. The Mat's element type must match TElem.
public Mat(Mat mat)
Parameters
matMatManaged 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
mMat<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() .
rowRangeRangeRange 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.
colRangeRange?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
mMat<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() .
rangesRange[]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
mMat<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() .
roiRectRegion of interest.
Mat(Size)
constructs 2D matrix of the specified size and type
public Mat(Size size)
Parameters
sizeSize2D 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
sizeSize2D array size: Size(cols, rows) . In the Size() constructor, the number of rows and the number of columns go in the reverse order.
sScalarAn 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
sizesIEnumerable<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
sizesIEnumerable<int>Array of integers specifying an n-dimensional array shape.
sScalarAn 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
sizesIEnumerable<int>Array of integers specifying an n-dimensional array shape.
dataArrayPointer 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.
stepsIEnumerable<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
Mat(int, int, Scalar)
constructs 2D matrix and fills it with the specified Scalar value.
public Mat(int rows, int cols, Scalar s)
Parameters
rowsintNumber of rows in a 2D array.
colsintNumber of columns in a 2D array.
sScalarAn 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
rowsintNumber of rows in a 2D array.
colsintNumber of columns in a 2D array.
dataArrayPointer 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.
steplongNumber 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
rowRangeRangeStart and end row of the extracted submatrix. The upper boundary is not included. To select all the rows, use Range.All().
colRangeRangeStart 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
rangesRange[]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
roiRectExtracted submatrix specified as a rectangle.
Property Value
- Mat<TElem>
this[int, int]
Gets or sets the element at the specified 2-D position.
public TElem this[int row, int col] { get; set; }
Parameters
Property Value
- 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
rowStartintStart row of the extracted submatrix. The upper boundary is not included.
rowEndintEnd row of the extracted submatrix. The upper boundary is not included.
colStartintStart column of the extracted submatrix. The upper boundary is not included.
colEndintEnd 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
ptrnint
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
sizesIEnumerable<int>Array of integers specifying an n-dimensional array shape.
datanintPointer 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.
stepsIEnumerable<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
rowsintNumber of rows in a 2D array.
colsintNumber of columns in a 2D array.
dataArrayPointer 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.
steplongNumber 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
rowsintNumber of rows in a 2D array.
colsintNumber of columns in a 2D array.
datanintPointer 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.
steplongNumber 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()
Enumerates all elements in row-major order (works for any dimensionality).
public IEnumerator<TElem> GetEnumerator()
Returns
- IEnumerator<TElem>
GetIndexer()
Gets a type-specific indexer for fast repeated element access. The indexer captures the data
pointer once, so it is faster than this[row, col] for tight loops.
public MatIndexer<TElem> GetIndexer()
Returns
- MatIndexer<TElem>
Remarks
The indexer keeps this matrix alive, but it caches the data pointer at creation time. Do not
keep using an indexer after an operation that may reallocate the matrix data (e.g.
Create(int, int, MatType)); obtain a fresh one instead. For occasional
access prefer the this[row, col] indexer, which resolves the pointer each time.
Reshape(int)
Changes the shape of channels of a 2D matrix without copying the data.
public Mat<TElem> Reshape(int rows)
Parameters
rowsintNew 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
newDimsint[]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
rowRangeRangeStart and end row of the extracted submatrix. The upper boundary is not included. To select all the rows, use Range.All().
colRangeRangeStart 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
rangesRange[]Array of selected ranges along each array dimension.
Returns
- Mat<TElem>
SubMat(Rect)
Extracts a rectangular submatrix.
public Mat<TElem> SubMat(Rect roi)
Parameters
roiRectExtracted 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
rowStartintStart row of the extracted submatrix. The upper boundary is not included.
rowEndintEnd row of the extracted submatrix. The upper boundary is not included.
colStartintStart column of the extracted submatrix. The upper boundary is not included.
colEndintEnd 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
matMat
Returns
- Mat<TElem>