【opencv.js】Mat 类型对象的创建、复制以及类型转换


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);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM