OpenCV 2.4.3.1

org.opencv.core
Class Mat

java.lang.Object
  extended by org.opencv.core.Mat
Direct Known Subclasses:
MatOfByte, MatOfDMatch, MatOfDouble, MatOfFloat, MatOfFloat4, MatOfFloat6, MatOfInt, MatOfInt4, MatOfKeyPoint, MatOfPoint, MatOfPoint2f, MatOfPoint3, MatOfPoint3f, MatOfRect

public class Mat
extends java.lang.Object

OpenCV C++ n-dimensional dense array class

class CV_EXPORTS Mat

// C++ code:

public:

//... a lot of methods......

/ *! includes several bit-fields:

- the magic signature

- continuity flag

- depth

- number of channels

int flags;

//! the array dimensionality, >= 2

int dims;

//! the number of rows and columns or (-1, -1) when the array has more than 2 dimensions

int rows, cols;

//! pointer to the data

uchar* data;

//! pointer to the reference counter;

// when array points to user-allocated data, the pointer is NULL

int* refcount;

// other members...

};

The class Mat represents an n-dimensional dense numerical single-channel or multi-channel array. It can be used to store real or complex-valued vectors and matrices, grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms (though, very high-dimensional histograms may be better stored in a SparseMat). The data layout of the array

M is defined by the array M.step[], so that the address of element (i_0,...,i_(M.dims-1)), where 0 <= i_k<M.size[k], is computed as:

addr(M_(i_0,...,i_(M.dims-1))) = M.data + M.step[0]*i_0 + M.step[1]*i_1 +... + M.step[M.dims-1]*i_(M.dims-1)

In case of a 2-dimensional array, the above formula is reduced to:

addr(M_(i,j)) = M.data + M.step[0]*i + M.step[1]*j

Note that M.step[i] >= M.step[i+1] (in fact, M.step[i] >= M.step[i+1]*M.size[i+1]). This means that 2-dimensional matrices are stored row-by-row, 3-dimensional matrices are stored plane-by-plane, and so on. M.step[M.dims-1] is minimal and always equal to the element size M.elemSize().

So, the data layout in Mat is fully compatible with CvMat, IplImage, and CvMatND types from OpenCV 1.x. It is also compatible with the majority of dense array types from the standard toolkits and SDKs, such as Numpy (ndarray), Win32 (independent device bitmaps), and others, that is, with any array that uses *steps* (or *strides*) to compute the position of a pixel. Due to this compatibility, it is possible to make a Mat header for user-allocated data and process it in-place using OpenCV functions.

There are many different ways to create a Mat object. The most popular options are listed below:

For example, CV_8UC1 means a 8-bit single-channel array, CV_32FC2 means a 2-channel (complex) floating-point array, and so on.

// C++ code:

// make a 7x7 complex matrix filled with 1+3j.

Mat M(7,7,CV_32FC2,Scalar(1,3));

// and now turn M to a 100x60 15-channel 8-bit matrix.

// The old content will be deallocated

M.create(100,60,CV_8UC(15));

As noted in the introduction to this chapter, create() allocates only a new array when the shape or type of the current array are different from the specified ones.

// C++ code:

// create a 100x100x100 8-bit array

int sz[] = {100, 100, 100};

Mat bigCube(3, sz, CV_8U, Scalar.all(0));

It passes the number of dimensions =1 to the Mat constructor but the created array will be 2-dimensional with the number of columns set to 1. So, Mat.dims is always >= 2 (can also be 0 when the array is empty).

// C++ code:

// add the 5-th row, multiplied by 3 to the 3rd row

M.row(3) = M.row(3) + M.row(5)*3;

// now copy the 7-th column to the 1-st column

// M.col(1) = M.col(7); // this will not work

Mat M1 = M.col(1);

M.col(7).copyTo(M1);

// create a new 320x240 image

Mat img(Size(320,240),CV_8UC3);

// select a ROI

Mat roi(img, Rect(10,10,100,100));

// fill the ROI with (0,255,0) (which is green in RGB space);

// the original 320x240 image will be modified

roi = Scalar(0,255,0);

Due to the additional datastart and dataend members, it is possible to compute a relative sub-array position in the main *container* array using locateROI():

// C++ code:

Mat A = Mat.eye(10, 10, CV_32S);

// extracts A columns, 1 (inclusive) to 3 (exclusive).

Mat B = A(Range.all(), Range(1, 3));

// extracts B rows, 5 (inclusive) to 9 (exclusive).

// that is, C ~ A(Range(5, 9), Range(1, 3))

Mat C = B(Range(5, 9), Range.all());

Size size; Point ofs;

C.locateROI(size, ofs);

// size will be (width=10,height=10) and the ofs will be (x=1, y=5)

As in case of whole matrices, if you need a deep copy, use the clone() method of the extracted sub-matrices.

// C++ code:

void process_video_frame(const unsigned char* pixels,

int width, int height, int step)

Mat img(height, width, CV_8UC3, pixels, step);

GaussianBlur(img, img, Size(7,7), 1.5, 1.5);

// C++ code:

double m[3][3] = {{a, b, c}, {d, e, f}, {g, h, i}};

Mat M = Mat(3, 3, CV_64F, m).inv();

Partial yet very common cases of this *user-allocated data* case are conversions from CvMat and IplImage to Mat. For this purpose, there are special constructors taking pointers to CvMat or IplImage and the optional flag indicating whether to copy the data or not.

Backward conversion from Mat to CvMat or IplImage is provided via cast operators Mat.operator CvMat() const and Mat.operator IplImage(). The operators do NOT copy the data.

// C++ code:

IplImage* img = cvLoadImage("greatwave.jpg", 1);

Mat mtx(img); // convert IplImage* -> Mat

CvMat oldmat = mtx; // convert Mat -> CvMat

CV_Assert(oldmat.cols == img->width && oldmat.rows == img->height &&

oldmat.data.ptr == (uchar*)img->imageData && oldmat.step == img->widthStep);

// C++ code:

// create a double-precision identity martix and add it to M.

M += Mat.eye(M.rows, M.cols, CV_64F);

// C++ code:

// create a 3x3 double-precision identity matrix

Mat M = (Mat_(3,3) << 1, 0, 0, 0, 1, 0, 0, 0, 1);

With this approach, you first call a constructor of the "Mat_" class with the proper parameters, and then you just put << operator followed by comma-separated values that can be constants, variables, expressions, and so on. Also, note the extra parentheses required to avoid compilation errors.

