forked from justadudewhohacks/opencv4nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchFeatures.js
63 lines (53 loc) · 1.61 KB
/
matchFeatures.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const cv = require('../');
const matchFeatures = ({ img1, img2, detector, matchFunc }) => {
// detect keypoints
const keyPoints1 = detector.detect(img1);
const keyPoints2 = detector.detect(img2);
// compute feature descriptors
const descriptors1 = detector.compute(img1, keyPoints1);
const descriptors2 = detector.compute(img2, keyPoints2);
// match the feature descriptors
const matches = matchFunc(descriptors1, descriptors2);
// only keep good matches
const bestN = 40;
const bestMatches = matches.sort(
(match1, match2) => match1.distance - match2.distance
).slice(0, bestN);
return cv.drawMatches(
img1,
img2,
keyPoints1,
keyPoints2,
bestMatches
);
};
const img1 = cv.imread('../data/s0.jpg');
const img2 = cv.imread('../data/s1.jpg');
// check if opencv compiled with extra modules and nonfree
if (cv.xmodules.xfeatures2d) {
const siftMatchesImg = matchFeatures({
img1,
img2,
detector: new cv.SIFTDetector({ nFeatures: 2000 }),
matchFunc: cv.matchFlannBased
});
cv.imshowWait('SIFT matches', siftMatchesImg);
} else {
console.log('skipping SIFT matches');
}
const orbMatchesImg = matchFeatures({
img1,
img2,
detector: new cv.ORBDetector(),
matchFunc: cv.matchBruteForceHamming
});
cv.imshowWait('ORB matches', orbMatchesImg);
// Match using the BFMatcher with crossCheck true
const bf = new cv.BFMatcher(cv.NORM_L2, true);
const orbBFMatchIMG = matchFeatures({
img1,
img2,
detector: new cv.ORBDetector(),
matchFunc: (desc1, desc2) => bf.match(desc1, desc2)
});
cv.imshowWait('ORB with BFMatcher - crossCheck true', orbBFMatchIMG);