forked from matheusgadelha/OpenCV-Features-Comparison
/
FeatureAlgorithm.cpp
57 lines (45 loc) · 1.35 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
#include "FeatureAlgorithm.hpp"
#include <cassert>
FeatureAlgorithm::FeatureAlgorithm(std::string n, cv::Ptr<cv::FeatureDetector> d, cv::Ptr<cv::DescriptorExtractor> e, cv::Ptr<cv::DescriptorMatcher> m)
: name(n)
, knMatchSupported(false)
, detector(d)
, extractor(e)
, matcher(m)
{
assert(d);
assert(e);
assert(m);
}
FeatureAlgorithm::FeatureAlgorithm(std::string n, cv::Ptr<cv::Feature2D> fe, cv::Ptr<cv::DescriptorMatcher> m)
: name(n)
, knMatchSupported(false)
, featureEngine(fe)
, matcher(m)
{
}
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);
}