Table of Contents

Class Mat

Namespace
OpenCvSharp
Assembly
OpenCvSharp.dll

OpenCV C++ n-dimensional dense array class (cv::Mat)

public class Mat : CvObject, IDisposable
Inheritance
Mat
Implements
Derived
Mat<TElem>
Inherited Members
Extension Methods

Constructors

Mat()

Creates empty Mat

Mat(Mat, Range, Range?)

creates a matrix header for a part of the bigger matrix

Mat(Mat, Range[])

creates a matrix header for a part of the bigger matrix

Mat(Mat, Rect)

creates a matrix header for a part of the bigger matrix

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.

Mat(MatShape, MatType, Scalar)

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

Mat(Size, MatType)

constructs 2D matrix of the specified size and type

Mat(Size, MatType, Scalar)

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

Mat(IEnumerable<int>, MatType)

constructs n-dimensional matrix

Mat(IEnumerable<int>, MatType, Scalar)

constructs n-dimensional matrix

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

constructor for matrix headers pointing to user-allocated data

Mat(int, int, MatType)

constructs 2D matrix of the specified size and type

Mat(int, int, MatType, Scalar)

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

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.

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

constructor for matrix headers pointing to user-allocated data

Mat(string, ImreadModes)

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

Fields

TypeMap

typeof(T) -> MatType

Properties

Cols

the number of columns or -1 when the array has more than 2 dimensions

Data

pointer to the data

DataEnd

The pointer that is possible to compute a relative sub-array position in the main container array using locateROI()

DataLimit

The pointer that is possible to compute a relative sub-array position in the main container array using locateROI()

DataPointer

unsafe pointer to the data

DataStart

The pointer that is possible to compute a relative sub-array position in the main container array using locateROI()

Dims

The array dimensionality. In OpenCV 5 this can also be 0 (a scalar; rows == cols == total() == 1) or 1 (a 1D array; dims == rows == 1, cols == total() == N); use Empty() to tell an empty matrix apart from a 0D scalar.

Flags

includes several bit-fields:

  • the magic signature
  • continuity flag
  • depth
  • number of channels
Height

the number of rows or -1 when the array has more than 2 dimensions

this[Range, Range]

Extracts a rectangular submatrix.

this[Range[]]

Extracts a rectangular submatrix.

this[Rect]

Extracts a rectangular submatrix.

this[int, int, int, int]

Extracts a rectangular submatrix.

this[Range, Range]

Extracts a rectangular submatrix.

Rows

the number of rows or -1 when the array has more than 2 dimensions

Width

the number of columns or -1 when the array has more than 2 dimensions

Methods

Add(Mat)
Add(Scalar)
AdjustROI(int, int, int, int)

Adjusts a submatrix size and position within the parent matrix.

Alignment(int)
AsRows<T>()

Returns a MatRowAccessor<T> that provides efficient row-by-row access with no P/Invoke per row or per element. The data pointer, step, and dimensions are captured once; all indexing is pure pointer arithmetic.

AsSpan<T>()

Creates a new span over the Mat. The matrix must be continuous (IsContinuous()); returns an empty span otherwise.

AssignTo(Mat, MatType?)

Provides a functional form of convertTo.

At<T>(int)

Returns a reference to the specified array element. For 2D matrices, i0 is the row index (dimension 0). Do NOT call this with a column index on a row-submatrix obtained from Row(int); use At<T>(int, int) or RowSpan<T>(int) instead. For performance-sensitive pixel loops, prefer AsRows<T>().

At<T>(int, int)

Returns a value to the specified array element.

At<T>(int, int, int)

Returns a value to the specified array element.

At<T>(params int[])

Returns a value to the specified array element.

BitwiseAnd(Mat)
BitwiseAnd(double)
BitwiseOr(Mat)
BitwiseOr(double)
Cast<TMat>()

Creates type-specific Mat instance from this.

Channels()

Returns the number of matrix channels.

CheckVector(int, int, bool)
Clone()

Creates a full copy of the matrix.

Clone(Rect)

Returns the partial Mat of the specified Mat

Col(int)

Creates a matrix header for the specified matrix column.

ColRange(Range)

Creates a matrix header for the specified column span.

ColRange(int, int)

Creates a matrix header for the specified column span.

ColRange(Range)

Creates a matrix header for the specified column span.

ConvertTo(OutputArray, MatType, double, double)

Converts an array to another data type with optional scaling.

CopyPixelsFrom(nint, long)

