forked from justadudewhohacks/opencv4nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplateMatching.js
30 lines (23 loc) · 865 Bytes
/
templateMatching.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const cv = require('../');
const findWaldo = async () => {
// Load images
const originalMat = await cv.imreadAsync(`${__dirname}/../data/findwaldo.jpg`);
const waldoMat = await cv.imreadAsync(`${__dirname}/../data/waldo.jpg`);
// Match template (the brightest locations indicate the highest match)
const matched = originalMat.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
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();