-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
main_ComputeFeatures_OpenCV.cpp
489 lines (424 loc) · 15.2 KB
/
main_ComputeFeatures_OpenCV.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// This file is part of OpenMVG, an Open Multiple View Geometry C++ library.
// Copyright (c) 2012, 2013 Pierre MOULON.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// The <cereal/archives> headers are special and must be included first.
#include <cereal/archives/json.hpp>
#include "openMVG/image/image_io.hpp"
#include "openMVG/features/regions_factory_io.hpp"
#include "openMVG/sfm/sfm.hpp"
#include "openMVG/system/timer.hpp"
#include "third_party/cmdLine/cmdLine.h"
#include "third_party/stlplus3/filesystemSimplified/file_system.hpp"
/// OpenCV Includes
#include <opencv2/opencv.hpp>
#include "opencv2/core/eigen.hpp"
#ifdef USE_OCVSIFT
#include "opencv2/features2d.hpp"
#endif
#include <cstdlib>
#include <fstream>
using namespace openMVG;
using namespace openMVG::image;
using namespace openMVG::features;
using namespace openMVG::sfm;
enum eGeometricModel
{
FUNDAMENTAL_MATRIX = 0,
ESSENTIAL_MATRIX = 1,
HOMOGRAPHY_MATRIX = 2
};
enum ePairMode
{
PAIR_EXHAUSTIVE = 0,
PAIR_CONTIGUOUS = 1,
PAIR_FROM_FILE = 2
};
///
//- Create an Image_describer interface that use an OpenCV feature extraction method
// i.e. with the AKAZE detector+descriptor
//--/!\ If you use a new Regions type you define and register it in
// "openMVG/features/regions_factory.hpp" file.
///
// Reuse the existing AKAZE floating point Keypoint.
typedef features::AKAZE_Float_Regions AKAZE_OpenCV_Regions;
// Define the Interface
class AKAZE_OCV_Image_describer : public Image_describer
{
public:
using Regions_type = AKAZE_OpenCV_Regions;
cv::Ptr<cv::Feature2D> extractor;
AKAZE_OCV_Image_describer():Image_describer(){
extractor = cv::AKAZE::create(cv::AKAZE::DESCRIPTOR_KAZE);
}
bool Set_configuration_preset(EDESCRIBER_PRESET preset) override
{
return false;
}
/**
@brief Detect regions on the image and compute their attributes (description)
@param image Image.
@param mask 8-bit gray image for keypoint filtering (optional).
Non-zero values depict the region of interest.
@return regions The detected regions and attributes (the caller must delete the allocated data)
*/
std::unique_ptr<Regions> Describe(
const Image<unsigned char>& image,
const Image<unsigned char> * mask = nullptr
) override
{
return Describe_AKAZE_OCV(image, mask);
}
/**
@brief Detect regions on the image and compute their attributes (description)
@param image Image.
@param mask 8-bit gray image for keypoint filtering (optional).
Non-zero values depict the region of interest.
@return regions The detected regions and attributes (the caller must delete the allocated data)
*/
std::unique_ptr<Regions_type> Describe_AKAZE_OCV(
const Image<unsigned char>& image,
const Image<unsigned char> * mask = nullptr
)
{
auto regions = std::unique_ptr<Regions_type>(new Regions_type);
cv::Mat img;
cv::eigen2cv(image.GetMat(), img);
cv::Mat m_mask;
if (mask != nullptr) {
cv::eigen2cv(mask->GetMat(), m_mask);
}
std::vector< cv::KeyPoint > vec_keypoints;
cv::Mat m_desc;
extractor->detectAndCompute(img, m_mask, vec_keypoints, m_desc);
if (!vec_keypoints.empty())
{
// reserve some memory for faster keypoint saving
regions->Features().reserve(vec_keypoints.size());
regions->Descriptors().reserve(vec_keypoints.size());
using DescriptorT = Descriptor<float, 64>;
DescriptorT descriptor;
int cpt = 0;
for (auto i_keypoint = vec_keypoints.begin(); i_keypoint != vec_keypoints.end(); ++i_keypoint, ++cpt){
const SIOPointFeature feat((*i_keypoint).pt.x, (*i_keypoint).pt.y, (*i_keypoint).size, (*i_keypoint).angle);
regions->Features().push_back(feat);
memcpy(descriptor.data(),
m_desc.ptr<typename DescriptorT::bin_type>(cpt),
DescriptorT::static_size*sizeof(DescriptorT::bin_type));
regions->Descriptors().push_back(descriptor);
}
}
return regions;
};
/// Allocate Regions type depending of the Image_describer
std::unique_ptr<Regions> Allocate() const override
{
return std::unique_ptr<Regions_type>(new Regions_type);
}
template<class Archive>
void serialize( Archive & ar )
{
}
};
#include <cereal/cereal.hpp>
#include <cereal/types/polymorphic.hpp>
CEREAL_REGISTER_TYPE_WITH_NAME(AKAZE_OCV_Image_describer, "AKAZE_OCV_Image_describer");
CEREAL_REGISTER_POLYMORPHIC_RELATION(openMVG::features::Image_describer, AKAZE_OCV_Image_describer)
#ifdef USE_OCVSIFT
///
//- Create an Image_describer interface that use and OpenCV extraction method
// i.e. with the SIFT detector+descriptor
// Regions is the same as classic SIFT : 128 unsigned char
class SIFT_OPENCV_Image_describer : public Image_describer
{
public:
using Regions_type = SIFT_Regions;
// Declare a SIFT detector
cv::Ptr<cv::Feature2D> siftdetector;
SIFT_OPENCV_Image_describer() : Image_describer() {
// Create a SIFT detector
siftdetector = cv::SIFT::create();
}
~SIFT_OPENCV_Image_describer() {}
bool Set_configuration_preset(EDESCRIBER_PRESET preset){
return true;
}
/**
@brief Detect regions on the image and compute their attributes (description)
@param image Image.
@param mask 8-bit gray image for keypoint filtering (optional).
Non-zero values depict the region of interest.
@return regions The detected regions and attributes (the caller must delete the allocated data)
*/
std::unique_ptr<Regions> Describe(
const image::Image<unsigned char>& image,
const image::Image<unsigned char> * mask = nullptr
) override
{
return Describe_SIFT_OPENCV(image, mask);
}
/**
@brief Detect regions on the image and compute their attributes (description)
@param image Image.
@param mask 8-bit gray image for keypoint filtering (optional).
Non-zero values depict the region of interest.
@return regions The detected regions and attributes (the caller must delete the allocated data)
*/
std::unique_ptr<Regions_type> Describe_SIFT_OPENCV(
const image::Image<unsigned char>& image,
const image::Image<unsigned char>* mask = nullptr
)
{
// Convert for opencv
cv::Mat img;
cv::eigen2cv(image.GetMat(), img);
// Convert mask image into cv::Mat
cv::Mat m_mask;
if (mask != nullptr) {
cv::eigen2cv(mask->GetMat(), m_mask);
}
// Create a Keypoints & Descriptors container
std::vector< cv::KeyPoint > v_keypoints;
cv::Mat m_desc;
// Process SIFT computation
siftdetector->detectAndCompute(img, m_mask, v_keypoints, m_desc);
auto regions = std::unique_ptr<Regions_type>(new Regions_type);
// reserve some memory for faster keypoint saving
regions->Features().reserve(v_keypoints.size());
regions->Descriptors().reserve(v_keypoints.size());
// Prepare a column vector with the sum of each descriptor
cv::Mat m_siftsum;
cv::reduce(m_desc, m_siftsum, 1, cv::REDUCE_SUM);
// Copy keypoints and descriptors in the regions
int cpt = 0;
for (auto i_kp = v_keypoints.begin();
i_kp != v_keypoints.end();
++i_kp, ++cpt)
{
SIOPointFeature feat((*i_kp).pt.x, (*i_kp).pt.y, (*i_kp).size, (*i_kp).angle);
regions->Features().push_back(feat);
Descriptor<unsigned char, 128> desc;
for (int j = 0; j < 128; j++)
{
desc[j] = static_cast<unsigned char>(512.0*sqrt(m_desc.at<float>(cpt, j)/m_siftsum.at<float>(cpt, 0)));
}
regions->Descriptors().push_back(desc);
}
return regions;
};
/// Allocate Regions type depending of the Image_describer
std::unique_ptr<Regions> Allocate() const override
{
return std::unique_ptr<Regions_type>(new Regions_type);
}
template<class Archive>
void serialize( Archive & ar )
{
}
};
CEREAL_REGISTER_TYPE_WITH_NAME(SIFT_OPENCV_Image_describer, "SIFT_OPENCV_Image_describer");
CEREAL_REGISTER_POLYMORPHIC_RELATION(openMVG::features::Image_describer, SIFT_OPENCV_Image_describer)
#endif //USE_OCVSIFT
/// Compute between the Views
/// Compute view image description (feature & descriptor extraction using OpenCV)
/// Compute putative local feature matches (descriptor matching)
/// Compute geometric coherent feature matches (robust model estimation from putative matches)
/// Export computed data
int main(int argc, char **argv)
{
CmdLine cmd;
std::string sSfM_Data_Filename;
std::string sOutDir = "";
bool bForce = false;
#ifdef USE_OCVSIFT
std::string sImage_Describer_Method = "AKAZE_OPENCV";
#endif
// required
cmd.add( make_option('i', sSfM_Data_Filename, "input_file") );
cmd.add( make_option('o', sOutDir, "outdir") );
// Optional
cmd.add( make_option('f', bForce, "force") );
#ifdef USE_OCVSIFT
cmd.add( make_option('m', sImage_Describer_Method, "describerMethod") );
#endif
try {
if (argc == 1) throw std::string("Invalid command line parameter.");
cmd.process(argc, argv);
} catch (const std::string& s) {
OPENMVG_LOG_INFO << "Usage: " << argv[0] << '\n'
<< "[-i|--input_file]: a SfM_Data file \n"
<< "[-o|--outdir] path \n"
<< "\n[Optional]\n"
<< "[-f|--force] Force to recompute data\n"
#ifdef USE_OCVSIFT
<< "[-m|--describerMethod]\n"
<< " (method to use to describe an image):\n"
<< " AKAZE_OPENCV (default),\n"
<< " SIFT_OPENCV: SIFT FROM OPENCV\n"
#endif
;
OPENMVG_LOG_ERROR << s;
return EXIT_FAILURE;
}
OPENMVG_LOG_INFO
<< " You called : "
<< argv[0]
<< "\n\t--input_file " << sSfM_Data_Filename
<< "\n\t--outdir " << sOutDir
#ifdef USE_OCVSIFT
<< "\n\t--describerMethod " << sImage_Describer_Method
#endif
<< "\n\t--force " << bForce;
if (sOutDir.empty()) {
OPENMVG_LOG_ERROR << "\nIt is an invalid output directory";
return EXIT_FAILURE;
}
// Create output dir
if (!stlplus::folder_exists(sOutDir))
{
if (!stlplus::folder_create(sOutDir))
{
OPENMVG_LOG_ERROR << "Cannot create output directory";
return EXIT_FAILURE;
}
}
//---------------------------------------
// a. Load input scene
//---------------------------------------
SfM_Data sfm_data;
if (!Load(sfm_data, sSfM_Data_Filename, ESfM_Data(VIEWS|INTRINSICS))) {
OPENMVG_LOG_ERROR << "The input file \""<< sSfM_Data_Filename << "\" cannot be read";
return false;
}
// Init the image_describer
// - retrieve the used one in case of pre-computed features
// - else create the desired one
using namespace openMVG::features;
std::unique_ptr<Image_describer> image_describer;
const std::string sImage_describer = stlplus::create_filespec(sOutDir, "image_describer", "json");
if (stlplus::is_file(sImage_describer))
{
// Dynamically load the image_describer from the file (will restore old used settings)
std::ifstream stream(sImage_describer.c_str());
if (!stream)
return false;
cereal::JSONInputArchive archive(stream);
archive(cereal::make_nvp("image_describer", image_describer));
}
else
{
#ifdef USE_OCVSIFT
if (sImage_Describer_Method == "AKAZE_OPENCV")
{
image_describer.reset(new AKAZE_OCV_Image_describer);
}
else
if (sImage_Describer_Method == "SIFT_OPENCV")
{
image_describer.reset(new SIFT_OPENCV_Image_describer());
}
else
{
OPENMVG_LOG_ERROR << "Unknown image describer method.";
return EXIT_FAILURE;
}
#else
image_describer.reset(new AKAZE_OCV_Image_describer);
#endif
// Export the used Image_describer and region type for:
// - dynamic future regions computation and/or loading
{
std::ofstream stream(sImage_describer.c_str());
if (!stream)
return false;
cereal::JSONOutputArchive archive(stream);
archive(cereal::make_nvp("image_describer", image_describer));
auto regions = image_describer->Allocate();
archive(cereal::make_nvp("regions_type", regions));
}
}
// Feature extraction routines
// For each View of the SfM_Data container:
// - if regions file exist continue,
// - if no file, compute features
{
system::Timer timer;
Image<unsigned char> imageGray;
system::LoggerProgress my_progress_bar(sfm_data.GetViews().size(), "- EXTRACT FEATURES -" );
// Use a boolean to track if we must stop feature extraction
bool preemptive_exit(false);
for (auto iterViews = sfm_data.views.cbegin();
iterViews != sfm_data.views.cend() && !preemptive_exit;
++iterViews)
{
const View * view = iterViews->second.get();
const std::string
sView_filename = stlplus::create_filespec(sfm_data.s_root_path, view->s_Img_path),
sFeat = stlplus::create_filespec(sOutDir, stlplus::basename_part(sView_filename), "feat"),
sDesc = stlplus::create_filespec(sOutDir, stlplus::basename_part(sView_filename), "desc");
//If features or descriptors file are missing, compute them
if (bForce || !stlplus::file_exists(sFeat) || !stlplus::file_exists(sDesc))
{
if (!ReadImage(sView_filename.c_str(), &imageGray))
continue;
//
// Look if there is occlusion feature mask
//
Image<unsigned char> * mask = nullptr; // The mask is null by default
const std::string
mask_filename_local =
stlplus::create_filespec(sfm_data.s_root_path,
stlplus::basename_part(sView_filename) + "_mask", "png"),
mask_filename_global =
stlplus::create_filespec(sfm_data.s_root_path, "mask", "png");
Image<unsigned char> imageMask;
// Try to read the local mask
if (stlplus::file_exists(mask_filename_local))
{
if (!ReadImage(mask_filename_local.c_str(), &imageMask))
{
OPENMVG_LOG_ERROR
<< "Invalid mask: " << mask_filename_local
<< "\nStopping feature extraction.";
preemptive_exit = true;
continue;
}
// Use the local mask only if it fits the current image size
if (imageMask.Width() == imageGray.Width() && imageMask.Height() == imageGray.Height())
mask = &imageMask;
}
else
{
// Try to read the global mask
if (stlplus::file_exists(mask_filename_global))
{
if (!ReadImage(mask_filename_global.c_str(), &imageMask))
{
OPENMVG_LOG_ERROR
<< "Invalid mask: " << mask_filename_global
<< "\nStopping feature extraction.";
preemptive_exit = true;
continue;
}
// Use the global mask only if it fits the current image size
if (imageMask.Width() == imageGray.Width() && imageMask.Height() == imageGray.Height())
mask = &imageMask;
}
}
// Compute features and descriptors and export them to files
auto regions = image_describer->Describe(imageGray, mask);
if (regions && !image_describer->Save(regions.get(), sFeat, sDesc)) {
OPENMVG_LOG_ERROR
<< "Cannot save regions for images: " << sView_filename
<< "\nStopping feature extraction.";
preemptive_exit = true;
continue;
}
++my_progress_bar;
}
}
OPENMVG_LOG_INFO << "Task done in (s): " << timer.elapsed();
}
return EXIT_SUCCESS;
}