Once the array is created, it is automatically managed via a reference-counting mechanism. If the array header is built on top of user-allocated data, you should handle the data by yourself. The array data is deallocated when no one points to it. If you want to release the data pointed by a array header before the array destructor is called, use Mat.release().

The next important thing to learn about the array class is element access. This manual already described how to compute an address of each array element. Normally, you are not required to use the formula directly in the code. If you know the array element type (which can be retrieved using the method Mat.type()), you can access the elementM_(ij) of a 2-dimensional array as:

// C++ code:

M.at(i,j) += 1.f;

assuming that M is a double-precision floating-point array. There are several variants of the method at for a different number of dimensions.

If you need to process a whole row of a 2D array, the most efficient way is to get the pointer to the row first, and then just use the plain C operator [] :

// C++ code:

// compute sum of positive matrix elements

// (assuming that M isa double-precision matrix)

double sum=0;

for(int i = 0; i < M.rows; i++)

const double* Mi = M.ptr(i);

for(int j = 0; j < M.cols; j++)

sum += std.max(Mi[j], 0.);

Some operations, like the one above, do not actually depend on the array shape. They just process elements of an array one by one (or elements from multiple arrays that have the same coordinates, for example, array addition). Such operations are called *element-wise*. It makes sense to check whether all the input/output arrays are continuous, namely, have no gaps at the end of each row. If yes, process them as a long single row:

// compute the sum of positive matrix elements, optimized variant

double sum=0;

int cols = M.cols, rows = M.rows;

if(M.isContinuous())

cols *= rows;

rows = 1;

for(int i = 0; i < rows; i++)

const double* Mi = M.ptr(i);

for(int j = 0; j < cols; j++)

sum += std.max(Mi[j], 0.);

In case of the continuous matrix, the outer loop body is executed just once. So, the overhead is smaller, which is especially noticeable in case of small matrices.

Finally, there are STL-style iterators that are smart enough to skip gaps between successive rows:

// C++ code:

// compute sum of positive matrix elements, iterator-based variant

double sum=0;

MatConstIterator_ it = M.begin(), it_end = M.end();

for(; it != it_end; ++it)

sum += std.max(*it, 0.);

The matrix iterators are random-access iterators, so they can be passed to any STL algorithm, including std.sort().

See Also:
org.opencv.core.Mat

Field Summary
 long nativeObj
           
 
Constructor Summary
Mat()
          Various Mat constructors These are various constructors that form a matrix.
Mat(int rows, int cols, int type)
          Various Mat constructors These are various constructors that form a matrix.
Mat(int rows, int cols, int type, Scalar s)
          Various Mat constructors These are various constructors that form a matrix.
Mat(long addr)
           
Mat(Mat m, Range rowRange)
          Various Mat constructors These are various constructors that form a matrix.
Mat(Mat m, Range rowRange, Range colRange)
          Various Mat constructors These are various constructors that form a matrix.
Mat(Mat m, Rect roi)
          Various Mat constructors These are various constructors that form a matrix.
Mat(Size size, int type)
          Various Mat constructors These are various constructors that form a matrix.
Mat(Size size, int type, Scalar s)
          Various Mat constructors These are various constructors that form a matrix.
 
Method Summary
 Mat adjustROI(int dtop, int dbottom, int dleft, int dright)
          Adjusts a submatrix size and position within the parent matrix.
 void assignTo(Mat m)
          Provides a functional form of convertTo.
 void assignTo(Mat m, int type)
          Provides a functional form of convertTo.
 int channels()
          Returns the number of matrix channels.
 int checkVector(int elemChannels)
           
 int checkVector(int elemChannels, int depth)
           
 int checkVector(int elemChannels, int depth, boolean requireContinuous)
           
 Mat clone()
          Creates a full copy of the array and the underlying data.
 Mat col(int x)
          Creates a matrix header for the specified matrix column.
 Mat colRange(int startcol, int endcol)
          Creates a matrix header for the specified row span.
 Mat colRange(Range r)
          Creates a matrix header for the specified row span.
 int cols()
           
 void convertTo(Mat m, int rtype)
          Converts an array to another data type with optional scaling.
 void convertTo(Mat m, int rtype, double alpha)
          Converts an array to another data type with optional scaling.
 void convertTo(Mat m, int rtype, double alpha, double beta)
          Converts an array to another data type with optional scaling.
 void copyTo(Mat m)
          Copies the matrix to another one.
 void copyTo(Mat m, Mat mask)
          Copies the matrix to another one.
 void create(int rows, int cols, int type)
          Allocates new array data if needed.
 void create(Size size, int type)
          Allocates new array data if needed.
 Mat cross(Mat m)
          Computes a cross-product of two 3-element vectors.
 long dataAddr()
           
 int depth()
          Returns the depth of a matrix element.
 Mat diag()
          Extracts a diagonal from a matrix, or creates a diagonal matrix.
 Mat diag(int d)
          Extracts a diagonal from a matrix, or creates a diagonal matrix.
static Mat diag(Mat d)
          Extracts a diagonal from a matrix, or creates a diagonal matrix.
 double dot(Mat m)
          Computes a dot-product of two vectors.
 java.lang.String dump()
           
 long elemSize()
          Returns the matrix element size in bytes.
 long elemSize1()
          Returns the size of each matrix element channel in bytes.
 boolean empty()
          Returns true if the array has no elements.
static Mat eye(int rows, int cols, int type)
          Returns an identity matrix of the specified size and type.
static Mat eye(Size size, int type)
          Returns an identity matrix of the specified size and type.
protected  void finalize()
           
 double[] get(int row, int col)
           
 int get(int row, int col, byte[] data)
           
 int get(int row, int col, double[] data)
           
 int get(int row, int col, float[] data)
           
 int get(int row, int col, int[] data)
           
 int get(int row, int col, short[] data)
           
 long getNativeObjAddr()
           
 int height()
           
 Mat inv()
          Inverses a matrix.
 Mat inv(int method)
          Inverses a matrix.
 boolean isContinuous()
          Reports whether the matrix is continuous or not.
 boolean isSubmatrix()
           
 void locateROI(Size wholeSize, Point ofs)
          Locates the matrix header within a parent matrix.
 Mat mul(Mat m)
          Performs an element-wise multiplication or division of the two matrices.
 Mat mul(Mat m, double scale)
          Performs an element-wise multiplication or division of the two matrices.
