上一篇我們定義了矩陣,這篇就來替它加上一些實用的方法。
toString 通常用來把物件轉成字串描述,這裡我們讓它輸出矩陣元素:
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;
};
這樣一來,就能用 console.log(mat); 輸出矩陣了。
雖然可以透過建構函式來複製,但仍提供一個方法方便記憶與使用:
Mat.prototype.clone = function(){
return new Mat(this.row, this.col, this.data);
};
我們有兩種方式取得矩陣元素。
由於 Mat 實際上以陣列形式保存資料,其排列如下:
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
其中 R、G、B、A 分別代表各通道數值,下標第一個是列號、第二個是行號。因此第 k 列、第 j 行的 G 通道值就是 Gkj。對於一個 Mat 型別的 mat 而言:
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 的部分參照,我們能取得矩陣的部分參照——例如取得某個像素點的資料陣列,改動它就會連帶改動矩陣資料;也能用其他資料型別的方式來讀取資料,這些都是普通 Array 辦不到的。以下是 at 方法的實作:
Mat.prototype.at = function(__type, __x, __y){
var type = __type, x = __x || 0, y = __y || 0, rowLen = this.col * this.channel * this.bytes, len = 1;
if(type.indexOf("Vec") > -1){
var temp = __type.match(/Vec(\d+)([a-z])/);
len = parseInt(temp[1]);
switch(temp[2]){
case "b": type = "uchar"; break;
case "s": type = "short"; break;
case "i": type = "int"; break;
case "f": type = "float"; break;
case "d": type = "double"; break;
}
}
switch(type){
case "uchar": return new Uint8Array(this.buffer, (y * rowLen + x), len); break;
case "short": return new Int16Array(this.buffer, (y * rowLen + x * 2), len); break;
case "int": return new Int32Array(this.buffer, (y * rowLen + x * 4), len); break;
case "float": return new Float32Array(this.buffer, (y * rowLen + x * 4), len); break;
case "doulble": return new Float64Array(this.buffer, (y * rowLen + x * 8), len); break;
default: console.error("不支持數據類型");
}
};
支援的資料型別有:uchar(無符號 8 位整數)、short(有符號 16 位整數)、int(有符號 32 位整數)、float(有符號 32 位浮點數)、double(有符號 64 位浮點數)。
向量形式的字串寫法是「Vec +(型別)+(個數)」,例如 Vecb4 就是 4 個無符號 8 位整數,這是取得一個像素點資料的常見方法。想取得 mat 第 j 列、第 k 行的像素資料,可用:
mat.at("Vecb4", j, k);
類似 at 的做法,我們可以輕易寫出取得某一列或某一行的方法:
Mat.prototype.getRow = function(__i){
var len = this.col * this.channel, rowLen = len * this.bytes, i = __i || 0;
return new this.data.constructor(this.buffer, i * rowLen, len);
};
Mat.prototype.getCol = function(__i){
var len = this.col * this.channel, rowLen = len * this.bytes, array = [], i = __i || 0;
function getAllElement(__constructor){
var row = this.row, channel = this.channel;
for(var j = 0; j < row; j++){ array.push(new __constructor(this.buffer, j * rowLen + i, 1 * channel)); }
}
getAllElement(this.data.constructor);
return array;
};
類似地,也能取得指定範圍的列與行:
Mat.prototype.rowRange = function(__i, __j){
var len = this.col * this.channel, rowLen = len * this.bytes, array = [], i = __i || 0, j = __j || this.row;
function getAllElement(__constructor){
var row = this.row;
for(var k = i; k <= j; k++){ array.push(new __constructor(this.buffer, k * rowLen, len)); }
}
getAllElement(this.data.constructor);
return array;
};
Mat.prototype.colRange = function(__i, __j){
var len = this.col * this.channel, rowLen = len * this.bytes, array = [], i = __i || 0, j = __j || this.col;
function getAllElement(__constructor){
var row = this.row, channel = this.channel;
for(var k = 0; k < row; k++){ array.push(new __constructor(this.buffer, k * rowLen + __i, (__j - __i + 1) * channel)); }
}
getAllElement(Float64Array);
return array;
};
這四種方法回傳的都是 Array<TypedArray> 型別的陣列。若要取得該陣列 rect 第 j 列、第 k 行的元素,用 rect[j][k] 即可。