现在我正在尝试在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;
}
我相信这是因为缓冲区大小错误,但无法正确处理。