static Mat ones(int rows, int cols, int type)
          Returns an array of all 1's of the specified size and type.
static Mat ones(Size size, int type)
          Returns an array of all 1's of the specified size and type.
 void push_back(Mat m)
          Adds elements to the bottom of the matrix.
 int put(int row, int col, byte[] data)
           
 int put(int row, int col, double... data)
           
 int put(int row, int col, float[] data)
           
 int put(int row, int col, int[] data)
           
 int put(int row, int col, short[] data)
           
 void release()
          Decrements the reference counter and deallocates the matrix if needed.
 Mat reshape(int cn)
          Changes the shape and/or the number of channels of a 2D matrix without copying the data.
 Mat reshape(int cn, int rows)
          Changes the shape and/or the number of channels of a 2D matrix without copying the data.
 Mat row(int y)
          Creates a matrix header for the specified matrix row.
 Mat rowRange(int startrow, int endrow)
          Creates a matrix header for the specified row span.
 Mat rowRange(Range r)
          Creates a matrix header for the specified row span.
 int rows()
           
 Mat setTo(Mat value)
          Sets all or some of the array elements to the specified value.
 Mat setTo(Mat value, Mat mask)
          Sets all or some of the array elements to the specified value.
 Mat setTo(Scalar s)
           
 Mat setTo(Scalar value, Mat mask)
          Sets all or some of the array elements to the specified value.
 Size size()
          Returns a matrix size.
 long step1()
          Returns a normalized step.
 long step1(int i)
          Returns a normalized step.
 Mat submat(int rowStart, int rowEnd, int colStart, int colEnd)
          Extracts a rectangular submatrix.
 Mat submat(Range rowRange, Range colRange)
          Extracts a rectangular submatrix.
 Mat submat(Rect roi)
          Extracts a rectangular submatrix.
 Mat t()
          Transposes a matrix.
 java.lang.String toString()
           
 long total()
          Returns the total number of array elements.
 int type()
          Returns the type of a matrix element.
 int width()
           
static Mat zeros(int rows, int cols, int type)
          Returns a zero array of the specified size and type.
static Mat zeros(Size size, int type)
          Returns a zero array of the specified size and type.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

nativeObj

public final long nativeObj
Constructor Detail

Mat

public Mat()

Various Mat constructors

These are various constructors that form a matrix. As noted in the "AutomaticAllocation", often the default constructor is enough, and the proper matrix will be allocated by an OpenCV function. The constructed matrix can further be assigned to another matrix or matrix expression or can be allocated with "Mat.create". In the former case, the old content is de-referenced.

See Also:
org.opencv.core.Mat.Mat

Mat

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

Various Mat constructors

These are various constructors that form a matrix. As noted in the "AutomaticAllocation", often the default constructor is enough, and the proper matrix will be allocated by an OpenCV function. The constructed matrix can further be assigned to another matrix or matrix expression or can be allocated with "Mat.create". In the former case, the old content is de-referenced.

Parameters:
rows - Number of rows in a 2D array.
cols - Number of columns in a 2D array.
type - Array type. Use CV_8UC1,..., CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n),..., CV_64FC(n) to create multi-channel (up to CV_MAX_CN channels) matrices.
See Also:
org.opencv.core.Mat.Mat

Mat

public Mat(int rows,
           int cols,
           int type,
           Scalar s)

Various Mat constructors

These are various constructors that form a matrix. As noted in the "AutomaticAllocation", often the default constructor is enough, and the proper matrix will be allocated by an OpenCV function. The constructed matrix can further be assigned to another matrix or matrix expression or can be allocated with "Mat.create". In the former case, the old content is de-referenced.

Parameters:
rows - Number of rows in a 2D array.
cols - Number of columns in a 2D array.
type - Array type. Use CV_8UC1,..., CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n),..., CV_64FC(n) to create multi-channel (up to CV_MAX_CN channels) matrices.
s - An optional value to initialize each matrix element with. To set all the matrix elements to the particular value after the construction, use the assignment operator Mat.operator=(const Scalar& value).
See Also:
org.opencv.core.Mat.Mat

Mat

public Mat(long addr)

Mat

public Mat(Mat m,
           Range rowRange)

Various Mat constructors

These are various constructors that form a matrix. As noted in the "AutomaticAllocation", often the default constructor is enough, and the proper matrix will be allocated by an OpenCV function. The constructed matrix can further be assigned to another matrix or matrix expression or can be allocated with "Mat.create". In the former case, the old content is de-referenced.

Parameters:
m - Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it. The reference counter, if any, is incremented. So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m. If you want to have an independent copy of the sub-array, use Mat.clone().
rowRange - Range of the m rows to take. As usual, the range start is inclusive and the range end is exclusive. Use Range.all() to take all the rows.
See Also:
org.opencv.core.Mat.Mat

Mat

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

Various Mat constructors

These are various constructors that form a matrix. As noted in the "AutomaticAllocation", often the default constructor is enough, and the proper matrix will be allocated by an OpenCV function. The constructed matrix can further be assigned to another matrix or matrix expression or can be allocated with "Mat.create". In the former case, the old content is de-referenced.

Parameters:
m - Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it. The reference counter, if any, is incremented. So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m. If you want to have an independent copy of the sub-array, use Mat.clone().
rowRange - Range of the m rows to take. As usual, the range start is inclusive and the range end is exclusive. Use Range.all() to take all the rows.
colRange - Range of the m columns to take. Use Range.all() to take all the columns.
See Also:
org.opencv.core.Mat.Mat

Mat

public Mat(Mat m,
           Rect roi)

Various Mat constructors

These are various constructors that form a matrix. As noted in the "AutomaticAllocation", often the default constructor is enough, and the proper matrix will be allocated by an OpenCV function. The constructed matrix can further be assigned to another matrix or matrix expression or can be allocated with "Mat.create". In the former case, the old content is de-referenced.

Parameters:
m - Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it. The reference counter, if any, is incremented. So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m. If you want to have an independent copy of the sub-array, use Mat.clone().
roi - Region of interest.
See Also:
org.opencv.core.Mat.Mat

Mat

public Mat(Size size,
           int type)

Various Mat constructors

These are various constructors that form a matrix. As noted in the "AutomaticAllocation", often the default constructor is enough, and the proper matrix will be allocated by an OpenCV function. The constructed matrix can further be assigned to another matrix or matrix expression or can be allocated with "Mat.create". In the former case, the old content is de-referenced.

