Skip to content

Commit 63a9a8e

Browse files
Merge pull request #154 from pchmelar/master
DescriptorMatchingKnn
2 parents 5119e36 + f004329 commit 63a9a8e

File tree

4 files changed

+223
-0
lines changed

4 files changed

+223
-0
lines changed

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"cc/modules/features2d/DescriptorMatch.cc",
8484
"cc/modules/features2d/FeatureDetector.cc",
8585
"cc/modules/features2d/descriptorMatching.cc",
86+
"cc/modules/features2d/descriptorMatchingKnn.cc",
8687
"cc/modules/features2d/detectors/AGASTDetector.cc",
8788
"cc/modules/features2d/detectors/AKAZEDetector.cc",
8889
"cc/modules/features2d/detectors/BRISKDetector.cc",
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#include "descriptorMatchingKnn.h"
2+
#include "Workers.h"
3+
4+
NAN_MODULE_INIT(DescriptorMatchingKnn::Init) {
5+
Nan::SetMethod(target, "matchKnnFlannBased", MatchKnnFlannBased);
6+
Nan::SetMethod(target, "matchKnnBruteForce", MatchKnnBruteForce);
7+
Nan::SetMethod(target, "matchKnnBruteForceL1", MatchKnnBruteForceL1);
8+
Nan::SetMethod(target, "matchKnnBruteForceHamming", MatchKnnBruteForceHamming);
9+
Nan::SetMethod(target, "matchKnnFlannBasedAsync", MatchKnnFlannBasedAsync);
10+
Nan::SetMethod(target, "matchKnnBruteForceAsync", MatchKnnBruteForceAsync);
11+
Nan::SetMethod(target, "matchKnnBruteForceL1Async", MatchKnnBruteForceL1Async);
12+
Nan::SetMethod(target, "matchKnnBruteForceHammingAsync", MatchKnnBruteForceHammingAsync);
13+
#if 2 <= CV_VERSION_MINOR
14+
Nan::SetMethod(target, "matchKnnBruteForceHammingLut", MatchKnnBruteForceHammingLut);
15+
Nan::SetMethod(target, "matchKnnBruteForceSL2", MatchKnnBruteForceSL2);
16+
Nan::SetMethod(target, "matchKnnBruteForceHammingLutAsync", MatchKnnBruteForceHammingLutAsync);
17+
Nan::SetMethod(target, "matchKnnBruteForceSL2Async", MatchKnnBruteForceSL2Async);
18+
#endif
19+
};
20+
21+
#if CV_VERSION_MINOR < 2
22+
23+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnFlannBased) {
24+
matchKnn(info, "FlannBased");
25+
}
26+
27+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForce) {
28+
matchKnn(info, "BruteForce");
29+
}
30+
31+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceL1) {
32+
matchKnn(info, "BruteForce-L1");
33+
}
34+
35+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceHamming) {
36+
matchKnn(info, "BruteForce-Hamming");
37+
}
38+
39+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnFlannBasedAsync) {
40+
matchKnnAsync(info, "FlannBased");
41+
}
42+
43+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceAsync) {
44+
matchKnnAsync(info, "BruteForce");
45+
}
46+
47+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceL1Async) {
48+
matchKnnAsync(info, "BruteForce-L1");
49+
}
50+
51+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceHammingAsync) {
52+
matchKnnAsync(info, "BruteForce-Hamming");
53+
}
54+
55+
#else
56+
57+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnFlannBased) {
58+
matchKnn(info, cv::DescriptorMatcher::FLANNBASED);
59+
}
60+
61+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForce) {
62+
matchKnn(info, cv::DescriptorMatcher::BRUTEFORCE);
63+
}
64+
65+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceL1) {
66+
matchKnn(info, cv::DescriptorMatcher::BRUTEFORCE_L1);
67+
}
68+
69+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceHamming) {
70+
matchKnn(info, cv::DescriptorMatcher::BRUTEFORCE_HAMMING);
71+
}
72+
73+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceHammingLut) {
74+
matchKnn(info, cv::DescriptorMatcher::BRUTEFORCE_HAMMINGLUT);
75+
}
76+
77+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceSL2) {
78+
matchKnn(info, cv::DescriptorMatcher::BRUTEFORCE_SL2);
79+
}
80+
81+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnFlannBasedAsync) {
82+
matchKnnAsync(info, cv::DescriptorMatcher::FLANNBASED);
83+
}
84+
85+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceAsync) {
86+
matchKnnAsync(info, cv::DescriptorMatcher::BRUTEFORCE);
87+
}
88+
89+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceL1Async) {
90+
matchKnnAsync(info, cv::DescriptorMatcher::BRUTEFORCE_L1);
91+
}
92+
93+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceHammingAsync) {
94+
matchKnnAsync(info, cv::DescriptorMatcher::BRUTEFORCE_HAMMING);
95+
}
96+
97+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceHammingLutAsync) {
98+
matchKnnAsync(info, cv::DescriptorMatcher::BRUTEFORCE_HAMMINGLUT);
99+
}
100+
101+
NAN_METHOD(DescriptorMatchingKnn::MatchKnnBruteForceSL2Async) {
102+
matchKnnAsync(info, cv::DescriptorMatcher::BRUTEFORCE_SL2);
103+
}
104+
105+
#endif
106+
107+
#if CV_VERSION_MINOR < 2
108+
void DescriptorMatchingKnn::matchKnn(Nan::NAN_METHOD_ARGS_TYPE info, std::string matcherType) {
109+
#else
110+
void DescriptorMatchingKnn::matchKnn(Nan::NAN_METHOD_ARGS_TYPE info, int matcherType) {
111+
#endif
112+
FF_METHOD_CONTEXT("matchKnn");
113+
114+
FF_ARG_INSTANCE(0, cv::Mat descFrom, Mat::constructor, FF_UNWRAP_MAT_AND_GET);
115+
FF_ARG_INSTANCE(1, cv::Mat descTo, Mat::constructor, FF_UNWRAP_MAT_AND_GET);
116+
FF_ARG_INT(2, int k);
117+
118+
std::vector<std::vector<cv::DMatch>> dmatches;
119+
cv::DescriptorMatcher::create(matcherType)->knnMatch(descFrom, descTo, dmatches, k);
120+
121+
FF_ARR jsMatches = FF_NEW_ARRAY(dmatches.size());
122+
uint i = 0;
123+
for (auto dmatch : dmatches) {
124+
FF_ARR jsMatchesKnn = FF_NEW_ARRAY(dmatch.size());
125+
uint j = 0;
126+
for (auto dmatchKnn : dmatch) {
127+
FF_OBJ jsMatchKnn = FF_NEW_INSTANCE(DescriptorMatch::constructor);
128+
FF_UNWRAP(jsMatchKnn, DescriptorMatch)->dmatch = dmatchKnn;
129+
jsMatchesKnn->Set(j++, jsMatchKnn);
130+
}
131+
jsMatches->Set(i++, jsMatchesKnn);
132+
}
133+
FF_RETURN(jsMatches);
134+
}
135+
136+
#if CV_VERSION_MINOR < 2
137+
void DescriptorMatchingKnn::matchKnnAsync(Nan::NAN_METHOD_ARGS_TYPE info, std::string matcherType) {
138+
#else
139+
void DescriptorMatchingKnn::matchKnnAsync(Nan::NAN_METHOD_ARGS_TYPE info, int matcherType) {
140+
#endif
141+
FF_METHOD_CONTEXT("matchKnnAsync");
142+
143+
MatchContext ctx;
144+
ctx.matcher = cv::DescriptorMatcher::create(matcherType);
145+
FF_ARG_INSTANCE(0, ctx.descFrom, Mat::constructor, FF_UNWRAP_MAT_AND_GET);
146+
FF_ARG_INSTANCE(1, ctx.descTo, Mat::constructor, FF_UNWRAP_MAT_AND_GET);
147+
FF_ARG_INT(2, ctx.k);
148+
FF_ARG_FUNC(3, v8::Local<v8::Function> cbFunc);
149+
150+
Nan::AsyncQueueWorker(new GenericAsyncWorker<MatchContext>(
151+
new Nan::Callback(cbFunc),
152+
ctx
153+
));
154+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "macros.h"
2+
#include <opencv2/core.hpp>
3+
#include <opencv2/features2d.hpp>
4+
#include "Mat.h"
5+
#include "KeyPoint.h"
6+
#include "DescriptorMatch.h"
7+
8+
#ifndef __FF_DESCRIPTORMATCHINGKNN_H__
9+
#define __FF_DESCRIPTORMATCHINGKNN_H__
10+
11+
class DescriptorMatchingKnn : public Nan::ObjectWrap {
12+
public:
13+
static NAN_MODULE_INIT(Init);
14+
15+
static NAN_METHOD(MatchKnnFlannBased);
16+
static NAN_METHOD(MatchKnnBruteForce);
17+
static NAN_METHOD(MatchKnnBruteForceL1);
18+
static NAN_METHOD(MatchKnnBruteForceHamming);
19+
static NAN_METHOD(MatchKnnBruteForceHammingLut);
20+
static NAN_METHOD(MatchKnnBruteForceSL2);
21+
static NAN_METHOD(MatchKnnFlannBasedAsync);
22+
static NAN_METHOD(MatchKnnBruteForceAsync);
23+
static NAN_METHOD(MatchKnnBruteForceL1Async);
24+
static NAN_METHOD(MatchKnnBruteForceHammingAsync);
25+
static NAN_METHOD(MatchKnnBruteForceHammingLutAsync);
26+
static NAN_METHOD(MatchKnnBruteForceSL2Async);
27+
#if CV_VERSION_MINOR < 2
28+
static void matchKnn(Nan::NAN_METHOD_ARGS_TYPE info, std::string matcherType);
29+
static void matchKnnAsync(Nan::NAN_METHOD_ARGS_TYPE info, std::string matcherType);
30+
#else
31+
static void matchKnn(Nan::NAN_METHOD_ARGS_TYPE info, int matcherType);
32+
static void matchKnnAsync(Nan::NAN_METHOD_ARGS_TYPE info, int matcherType);
33+
#endif
34+
35+
struct MatchContext {
36+
public:
37+
cv::Ptr<cv::DescriptorMatcher> matcher;
38+
cv::Mat descFrom;
39+
cv::Mat descTo;
40+
int k;
41+
std::vector<std::vector<cv::DMatch>> dmatches;
42+
43+
const char* execute() {
44+
matcher->knnMatch(descFrom, descTo, dmatches, k);
45+
return "";
46+
}
47+
48+
FF_VAL getReturnValue() {
49+
FF_ARR jsMatches = FF_NEW_ARRAY(dmatches.size());
50+
uint i = 0;
51+
for (auto dmatch : dmatches) {
52+
FF_ARR jsMatchesKnn = FF_NEW_ARRAY(dmatch.size());
53+
uint j = 0;
54+
for (auto dmatchKnn : dmatch) {
55+
FF_OBJ jsMatchKnn = FF_NEW_INSTANCE(DescriptorMatch::constructor);
56+
FF_UNWRAP(jsMatchKnn, DescriptorMatch)->dmatch = dmatchKnn;
57+
jsMatchesKnn->Set(j++, jsMatchKnn);
58+
}
59+
jsMatches->Set(i++, jsMatchesKnn);
60+
}
61+
return jsMatches;
62+
}
63+
};
64+
};
65+
66+
#endif

cc/modules/features2d/features2d.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "KeyPointMatch.h"
44
#include "DescriptorMatch.h"
55
#include "descriptorMatching.h"
6+
#include "descriptorMatchingKnn.h"
67
#include "detectors/AGASTDetector.h"
78
#include "detectors/AKAZEDetector.h"
89
#include "detectors/BRISKDetector.h"
@@ -18,6 +19,7 @@ NAN_MODULE_INIT(Features2d::Init) {
1819
KeyPointMatch::Init(target);
1920
DescriptorMatch::Init(target);
2021
DescriptorMatching::Init(target);
22+
DescriptorMatchingKnn::Init(target);
2123
AGASTDetector::Init(target);
2224
AKAZEDetector::Init(target);
2325
BRISKDetector::Init(target);

0 commit comments

Comments
 (0)