Table of Contents

Class Mat

Namespace
Emgu.CV
Assembly
Emgu.CV.dll

The equivalent of cv::Mat

[JsonConverter(typeof(Mat.MatJsonConverter))]
public class Mat : UnmanagedObject, IEquatable<Mat>, IInputOutputArray, IInputArrayOfArrays, IOutputArrayOfArrays, IOutputArray, IInputArray, IDisposable, ISerializable
Inheritance
Mat
Implements
Derived
Inherited Members
Extension Methods

Constructors

Mat()

Create an empty cv::Mat

public Mat()

Mat(Mat, Range, Range)

Create a mat header for the specific ROI

public Mat(Mat mat, Range rowRange, Range colRange)

Parameters

mat Mat

The mat where the new Mat header will share data from

rowRange Range

The region of interest

colRange Range

The region of interest

Mat(Mat, Rectangle)

Create a mat header for the specific ROI

public Mat(Mat mat, Rectangle roi)

Parameters

mat Mat

The mat where the new Mat header will share data from

roi Rectangle

The region of interest

Mat(Size, DepthType, int)

Create a mat of the specific type.

public Mat(Size size, DepthType type, int channels)

Parameters

size Size

Size of the Mat

type DepthType

Mat element type

channels int

Number of channels

Mat(Size, DepthType, int, nint, int)

Create a Mat header from existing data

public Mat(Size size, DepthType type, int channels, nint data, int step)

Parameters

size Size

Size of the Mat

type DepthType

Mat element type

channels int

Number of channels

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 deallocated, so you should take care of it.

step int

Number of bytes each matrix row occupies. The value should include the padding bytes at the end of each row, if any.

Mat(int, int, DepthType, int)

Create a mat of the specific type.

public Mat(int rows, int cols, DepthType type, int channels)

Parameters

rows int

Number of rows in a 2D array.

cols int

Number of columns in a 2D array.

type DepthType

Mat element type

channels int

Number of channels

Mat(int, int, DepthType, int, nint, int)

Create a Mat header from existing data

public Mat(int rows, int cols, DepthType type, int channels, nint data, int step)

Parameters

rows int

Number of rows in a 2D array.

cols int

Number of columns in a 2D array.

type DepthType

Mat element type

channels int

Number of channels

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 deallocated, so you should take care of it.

step int

Number of bytes each matrix row occupies. The value should include the padding bytes at the end of each row, if any.

Mat(int[], DepthType, nint, nint[])

Create multi-dimension mat using existing data.

public Mat(int[] sizes, DepthType type, nint data, nint[] steps = null)

Parameters

sizes int[]

The sizes of each dimension

type DepthType

The type of data

data nint

The pointer to the unmanaged data

steps nint[]

The steps

Mat(SerializationInfo, StreamingContext)

Constructor used to deserialize runtime serialized object

public Mat(SerializationInfo info, StreamingContext context)

Parameters

info SerializationInfo

The serialization info

context StreamingContext

The streaming context

Mat(string, ImreadModes)

Load the Mat from file

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

Parameters

fileName string

The name of the file

loadType ImreadModes

File loading method

Properties

Cols

The number of columns

public int Cols { get; }

Property Value

int

DataPointer

Pointer to the beginning of the raw data

public nint DataPointer { get; }

Property Value

nint

Depth

Depth type

public DepthType Depth { get; }

Property Value

DepthType

Dims

The matrix dimensionality

public int Dims { get; }

Property Value

int

ElementSize

The size of the elements in this matrix

public int ElementSize { get; }

Property Value

int

Height

Get the height of the mat.

public int Height { get; }

Property Value

int

IsContinuous

True if the data is continues

public bool IsContinuous { get; }

Property Value

bool

IsEmpty

True if the Mat is empty

public bool IsEmpty { get; }

Property Value

bool

IsSubmatrix

True if the matrix is a submatrix of another matrix

public bool IsSubmatrix { get; }

Property Value

bool

NumberOfChannels

Number of channels

