forked from nut-tree/opencv4nodejs
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathasyncMatchFeatures.ts
54 lines (48 loc) · 1.39 KB
/
asyncMatchFeatures.ts
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
import * as cv from '../../';
const detectAndComputeAsync = (det: cv.FeatureDetector, img: cv.Mat) =>
det.detectAsync(img)
.then(kps => det.computeAsync(img, kps)
.then(desc => ({ kps, desc }))
);
const img1 = cv.imread('../../data/s0.jpg');
const img2 = cv.imread('../../data/s1.jpg');
const detectorNames = [
'AKAZE',
'BRISK',
'KAZE',
'ORB'
];
const createDetectorFromName = (name: string) => new cv[`${name}Detector`]();
// create 4 promises -> each detector detects and computes descriptors for img1 and img2
const promises = detectorNames
.map(createDetectorFromName)
.map(det =>
// also detect and compute descriptors for img1 and img2 async
Promise.all([detectAndComputeAsync(det, img1), detectAndComputeAsync(det, img2)])
.then(allResults =>
cv.matchBruteForceAsync(
allResults[0].desc,
allResults[1].desc
)
.then(matches => ({
matches,
kps1: allResults[0].kps,
kps2: allResults[1].kps
}))
)
);
Promise.all(promises)
.then((allResults) => {
allResults.forEach((result, i) => {
const drawMatchesImg = cv.drawMatches(
img1,
img2,
result.kps1,
result.kps2,
result.matches
);
cv.imshowWait(detectorNames[i], drawMatchesImg);
cv.destroyAllWindows();
});
})
.catch(err => console.error(err));