forked from SolarFramework/SolARModuleOpenCV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolARKeypointDetectorOpencv.cpp
More file actions
225 lines (200 loc) · 8.07 KB
/
SolARKeypointDetectorOpencv.cpp
File metadata and controls
225 lines (200 loc) · 8.07 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* @copyright Copyright (c) 2017 B-com http://www.b-com.com/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SolARKeypointDetectorOpencv.h"
#include "SolAROpenCVHelper.h"
#include "core/Log.h"
XPCF_DEFINE_FACTORY_CREATE_INSTANCE(SolAR::MODULES::OPENCV::SolARKeypointDetectorOpencv)
namespace xpcf = org::bcom::xpcf;
using namespace cv;
#if ((CV_VERSION_MAJOR < 4 ) || (CV_VERSION_MINOR < 4 ))
using namespace cv::xfeatures2d;
#endif
namespace SolAR {
using namespace datastructure;
using namespace api::features;
namespace MODULES {
namespace OPENCV {
static std::map<std::string,IKeypointDetector::KeypointDetectorType> stringToType = {{"SIFT",IKeypointDetector::KeypointDetectorType::SIFT},
{"AKAZE",IKeypointDetector::KeypointDetectorType::AKAZE},
{"AKAZE2",IKeypointDetector::KeypointDetectorType::AKAZE2},
{"ORB",IKeypointDetector::KeypointDetectorType::ORB},
{"BRISK",IKeypointDetector::KeypointDetectorType::BRISK},
{"FEATURE_TO_TRACK", IKeypointDetector::KeypointDetectorType::FEATURE_TO_TRACK}};
static std::map<IKeypointDetector::KeypointDetectorType,std::string> typeToString = {{IKeypointDetector::KeypointDetectorType::SIFT, "SIFT"},
{IKeypointDetector::KeypointDetectorType::AKAZE, "AKAZE"},
{IKeypointDetector::KeypointDetectorType::AKAZE2,"AKAZE2"},
{IKeypointDetector::KeypointDetectorType::ORB,"ORB"},
{IKeypointDetector::KeypointDetectorType::BRISK,"BRISK"},
{IKeypointDetector::KeypointDetectorType::FEATURE_TO_TRACK,"FEATURE_TO_TRACK"}};
SolARKeypointDetectorOpencv::SolARKeypointDetectorOpencv():ConfigurableBase(xpcf::toUUID<SolARKeypointDetectorOpencv>())
{
declareInterface<api::features::IKeypointDetector>(this);
declareProperty("imageRatio", m_imageRatio);
declareProperty("nbDescriptors", m_nbDescriptors);
declareProperty("threshold", m_threshold);
declareProperty("nbOctaves", m_nbOctaves);
declareProperty("type", m_type);
LOG_DEBUG("SolARKeypointDetectorOpencv constructor");
}
SolARKeypointDetectorOpencv::~SolARKeypointDetectorOpencv()
{
LOG_DEBUG("SolARKeypointDetectorOpencv destructor");
}
xpcf::XPCFErrorCode SolARKeypointDetectorOpencv::onConfigured()
{
LOG_DEBUG(" SolARKeypointDetectorOpencv onConfigured");
if (stringToType.find(m_type) != stringToType.end())
{
setType(stringToType.at(m_type));
return xpcf::XPCFErrorCode::_SUCCESS;
}
else
{
LOG_WARNING("Keypoint detector of type {} defined in your configuration file does not exist", m_type);
return xpcf::_ERROR_NOT_IMPLEMENTED;
}
}
void SolARKeypointDetectorOpencv::setType(KeypointDetectorType type)
{
/*
* SURF,
ORB,
SIFT,
DAISY,
LATCH,
AKAZE,
AKAZEUP,
BRISK,
BRIEF,
FEATURE_TO_TRACK
*/
m_type=typeToString.at(type);
switch (type) {
case (KeypointDetectorType::SIFT):
LOG_DEBUG("KeypointDetectorImp::setType(SIFT)");
if (m_threshold > 0)
m_detector = SIFT::create(m_nbDescriptors, m_nbOctaves, 0.04, m_threshold);
else
m_detector = SIFT::create(m_nbDescriptors);
break;
case (KeypointDetectorType::AKAZE):
LOG_DEBUG("KeypointDetectorImp::setType(AKAZE)");
if (m_threshold > 0)
m_detector = AKAZE::create(cv::AKAZE::DESCRIPTOR_MLDB, 0, 3, m_threshold, m_nbOctaves);
else
m_detector = AKAZE::create();
break;
case (KeypointDetectorType::AKAZE2):
LOG_DEBUG("KeypointDetectorImp::setType(AKAZE2)");
if (m_threshold > 0)
m_detector = AKAZE2::create(5, 0, 3, m_threshold, m_nbOctaves);
else
m_detector = AKAZE2::create();
break;
case (KeypointDetectorType::ORB):
LOG_DEBUG("KeypointDetectorImp::setType(ORB)");
if (m_nbDescriptors > 0)
m_detector=ORB::create(m_nbDescriptors, 1.2f, m_nbOctaves);
else
m_detector = ORB::create();
break;
case (KeypointDetectorType::BRISK):
LOG_DEBUG("KeypointDetectorImp::setType(BRISK)");
if (m_threshold > 0)
m_detector = BRISK::create((int)m_threshold, m_nbOctaves);
else
m_detector=BRISK::create();
break;
default :
LOG_DEBUG("KeypointDetectorImp::setType(AKAZE)");
m_detector=AKAZE::create();
break;
}
}
IKeypointDetector::KeypointDetectorType SolARKeypointDetectorOpencv::getType()
{
return stringToType.at(m_type);
}
void SolARKeypointDetectorOpencv::detect(const SRef<Image> image, std::vector<Keypoint> & keypoints)
{
std::vector<cv::KeyPoint> kpts;
// the input image is down-scaled to accelerate the keypoints extraction
float ratioInv=1.f/m_imageRatio;
keypoints.clear();
// instantiation of an opencv image from an input IImage
cv::Mat opencvImage = SolAROpenCVHelper::mapToOpenCV(image);
cv::Mat img_1;
if (opencvImage.channels() != 1)
cvtColor(opencvImage, img_1, COLOR_BGR2GRAY);
else
img_1 = opencvImage;
cv::resize(img_1, img_1, Size(img_1.cols*m_imageRatio,img_1.rows*m_imageRatio), 0, 0);
try
{
if (m_type == "FEATURE_TO_TRACK") {
std::vector<cv::Point2f> corners;
cv::goodFeaturesToTrack(img_1, corners, m_nbDescriptors, 0.008, 3, cv::Mat(), 3);
cornerSubPix(img_1, corners, cv::Size(7, 7), Size(-1, -1), cv::TermCriteria(TermCriteria::COUNT | TermCriteria::EPS, 20, 0.03));
for (auto it : corners)
kpts.push_back(cv::KeyPoint(it, 0.f));
}
else {
if(!m_detector){
LOG_DEBUG(" detector is initialized with default value : {}", this->m_type)
setType(stringToType.at(this->m_type));
}
m_detector->detect(img_1, kpts, Mat());
// group keypoints according to octave
std::map<int, std::vector<cv::KeyPoint>> kpOctaves;
for (const auto &it : kpts)
kpOctaves[it.octave].push_back(it);
int nbOctaves = static_cast<int>(kpOctaves.size());
if (nbOctaves > 0) {
int nbKpPerOctave = m_nbDescriptors / nbOctaves;
kpts.clear();
// get best feature per octave
for (auto it = kpOctaves.rbegin(); it != kpOctaves.rend(); it++) {
nbOctaves--;
if (nbOctaves != 0)
kptsFilter.retainBest(it->second, nbKpPerOctave);
else
kptsFilter.retainBest(it->second, m_nbDescriptors - static_cast<int>(kpts.size()));
kpts.insert(kpts.end(), it->second.begin(), it->second.end());
}
}
}
}
catch (Exception& e)
{
LOG_ERROR("Feature : {}", m_detector->getDefaultName())
LOG_ERROR("{}",e.msg)
return;
}
int kpID=0;
for(const auto& keypoint : kpts){
Keypoint kpa;
float px = keypoint.pt.x*ratioInv;
float py = keypoint.pt.y*ratioInv;
cv::Vec3b bgr{ 0, 0, 0 };
if (opencvImage.channels() == 3)
bgr = opencvImage.at<cv::Vec3b>((int)py, (int)px);
kpa.init(kpID++, px, py, bgr[2], bgr[1], bgr[0], keypoint.size, keypoint.angle, keypoint.response, keypoint.octave, keypoint.class_id) ;
keypoints.push_back(kpa);
}
}
}
}
} // end of namespace SolAR