1


Right now I'm trying to use screenshot made with node module robot-js in opencv.

But for some reason it appears in grayscale.
This is my code:

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

I belive this is because of wrong buffer size, but can't get it right.

enter image description here

Samuel Hulla
  • 6,617
  • 7
  • 36
  • 70
255h
  • 11
  • 3
  • CV_8UC1 is for greyscale images, you need CV_8UC3 for color images or CV_8UC4 for images with and alpha channel. Now, by the [documentation] (http://getrobot.net/api/image.html#GetData), it says that you get uint32 with ARGB, this means you need CV_8UC4. Also, remember a lot of OpenCV functions work with BGR or BGRA images. – api55 Aug 05 '18 at 15:14

2 Answers2

0

Solved. Final function that transform robot-js image to opencv matrix looks like:

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

There is no need to use any additional buffers. And yes, problem was with chanels amount. Thx for help.

UPD: 08/08/2018 Using node4opencv can be done even easier

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

Btw node opencv library is incomplete and outdated :C

255h
  • 11
  • 3
0

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
}
judgeou
  • 1
  • 1
  • 3
    While this code may answer the question, providing additional context regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. – KMR Jan 22 '19 at 04:05