opencv4nodejs icon indicating copy to clipboard operation
opencv4nodejs copied to clipboard

How to work with the desktop screen using robotjs and opencv4nodejs?

Open bigturtle88 opened this issue 5 years ago • 2 comments

I used one of the examples. I want to take a screenshot of the desktop. And find on it the picture I'm uploading. The problem is that I do not understand how to correctly convey the screenshot of the desktop. I am getting a gray window and an incorrect image.

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

const findWaldo = async () => {

var screen = robot.screen.capture(0, 0, 1024, 768); // I take screenshot here

const originalMat = new cv.Mat(screen.image, 1024, 768, cv.CV_8UC3);  //Here's the processing



const waldoMat = await cv.imreadAsync(`${__dirname}/bobber.png`);
const matched = originalMat.matchTemplate(waldoMat, 5);

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

  // Draw bounding rectangle
  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 avatar Aug 29 '20 21:08 bigturtle88

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

bigturtle88 avatar Aug 30 '20 15:08 bigturtle88

const cv = require('opencv4nodejs');
const robot = require('robotjs')
const width = 1920;
const height = 1080;
const findWaldo = async () => {
  // Load images
    const screenshot = robot.screen.capture(0, 0, width, height);
    const image = new cv.Mat(screenshot.image, height, width, cv.CV_8UC4);
    const waldoMat = await cv.imreadAsync(`./data/waldo.jpg`, cv.CV_8U);
    const matched = (new cv.Mat(screenshot.image, height, width, cv.CV_8U)).matchTemplate(waldoMat , 5);

  // Use minMaxLoc to locate the highest value (or lower, depending of the type of matching method)
  const minMax = matched.minMaxLoc();
  const { maxLoc: { x, y } } = minMax;

  // Draw bounding rectangle
  image.drawRectangle(
    new cv.Rect(x, y, waldoMat.cols, waldoMat.rows),
    new cv.Vec(0, 277, 0),
    5,
    cv.LINE_8
  );

  // Open result in new window
  cv.imshow('We\'ve found Waldo!', image );
  cv.waitKey();
};

// noinspection JSIgnoredPromiseFromCall
findWaldo();

The green square is finding the wrong area.

https://ibb.co/JzVTF8C

I just opened the picture, made a screenshot. But the code doesn't find Waldo. How to make it work?

bigturtle88 avatar Sep 29 '20 07:09 bigturtle88