Class Mat
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
MatThe mat where the new Mat header will share data from
rowRange
RangeThe region of interest
colRange
RangeThe region of interest
Mat(Mat, Rectangle)
Create a mat header for the specific ROI
public Mat(Mat mat, Rectangle roi)
Parameters
Mat(Size, DepthType, int)
Create a mat of the specific type.
public Mat(Size size, DepthType type, int channels)
Parameters
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
SizeSize of the Mat
type
DepthTypeMat element type
channels
intNumber of channels
data
nintPointer 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
intNumber 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
intNumber of rows in a 2D array.
cols
intNumber of columns in a 2D array.
type
DepthTypeMat element type
channels
intNumber 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
intNumber of rows in a 2D array.
cols
intNumber of columns in a 2D array.
type
DepthTypeMat element type
channels
intNumber of channels
data
nintPointer 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
intNumber 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
DepthTypeThe type of data
data
nintThe 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
SerializationInfoThe serialization info
context
StreamingContextThe streaming context
Mat(string, ImreadModes)
Load the Mat from file
public Mat(string fileName, ImreadModes loadType = ImreadModes.Color)
Parameters
fileName
stringThe name of the file
loadType
ImreadModesFile loading method
Properties
Cols
The number of columns
public int Cols { get; }
Property Value
DataPointer
Pointer to the beginning of the raw data
public nint DataPointer { get; }
Property Value
Depth
Depth type
public DepthType Depth { get; }
Property Value
Dims
The matrix dimensionality
public int Dims { get; }
Property Value
ElementSize
The size of the elements in this matrix
public int ElementSize { get; }
Property Value
Height
Get the height of the mat.
public int Height { get; }
Property Value
IsContinuous
True if the data is continues
public bool IsContinuous { get; }
Property Value
IsEmpty
True if the Mat is empty
public bool IsEmpty { get; }
Property Value
IsSubmatrix
True if the matrix is a submatrix of another matrix
public bool IsSubmatrix { get; }
Property Value
NumberOfChannels
Number of channels
public int NumberOfChannels { get; }
Property Value
Rows
The number of rows
public int Rows { get; }
Property Value
Size
The size of this matrix
public Size Size { get; }
Property Value
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
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
Width
Get the width of the mat
public int Width { get; }
Property Value
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
intA 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
IOutputArrayOutput matrix; if it does not have a proper size or type before the operation, it is reallocated.
rtype
DepthTypeDesired 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
doubleOptional scale factor.
beta
doubleOptional 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
IOutputArrayThe output array to copy to
mask
IInputArrayOperation 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
intNew number of rows.
cols
intNew number of columns.
type
DepthTypeNew matrix element depth type.
channels
intNew matrix number of channels
Cross(IInputArray)
Computes a cross-product of two 3-element vectors.
public Mat Cross(IInputArray m)
Parameters
m
IInputArrayAnother 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
SerializationInfoSerialization info
context
StreamingContextStreaming 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
intIndex 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
IInputArrayAnother 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
MatThe 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
intNumber of rows.
cols
intNumber of columns.
type
DepthTypeMat element type
channels
intNumber 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
boolIf 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
SerializationInfoSerialization info
context
StreamingContextstreaming 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
intThe 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
AccessTypeAccess type
usageFlags
UMat.UsageUsage 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
intNumber of rows.
cols
intNumber of columns.
type
DepthTypeMat element type
channels
intNumber 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
intThe value
PushBack(Mat)
Adds elements to the bottom of the matrix
public void PushBack(Mat value)
Parameters
value
MatThe 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
intNew number of channels. If the parameter is 0, the number of channels remains the same.
rows
intNew 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
intA 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
stringThe 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
IInputArrayThe value to set to
mask
IInputArrayOptional mask
SetTo(MCvScalar, IInputArray)
Set the mat to the specific value
public void SetTo(MCvScalar value, IInputArray mask = null)
Parameters
value
MCvScalarThe value to set to
mask
IInputArrayOptional 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
boolIf 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
intNumber of rows.
cols
intNumber of columns.
type
DepthTypeMat element type
channels
intNumber 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
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
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
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
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
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
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
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
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
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
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
MatThe first mat to apply bitwise OR operation
mat2
MatThe 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
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
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
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
Returns
- Mat
The result of the OR operation
operator /(Mat, double)
mat
/ scale
public static Mat operator /(Mat mat, double scale)
Parameters
Returns
- Mat
mat
/scale
operator /(double, Mat)
scale
/ mat
public static Mat operator /(double scale, Mat mat)
Parameters
Returns
- Mat
scale
/mat
operator *(Mat, double)
mat
* scale
public static Mat operator *(Mat mat, double scale)
Parameters
Returns
- Mat
mat
*scale
operator *(double, Mat)
scale
*mat
public static Mat operator *(double scale, Mat mat)
Parameters
Returns
- Mat
scale
*mat
operator ~(Mat)
Compute the complement Mat
public static Mat operator ~(Mat mat)
Parameters
mat
MatThe 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
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
Returns
- Mat
The result of element wise subtracting
value
frommat
operator -(Mat, double)
Element wise subtract value from the current mat
public static Mat operator -(Mat mat, double value)
Parameters
Returns
- Mat
mat
-value
operator -(MCvScalar, Mat)
Element wise subtract value from the current mat
public static Mat operator -(MCvScalar value, Mat mat)
Parameters
Returns
- Mat
value
-mat
operator -(double, Mat)
value
- mat
public static Mat operator -(double value, Mat mat)
Parameters
Returns
- Mat
value
-mat