const cv = require('opencv4nodejs');

// cv.imread('/Users/lpan/Downloads/Screenshot_2023-05-19-14-09-11-82_40deb401b9ffe8e1df2f1cc5ba480b12.jpg', function (err, im) {
//     if (err) return console.error('error loading image');
//
//     var output = im.matchTemplate('../assets/templates/valid_btn_template.png', 3);
//
//     var matches = output.templateMatches(0.80, 1.0, 5, false);
//
//     console.log(matches);
// });

const findWaldo = async () => {
    // Load images
    const originalMat = await cv.imreadAsync(`/Users/lpan/Downloads/Screenshot_2023-05-20-15-20-22-860_com.android.chrome.jpg`);
    const waldoMat = await cv.imreadAsync(`${__dirname}/../assets/templates/welcome_later_btn.png`);

    // Match template (the brightest locations indicate the highest match)
    const matched = originalMat.matchTemplate(waldoMat, 3);

    // 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;
    console.log("x is " + x);
    console.log("y is " + y);
    // Draw bounding rectangle
    originalMat.drawRectangle(
        new cv.Rect(x, y, waldoMat.cols, waldoMat.rows),
        new cv.Vec(0, 255, 0),
        2,
        cv.LINE_8
    );

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

// noinspection JSIgnoredPromiseFromCall
findWaldo();