public int NumberOfChannels { get; }

Property Value

int

Rows

The number of rows

public int Rows { get; }

Property Value

int

Size

The size of this matrix

public Size Size { get; }

Property Value

Size

SizeOfDimension

Get an array of the size of the dimensions. e.g. if the mat is 9x10x11, the array of {9, 10, 11} will be returned.

public int[] SizeOfDimension { get; }

Property Value

int[]

Step

Step

public int Step { get; }

Property Value

int

Total

The method returns the number of array elements (a number of pixels if the array represents an image)

public nint Total { get; }

Property Value

nint

Width

Get the width of the mat

public int Width { get; }

Property Value

int

Methods

Clone()

Make a clone of the current Mat

public Mat Clone()

Returns

Mat

A clone of the current Mat

Col(int)

Creates a matrix header for the specified matrix column.

public Mat Col(int x)

Parameters

x int

A 0-based column index.

Returns

Mat

A matrix header for the specified matrix column.

ConvertTo(IOutputArray, DepthType, double, double)

Converts an array to another data type with optional scaling.

public void ConvertTo(IOutputArray m, DepthType rtype, double alpha = 1, double beta = 0)

Parameters

m IOutputArray

Output matrix; if it does not have a proper size or type before the operation, it is reallocated.

rtype DepthType

Desired output matrix type or, rather, the depth since the number of channels are the same as the input has; if rtype is negative, the output matrix will have the same type as the input.

alpha double

Optional scale factor.

beta double

Optional delta added to the scaled values.

CopyTo(IOutputArray, IInputArray)

Copy the data in this cv::Mat to an output array

public void CopyTo(IOutputArray m, IInputArray mask = null)

Parameters

m IOutputArray

The output array to copy to

mask IInputArray

Operation mask. Its non-zero elements indicate which matrix elements need to be copied.

CopyTo<T>(T[])

Copy data from this Mat to the managed array

public void CopyTo<T>(T[] data)

Parameters

data T[]

The managed array where data will be copied to.

Type Parameters

T

The type of managed data array

Create(int, int, DepthType, int)

Allocates new array data if needed.

public void Create(int rows, int cols, DepthType type, int channels)

Parameters

rows int

New number of rows.

cols int

New number of columns.

type DepthType

New matrix element depth type.

channels int

New matrix number of channels

Cross(IInputArray)

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

public Mat Cross(IInputArray m)

Parameters

m IInputArray

Another cross-product operand.

Returns

Mat

Cross-product of two 3-element vectors.

DeserializeObjectData(SerializationInfo, StreamingContext)

A function used for runtime deserailization of the object

protected virtual void DeserializeObjectData(SerializationInfo info, StreamingContext context)

Parameters

info SerializationInfo

Serialization info

context StreamingContext

Streaming context

Diag(int)

Extracts a diagonal from a matrix. The method makes a new header for the specified matrix diagonal. The new matrix is represented as a single-column matrix. Similarly to Mat::row and Mat::col, this is an O(1) operation.

public Mat Diag(int d = 0)

Parameters

d int

Index of the diagonal, with the following values: d=0 is the main diagonal; d < 0 is a diagonal from the lower half. For example, d=-1 means the diagonal is set immediately below the main one; d > 0 is a diagonal from the upper half. For example, d=1 means the diagonal is set immediately above the main one.

Returns

Mat

A diagonal from a matrix

DisposeObject()

Release all the unmanaged memory associated with this object.

protected override void DisposeObject()

Dot(IInputArray)

Computes a dot-product of two vectors.

public double Dot(IInputArray m)

Parameters

m IInputArray

Another dot-product operand

Returns

double

The dot-product of two vectors.

Equals(Mat)

Compares two Mats and check if they are equal

public bool Equals(Mat other)

Parameters

other Mat

The other mat to compare with

Returns

bool

True if the two Mats are equal

Eye(int, int, DepthType, int)

Returns an identity matrix of the specified size and type.

public static Mat Eye(int rows, int cols, DepthType type, int channels)

