Table of Contents

Class Matrix<TDepth>

Namespace
Emgu.CV
Assembly
Emgu.CV.dll

A Matrix is a wrapper to cvMat of OpenCV.

public class Matrix<TDepth> : CvArray<TDepth>, IXmlSerializable, IInputOutputArray, IInputArrayOfArrays, IOutputArrayOfArrays, IOutputArray, IInputArray, IDisposable, ISerializable, IEquatable<Matrix<TDepth>>, ICloneable where TDepth : new()

Type Parameters

TDepth

Depth of this matrix (either Byte, SByte, Single, double, UInt16, Int16 or Int32)

Inheritance
CvArray<TDepth>
Matrix<TDepth>
Implements
IXmlSerializable
Derived
Inherited Members
Extension Methods

Constructors

Matrix()

The default constructor which allows Data to be set later on

protected Matrix()

Matrix(Size)

Create a matrix of the specific size

public Matrix(Size size)

Parameters

size Size

The size of the matrix

Matrix(int, int)

Create a matrix of the specific size

public Matrix(int rows, int cols)

Parameters

rows int

The number of rows (height)

cols int

The number of cols (width)

Matrix(int, int, int)

Create a matrix of the specific size and channels

public Matrix(int rows, int cols, int channels)

Parameters

rows int

The number of rows

cols int

The number of cols

channels int

The number of channels

Matrix(int, int, int, nint, int)

Create a Matrix (only header is allocated) using the Pinned/Unmanaged data. The data is not freed by the disposed function of this class

public Matrix(int rows, int cols, int channels, nint data, int step)

Parameters

rows int

The number of rows

cols int

The number of cols

channels int

The number of channels

data nint

The Pinned/Unmanaged data, the data must not be release before the Matrix is Disposed

step int

The step (row stride in bytes)

Remarks

The caller is responsible for allocating and freeing the block of memory specified by the data parameter, however, the memory should not be released until the related Matrix is released.

Matrix(int, int, nint)

Create a Matrix (only header is allocated) using the Pinned/Unmanaged data. The data is not freed by the disposed function of this class

public Matrix(int rows, int cols, nint data)

Parameters

rows int

The number of rows

cols int

The number of cols

data nint

The Pinned/Unmanaged data, the data must not be release before the Matrix is Disposed

Remarks

The caller is responsible for allocating and freeing the block of memory specified by the data parameter, however, the memory should not be released until the related Matrix is released.

Matrix(int, int, nint, int)

Create a Matrix (only header is allocated) using the Pinned/Unmanaged data. The data is not freed by the disposed function of this class

public Matrix(int rows, int cols, nint data, int step)

Parameters

rows int

The number of rows

cols int

The number of cols

data nint

The Pinned/Unmanaged data, the data must not be release before the Matrix is Disposed

step int

The step (row stride in bytes)

Remarks

The caller is responsible for allocating and freeing the block of memory specified by the data parameter, however, the memory should not be released until the related Matrix is released.

Matrix(SerializationInfo, StreamingContext)

Constructor used to deserialize runtime serialized object

public Matrix(SerializationInfo info, StreamingContext context)

Parameters

info SerializationInfo

The serialization info

context StreamingContext

The streaming context

Matrix(TDepth[,])

Create a matrix using the specific data.

public Matrix(TDepth[,] data)

Parameters

data TDepth[,]

The data will be used as the Matrix data storage. You need to make sure that the data object live as long as this Matrix object

Remarks

The data will be used as the Matrix data storage. You need to make sure that the data object live as long as this Matrix object

Matrix(TDepth[])

Create a matrix using the specific data

public Matrix(TDepth[] data)

Parameters

data TDepth[]

the data for this matrix

Properties

Data

Get or Set the data for this matrix

public TDepth[,] Data { get; set; }

Property Value

TDepth[,]

Det

Returns determinant of the square matrix

public double Det { get; }

Property Value

double

this[int, int]

Get or Set the value in the specific row and col

public TDepth this[int row, int col] { get; set; }

Parameters

row int

the row of the element

col int

the col of the element

Property Value

TDepth

The element on the specific row and col

MCvMat

The MCvMat structure format