Copies row data from an unmanaged buffer that has its own row stride into this matrix. Performs no pixel-format conversion; the source buffer must already match this matrix's channel layout (convert with Cv2.CvtColor after the copy if it doesn't).

CopyPixelsTo(nint, long)

Copies this matrix's row data into an unmanaged buffer that has its own row stride, such as a UI framework's bitmap buffer. Handles submatrices and non-continuous matrices correctly. Performs no pixel-format conversion; convert the channel layout beforehand (e.g. via Cv2.CvtColor) if the destination expects a different channel order or count.

CopyTo(Mat, InputArray)

Copies the matrix to another one.

CopyTo(OutputArray, InputArray)

Copies the matrix to another one.

Create(MatType, params int[])

Allocates new array data if needed.

Create(Size, MatType)

Allocates new array data if needed.

Create(int, int, MatType)

Allocates new array data if needed.

Cross(InputArray)

Computes a cross-product of two 3-element vectors.

Depth()

Returns the depth of a matrix element.

Diag(Mat)

Extracts a diagonal from a matrix, or creates a diagonal matrix.

Diag(MatDiagType)

Single-column matrix that forms a diagonal matrix or index of the diagonal, with the following values:

DisposeManaged()

Releases managed resources, including the SafeHandle if present.

Divide(Mat)
Divide(double)
Dot(InputArray)

Computes a dot-product of two vectors.

Dump(FormatType)

Returns a string that represents each element value of Mat. This method corresponds to std::ostream << Mat

ElemSize()

Returns the matrix element size in bytes.

ElemSize1()

Returns the size of each matrix element channel in bytes.

Empty()

Returns true if the array has no elements.

EmptyClone()

Makes a Mat that have the same size, depth and channels as this image

Equals(Mat)

operator ==

Equals(double)

operator ==

Eye(Size, MatType)

Returns an identity matrix of the specified size and type.

Eye(int, int, MatType)

Returns an identity matrix of the specified size and type.

EyeMat(Size, MatType)

Returns an identity matrix of the specified size and type as Mat. Unlike Eye(Size, MatType), this method returns a Mat directly, so a single using statement is sufficient to manage its lifetime.

EyeMat(int, int, MatType)

Returns an identity matrix of the specified size and type as Mat. Unlike Eye(int, int, MatType), this method returns a Mat directly, so a single using statement is sufficient to manage its lifetime.

ForEachAsByte(MatForeachFunctionByte)

Runs the given functor over all matrix elements in parallel.

ForEachAsDouble(MatForeachFunctionDouble)

Runs the given functor over all matrix elements in parallel.

ForEachAsFloat(MatForeachFunctionFloat)

Runs the given functor over all matrix elements in parallel.

ForEachAsInt16(MatForeachFunctionInt16)

Runs the given functor over all matrix elements in parallel.

ForEachAsInt32(MatForeachFunctionInt32)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec2b(MatForeachFunctionVec2b)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec2d(MatForeachFunctionVec2d)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec2f(MatForeachFunctionVec2f)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec2i(MatForeachFunctionVec2i)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec2s(MatForeachFunctionVec2s)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec3b(MatForeachFunctionVec3b)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec3d(MatForeachFunctionVec3d)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec3f(MatForeachFunctionVec3f)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec3i(MatForeachFunctionVec3i)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec3s(MatForeachFunctionVec3s)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec4b(MatForeachFunctionVec4b)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec4d(MatForeachFunctionVec4d)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec4f(MatForeachFunctionVec4f)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec4i(MatForeachFunctionVec4i)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec4s(MatForeachFunctionVec4s)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec6b(MatForeachFunctionVec6b)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec6d(MatForeachFunctionVec6d)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec6f(MatForeachFunctionVec6f)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec6i(MatForeachFunctionVec6i)

Runs the given functor over all matrix elements in parallel.

ForEachAsVec6s(MatForeachFunctionVec6s)

Runs the given functor over all matrix elements in parallel.

FromArray<TElem>(IEnumerable<TElem>)

Initializes as N x 1 matrix and copies array data to this

FromArray<TElem>(TElem[,])

Initializes as M x N matrix and copies array data to this

FromArray<TElem>(params TElem[])

Initializes as N x 1 matrix and copies array data to this

FromImageData(byte[], ImreadModes)

Creates the Mat instance from image data (using cv::decode)

FromImageData(ReadOnlySpan<byte>, ImreadModes)

Reads image from the specified buffer in memory.

FromNativePointer(nint)

Creates from native cv::Mat* pointer

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

