首页 > 解决方案 > robots-js + opencv 截图以灰度显示

问题描述


现在我正在尝试在opencv中使用节点模块robot-js制作的屏幕截图。

但由于某种原因,它以灰度显示。
这是我的代码:

My code:
    'use strict';

    const opencv = require('opencv'),
      robot  = require('robot-js'),
      fs     = require ("fs");


    var process         = robot.Process.getList("calc.exe")[0],
        stream_wnd      = new opencv.NamedWindow('Video',0),
        target_wnd      = process.getWindows()[0],
        _image          = robot.Image(),
        _bounds         = target_wnd.getBounds ();

    while(true)
    {



    robot.Screen.grabScreen(_image,0,0,_bounds.w,_bounds.h,
                            process.getWindows()[0]);

    stream_wnd.show(img2mat(_image,_bounds.h ,_bounds.w ));

    stream_wnd.blockingWaitKey(1);
}   

// imaget to matrix
function img2mat(img,size_x,size_y)
{
    var _buffer     = Buffer(size_x*size_y),
        _data       = img.getData(),
        _matrix     = new opencv.Matrix(size_x,size_y,opencv.Constants.CV_8UC1);

    for(let index = 0; index < size_x*size_y ; index++)
    {
        _buffer[index] = _data[index] ; 
    }
    _matrix.put(_buffer);
    return _matrix;
}

我相信这是因为缓冲区大小错误,但无法正确处理。

在此处输入图像描述

标签: node.jsopencvrobotjs

解决方案


解决了。将 robot-js 图像转换为 opencv 矩阵的最终函数如下所示:

function img2mat(img,size_x,size_y)
{

    let _data       = img.getData(),
        _matrix     = new opencv.Matrix(size_x,size_y,opencv.Constants.CV_8UC4);

    _matrix.put(_data);
    return _matrix;
}

无需使用任何额外的缓冲区。是的,问题在于香奈儿的数量。谢谢帮助。

UPD:2018 年 8 月 8 日使用 node4opencv 可以更轻松地完成

// imaget to matrix
function img2mat(img,size_x,size_y)
{
    return new opencv.Mat(img.getData(),size_x,size_y,opencv.CV_8UC4);
}

顺便说一句,节点 opencv 库不完整且已过时:C


推荐阅读