Class Matrix<TDepth>
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
-
IXmlSerializableIEquatable<Matrix<TDepth>>
- 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
SizeThe size of the matrix
Matrix(int, int)
Create a matrix of the specific size
public Matrix(int rows, int cols)
Parameters
Matrix(int, int, int)
Create a matrix of the specific size and channels
public Matrix(int rows, int cols, int channels)
Parameters
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
intThe number of rows
cols
intThe number of cols
channels
intThe number of channels
data
nintThe Pinned/Unmanaged data, the data must not be release before the Matrix is Disposed
step
intThe 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
intThe number of rows
cols
intThe number of cols
data
nintThe 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
intThe number of rows
cols
intThe number of cols
data
nintThe Pinned/Unmanaged data, the data must not be release before the Matrix is Disposed
step
intThe 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
SerializationInfoThe serialization info
context
StreamingContextThe 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
this[int, int]
Get or Set the value in the specific row
and col
public TDepth this[int row, int col] { get; set; }
Parameters
Property Value
- TDepth
The element on the specific
row
andcol
MCvMat
The MCvMat structure format
public MCvMat MCvMat { get; }
Property Value
ManagedArray
Get the underneath managed array
public override Array ManagedArray { get; set; }
Property Value
NumberOfChannels
Get the number of channels for this matrix
public override int NumberOfChannels { get; }
Property Value
Size
Get the size of the array
public override Size Size { get; }
Property Value
Sum
Return the sum of the elements in this matrix
public double Sum { get; }
Property Value
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
TDepthThe 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
intThe number of rows
cols
intThe number of columns
numberOfChannels
intThe 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
Returns
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
doublethe scaling factor to apply during conversion (defaults to 1.0 -- no scaling)
shift
doublethe 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
intthe 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
intZero-based index of the selected column
endCol
intZero-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
intArray 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
intthe 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
intZero-based index of the starting row (inclusive) of the span
endRow
intZero-based index of the ending row (exclusive) of the span
deltaRow
intIndex 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
Rectanglethe 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
doubleThe minimum value
maxValue
doubleThe maximum value
minLocation
PointThe minimum location
maxLocation
PointThe maximum location
mask
IInputArrayThe 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
doubleThe 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
intZero-based index of the starting column (inclusive) of the span
endCol
intZero-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
intZero-based index of the starting row (inclusive) of the span
endRow
intZero-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
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
TDepthThe 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
TDepthThe 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
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
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
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
Returns
- Matrix<TDepth>
mat1
/val
operator *(Matrix<TDepth>, Matrix<TDepth>)
mat1
* mat2
public static Matrix<TDepth> operator *(Matrix<TDepth> mat1, Matrix<TDepth> mat2)
Parameters
Returns
- Matrix<TDepth>
mat1
*mat2
operator *(Matrix<TDepth>, double)
mat1
* val
public static Matrix<TDepth> operator *(Matrix<TDepth> mat1, double val)
Parameters
Returns
- Matrix<TDepth>
mat1
*val
operator *(double, Matrix<TDepth>)
val
* mat1
public static Matrix<TDepth> operator *(double val, Matrix<TDepth> mat1)
Parameters
Returns
- Matrix<TDepth>
val
*mat1
operator -(Matrix<TDepth>, Matrix<TDepth>)
mat1
- mat2
public static Matrix<TDepth> operator -(Matrix<TDepth> mat1, Matrix<TDepth> mat2)
Parameters
Returns
- Matrix<TDepth>
mat1
-mat2
operator -(Matrix<TDepth>, double)
mat1
- val
public static Matrix<TDepth> operator -(Matrix<TDepth> mat1, double val)
Parameters
Returns
- Matrix<TDepth>
mat1
-val
operator -(double, Matrix<TDepth>)
val
- mat1
public static Matrix<TDepth> operator -(double val, Matrix<TDepth> mat1)
Parameters
Returns
- Matrix<TDepth>
val
-mat1