public MCvMat MCvMat { get; }

Property Value

MCvMat

ManagedArray

Get the underneath managed array

public override Array ManagedArray { get; set; }

Property Value

Array

NumberOfChannels

Get the number of channels for this matrix

public override int NumberOfChannels { get; }

Property Value

int

Size

Get the size of the array

public override Size Size { get; }

Property Value

Size

Sum

Return the sum of the elements in this matrix

public double Sum { get; }

Property Value

double

Methods

Add(Matrix<TDepth>)

Elementwise add another matrix with the current matrix

public Matrix<TDepth> Add(Matrix<TDepth> mat2)

Parameters

mat2 Matrix<TDepth>

The matrix to be added to the current matrix

Returns

Matrix<TDepth>

The result of elementwise adding mat2 to the current matrix

Add(TDepth)

Elementwise add a color val to the current matrix

public Matrix<TDepth> Add(TDepth val)

Parameters

val TDepth

The value to be added to the current matrix

Returns

Matrix<TDepth>

The result of elementwise adding val from the current matrix

AllocateData(int, int, int)

Allocate data for the array

protected override void AllocateData(int rows, int cols, int numberOfChannels)

Parameters

rows int

The number of rows

cols int

The number of columns

numberOfChannels int

The number of channels for this matrix

Clone()

Make a copy of this matrix

public virtual Matrix<TDepth> Clone()

Returns

Matrix<TDepth>

A copy if this matrix

Cmp(Matrix<TDepth>, CmpType)

This function compare the current matrix with mat2 and returns the comparison mask

public Matrix<byte> Cmp(Matrix<TDepth> mat2, CmpType type)

Parameters

mat2 Matrix<TDepth>

The other matrix to compare with

type CmpType

Comparison type

Returns

Matrix<byte>

The comparison mask

ConcateHorizontal(Matrix<TDepth>)

Concate the current matrix with another matrix horizontally. If this matrix is n x m1 and otherMatrix is n x m2, the resulting matrix is n x (m1 + m2).

public Matrix<TDepth> ConcateHorizontal(Matrix<TDepth> otherMatrix)

Parameters

otherMatrix Matrix<TDepth>

The other matrix to concate

Returns

Matrix<TDepth>

A matrix that is the horizontal concatening of this matrix and otherMatrix

ConcateVertical(Matrix<TDepth>)

Concate the current matrix with another matrix vertically. If this matrix is n1 x m and otherMatrix is n2 x m, the resulting matrix is (n1+n2) x m.

public Matrix<TDepth> ConcateVertical(Matrix<TDepth> otherMatrix)

Parameters

otherMatrix Matrix<TDepth>

The other matrix to concate

Returns

Matrix<TDepth>

A new matrix that is the vertical concatening of this matrix and otherMatrix

Convert<TOtherDepth>(double, double)

Convert this matrix to different depth

public Matrix<TOtherDepth> Convert<TOtherDepth>(double scale = 1, double shift = 0) where TOtherDepth : new()

Parameters

scale double

the scaling factor to apply during conversion (defaults to 1.0 -- no scaling)

shift double

the shift factor to apply during conversion (defaults to 0.0 -- no shifting)

Returns

Matrix<TOtherDepth>

Matrix of different depth

Type Parameters

TOtherDepth

The depth type to convert to

CopyBlank()

Return a matrix of the same size with all elements equals 0

public Matrix<TDepth> CopyBlank()

Returns

Matrix<TDepth>

A matrix of the same size with all elements equals 0

DisposeObject()

Release the matrix and all the memory associate with it

protected override void DisposeObject()

Equals(Matrix<TDepth>)

Return true if every element of this matrix equals elements in mat2

public bool Equals(Matrix<TDepth> mat2)

Parameters

mat2 Matrix<TDepth>

The other matrix to compare with

Returns

bool

true if every element of this matrix equals elements in mat2

GetCol(int)

Get the specific column of the matrix

public Matrix<TDepth> GetCol(int col)

Parameters

col int

the index of the column to be reterived

Returns

Matrix<TDepth>

the specific column of the matrix

GetCols(int, int)

Get the Matrix, corresponding to a specified column span of the input array

