// 1. default constructorletmat = newcv.Mat();
// 2. two-dimensional arrays by size and typeletmat = newcv.Mat(size, type);
// 3. two-dimensional arrays by rows, cols, and typeletmat = newcv.Mat(rows, cols, type);
// 4. two-dimensional arrays by rows, cols, and type with initialization valueletmat = newcv.Mat(rows, cols, type, newcv.Scalar());
There are 3 static functions:
// 1. Create a Mat which is full of zerosletmat = cv.Mat.zeros(rows, cols, type);
// 2. Create a Mat which is full of onesletmat = cv.Mat.ones(rows, cols, type);
// 3. Create a Mat which is an identity matrixletmat = cv.Mat.eye(rows, cols, type);
There are 2 factory functions:
// 1. Use JS array to construct a mat.// For example: let mat = cv.matFromArray(2, 2, cv.CV_8UC1, [1, 2, 3, 4]);letmat = cv.matFromArray(rows, cols, type, array);
// 2. Use imgData to construct a matletctx = canvas.getContext("2d");
letimgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
letmat = cv.matFromImageData(imgData);
How to copy Mat
There are 2 ways to copy a Mat:
// 1. Cloneletdst = src.clone();
// 2. CopyTo(only entries indicated in the mask are copied)src.copyTo(dst, mask);
How to convert the type of Mat
We use the function: convertTo(m, rtype, alpha = 1, beta = 0)
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.
src.convertTo(dst, rtype);
How use MatVector
letmat = newcv.Mat();
// Initialise a MatVectorletmatVec = newcv.MatVector();
// Push a Mat back into MatVectormatVec.push_back(mat);
// Get a Mat fom MatVectorletcnt = matVec.get(0);
mat.delete(); matVec.delete(); cnt.delete();
Accessing and Modifying pixel values
Firstly, you should know the following type relationship:
Data Properties
C++ Type
JavaScript Typed Array
Mat Type
data
uchar
Uint8Array
CV_8U
data8S
char
Int8Array
CV_8S
data16U
ushort
Uint16Array
CV_16U
data16S
short
Int16Array
CV_16S
data32S
int
Int32Array
CV_32S
data32F
float
Float32Array
CV_32F
data64F
double
Float64Array
CV_64F
1. data
letrow = 3, col = 4;
letsrc = cv.imread("canvasInput");
if (src.isContinuous()) {
letR = src.data[row * src.cols * src.channels() + col * src.channels()];
letG = src.data[row * src.cols * src.channels() + col * src.channels() + 1];
letB = src.data[row * src.cols * src.channels() + col * src.channels() + 2];
letA = src.data[row * src.cols * src.channels() + col * src.channels() + 3];
}
2. at
Mat Type
At Manipulation
CV_8U
ucharAt
CV_8S
charAt
CV_16U
ushortAt
CV_16S
shortAt
CV_32S
intAt
CV_32F
floatAt
CV_64F
doubleAt
letrow = 3, col = 4;
letsrc = cv.imread("canvasInput");
letR = src.ucharAt(row, col * src.channels());
letG = src.ucharAt(row, col * src.channels() + 1);
letB = src.ucharAt(row, col * src.channels() + 2);
letA = src.ucharAt(row, col * src.channels() + 3);
mat.ucharPtr(k) get the k th row of the mat. mat.ucharPtr(i, j) get the i th row and the j th column of the mat.
Image ROI
Sometimes, you will have to play with certain region of images. For eye detection in images, first face detection is done all over the image and when face is obtained, we select the face region alone and search for eyes inside it instead of searching whole image. It improves accuracy (because eyes are always on faces) and performance (because we search for a small area)
Sometimes you will need to work separately on R,G,B channels of image. Then you need to split the RGB images to single planes. Or another time, you may need to join these individual channels to RGB image.
letsrc = cv.imread("canvasInput");
letrgbaPlanes = newcv.MatVector();
// Split the Matcv.split(src, rgbaPlanes);
// Get R channelletR = rgbaPlanes.get(0);
// Merge all channelscv.merge(rgbaPlanes, src);
src.delete(); rgbaPlanes.delete(); R.delete();
Making Borders for Images (Padding)
If you want to create a border around the image, something like a photo frame, you can use cv.copyMakeBorder() function. But it has more applications for convolution operation, zero padding etc. This function takes following arguments:
src - input image
top, bottom, left, right - border width in number of pixels in corresponding directions
borderType - Flag defining what kind of border to be added. It can be following types:
cv.BORDER_CONSTANT - Adds a constant colored border. The value should be given as next argument.
cv.BORDER_REFLECT - Border will be mirror reflection of the border elements, like this : fedcba|abcdefgh|hgfedcb