【问题标题】:robot-js + opencv screenshot appears in grayscalerobots-js + opencv 截图以灰度显示
【发布时间】:2018-08-05 14:32:32
【问题描述】:


现在我正在尝试在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;
}

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

【问题讨论】:

  • CV_8UC1 用于灰度图像,您需要 CV_8UC3 用于彩色图像或 CV_8UC4 用于具有 alpha 通道的图像。现在,通过 [文档] (getrobot.net/api/image.html#GetData),它说您获得了带有 ARGB 的 uint32,这意味着您需要 CV_8UC4。另外,请记住很多 OpenCV 函数都适用于 BGR 或 BGRA 图像。

标签: node.js opencv robotjs


【解决方案1】:

解决了。 将 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;
}

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

更新日期: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

【讨论】:

    【解决方案2】:

    2019-01-22

    const cv = require('opencv4nodejs')
    const robot = require('robotjs')
    
    function img2mat (img, width, height) {
      return new cv.Mat(img, height, width, cv.CV_8UC4)
    }
    
    function screen2mat () {
      let cap = robot.screen.capture(0, 0, 1920, 1080)
      let mat = img2mat(cap.image, 1920, 1080)
      return mat
    }
    

    【讨论】:

    • 虽然此代码可以回答问题,但提供有关 如何 和/或 为什么 解决问题的附加上下文将改善答案的长期价值。
    猜你喜欢
    • 2017-08-30
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多