constructor for matrix headers pointing to user-allocated data

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

constructor for matrix headers pointing to user-allocated data

FromPixelData(int, int, MatType, Array, long)

Constructor for matrix headers pointing to user-allocated data.

FromPixelData(int, int, MatType, nint, long)

constructor for matrix headers pointing to user-allocated data

FromStream(Stream, ImreadModes)

Creates the Mat instance from System.IO.Stream

GetArray<T>(out T[])

Get the data of this matrix as array

GetGenericIndexer<T>()

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

GetRectangularArray<T>(out T[,])

Get the data of this matrix as array

GetUMat(AccessFlag, UMatUsageFlags)

Retrieve UMat from Mat

GetUnsafeGenericIndexer<T>()

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

Get<T>(int)

Returns a value to the specified array element.

Get<T>(int, int)

Returns a value to the specified array element.

Get<T>(int, int, int)

Returns a value to the specified array element.

Get<T>(params int[])

Returns a value to the specified array element.

GreaterThan(Mat)

operator >

GreaterThan(double)

operator >

GreaterThanOrEqual(Mat)

operator >=

GreaterThanOrEqual(double)

operator >=

ImDecode(byte[], ImreadModes)

Creates the Mat instance from image data (using cv::decode)

ImDecode(ReadOnlySpan<byte>, ImreadModes)

Reads image from the specified buffer in memory.

Inv(DecompTypes)

Inverses a matrix.

IsContinuous()

Reports whether the matrix is continuous or not.

IsSubmatrix()

Returns whether this matrix is a part of other matrix or not.

LessThan(Mat)

operator <

LessThan(double)

operator <

LessThanOrEqual(Mat)

operator <=

LessThanOrEqual(double)

operator <=

LocateROI(out Size, out Point)

Locates the matrix header within a parent matrix.

Mul(InputArray, double)

Performs an element-wise multiplication or division of the two matrices.

Multiply(Mat)
Multiply(double)
Negate()
NotEquals(Mat)

operator !=

NotEquals(double)

operator !=

Ones(MatShape, MatType)

Returns an array of all 1's of the specified shape and type (OpenCV 5, MatShape).

Ones(MatType, params int[])

Returns an array of all 1’s of the specified size and type.

Ones(Size, MatType)

Returns an array of all 1’s of the specified size and type.

Ones(int, int, MatType)

Returns an array of all 1’s of the specified size and type.

OnesComplement()
OnesMat(Size, MatType)

Returns an array of all 1's of the specified size and type as Mat. Unlike Ones(Size, MatType), this method returns a Mat directly, so a single using statement is sufficient to manage its lifetime.

OnesMat(int, int, MatType)

Returns an array of all 1's of the specified size and type as Mat. Unlike Ones(int, int, MatType), this method returns a Mat directly, so a single using statement is sufficient to manage its lifetime.

Plus()
PopBack(int)

removes several hyper-planes from bottom of the matrix (Mat.pop_back)

Ptr(int)

Returns a pointer to the specified matrix row.

Ptr(int, int)

Returns a pointer to the specified matrix element.

Ptr(int, int, int)

Returns a pointer to the specified matrix element.

Ptr(params int[])

Returns a pointer to the specified matrix element.

PushBack(Mat)

Adds elements to the bottom of the matrix. (Mat.push_back)

PushBack(Point)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Point2d)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Point2f)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Point3d)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Point3f)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Point3i)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Rect)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Rect2d)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Rect2f)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Size)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Size2d)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Size2f)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec2b)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec2d)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec2f)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec2i)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec2s)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec2w)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec3b)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec3d)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec3f)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec3i)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec3s)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec3w)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec4b)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec4d)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec4f)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec4i)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec4s)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec4w)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec6b)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec6d)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec6f)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec6i)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec6s)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(Vec6w)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(byte)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(double)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(short)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(int)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(sbyte)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(float)

Adds elements to the bottom of the matrix. (Mat::push_back)

PushBack(ushort)

Adds elements to the bottom of the matrix. (Mat::push_back)

Release()

Releases the resources

Reserve(int)

Reserves space for the certain number of rows.

The method reserves space for sz rows. If the matrix already has enough space to store sz rows, nothing happens. If the matrix is reallocated, the first Mat::rows rows are preserved. The method emulates the corresponding method of the STL vector class.

ReserveBuffer(int)

Reserves space for the certain number of bytes.