public Matrix<TDepth> GetCols(int startCol, int endCol)

Parameters

startCol int

Zero-based index of the selected column

endCol int

Zero-based index of the ending column (exclusive) of the span

Returns

Matrix<TDepth>

the specific column span of the matrix

GetDiag()

Return the main diagonal element of this matrix

public Matrix<TDepth> GetDiag()

Returns

Matrix<TDepth>

The main diagonal element of this matrix

GetDiag(int)

Return the specific diagonal elements of this matrix

public Matrix<TDepth> GetDiag(int diag)

Parameters

diag int

Array diagonal. Zero corresponds to the main diagonal, -1 corresponds to the diagonal above the main etc., 1 corresponds to the diagonal below the main etc

Returns

Matrix<TDepth>

The specific diagonal elements of this matrix

GetRow(int)

Get the specific row of the matrix

public Matrix<TDepth> GetRow(int row)

Parameters

row int

the index of the row to be reterived

Returns

Matrix<TDepth>

the specific row of the matrix

GetRows(int, int, int)

Return the matrix corresponding to a specified row span of the input array

public Matrix<TDepth> GetRows(int startRow, int endRow, int deltaRow)

Parameters

startRow int

Zero-based index of the starting row (inclusive) of the span

endRow int

Zero-based index of the ending row (exclusive) of the span

deltaRow int

Index step in the row span. That is, the function extracts every delta_row-th row from start_row and up to (but not including) end_row

Returns

Matrix<TDepth>

A matrix corresponding to a specified row span of the input array

GetSubRect(Rectangle)

Get a submatrix corresponding to a specified rectangle

public Matrix<TDepth> GetSubRect(Rectangle rect)

Parameters

rect Rectangle

the rectangle area of the sub-matrix

Returns

Matrix<TDepth>

A submatrix corresponding to a specified rectangle

MinMax(out double, out double, out Point, out Point, IInputArray)

Returns the min / max locations and values for the matrix

public void MinMax(out double minValue, out double maxValue, out Point minLocation, out Point maxLocation, IInputArray mask = null)

Parameters

minValue double

The minimum value

maxValue double

The maximum value

minLocation Point

The minimum location

maxLocation Point

The maximum location

mask IInputArray

The optional mask

Mul(Matrix<TDepth>)

Multiply the current matrix with mat2

public Matrix<TDepth> Mul(Matrix<TDepth> mat2)

Parameters

mat2 Matrix<TDepth>

The matrix to be multiplied

Returns

Matrix<TDepth>

Result matrix of the multiplication

Mul(double)

Multiply the current matrix with scale

public Matrix<TDepth> Mul(double scale)

Parameters

scale double

The scale to be multiplied

Returns

Matrix<TDepth>

The scaled matrix

RemoveCols(int, int)

Return the matrix without a specified column span of the input array

public Matrix<TDepth> RemoveCols(int startCol, int endCol)

Parameters

startCol int

Zero-based index of the starting column (inclusive) of the span

endCol int

Zero-based index of the ending column (exclusive) of the span

Returns

Matrix<TDepth>

The matrix without a specified column span of the input array

RemoveRows(int, int)

Return the matrix without a specified row span of the input array

public Matrix<TDepth> RemoveRows(int startRow, int endRow)

Parameters

startRow int

Zero-based index of the starting row (inclusive) of the span

endRow int

Zero-based index of the ending row (exclusive) of the span

Returns

Matrix<TDepth>

The matrix without a specified row span of the input array

Reshape(int, int)

Get reshaped matrix which also share the same data with the current matrix

public Matrix<TDepth> Reshape(int newChannels, int newRows)

Parameters

newChannels int

the new number of channles

newRows int

The new number of rows

Returns

Matrix<TDepth>

A reshaped matrix which also share the same data with the current matrix

Split()

Get all channels for the multi channel matrix

public Matrix<TDepth>[] Split()

Returns

Matrix<TDepth>[]

Each individual channel of this matrix

Sub(Matrix<TDepth>)

Elementwise subtract another matrix from the current matrix

public Matrix<TDepth> Sub(Matrix<TDepth> mat2)

Parameters

mat2 Matrix<TDepth>

The matrix to be subtracted to the current matrix

