node.js - 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;
}
我相信这是因为缓冲区大小错误,但无法正确处理。
解决方案
解决了。将 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
推荐阅读
- angular - 什么时候在 Angular 中使用 Renderer2 而不是结构指令更好
- python - 无论操作系统语言环境如何,如何创建文件夹和文件
- python - Plotly:如何使用 plotly 作为 pandas 的绘图后端来制作不同的绘图?
- c# - xpath按元素和属性查找节点,其中包含具有特定id的子元素
- c# - EventAggregator 替代方案
- dart - 使用 "throw" 抛出自定义 FormatException 并尝试通过 try on 处理它。飞镖语言
- azure - Azure 存储帐户具有由层次结构文件夹组成的容器:- 如果文件未上传到选定文件夹,则创建自动电子邮件
- r - 将列值从因子转换为列名并为其分配值
- python - 如何从命令提示符窗口运行我新创建的 hello_world.py 文件?
- css - CSS/RegEx:选择某个属性在某些位置仅包含数字的元素