Parameters:
size - 2D array size: Size(cols, rows). In the Size() constructor, the number of rows and the number of columns go in the reverse order.
type - Array type. Use CV_8UC1,..., CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n),..., CV_64FC(n) to create multi-channel (up to CV_MAX_CN channels) matrices.
See Also:
org.opencv.core.Mat.Mat

Mat

public Mat(Size size,
           int type,
           Scalar s)

Various Mat constructors

These are various constructors that form a matrix. As noted in the "AutomaticAllocation", often the default constructor is enough, and the proper matrix will be allocated by an OpenCV function. The constructed matrix can further be assigned to another matrix or matrix expression or can be allocated with "Mat.create". In the former case, the old content is de-referenced.

Parameters:
size - 2D array size: Size(cols, rows). In the Size() constructor, the number of rows and the number of columns go in the reverse order.
type - Array type. Use CV_8UC1,..., CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n),..., CV_64FC(n) to create multi-channel (up to CV_MAX_CN channels) matrices.
s - An optional value to initialize each matrix element with. To set all the matrix elements to the particular value after the construction, use the assignment operator Mat.operator=(const Scalar& value).
See Also:
org.opencv.core.Mat.Mat
Method Detail

adjustROI

public Mat adjustROI(int dtop,
                     int dbottom,
                     int dleft,
                     int dright)

Adjusts a submatrix size and position within the parent matrix.

The method is complimentary to"Mat.locateROI". The typical use of these functions is to determine the submatrix position within the parent matrix and then shift the position somehow. Typically, it can be required for filtering operations when pixels outside of the ROI should be taken into account. When all the method parameters are positive, the ROI needs to grow in all directions by the specified amount, for example:

// C++ code:

A.adjustROI(2, 2, 2, 2);

In this example, the matrix size is increased by 4 elements in each direction. The matrix is shifted by 2 elements to the left and 2 elements up, which brings in all the necessary pixels for the filtering with the 5x5 kernel.

adjustROI forces the adjusted ROI to be inside of the parent matrix that is boundaries of the adjusted ROI are constrained by boundaries of the parent matrix. For example, if the submatrix A is located in the first row of a parent matrix and you called A.adjustROI(2, 2, 2, 2) then A will not be increased in the upward direction.

The function is used internally by the OpenCV filtering functions, like "filter2D", morphological operations, and so on.

Parameters:
dtop - Shift of the top submatrix boundary upwards.
dbottom - Shift of the bottom submatrix boundary downwards.
dleft - Shift of the left submatrix boundary to the left.
dright - Shift of the right submatrix boundary to the right.
See Also:
org.opencv.core.Mat.adjustROI, Imgproc.copyMakeBorder(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int, int, int, org.opencv.core.Scalar)

assignTo

public void assignTo(Mat m)

Provides a functional form of convertTo.

This is an internally used method called by the "MatrixExpressions" engine.

Parameters:
m - Destination array.
See Also:
org.opencv.core.Mat.assignTo

assignTo

public void assignTo(Mat m,
                     int type)

Provides a functional form of convertTo.

This is an internally used method called by the "MatrixExpressions" engine.

Parameters:
m - Destination array.
type - Desired destination array depth (or -1 if it should be the same as the source type).
See Also:
org.opencv.core.Mat.assignTo

channels

public int channels()

Returns the number of matrix channels.

The method returns the number of matrix channels.

See Also:
org.opencv.core.Mat.channels

checkVector

public int checkVector(int elemChannels)

checkVector

public int checkVector(int elemChannels,
                       int depth)

checkVector

public int checkVector(int elemChannels,
                       int depth,
                       boolean requireContinuous)

clone

public Mat clone()

Creates a full copy of the array and the underlying data.

The method creates a full copy of the array. The original step[] is not taken into account. So, the array copy is a continuous array occupying total()*elemSize() bytes.

Overrides:
clone in class java.lang.Object
See Also:
org.opencv.core.Mat.clone

col

public Mat col(int x)

Creates a matrix header for the specified matrix column.

The method makes a new header for the specified matrix column and returns it. This is an O(1) operation, regardless of the matrix size. The underlying data of the new matrix is shared with the original matrix. See also the "Mat.row" description.

Parameters:
x - A 0-based column index.
See Also:
org.opencv.core.Mat.col

colRange

public Mat colRange(int startcol,
                    int endcol)

Creates a matrix header for the specified row span.

The method makes a new header for the specified column span of the matrix. Similarly to "Mat.row" and "Mat.col", this is an O(1) operation.

Parameters:
startcol - An inclusive 0-based start index of the column span.
endcol - An exclusive 0-based ending index of the column span.
See Also:
org.opencv.core.Mat.colRange

colRange

public Mat colRange(Range r)

Creates a matrix header for the specified row span.

The method makes a new header for the specified column span of the matrix. Similarly to "Mat.row" and "Mat.col", this is an O(1) operation.

Parameters:
r - "Range" structure containing both the start and the end indices.
See Also:
org.opencv.core.Mat.colRange

cols

public int cols()

convertTo

public void convertTo(Mat m,
                      int rtype)

Converts an array to another data type with optional scaling.

The method converts source pixel values to the target data type. saturate_cast<> is applied at the end to avoid possible overflows:

m(x,y) = saturate _ cast<rType>(alpha(*this)(x,y) + beta)

Parameters:
m - output matrix; if it does not have a proper size or type before the operation, it is reallocated.
rtype - 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.
See Also:
org.opencv.core.Mat.convertTo

convertTo

public void convertTo(Mat m,
                      int rtype,
                      double alpha)

Converts an array to another data type with optional scaling.

The method converts source pixel values to the target data type. saturate_cast<> is applied at the end to avoid possible overflows:

m(x,y) = saturate _ cast<rType>(alpha(*this)(x,y) + beta)

Parameters:
m - output matrix; if it does not have a proper size or type before the operation, it is reallocated.
rtype - 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 - optional scale factor.
See Also:
org.opencv.core.Mat.convertTo

convertTo

public void convertTo(Mat m,
                      int rtype,
                      double alpha,
                      double beta)

Converts an array to another data type with optional scaling.

The method converts source pixel values to the target data type. saturate_cast<> is applied at the end to avoid possible overflows:

m(x,y) = saturate _ cast<rType>(alpha(*this)(x,y) + beta)