Returns

Matrix<TDepth>

The result of elementwise subtracting mat2 from the current matrix

Sub(TDepth)

Elementwise subtract a color val to the current matrix

public Matrix<TDepth> Sub(TDepth val)

Parameters

val TDepth

The value to be subtracted from the current matrix

Returns

Matrix<TDepth>

The result of elementwise subtracting val from the current matrix

SubR(TDepth)

result = val - this

public Matrix<TDepth> SubR(TDepth val)

Parameters

val TDepth

The value which subtract this matrix

Returns

Matrix<TDepth>

val - this

Transpose()

Returns the transpose of this matrix

public Matrix<TDepth> Transpose()

Returns

Matrix<TDepth>

The transpose of this matrix

Operators

operator +(Matrix<TDepth>, Matrix<TDepth>)

Elementwise add mat1 with mat2

public static Matrix<TDepth> operator +(Matrix<TDepth> mat1, Matrix<TDepth> mat2)

Parameters

mat1 Matrix<TDepth>

The Matrix to be added

mat2 Matrix<TDepth>

The Matrix to be added

Returns

Matrix<TDepth>

The elementwise sum of the two matrices

operator +(Matrix<TDepth>, double)

Elementwise add mat1 with val

public static Matrix<TDepth> operator +(Matrix<TDepth> mat1, double val)

Parameters

mat1 Matrix<TDepth>

The Matrix to be added

val double

The value to be added

Returns

Matrix<TDepth>

The matrix plus the value

operator +(double, Matrix<TDepth>)

val + mat1

public static Matrix<TDepth> operator +(double val, Matrix<TDepth> mat1)

Parameters

val double

The value to be added

mat1 Matrix<TDepth>

The Matrix to be added

Returns

Matrix<TDepth>

The matrix plus the value

operator /(Matrix<TDepth>, double)

mat1 / val

public static Matrix<TDepth> operator /(Matrix<TDepth> mat1, double val)

Parameters

mat1 Matrix<TDepth>

The Matrix to be divided

val double

The value to be divided

Returns

Matrix<TDepth>

mat1 / val

operator *(Matrix<TDepth>, Matrix<TDepth>)

mat1 * mat2

public static Matrix<TDepth> operator *(Matrix<TDepth> mat1, Matrix<TDepth> mat2)

Parameters

mat1 Matrix<TDepth>

The Matrix to be multiplied

mat2 Matrix<TDepth>

The Matrix to be multiplied

Returns

Matrix<TDepth>

mat1 * mat2

operator *(Matrix<TDepth>, double)

mat1 * val

public static Matrix<TDepth> operator *(Matrix<TDepth> mat1, double val)

Parameters

mat1 Matrix<TDepth>

The Matrix to be multiplied

val double

The value to be multiplied

Returns

Matrix<TDepth>

mat1 * val

operator *(double, Matrix<TDepth>)

val * mat1

public static Matrix<TDepth> operator *(double val, Matrix<TDepth> mat1)

Parameters

val double

The value to be multiplied

mat1 Matrix<TDepth>

The matrix to be multiplied

Returns

Matrix<TDepth>

val * mat1

operator -(Matrix<TDepth>, Matrix<TDepth>)

mat1 - mat2

public static Matrix<TDepth> operator -(Matrix<TDepth> mat1, Matrix<TDepth> mat2)

Parameters

mat1 Matrix<TDepth>

The Matrix to be subtracted

mat2 Matrix<TDepth>

The matrix to subtract

Returns

Matrix<TDepth>

mat1 - mat2

operator -(Matrix<TDepth>, double)

mat1 - val

public static Matrix<TDepth> operator -(Matrix<TDepth> mat1, double val)

Parameters

mat1 Matrix<TDepth>

The Matrix to be subtracted

val double

The value to be subtracted

Returns

Matrix<TDepth>

mat1 - val

operator -(double, Matrix<TDepth>)

val - mat1

public static Matrix<TDepth> operator -(double val, Matrix<TDepth> mat1)

Parameters

val double

The value to be subtracted

mat1 Matrix<TDepth>

The Matrix to be subtracted

Returns

Matrix<TDepth>

val - mat1