Parameters

rows int

Number of rows.

cols int

Number of columns.

type DepthType

Mat element type

channels int

Number of channels

Returns

Mat

An identity matrix of the specified size and type.

GetData(bool)

Get a copy of the data values as an array

public Array GetData(bool jagged = true)

Parameters

jagged bool

If true, a jagged array will returned. Otherwise it will return a regular array.

Returns

Array

a copy of the data values as an array

GetDataPointer(params int[])

Get a pointer to the raw data given the specific index

public nint GetDataPointer(params int[] index)

Parameters

index int[]

The index to the Mat data

Returns

nint

A pointer to the raw data given the specific index

GetInputArray()

Pointer to the InputArray

public InputArray GetInputArray()

Returns

InputArray

The input array

GetInputOutputArray()

Pointer to the InputOutputArray

public InputOutputArray GetInputOutputArray()

Returns

InputOutputArray

The input output array

GetObjectData(SerializationInfo, StreamingContext)

A function used for runtime serialization of the object

public void GetObjectData(SerializationInfo info, StreamingContext context)

Parameters

info SerializationInfo

Serialization info

context StreamingContext

streaming context

GetOutputArray()

Pointer to the OutputArray

public OutputArray GetOutputArray()

Returns

OutputArray

The output array

GetRawData(params int[])

Gets the binary data from the specific indices.

public byte[] GetRawData(params int[] indices)

Parameters

indices int[]

The indices.

Returns

byte[]

The raw data in byte

Exceptions

NotImplementedException

Indices of length more than 2 is not implemented

GetSpan<T>(int)

Returns a memory span that wraps the underlying memory buffer.

public Span<T> GetSpan<T>(int size = 0) where T : struct

Parameters

size int

The size of the Span. If it is less than or equals to 0, the full span is used.

Returns

Span<T>

A memory span that wraps the underlying memory buffer.

Type Parameters

T

GetUMat(AccessType, Usage)

Convert this Mat to UMat

public UMat GetUMat(AccessType access, UMat.Usage usageFlags = Usage.Default)

Parameters

access AccessType

Access type

usageFlags UMat.Usage

Usage flags

Returns

UMat

The UMat

GetValueRange()

Get the minimum and maximum value across all channels of the mat

public RangeF GetValueRange()

Returns

RangeF

The range that contains the minimum and maximum values

MinMax(out double[], out double[], out Point[], out Point[])

Returns the min / max location and values for the image

public void MinMax(out double[] minValues, out double[] maxValues, out Point[] minLocations, out Point[] maxLocations)

Parameters

minValues double[]

The minimum values for each channel

maxValues double[]

The maximum values for each channel

minLocations Point[]

The minimum locations for each channel

maxLocations Point[]

The maximum locations for each channel

Ones(int, int, DepthType, int)

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

public static Mat Ones(int rows, int cols, DepthType type, int channels)

Parameters

rows int

Number of rows.

cols int

Number of columns.

type DepthType

Mat element type

channels int

Number of channels

Returns

Mat

An array of all 1's of the specified size and type.

PopBack(int)

The method removes one or more rows from the bottom of the matrix

public void PopBack(int value)

Parameters

value int

The value

PushBack(Mat)

Adds elements to the bottom of the matrix

public void PushBack(Mat value)

Parameters

value Mat

The value

Reshape(int, int)

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

public Mat Reshape(int cn, int rows = 0)

Parameters

cn int

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

rows int

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

Returns

Mat

A new mat header that has different shape

Row(int)

Creates a matrix header for the specified matrix row.

public Mat Row(int y)

Parameters

y int

A 0-based row index.

Returns

Mat

A matrix header for the specified matrix row.

Save(string)

Save this image to the specific file.

public void Save(string fileName)

Parameters

fileName string

The name of the file to be saved to

Remarks

The image format is chosen depending on the filename extension, see cvLoadImage. Only 8-bit single-channel or 3-channel (with 'BGR' channel order) images can be saved using this function. If the format, depth or channel order is different, use cvCvtScale and cvCvtColor to convert it before saving, or use universal cvSave to save the image to XML or YAML format.

