美文网首页
openCV常用操作

openCV常用操作

作者: lhdoeo | 来源:发表于2018-06-03 20:58 被阅读0次

    const cv = require('opencv4nodejs');

    创建矩阵

    const rows = 100; // height
    const cols = 100; // width

    var emptyMat = new cv.Mat(rows, cols, cv.CV_8UC3);
    8位无符号3通道矩阵,就是常见的RGB数组[0-255,0-255,0-255]

    var whiteMat = new cv.Mat(rows, cols, cv.CV_8UC1, 255);
    var blueMat = new cv.Mat(rows, cols, cv.CV_8UC3, [255, 0, 0]);

    • 使用Array创建
      const matData = [
      [[255, 0, 0], [255, 0, 0], [255, 0, 0]],
      [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
      [[255, 0, 0], [255, 0, 0], [255, 0, 0]]
      ];
      var matFromArray = new cv.Mat(matData, cv.CV_8UC3);

    • 使用Buffer创建
      const charData = [255, 0, ...];
      const matFromArray = new cv.Mat(Buffer.from(charData), rows, cols, cv.CV_8UC3);

    • 创建一个点
      const pt2 = new cv.Point(100, 100);
      const pt3 = new cv.Point(100, 100, 0.5);

    • 创建一个向量
      const vec2 = new cv.Vec(100, 100);
      const vec3 = new cv.Vec(100, 100, 0.5);
      const vec4 = new cv.Vec(100, 100, 0.5, 0.5);

    矩阵、向量操作

    const mat0 = new cv.Mat(...);
    const mat1 = new cv.Mat(...);

    • 四则运算
      const matMultipliedByScalar = mat0.mul(0.5); 标量乘法
      const matDividedByScalar = mat0.div(2); 标量除法
      const mat0PlusMat1 = mat0.add(mat1); 矩阵加法
      const mat0MinusMat1 = mat0.sub(mat1); 矩阵减法
      const mat0MulMat1 = mat0.hMul(mat1); 矩阵乘法
      const mat0DivMat1 = mat0.hDiv(mat1); 矩阵除法

    • 矩阵逻辑运算操作
      const mat0AndMat1 = mat0.and(mat1);
      const mat0OrMat1 = mat0.or(mat1);
      const mat0bwAndMat1 = mat0.bitwiseAnd(mat1);
      const mat0bwOrMat1 = mat0.bitwiseOr(mat1);
      const mat0bwXorMat1 = mat0.bitwiseXor(mat1);
      const mat0bwNot = mat0.bitwiseNot();

    访问矩阵、向量数据

    const matBGR = new cv.Mat(..., cv.CV_8UC3);
    const matGray = new cv.Mat(..., cv.CV_8UC1);

    • 获取到像素点的值
      const vec3 = matBGR.at(200, 100);
      const grayVal = matGray.at(200, 100);

    • 使用解构赋值语法获取值
      const [b, g, r] = matBGR.atRaw(200, 100);

    • 设置单个像素点的值
      matBGR.set(50, 50, [255, 0, 0]);
      matBGR.set(50, 50, new Vec(255, 0, 0));
      matGray.set(50, 50, 255);

    • 得到一个偏移量为( 50, 50)矩阵区域的25x25的小区域
      const width = 25;
      const height = 25;
      const region = matBGR.getRegion(new cv.Rect(50, 50, width, height));
      const matAsBuffer = matBGR.getData();
      const matAsArray = matBGR.getDataAsArray();

    IO

    • 读取图片
      const mat = cv.imread('./path/img.jpg');
      cv.imreadAsync('./path/img.jpg', (err, mat) => {})

    • 保存图片
      cv.imwrite('./path/img.png', mat);
      cv.imwriteAsync('./path/img.jpg', mat,(err) => {})

    • 展示图片
      cv.imshow('a window name', mat);
      cv.waitKey();

    • 加载base64编码的图片
      const base64text='data:image/png;base64,R0lGO..'; //base64字符串
      const base64data =base64text.replace('data:image/jpeg;base64','')

    const buffer = Buffer.from(base64data,'base64');
    const image = cv.imdecode(buffer);

    • 转换矩阵为base64编码的图片
      const outBase64 = cv.imencode('.jpg', croppedImage).toString('base64');
      const htmlImg='<img src=data:image/jpeg;base64,'+outBase64 + '>'; //Create insert into HTML compatible <img> tag

    • 捕获webcam资源
      const devicePort = 0;
      const wCap = new cv.VideoCapture(devicePort);

    • 捕获视频资源
      const vCap = new cv.VideoCapture('./path/video.mp4');

    • 读取帧
      const frame = vCap.read();
      vCap.readAsync((err, frame) => {});

    • 循环获取
      const delay = 10;
      let done = false;
      while (!done) {
        let frame = vCap.read();
        if (frame.empty) {
          vCap.reset();
          frame = vCap.read();
        }
          const key = cv.waitKey(delay);
          done = key !== 255;
      }

    • 类型转换
      const matSignedInt = matBGR.convertTo(cv.CV_32SC3);
      const matDoublePrecision = matBGR.convertTo(cv.CV_64FC3);

    • 转换颜色制度
      const matGray = matBGR.bgrToGray();
      const matHSV = matBGR.cvtColor(cv.COLOR_BGR2HSV);
      const matLab = matBGR.cvtColor(cv.COLOR_BGR2Lab);

    • 调整矩阵大小
      const matHalfSize = matBGR.rescale(0.5);
      const mat100x100 = matBGR.resize(100, 100);
      const matMaxDimIs100 = matBGR.resizeToMax(100);

    • 获取通道、通过通道构建矩阵
      const [matB, matG, matR] = matBGR.splitChannels();
      const matRGB = new cv.Mat([matR, matB, matG]);

    相关文章

      网友评论

          本文标题:openCV常用操作

          本文链接:https://www.haomeiwen.com/subject/aiqssftx.html