The method reserves space for sz bytes. If the matrix already has enough space to store sz bytes, nothing happens. If matrix has to be reallocated its previous content could be lost.

Reshape(int, MatShape)

Changes the shape (and optionally the number of channels) of the matrix without copying the data, using a MatShape (OpenCV 5).

Reshape(int, int)

Changes the shape and/or the number of channels of a 2D matrix without copying the data.

Reshape(int, int[])

Changes the shape and/or the number of channels of a 2D matrix without copying the data.

Resize(int)

Changes the number of matrix rows.

Resize(int, Scalar)

Changes the number of matrix rows.

Row(int)

Creates a matrix header for the specified matrix row.

RowRange(Range)

Creates a matrix header for the specified row span.

RowRange(int, int)

Creates a matrix header for the specified row span.

RowRange(Range)

Creates a matrix header for the specified row span.

RowSpan<T>(int)

Returns a Span<T> over a single row of this matrix without allocating a submatrix object. Padding bytes between rows are excluded from the span. For iterating all rows in a loop, prefer AsRows<T>() which captures the step once.

SetArray<T>(T[])

Set the specified array data to this matrix

SetRectangularArray<T>(T[,])

Set the specified array data to this matrix

SetTo(InputArray, Mat?)

Sets all or some of the array elements to the specified value.

SetTo(Scalar, Mat?)

Sets all or some of the array elements to the specified value.

Set<T>(int, int, int, T)

Set a value to the specified array element.

Set<T>(int, int, T)

Set a value to the specified array element.

Set<T>(int, T)

Set a value to the specified array element.

Set<T>(int[], T)

Set a value to the specified array element.

Shape()

Returns the shape of the matrix as a MatShape (OpenCV 5), including its data layout and channel count, and distinguishing an empty matrix from a 0-D scalar.

Size()

Returns a matrix size.

Size(int)

Returns a matrix size.

Step()

Returns number of bytes each matrix row occupies.

Step(int)

Returns number of bytes each matrix row occupies.

Step1(int)

Returns a normalized step.

SubMat(Range, Range)

Extracts a rectangular submatrix.

SubMat(Range[])

Extracts a rectangular submatrix.

SubMat(Rect)

Extracts a rectangular submatrix.

SubMat(int, int, int, int)

Extracts a rectangular submatrix.

SubMat(Range, Range)

Extracts a rectangular submatrix.

Subtract(Mat)
Subtract(Scalar)
T()

Transposes a matrix.

ToBytes(string, ImageEncodingParam[]?)

Encodes an image into a memory buffer.

ToMemoryStream(string, ImageEncodingParam[]?)

Converts Mat to System.IO.MemoryStream

ToString()

Returns a string that represents this Mat.

Total()

Returns the total number of array elements.

Total(int, int)

Returns the total number of array elements. The method returns the number of elements within a certain sub-array slice with startDim <= dim < endDim

Type()

Returns the type of a matrix element.

WriteToStream(Stream, string, ImageEncodingParam[]?)

Writes image data encoded from this Mat to System.IO.Stream

Xor(Mat)
Xor(double)
Zeros(MatShape, MatType)

Returns a zero array of the specified shape and type (OpenCV 5, MatShape).

Zeros(MatType, params int[])

Returns a zero array of the specified size and type.

Zeros(Size, MatType)

Returns a zero array of the specified size and type.

Zeros(int, int, MatType)

Returns a zero array of the specified size and type.

ZerosMat(Size, MatType)

Returns a zero array of the specified size and type as Mat. Unlike Zeros(Size, MatType), this method returns a Mat directly, so a single using statement is sufficient to manage its lifetime.

ZerosMat(int, int, MatType)

Returns a zero array of the specified size and type as Mat. Unlike Zeros(int, int, MatType), this method returns a Mat directly, so a single using statement is sufficient to manage its lifetime.

Operators

operator +(Mat, Mat)
operator +(Mat, Scalar)
operator +(Scalar, Mat)
operator &(Mat, Mat)
operator &(Mat, double)
operator &(double, Mat)
operator |(Mat, Mat)
operator |(Mat, double)
operator |(double, Mat)
operator /(Mat, Mat)
operator /(Mat, double)
operator /(double, Mat)
operator ^(Mat, Mat)
operator ^(Mat, double)
operator ^(double, Mat)
operator *(Mat, Mat)
operator *(Mat, double)
operator *(double, Mat)
operator ~(Mat)
operator -(Mat, Mat)
operator -(Mat, Scalar)
operator -(Scalar, Mat)
operator -(Mat)
operator +(Mat)