From 0f96bf770909981a40258615e5015c0d0e13479e Mon Sep 17 00:00:00 2001 From: matlabbe Date: Mon, 30 Jan 2017 22:24:51 -0500 Subject: [PATCH] Added FREAK detector/descriptor option with OpenCV3 --- corelib/include/rtabmap/core/Features2d.h | 26 ++++++- corelib/src/Features2d.cpp | 69 ++++++++++++++++ guilib/src/PreferencesDialog.cpp | 22 +++++- guilib/src/ui/preferencesDialog.ui | 95 ++++++++++++++++++++--- 4 files changed, 199 insertions(+), 13 deletions(-) diff --git a/corelib/include/rtabmap/core/Features2d.h b/corelib/include/rtabmap/core/Features2d.h index b4d9f6c5..c49cbdd0 100644 --- a/corelib/include/rtabmap/core/Features2d.h +++ b/corelib/include/rtabmap/core/Features2d.h @@ -104,7 +104,8 @@ public: kFeatureGfttFreak=5, kFeatureGfttBrief=6, kFeatureBrisk=7, - kFeatureGfttOrb=8}; //new 0.10.11 + kFeatureGfttOrb=8, //new 0.10.11 + kFeatureFreak=9}; //new 0.11.14 static Feature2D * create(const ParametersMap & parameters = ParametersMap()); static Feature2D * create(Feature2D::Type type, const ParametersMap & parameters = ParametersMap()); // for convenience @@ -431,6 +432,29 @@ private: cv::Ptr brisk_; }; +//FREAK +class RTABMAP_EXP FREAK : public Feature2D +{ +public: + FREAK(const ParametersMap & parameters = ParametersMap()); + virtual ~FREAK(); + + virtual void parseParameters(const ParametersMap & parameters); + virtual Feature2D::Type getType() const { return kFeatureFreak; } + +private: + virtual std::vector generateKeypointsImpl(const cv::Mat & image, const cv::Rect & roi, const cv::Mat & mask = cv::Mat()) const; + virtual cv::Mat generateDescriptorsImpl(const cv::Mat & image, std::vector & keypoints) const; + +private: + bool orientationNormalized_; + bool scaleNormalized_; + float patternScale_; + int nOctaves_; + + cv::Ptr _freak; +}; + } diff --git a/corelib/src/Features2d.cpp b/corelib/src/Features2d.cpp index dc89ddf0..6991030a 100644 --- a/corelib/src/Features2d.cpp +++ b/corelib/src/Features2d.cpp @@ -1418,4 +1418,73 @@ cv::Mat BRISK::generateDescriptorsImpl(const cv::Mat & image, std::vector(new CV_FREAK(orientationNormalized_, scaleNormalized_, patternScale_, nOctaves_)); +#else +#ifdef HAVE_OPENCV_XFEATURES2D + _freak = CV_FREAK::create(orientationNormalized_, scaleNormalized_, patternScale_, nOctaves_); +#else + UWARN("RTAB-Map is not built with OpenCV xfeatures2d module so Freak cannot be used!"); +#endif +#endif +} + +std::vector FREAK::generateKeypointsImpl(const cv::Mat & image, const cv::Rect & roi, const cv::Mat & mask) const +{ + UASSERT(!image.empty() && image.channels() == 1 && image.depth() == CV_8U); + std::vector keypoints; +#ifdef HAVE_OPENCV_XFEATURES2D + cv::Mat imgRoi(image, roi); + cv::Mat maskRoi; + if (!mask.empty()) + { + maskRoi = cv::Mat(mask, roi); + } + _freak->detect(imgRoi, keypoints, maskRoi); // Opencv keypoints +#else + UWARN("RTAB-Map is not built with OpenCV3 xfeatures2d module so Freak (for keypoint detection) cannot be used!"); +#endif + return keypoints; +} + +cv::Mat FREAK::generateDescriptorsImpl(const cv::Mat & image, std::vector & keypoints) const +{ + UASSERT(!image.empty() && image.channels() == 1 && image.depth() == CV_8U); + cv::Mat descriptors; +#if CV_MAJOR_VERSION < 3 + _freak->compute(image, keypoints, descriptors); +#else +#ifdef HAVE_OPENCV_XFEATURES2D + _freak->compute(image, keypoints, descriptors); +#else + UWARN("RTAB-Map is not built with OpenCV xfeatures2d module so Freak cannot be used!"); +#endif +#endif + return descriptors; +} + } diff --git a/guilib/src/PreferencesDialog.cpp b/guilib/src/PreferencesDialog.cpp index 4d6f07a7..bec5fd44 100644 --- a/guilib/src/PreferencesDialog.cpp +++ b/guilib/src/PreferencesDialog.cpp @@ -176,8 +176,11 @@ PreferencesDialog::PreferencesDialog(QWidget * parent) : #endif #endif -#if CV_MAJOR_VERSION == 3 +#if CV_MAJOR_VERSION >= 3 _ui->groupBox_fast_opencv2->setEnabled(false); +#else + _ui->comboBox_detector_strategy->setItemData(9, 0, Qt::UserRole - 1); // No FREAK (detector+descriptor version) + _ui->reextract_type->setItemData(9, 0, Qt::UserRole - 1); // No FREAK (detector+descriptor version) #endif _ui->comboBox_cameraImages_odomFormat->setItemData(4, 0, Qt::UserRole - 1); @@ -2253,6 +2256,23 @@ bool PreferencesDialog::validateForm() } #endif +#if CV_MAJOR_VERSION < 3 + if (_ui->comboBox_detector_strategy->currentIndex() == Feature2D::kFeatureFreak) + { + QMessageBox::warning(this, tr("Parameter warning"), + tr("Selected feature type (FREAK detector) is not available on OpenCV2. ORB is set instead " + "for the bag-of-words dictionary.")); + _ui->comboBox_detector_strategy->setCurrentIndex(Feature2D::kFeatureOrb); + } + if (_ui->reextract_type->currentIndex() == Feature2D::kFeatureFreak) + { + QMessageBox::warning(this, tr("Parameter warning"), + tr("Selected feature type (FREAK detector) is not available on OpenCV2. ORB is set instead " + "for the re-extraction of features on loop closure.")); + _ui->reextract_type->setCurrentIndex(Feature2D::kFeatureOrb); + } +#endif + // optimization strategy if(_ui->graphOptimization_type->currentIndex() == 0 && !Optimizer::isAvailable(Optimizer::kTypeTORO)) { diff --git a/guilib/src/ui/preferencesDialog.ui b/guilib/src/ui/preferencesDialog.ui index 78a0aa52..37203494 100644 --- a/guilib/src/ui/preferencesDialog.ui +++ b/guilib/src/ui/preferencesDialog.ui @@ -63,16 +63,25 @@ 0 - 0 - 673 - 2649 + -274 + 677 + 2512 0 - + + 0 + + + 0 + + + 0 + + 0 @@ -86,7 +95,7 @@ QFrame::Raised - 1 + 19 @@ -4322,7 +4331,16 @@ when using the file type, logs are saved in LogRtabmap.txt (located in the worki Directory of images (optional settings) - + + 0 + + + 0 + + + 0 + + 0 @@ -6467,6 +6485,11 @@ generate the number of words requested. GFTT+ORB + + + FREAK + + @@ -10592,7 +10615,16 @@ If set to false, classic RTAB-Map loop closure detection is done using only imag - + + 0 + + + 0 + + + 0 + + 0 @@ -10732,7 +10764,16 @@ Lower the ratio -> higher the precision. 0 means disabled, matching the neare - + + 0 + + + 0 + + + 0 + + 0 @@ -10890,7 +10931,16 @@ Lower the ratio -> higher the precision. 0 means disabled, matching the neare 0 - + + 0 + + + 0 + + + 0 + + 0 @@ -10970,7 +11020,16 @@ Lower the ratio -> higher the precision. 0 means disabled, matching the neare 0 - + + 0 + + + 0 + + + 0 + + 0 @@ -11082,7 +11141,16 @@ Lower the ratio -> higher the precision. 0 means disabled, matching the neare 0 - + + 0 + + + 0 + + + 0 + + 0 @@ -11299,6 +11367,11 @@ Lower the ratio -> higher the precision. 0 means disabled, matching the neare GFTT+ORB + + + FREAK + +