SetTo(IInputArray, IInputArray)

Set the mat to the specific value

public void SetTo(IInputArray value, IInputArray mask = null)

Parameters

value IInputArray

The value to set to

mask IInputArray

Optional mask

SetTo(MCvScalar, IInputArray)

Set the mat to the specific value

public void SetTo(MCvScalar value, IInputArray mask = null)

Parameters

value MCvScalar

The value to set to

mask IInputArray

Optional mask

SetTo<T>(T[])

Copy data from managed array to this Mat

public void SetTo<T>(T[] data)

Parameters

data T[]

The managed array where data will be copied from

Type Parameters

T

The type of managed data array

Split()

Split current Image into an array of gray scale images where each element in the array represent a single color channel of the original image

public Mat[] Split()

Returns

Mat[]

An array of gray scale images where each element in the array represent a single color channel of the original image

T()

Transposes a matrix.

public Mat T()

Returns

Mat

The transposes of the matrix.

ToImage<TColor, TDepth>(bool)

Convert this Mat to Image

public Image<TColor, TDepth> ToImage<TColor, TDepth>(bool tryShareData = false) where TColor : struct, IColor where TDepth : new()

Parameters

tryShareData bool

If true, we will try to see if we can create an Image object that shared the pixel memory with this Mat.

Returns

Image<TColor, TDepth>

The image

Type Parameters

TColor

The type of Color

TDepth

The type of Depth

Zeros(int, int, DepthType, int)

Returns a zero array of the specified size and type.

public static Mat Zeros(int rows, int cols, DepthType type, int channels)

Parameters

rows int

Number of rows.

cols int

Number of columns.

type DepthType

Mat element type

channels int

Number of channels

Returns

Mat

A zero array of the specified size and type.

Operators

operator +(Mat, Mat)

Element wise add mat1 with mat2

public static Mat operator +(Mat mat1, Mat mat2)

Parameters

mat1 Mat

The first image to be added

mat2 Mat

The second image to be added

Returns

Mat

The sum of the two images

operator +(Mat, MCvScalar)

Element wise add mat with value

public static Mat operator +(Mat mat, MCvScalar value)

Parameters

mat Mat

The mat to be added

value MCvScalar

The value to be added

Returns

Mat

The mat plus the value

operator +(Mat, double)

Element wise add mat with value

public static Mat operator +(Mat mat, double value)

Parameters

mat Mat

The mat to be added

value double

The value to be added

Returns

Mat

The images plus the value

operator +(MCvScalar, Mat)

Element wise add mat with value

public static Mat operator +(MCvScalar value, Mat mat)

Parameters

value MCvScalar

The color to be added

mat Mat

The mat to be added

Returns

Mat

The images plus the color

operator +(double, Mat)

Element wise add mat1 with value

public static Mat operator +(double value, Mat mat1)

Parameters

value double

The value to be added

mat1 Mat

The mat to be added

Returns

Mat

The mat plus the value

operator &(Mat, Mat)

Perform an element wise AND operation on the two mats

public static Mat operator &(Mat mat1, Mat mat2)

Parameters

mat1 Mat

The first mat to AND

mat2 Mat

The second mat to AND

Returns

Mat

The result of the AND operation

operator &(Mat, MCvScalar)

Perform an element wise AND operation using a mat and a scalar

public static Mat operator &(Mat mat1, MCvScalar val)

Parameters

mat1 Mat

The first mat to AND

val MCvScalar

The value to AND

Returns

Mat

The result of the AND operation

operator &(Mat, double)

Perform an element wise AND operation using a mat and a scalar

public static Mat operator &(Mat mat1, double val)

Parameters

mat1 Mat

The first image to AND

val double

The value to AND

Returns

Mat

The result of the AND operation

operator &(MCvScalar, Mat)

Perform an element wise AND operation using a mat and a scalar

