JavaScript 寫法類似於 C++ 寫法。
相關內容詳細介紹請移步官網:【https://docs.opencv.org/3.3.1/de/d06/tutorial_js_basic_ops.html】
Mat 類型對象的創建
// 1. 默認構造方式
let mat = new cv.Mat();
// 2. Create a Mat by size and type
let mat = new cv.Mat(size, type);
// 3. Create a Mat by rows, cols, and type
let mat = new cv.Mat(rows, cols, type)
// 4. Create a Mat by rows, cols, and type with initialization value
let mat = new cv.Mat(rows, cols, type, new cv.Scalar());
// 5. Create a Mat which is full of zeros
let mat = cv.Mat.zeros(rows, cols, type);
// 6. Create a Mat which is full of ones
let mat = cv.Mat.ones(rows, cols, type);
// 7. 對角矩陣
let mat = cv.Mat.eye(rows, cols, type);
// 8. Use JS array to construct a mat. // For example: let mat = cv.matFromArray(2, 2, cv.CV_8UC1, [1, 2, 3, 4]);
let mat = cv.matFromArray(rows, cols, type, array);
// 9. Use imgData to construct a mat
let ctx = canvas.getContext("2d");
let imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
let mat = cv.matFromImageData(imgData);
Mat 類型對象的復制
// 1. Clone
let dst = src.clone();
// 2. CopyTo(only entries indicated in the mask are copied)
src.copyTo(dst, mask);
Mat 類型對象的類型轉換
src.convertTo(dst, rtype);