octalmage / robotjs

Node.js Desktop Automation.
http://robotjs.io
MIT License
12.31k stars 951 forks source link

How to make screenshot of desktop opencv4nodejs + robotjs? #600

Open bigturtle88 opened 4 years ago

bigturtle88 commented 4 years ago

const cv = require('opencv4nodejs');
const robot = require('robotjs');
var Jimp = require( 'jimp' );

const findWaldo = async () => {

var screen = robot.captureScreen();
 //await screenCaptureToFile( screen );
  // Load images

//  const originalMat = await cv.imreadAsync(`${__dirname}/fone.png`);
console.log(screen);
  const originalMat = new cv.Mat(screen.image , screen.width, screen.height, cv.CV_8UC3);

  const waldoMat = await cv.imreadAsync(`${__dirname}/fire.png`);

  const matched = originalMat.matchTemplate(waldoMat, 5);

  const minMax = matched.minMaxLoc();
  const { maxLoc: { x, y } } = minMax;

  originalMat.drawRectangle(
    new cv.Rect(x, y, waldoMat.cols, waldoMat.rows),
    new cv.Vec(0, 255, 0),
    1,
    cv.LINE_8
  );

  cv.imshow('We\'ve found Waldo!', originalMat);
  cv.waitKey();

};

I am getting such a gray image.

https://i.stack.imgur.com/7AJ4F.png

How can a full-screenshot of desktop image be obtained for recognition. In this case, do not save the file but work with the buffer?

bigturtle88 commented 4 years ago
const cv = require('opencv4nodejs')
const robot = require('robotjs')

const width = 1920;
const height = 1080;

(async() => {

    const screenshot = robot.screen.capture(0, 0, width, height);
    const image = new cv.Mat(screenshot.image, height, width, cv.CV_8UC4);

    const template = cv.imread('./template.png', cv.CV_8U);
    const matched = (new cv.Mat(screenshot.image, height, width, cv.CV_8U)).matchTemplate(template, 5);

    const minMax = matched.minMaxLoc();
    const { maxLoc: { x, y } } = minMax;

    image.drawRectangle(
        new cv.Rect(x, y, template.cols, template.rows),
        new cv.Vec(0, 255, 0),
        2,
        cv.LINE_8
    );

    cv.imshow('Result', image);
    cv.waitKey();

})();

In this way, the picture is correct. But opencv can't find a template

leoxiaoping commented 3 years ago

I have the same problem