public static Mat operator &(MCvScalar val, Mat mat1)

Parameters

val MCvScalar

The scalar to AND

mat1 Mat

The first mat to AND

Returns

Mat

The result of the AND operation

operator &(double, Mat)

Perform an element wise AND operation using a mat and a color

public static Mat operator &(double val, Mat mat1)

Parameters

val double

The value to AND

mat1 Mat

The first mat to AND

Returns

Mat

The result of the AND operation

operator |(Mat, Mat)

Perform an element wise OR operation with another mat and return the result

public static Mat operator |(Mat mat1, Mat mat2)

Parameters

mat1 Mat

The first mat to apply bitwise OR operation

mat2 Mat

The second mat to apply bitwise OR operation

Returns

Mat

The result of the OR operation

operator |(Mat, MCvScalar)

Perform an binary OR operation with some scalar

public static Mat operator |(Mat mat1, MCvScalar val)

Parameters

mat1 Mat

The mat to OR

val MCvScalar

The value to OR

Returns

Mat

The result of the OR operation

operator |(Mat, double)

Perform an binary OR operation with some value

public static Mat operator |(Mat mat1, double val)

Parameters

mat1 Mat

The mat to OR

val double

The color to OR

Returns

Mat

The result of the OR operation

operator |(MCvScalar, Mat)

Perform an binary OR operation with some scalar

public static Mat operator |(MCvScalar val, Mat mat1)

Parameters

val MCvScalar

The color to OR

mat1 Mat

The mat to OR

Returns

Mat

The result of the OR operation

operator |(double, Mat)

Perform an binary OR operation with some color

public static Mat operator |(double val, Mat mat1)

Parameters

val double

The color to OR

mat1 Mat

The mat to OR

Returns

Mat

The result of the OR operation

operator /(Mat, double)

mat / scale

public static Mat operator /(Mat mat, double scale)

Parameters

mat Mat

The mat

scale double

The division scale

Returns

Mat

mat / scale

operator /(double, Mat)

scale / mat

public static Mat operator /(double scale, Mat mat)

Parameters

scale double

The scale

mat Mat

The mat

Returns

Mat

scale / mat

operator *(Mat, double)

mat * scale

public static Mat operator *(Mat mat, double scale)

Parameters

mat Mat

The mat

scale double

The multiplication scale

Returns

Mat

mat * scale

operator *(double, Mat)

scale*mat

public static Mat operator *(double scale, Mat mat)

Parameters

scale double

The multiplication scale

mat Mat

The mat

Returns

Mat

scale*mat

operator ~(Mat)

Compute the complement Mat

public static Mat operator ~(Mat mat)

Parameters

mat Mat

The mat to be inverted

Returns

Mat

The complement image

operator -(Mat, Mat)

Element wise subtract another mat from the current mat

public static Mat operator -(Mat mat1, Mat mat2)

Parameters

mat1 Mat

The mat to be subtracted from.

mat2 Mat

The second image to be subtracted from mat1

Returns

Mat

The result of element wise subtracting img2 from mat1

operator -(Mat, MCvScalar)

Element wise subtract another mat from the current mat

public static Mat operator -(Mat mat, MCvScalar value)

Parameters

mat Mat

The mat to be subtracted

value MCvScalar

The value to be subtracted

Returns

Mat

The result of element wise subtracting value from mat

operator -(Mat, double)

Element wise subtract value from the current mat

public static Mat operator -(Mat mat, double value)

Parameters

mat Mat

The mat to be subtracted

value double

The value to be subtracted

Returns

Mat

mat - value

operator -(MCvScalar, Mat)

Element wise subtract value from the current mat

public static Mat operator -(MCvScalar value, Mat mat)

Parameters

value MCvScalar

The color to be subtracted

mat Mat

The mat to be subtracted

Returns

Mat

value - mat

operator -(double, Mat)

value - mat

public static Mat operator -(double value, Mat mat)

Parameters

value double

The value to be subtracted

mat Mat

The mat to be subtracted

Returns

Mat

value - mat