Parameters:
m - output matrix; if it does not have a proper size or type before the operation, it is reallocated.
rtype - 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 - optional scale factor.
beta - optional delta added to the scaled values.
See Also:
org.opencv.core.Mat.convertTo

copyTo

public void copyTo(Mat m)

Copies the matrix to another one.

The method copies the matrix data to another matrix. Before copying the data, the method invokes

// C++ code:

m.create(this->size(), this->type);

so that the destination matrix is reallocated if needed. While m.copyTo(m); works flawlessly, the function does not handle the case of a partial overlap between the source and the destination matrices.

When the operation mask is specified, and the Mat.create call shown above reallocated the matrix, the newly allocated matrix is initialized with all zeros before copying the data.

Parameters:
m - Destination matrix. If it does not have a proper size or type before the operation, it is reallocated.
See Also:
org.opencv.core.Mat.copyTo

copyTo

public void copyTo(Mat m,
                   Mat mask)

Copies the matrix to another one.

The method copies the matrix data to another matrix. Before copying the data, the method invokes

// C++ code:

m.create(this->size(), this->type);

so that the destination matrix is reallocated if needed. While m.copyTo(m); works flawlessly, the function does not handle the case of a partial overlap between the source and the destination matrices.

When the operation mask is specified, and the Mat.create call shown above reallocated the matrix, the newly allocated matrix is initialized with all zeros before copying the data.

Parameters:
m - Destination matrix. If it does not have a proper size or type before the operation, it is reallocated.
mask - Operation mask. Its non-zero elements indicate which matrix elements need to be copied.
See Also:
org.opencv.core.Mat.copyTo

create

public void create(int rows,
                   int cols,
                   int type)

Allocates new array data if needed.

This is one of the key Mat methods. Most new-style OpenCV functions and methods that produce arrays call this method for each output array. The method uses the following algorithm:

Such a scheme makes the memory management robust and efficient at the same time and helps avoid extra typing for you. This means that usually there is no need to explicitly allocate output arrays. That is, instead of writing:

// C++ code:

Mat color;...

Mat gray(color.rows, color.cols, color.depth());

cvtColor(color, gray, CV_BGR2GRAY);

you can simply write:

Mat color;...

Mat gray;

cvtColor(color, gray, CV_BGR2GRAY);

because cvtColor, as well as the most of OpenCV functions, calls Mat.create() for the output array internally.

Parameters:
rows - New number of rows.
cols - New number of columns.
type - New matrix type.
See Also:
org.opencv.core.Mat.create

create

public void create(Size size,
                   int type)

Allocates new array data if needed.

This is one of the key Mat methods. Most new-style OpenCV functions and methods that produce arrays call this method for each output array. The method uses the following algorithm:

Such a scheme makes the memory management robust and efficient at the same time and helps avoid extra typing for you. This means that usually there is no need to explicitly allocate output arrays. That is, instead of writing:

// C++ code:

Mat color;...

Mat gray(color.rows, color.cols, color.depth());

cvtColor(color, gray, CV_BGR2GRAY);

you can simply write:

Mat color;...

Mat gray;

cvtColor(color, gray, CV_BGR2GRAY);

because cvtColor, as well as the most of OpenCV functions, calls Mat.create() for the output array internally.

Parameters:
size - Alternative new matrix size specification: Size(cols, rows)
type - New matrix type.
See Also:
org.opencv.core.Mat.create

cross

public Mat cross(Mat m)

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

The method computes a cross-product of two 3-element vectors. The vectors must be 3-element floating-point vectors of the same shape and size. The result is another 3-element vector of the same shape and type as operands.

Parameters:
m - Another cross-product operand.
See Also:
org.opencv.core.Mat.cross

dataAddr

public long dataAddr()

depth

public int depth()

Returns the depth of a matrix element.

The method returns the identifier of the matrix element depth (the type of each individual channel). For example, for a 16-bit signed 3-channel array, the method returns CV_16S. A complete list of matrix types contains the following values:

See Also:
org.opencv.core.Mat.depth

diag

public Mat diag()

Extracts a diagonal from a matrix, or creates a diagonal 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.

See Also:
org.opencv.core.Mat.diag

diag

public Mat diag(int d)

Extracts a diagonal from a matrix, or creates a diagonal 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.

Parameters:
d - Single-column matrix that forms a diagonal matrix or 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.
See Also:
org.opencv.core.Mat.diag

diag

public static Mat diag(Mat d)

Extracts a diagonal from a matrix, or creates a diagonal 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.

Parameters:
d - Single-column matrix that forms a diagonal matrix or 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.
See Also:
org.opencv.core.Mat.diag

dot

public double dot(Mat m)

Computes a dot-product of two vectors.

The method computes a dot-product of two matrices. If the matrices are not single-column or single-row vectors, the top-to-bottom left-to-right scan ordering is used to treat them as 1D vectors. The vectors must have the same size and type. If the matrices have more than one channel, the dot products from all the channels are summed together.

Parameters:
m - another dot-product operand.
See Also:
org.opencv.core.Mat.dot

dump

public java.lang.String dump()

elemSize

public long elemSize()

Returns the matrix element size in bytes.

The method returns the matrix element size in bytes. For example, if the matrix type is CV_16SC3, the method returns 3*sizeof(short) or 6.

See Also:
org.opencv.core.Mat.elemSize

elemSize1

public long elemSize1()

Returns the size of each matrix element channel in bytes.

The method returns the matrix element channel size in bytes, that is, it ignores the number of channels. For example, if the matrix type is CV_16SC3, the method returns sizeof(short) or 2.

See Also:
org.opencv.core.Mat.elemSize1

empty

public boolean empty()

Returns true if the array has no elements.

The method returns true if Mat.total() is 0 or if Mat.data is NULL. Because of pop_back() and resize() methods M.total() == 0 does not imply that M.data == NULL.

See Also:
org.opencv.core.Mat.empty

eye

public static Mat eye(int rows,
                      int cols,
                      int type)

Returns an identity matrix of the specified size and type.

The method returns a Matlab-style identity matrix initializer, similarly to "Mat.zeros". Similarly to"Mat.ones", you can use a scale operation to create a scaled identity matrix efficiently:

// C++ code:

// make a 4x4 diagonal matrix with 0.1's on the diagonal.

Mat A = Mat.eye(4, 4, CV_32F)*0.1;

