Objective
In the previous article, we defined the matrix, this article we will add some common methods to the matrix.
ToString Method
The ToString method is often used as a description of the object as a string, so we define this method as an output matrix element.
Mat.prototype.toString = function () {
var tempdata = this.data,
text = "Mat (" + This.type + ") = {\ n",
num = this . Col * This.channel;
for (var i = 0; i < This.row i++) {
text = ' [For
(var j = 0; J < num; J + +) {
Text = = (Tempdata[i * num + j] + ",");
}
Text + "]\n";
}
Text + = "}";
return text;
In this way, we can go through:
Console.log (MAT);
To output the matrix.
Clone method
In fact, we can clone operations through constructors, but still provide a way to facilitate memory and use.
Mat.prototype.clone = function () {return
new Mat (This.row, This.col, this.data);
Gets the specified element
We have two ways to get the matrix element.
Array method
Because the mat actually holds the data as an array, the data looks like this:
R00 G00 B00 A00 R01 G01 B01 A01 ... r0n g0n b0n a0n
R10 G10 B10 A10 R11 G11 B11 A11 ... r1n g1n b1n a1n
......
Rm0 Gm0 Bm0 Am0 Rm1 Gm1 Bm1 Am1 ... RMN Gmn Bmn Amn
Where capital R, G, B, and a represent the values of each channel, and the subscript first represents the line number, and the second represents the column number. That is, line K, the G-Channel value of column J is gkj.
We can easily get to a mat type of mat, the K line, each element of the J-column Pixel is:
Rkj = mat.data[(k * Mat.col + j) * 4 + 0]
Gkj = mat.data[(k * Mat.col + j) * 4 + 1]
Bkj = mat.data[(k * Mat.col + j) * 4 + 2]
AKJ = mat.data[(k * Mat.col + j) * 4 + 3]
Buffer Partial Reference method