forked from BloodAxe/OpenCV-Features-Comparison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeatureAlgorithm.cpp
82 lines (64 loc) · 2.33 KB
/
FeatureAlgorithm.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "FeatureAlgorithm.hpp"
#include <cassert>
static cv::Ptr<cv::flann::IndexParams> indexParamsForDescriptorType(int descriptorType, int defaultNorm)
{
switch (defaultNorm)
{
case cv::NORM_L2:
return cv::Ptr<cv::flann::IndexParams>(new cv::flann::KDTreeIndexParams());
case cv::NORM_HAMMING:
return cv::Ptr<cv::flann::IndexParams>(new cv::flann::LshIndexParams(20, 15, 2));
default:
CV_Assert(false && "Unsupported descriptor type");
};
}
cv::Ptr<cv::DescriptorMatcher> matcherForDescriptorType(int descriptorType, int defaultNorm, bool bruteForce)
{
if (bruteForce)
return cv::Ptr<cv::DescriptorMatcher>(new cv::BFMatcher(defaultNorm, true));
else
return cv::Ptr<cv::DescriptorMatcher>(new cv::FlannBasedMatcher(indexParamsForDescriptorType(descriptorType, defaultNorm)));
}
FeatureAlgorithm::FeatureAlgorithm(const std::string& n, cv::Ptr<cv::FeatureDetector> d, cv::Ptr<cv::DescriptorExtractor> e, bool useBruteForceMather)
: name(n)
, knMatchSupported(false)
, detector(d)
, extractor(e)
, matcher(matcherForDescriptorType(e->descriptorSize(), e->defaultNorm(), useBruteForceMather))
{
CV_Assert(d);
CV_Assert(e);
}
FeatureAlgorithm::FeatureAlgorithm(const std::string& n, cv::Ptr<cv::Feature2D> fe, bool useBruteForceMather)
: name(n)
, knMatchSupported(false)
, featureEngine(fe)
, matcher(matcherForDescriptorType(fe->descriptorSize(), fe->defaultNorm(), useBruteForceMather))
{
CV_Assert(fe);
}
bool FeatureAlgorithm::extractFeatures(const cv::Mat& image, Keypoints& kp, Descriptors& desc) const
{
assert(!image.empty());
if (featureEngine)
{
(*featureEngine)(image, cv::noArray(), kp, desc);
}
else
{
detector->detect(image, kp);
if (kp.empty())
return false;
extractor->compute(image, kp, desc);
}
return kp.size() > 0;
}
void FeatureAlgorithm::matchFeatures(const Descriptors& train, const Descriptors& query, Matches& matches) const
{
matcher->match(query, train, matches);
}
void FeatureAlgorithm::matchFeatures(const Descriptors& train, const Descriptors& query, int k, std::vector<Matches>& matches) const
{
assert(knMatchSupported);
matcher->knnMatch(query, train, matches, k);
}