Parameters:
rows - Number of rows.
cols - Number of columns.
type - Created matrix type.
See Also:
org.opencv.core.Mat.eye

eye

public static Mat eye(Size size,
                      int type)

Returns an identity matrix of the specified size and type.

The method returns a Matlab-style identity matrix initializer, similarly to "Mat.zeros". Similarly to"Mat.ones", you can use a scale operation to create a scaled identity matrix efficiently:

// C++ code:

// make a 4x4 diagonal matrix with 0.1's on the diagonal.

Mat A = Mat.eye(4, 4, CV_32F)*0.1;

Parameters:
size - Alternative matrix size specification as Size(cols, rows).
type - Created matrix type.
See Also:
org.opencv.core.Mat.eye

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

get

public double[] get(int row,
                    int col)

get

public int get(int row,
               int col,
               byte[] data)

get

public int get(int row,
               int col,
               double[] data)

get

public int get(int row,
               int col,
               float[] data)

get

public int get(int row,
               int col,
               int[] data)

get

public int get(int row,
               int col,
               short[] data)

getNativeObjAddr

public long getNativeObjAddr()

height

public int height()

inv

public Mat inv()

Inverses a matrix.

The method performs a matrix inversion by means of matrix expressions. This means that a temporary matrix inversion object is returned by the method and can be used further as a part of more complex matrix expressions or can be assigned to a matrix.

See Also:
org.opencv.core.Mat.inv

inv

public Mat inv(int method)

Inverses a matrix.

The method performs a matrix inversion by means of matrix expressions. This means that a temporary matrix inversion object is returned by the method and can be used further as a part of more complex matrix expressions or can be assigned to a matrix.

Parameters:
method - Matrix inversion method. Possible values are the following:
  • DECOMP_LU is the LU decomposition. The matrix must be non-singular.
  • DECOMP_CHOLESKY is the Cholesky LL^T decomposition for symmetrical positively defined matrices only. This type is about twice faster than LU on big matrices.
  • DECOMP_SVD is the SVD decomposition. If the matrix is singular or even non-square, the pseudo inversion is computed.
See Also:
org.opencv.core.Mat.inv

isContinuous

public boolean isContinuous()

Reports whether the matrix is continuous or not.

The method returns true if the matrix elements are stored continuously without gaps at the end of each row. Otherwise, it returns false. Obviously, 1x1 or 1xN matrices are always continuous. Matrices created with "Mat.create" are always continuous. But if you extract a part of the matrix using "Mat.col", "Mat.diag", and so on, or constructed a matrix header for externally allocated data, such matrices may no longer have this property. The continuity flag is stored as a bit in the Mat.flags field and is computed automatically when you construct a matrix header. Thus, the continuity check is a very fast operation, though theoretically it could be done as follows:

// C++ code:

// alternative implementation of Mat.isContinuous()

bool myCheckMatContinuity(const Mat& m)

//return (m.flags & Mat.CONTINUOUS_FLAG) != 0;

return m.rows == 1 || m.step == m.cols*m.elemSize();

The method is used in quite a few of OpenCV functions. The point is that element-wise operations (such as arithmetic and logical operations, math functions, alpha blending, color space transformations, and others) do not depend on the image geometry. Thus, if all the input and output arrays are continuous, the functions can process them as very long single-row vectors. The example below illustrates how an alpha-blending function can be implemented.

template

void alphaBlendRGBA(const Mat& src1, const Mat& src2, Mat& dst)

const float alpha_scale = (float)std.numeric_limits.max(),

inv_scale = 1.f/alpha_scale;

CV_Assert(src1.type() == src2.type() &&

src1.type() == CV_MAKETYPE(DataType.depth, 4) &&

src1.size() == src2.size());

Size size = src1.size();

dst.create(size, src1.type());

// here is the idiom: check the arrays for continuity and,

// if this is the case,

// treat the arrays as 1D vectors

if(src1.isContinuous() && src2.isContinuous() && dst.isContinuous())

size.width *= size.height;

size.height = 1;

size.width *= 4;

for(int i = 0; i < size.height; i++)

// when the arrays are continuous,

// the outer loop is executed only once

const T* ptr1 = src1.ptr(i);

const T* ptr2 = src2.ptr(i);

T* dptr = dst.ptr(i);

for(int j = 0; j < size.width; j += 4)

float alpha = ptr1[j+3]*inv_scale, beta = ptr2[j+3]*inv_scale;

dptr[j] = saturate_cast(ptr1[j]*alpha + ptr2[j]*beta);

dptr[j+1] = saturate_cast(ptr1[j+1]*alpha + ptr2[j+1]*beta);

dptr[j+2] = saturate_cast(ptr1[j+2]*alpha + ptr2[j+2]*beta);

dptr[j+3] = saturate_cast((1 - (1-alpha)*(1-beta))*alpha_scale);

This approach, while being very simple, can boost the performance of a simple element-operation by 10-20 percents, especially if the image is rather small and the operation is quite simple.

Another OpenCV idiom in this function, a call of "Mat.create" for the destination array, that allocates the destination array unless it already has the proper size and type. And while the newly allocated arrays are always continuous, you still need to check the destination array because "Mat.create" does not always allocate a new matrix.

See Also:
org.opencv.core.Mat.isContinuous

isSubmatrix

public boolean isSubmatrix()

locateROI

public void locateROI(Size wholeSize,
                      Point ofs)

Locates the matrix header within a parent matrix.

After you extracted a submatrix from a matrix using "Mat.row", "Mat.col", "Mat.rowRange", "Mat.colRange", and others, the resultant submatrix points just to the part of the original big matrix. However, each submatrix contains information (represented by datastart and dataend fields) that helps reconstruct the original matrix size and the position of the extracted submatrix within the original matrix. The method locateROI does exactly that.

Parameters:
wholeSize - Output parameter that contains the size of the whole matrix containing *this as a part.
ofs - Output parameter that contains an offset of *this inside the whole matrix.
See Also:
org.opencv.core.Mat.locateROI

mul

public Mat mul(Mat m)

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

The method returns a temporary object encoding per-element array multiplication, with optional scale. Note that this is not a matrix multiplication that corresponds to a simpler "*" operator. Example:

// C++ code:

Mat C = A.mul(5/B); // equivalent to divide(A, B, C, 5)

Parameters:
m - Another array of the same type and the same size as *this, or a matrix expression.
See Also:
org.opencv.core.Mat.mul

mul

public Mat mul(Mat m,
               double scale)

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

The method returns a temporary object encoding per-element array multiplication, with optional scale. Note that this is not a matrix multiplication that corresponds to a simpler "*" operator. Example:

// C++ code:

Mat C = A.mul(5/B); // equivalent to divide(A, B, C, 5)

Parameters:
m - Another array of the same type and the same size as *this, or a matrix expression.
scale - Optional scale factor.
See Also:
org.opencv.core.Mat.mul

ones

public static Mat ones(int rows,
                       int cols,
                       int type)

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

The method returns a Matlab-style 1's array initializer, similarly to"Mat.zeros". Note that using this method you can initialize an array with an arbitrary value, using the following Matlab idiom:

// C++ code:

Mat A = Mat.ones(100, 100, CV_8U)*3; // make 100x100 matrix filled with 3.

The above operation does not form a 100x100 matrix of 1's and then multiply it by 3. Instead, it just remembers the scale factor (3 in this case) and use it when actually invoking the matrix initializer.

Parameters:
rows - Number of rows.
cols - Number of columns.
type - Created matrix type.
See Also:
org.opencv.core.Mat.ones

ones

public static Mat ones(Size size,
                       int type)

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

The method returns a Matlab-style 1's array initializer, similarly to"Mat.zeros". Note that using this method you can initialize an array with an arbitrary value, using the following Matlab idiom:

// C++ code:

Mat A = Mat.ones(100, 100, CV_8U)*3; // make 100x100 matrix filled with 3.

The above operation does not form a 100x100 matrix of 1's and then multiply it by 3. Instead, it just remembers the scale factor (3 in this case) and use it when actually invoking the matrix initializer.

Parameters:
size - Alternative to the matrix size specification Size(cols, rows).
type - Created matrix type.
See Also:
org.opencv.core.Mat.ones

push_back

public void push_back(Mat m)

Adds elements to the bottom of the matrix.

The methods add one or more elements to the bottom of the matrix. They emulate the corresponding method of the STL vector class. When elem is Mat, its type and the number of columns must be the same as in the container matrix.

Parameters:
m - a m
See Also:
org.opencv.core.Mat.push_back

put

public int put(int row,
               int col,
               byte[] data)

put

public int put(int row,
               int col,
               double... data)

put

public int put(int row,
               int col,
               float[] data)

put

public int put(int row,
               int col,
               int[] data)

put

public int put(int row,
               int col,
               short[] data)

release

public void release()

Decrements the reference counter and deallocates the matrix if needed.

The method decrements the reference counter associated with the matrix data. When the reference counter reaches 0, the matrix data is deallocated and the data and the reference counter pointers are set to NULL's. If the matrix header points to an external data set (see "Mat.Mat"), the reference counter is NULL, and the method has no effect in this case.

This method can be called manually to force the matrix data deallocation. But since this method is automatically called in the destructor, or by any other method that changes the data pointer, it is usually not needed. The reference counter decrement and check for 0 is an atomic operation on the platforms that support it. Thus, it is safe to operate on the same matrices asynchronously in different threads.

See Also:
org.opencv.core.Mat.release

reshape

public Mat reshape(int cn)

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

The method makes a new matrix header for *this elements. The new matrix may have a different size and/or different number of channels. Any combination is possible if:

  • No extra elements are included into the new matrix and no elements are excluded. Consequently, the product rows*cols*channels() must stay the same after the transformation.
  • No data is copied. That is, this is an O(1) operation. Consequently, if you change the number of rows, or the operation changes the indices of elements row in some other way, the matrix must be continuous. See "Mat.isContinuous".

For example, if there is a set of 3D points stored as an STL vector, and you want to represent the points as a 3xN matrix, do the following:

// C++ code:

std.vector vec;...

Mat pointMat = Mat(vec). // convert vector to Mat, O(1) operation

reshape(1). // make Nx3 1-channel matrix out of Nx1 3-channel.

// Also, an O(1) operation

t(); // finally, transpose the Nx3 matrix.

// This involves copying all the elements

Parameters:
cn - New number of channels. If the parameter is 0, the number of channels remains the same.
See Also:
org.opencv.core.Mat.reshape

reshape

public Mat reshape(int cn,
                   int rows)

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

The method makes a new matrix header for *this elements. The new matrix may have a different size and/or different number of channels. Any combination is possible if:

  • No extra elements are included into the new matrix and no elements are excluded. Consequently, the product rows*cols*channels() must stay the same after the transformation.
  • No data is copied. That is, this is an O(1) operation. Consequently, if you change the number of rows, or the operation changes the indices of elements row in some other way, the matrix must be continuous. See "Mat.isContinuous".

For example, if there is a set of 3D points stored as an STL vector, and you want to represent the points as a 3xN matrix, do the following:

// C++ code:

std.vector vec;...

Mat pointMat = Mat(vec). // convert vector to Mat, O(1) operation

reshape(1). // make Nx3 1-channel matrix out of Nx1 3-channel.

// Also, an O(1) operation

t(); // finally, transpose the Nx3 matrix.

// This involves copying all the elements

Parameters:
cn - New number of channels. If the parameter is 0, the number of channels remains the same.
rows - New number of rows. If the parameter is 0, the number of rows remains the same.
See Also:
org.opencv.core.Mat.reshape

row

public Mat row(int y)

Creates a matrix header for the specified matrix row.

The method makes a new header for the specified matrix row and returns it. This is an O(1) operation, regardless of the matrix size. The underlying data of the new matrix is shared with the original matrix. Here is the example of one of the classical basic matrix processing operations, axpy, used by LU and many other algorithms:

// C++ code:

inline void matrix_axpy(Mat& A, int i, int j, double alpha)

A.row(i) += A.row(j)*alpha;

Note:

In the current implementation, the following code does not work as expected:

// C++ code:

Mat A;...

A.row(i) = A.row(j); // will not work

This happens because A.row(i) forms a temporary header that is further assigned to another header. Remember that each of these operations is O(1), that is, no data is copied. Thus, the above assignment is not true if you may have expected the j-th row to be copied to the i-th row. To achieve that, you should either turn this simple assignment into an expression or use the "Mat.copyTo" method:

Mat A;...

// works, but looks a bit obscure.

A.row(i) = A.row(j) + 0;

// this is a bit longer, but the recommended method.

A.row(j).copyTo(A.row(i));

Parameters:
y - A 0-based row index.
See Also:
org.opencv.core.Mat.row

rowRange

public Mat rowRange(int startrow,
                    int endrow)

Creates a matrix header for the specified row span.

The method makes a new header for the specified row span of the matrix. Similarly to "Mat.row" and "Mat.col", this is an O(1) operation.

Parameters:
startrow - An inclusive 0-based start index of the row span.
endrow - An exclusive 0-based ending index of the row span.
See Also:
org.opencv.core.Mat.rowRange

rowRange

public Mat rowRange(Range r)

Creates a matrix header for the specified row span.

The method makes a new header for the specified row span of the matrix. Similarly to "Mat.row" and "Mat.col", this is an O(1) operation.

Parameters:
r - "Range" structure containing both the start and the end indices.
See Also:
org.opencv.core.Mat.rowRange

rows

public int rows()

setTo

public Mat setTo(Mat value)

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

Parameters:
value - Assigned scalar converted to the actual array type.
See Also:
org.opencv.core.Mat.setTo

setTo

public Mat setTo(Mat value,
                 Mat mask)

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

Parameters:
value - Assigned scalar converted to the actual array type.
mask - Operation mask of the same size as *this. This is an advanced variant of the Mat.operator=(const Scalar& s) operator.
See Also:
org.opencv.core.Mat.setTo

setTo

public Mat setTo(Scalar s)

setTo

public Mat setTo(Scalar value,
                 Mat mask)

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

Parameters:
value - Assigned scalar converted to the actual array type.
mask - Operation mask of the same size as *this. This is an advanced variant of the Mat.operator=(const Scalar& s) operator.
See Also:
org.opencv.core.Mat.setTo

size

public Size size()

Returns a matrix size.

The method returns a matrix size: Size(cols, rows). When the matrix is more than 2-dimensional, the returned size is (-1, -1).

See Also:
org.opencv.core.Mat.size

step1

public long step1()

Returns a normalized step.

The method returns a matrix step divided by "Mat.elemSize1()". It can be useful to quickly access an arbitrary matrix element.

See Also:
org.opencv.core.Mat.step1

step1

public long step1(int i)

Returns a normalized step.

The method returns a matrix step divided by "Mat.elemSize1()". It can be useful to quickly access an arbitrary matrix element.

Parameters:
i - a i
See Also:
org.opencv.core.Mat.step1

submat

public Mat submat(int rowStart,
                  int rowEnd,
                  int colStart,
                  int colEnd)

Extracts a rectangular submatrix.

The operators make a new header for the specified sub-array of *this. They are the most generalized forms of "Mat.row", "Mat.col", "Mat.rowRange", and "Mat.colRange". For example, A(Range(0, 10), Range.all()) is equivalent to A.rowRange(0, 10). Similarly to all of the above, the operators are O(1) operations, that is, no matrix data is copied.

Parameters:
rowStart - a rowStart
rowEnd - a rowEnd
colStart - a colStart
colEnd - a colEnd
See Also:
org.opencv.core.Mat.operator()

submat

public Mat submat(Range rowRange,
                  Range colRange)

Extracts a rectangular submatrix.

The operators make a new header for the specified sub-array of *this. They are the most generalized forms of "Mat.row", "Mat.col", "Mat.rowRange", and "Mat.colRange". For example, A(Range(0, 10), Range.all()) is equivalent to A.rowRange(0, 10). Similarly to all of the above, the operators are O(1) operations, that is, no matrix data is copied.

Parameters:
rowRange - Start and end row of the extracted submatrix. The upper boundary is not included. To select all the rows, use Range.all().
colRange - Start and end column of the extracted submatrix. The upper boundary is not included. To select all the columns, use Range.all().
See Also:
org.opencv.core.Mat.operator()

submat

public Mat submat(Rect roi)

Extracts a rectangular submatrix.

The operators make a new header for the specified sub-array of *this. They are the most generalized forms of "Mat.row", "Mat.col", "Mat.rowRange", and "Mat.colRange". For example, A(Range(0, 10), Range.all()) is equivalent to A.rowRange(0, 10). Similarly to all of the above, the operators are O(1) operations, that is, no matrix data is copied.

Parameters:
roi - Extracted submatrix specified as a rectangle.
See Also:
org.opencv.core.Mat.operator()

t

public Mat t()

Transposes a matrix.

The method performs matrix transposition by means of matrix expressions. It does not perform the actual transposition but returns a temporary matrix transposition object that can be further used as a part of more complex matrix expressions or can be assigned to a matrix:

// C++ code:

Mat A1 = A + Mat.eye(A.size(), A.type)*lambda;

Mat C = A1.t()*A1; // compute (A + lambda*I)^t * (A + lamda*I)

See Also:
org.opencv.core.Mat.t

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

total

public long total()

Returns the total number of array elements.

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

See Also:
org.opencv.core.Mat.total

type

public int type()

Returns the type of a matrix element.

The method returns a matrix element type. This is an identifier compatible with the CvMat type system, like CV_16SC3 or 16-bit signed 3-channel array, and so on.

See Also:
org.opencv.core.Mat.type

width

public int width()

zeros

public static Mat zeros(int rows,
                        int cols,
                        int type)

Returns a zero array of the specified size and type.

The method returns a Matlab-style zero array initializer. It can be used to quickly form a constant array as a function parameter, part of a matrix expression, or as a matrix initializer.

// C++ code:

Mat A;

A = Mat.zeros(3, 3, CV_32F);

In the example above, a new matrix is allocated only if A is not a 3x3 floating-point matrix. Otherwise, the existing matrix A is filled with zeros.

Parameters:
rows - Number of rows.
cols - Number of columns.
type - Created matrix type.
See Also:
org.opencv.core.Mat.zeros

zeros

public static Mat zeros(Size size,
                        int type)

Returns a zero array of the specified size and type.

The method returns a Matlab-style zero array initializer. It can be used to quickly form a constant array as a function parameter, part of a matrix expression, or as a matrix initializer.

// C++ code:

Mat A;

A = Mat.zeros(3, 3, CV_32F);

In the example above, a new matrix is allocated only if A is not a 3x3 floating-point matrix. Otherwise, the existing matrix A is filled with zeros.

Parameters:
size - Alternative to the matrix size specification Size(cols, rows).
type - Created matrix type.
See Also:
org.opencv.core.Mat.zeros

Official OpenCV 2.4 Documentation