features.cpp
Go to the documentation of this file.
00001 /* This file is part of RGBDSLAM.
00002  * 
00003  * RGBDSLAM is free software: you can redistribute it and/or modify
00004  * it under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation, either version 3 of the License, or
00006  * (at your option) any later version.
00007  * 
00008  * RGBDSLAM is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  * 
00013  * You should have received a copy of the GNU General Public License
00014  * along with RGBDSLAM.  If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 #include <cv.h>
00017 #if CV_MAJOR_VERSION > 2 || CV_MINOR_VERSION >= 4
00018 #include "opencv2/core/core.hpp"
00019 #include "opencv2/features2d/features2d.hpp"
00020 #include "opencv2/highgui/highgui.hpp"
00021 #ifdef CV_NONFREE
00022 #include "opencv2/nonfree/nonfree.hpp"
00023 #endif
00024 #endif
00025 #include "aorb.h"
00026 
00027 #include "feature_adjuster.h"
00028 #include "parameter_server.h"
00029 #include "ros/ros.h"
00030 //For tolower and transform
00031 #include <string>
00032 #include <algorithm>
00033 
00034 using namespace cv;
00035 
00036 StatefulFeatureDetector* adjusterWrapper(cv::Ptr<DetectorAdjuster> detadj, int min, int max)
00037 {
00038   int iterations = ParameterServer::instance()->get<int>("adjuster_max_iterations");
00039   ROS_INFO("Using adjusted keypoint detector with %d maximum iterations, keeping the number of keypoints between %d and %d", iterations, min, max);
00040   return new  VideoDynamicAdaptedFeatureDetector(detadj, min, max, iterations);
00041 }
00042 
00043 StatefulFeatureDetector* adjustedGridWrapper(cv::Ptr<DetectorAdjuster> detadj)
00044 {
00045   ParameterServer* params = ParameterServer::instance();
00046   //Try to get between "max" keypoints and 1.5 times of that.
00047   //We actually want exactly "max_keypoints" keypoints. Therefore
00048   //it's better to overshoot and then cut away the excessive keypoints
00049   int min = params->get<int>("max_keypoints"); //Shall not get below max
00050   int max = min * 1.5; //
00051 
00052   int gridRes = params->get<int>("detector_grid_resolution");
00053   int gridcells = gridRes*gridRes;
00054   int gridmin = round(min/static_cast<float>(gridcells));
00055   int gridmax =  round(max/static_cast<float>(gridcells));
00056   ROS_INFO("Using gridded keypoint detector with %dx%d cells, keeping %d keypoints in total.", gridRes, gridRes, max);
00057   
00058   StatefulFeatureDetector* detector = adjusterWrapper(detadj, gridmin, gridmax);
00059 
00060   return new VideoGridAdaptedFeatureDetector(detector, max, gridRes, gridRes);
00061 }
00062 
00063 //Use Grid or Dynamic or GridDynamic as prefix of FAST, SIFT, SURF or AORB
00064 FeatureDetector* createDetector(const std::string& detectorName){
00065   //For anything but SIFTGPU
00066   DetectorAdjuster* detAdj = NULL;
00067 
00068   ROS_INFO_STREAM("Using " << detectorName << " keypoint detector.");
00069   if( detectorName == "SIFTGPU" ) {
00070     return NULL;// Will not be used
00071   } 
00072   else if(detectorName == "FAST") {
00073      detAdj = new DetectorAdjuster("FAST", 20);
00074   }
00075 #ifdef CV_NONFREE
00076   else if(detectorName == "SURF" || detectorName == "SURF128") {
00077      detAdj = new DetectorAdjuster("SURF", 200);
00078   }
00079   else if(detectorName == "SIFT") {
00080      detAdj = new DetectorAdjuster("SIFT", 0.04, 0.0001);
00081   }
00082 #else
00083   else if(detectorName == "SURF" || detectorName == "SURF128" || detectorName == "SIFT") 
00084   {
00085      ROS_ERROR("OpenCV non-free functionality (%s) not built in.", detectorName.c_str());
00086      ROS_ERROR("To enable non-free functionality build with CV_NONFREE set.");
00087      ROS_WARN("Using ORB feature detection instead.");
00088      ParameterServer::instance()->set("feature_detector_type", std::string("ORB"));
00089      detAdj = new DetectorAdjuster("AORB", 20);
00090   }
00091 #endif
00092   else if(detectorName == "ORB") {
00093      detAdj = new DetectorAdjuster("AORB", 20);
00094   } 
00095   else {
00096     ROS_ERROR("Unsupported Keypoint Detector. Using ORB as fallback.");
00097     return createDetector("ORB");
00098   }
00099   assert(detAdj != NULL && "No valid detector aduster");
00100 
00101   ParameterServer* params = ParameterServer::instance();
00102   bool gridWrap = (params->get<int>("detector_grid_resolution") > 1);
00103   bool dynaWrap = (params->get<int>("adjuster_max_iterations") > 0);
00104 
00105   if(dynaWrap && gridWrap){
00106     return adjustedGridWrapper(detAdj);
00107   }
00108   else if(dynaWrap){
00109     int min = params->get<int>("max_keypoints");
00110     int max = min * 1.5; //params->get<int>("max_keypoints");
00111     return adjusterWrapper(detAdj, min, max);
00112   }
00113   else return detAdj;
00114 }
00115 
00116 DescriptorExtractor* createDescriptorExtractor(const std::string& descriptorType) 
00117 {
00118     DescriptorExtractor* extractor = 0;
00119     if(descriptorType == "ORB") {
00120         extractor = new OrbDescriptorExtractor();
00121     }
00122 #ifdef CV_NONFREE
00123     else if(descriptorType == "SIFT") {
00124         extractor = new SiftDescriptorExtractor();/*( double magnification=SIFT::DescriptorParams::GET_DEFAULT_MAGNIFICATION(), bool isNormalize=true, bool recalculateAngles=true, int nOctaves=SIFT::CommonParams::DEFAULT_NOCTAVES, int nOctaveLayers=SIFT::CommonParams::DEFAULT_NOCTAVE_LAYERS, int firstOctave=SIFT::CommonParams::DEFAULT_FIRST_OCTAVE, int angleMode=SIFT::CommonParams::FIRST_ANGLE )*/
00125     }
00126     else if(descriptorType == "SURF") {
00127         extractor = new SurfDescriptorExtractor();/*( int nOctaves=4, int nOctaveLayers=2, bool extended=false )*/
00128     }
00129     else if(descriptorType == "SURF128") {
00130         extractor = new SurfDescriptorExtractor();/*( int nOctaves=4, int nOctaveLayers=2, bool extended=false )*/
00131         extractor->set("extended", 1);
00132     }
00133 #else
00134     else if(descriptorType == "SURF128" || descriptorType == "SIFT" || descriptorType == "SURF") 
00135     {
00136         ROS_ERROR("OpenCV non-free functionality (%s) not built in.", descriptorType.c_str());
00137         ROS_ERROR("To enable non-free functionality build with CV_NONFREE set.");
00138         ROS_WARN("Using ORB feature detection instead.");
00139         ParameterServer::instance()->set("feature_extractor_type", std::string("ORB"));
00140         return createDescriptorExtractor("ORB");
00141     }
00142 #endif
00143     else if(descriptorType == "BRIEF") {
00144         extractor = new BriefDescriptorExtractor();
00145     }
00146     else if(descriptorType == "BRISK") {
00147         extractor = new cv::BRISK();/*brisk default: (int thresh=30, int octaves=3, float patternScale=1.0f)*/
00148     }
00149     else if(descriptorType == "FREAK") {
00150         extractor = new cv::FREAK();
00151     }
00152     else if(descriptorType == "SIFTGPU") {
00153       ROS_DEBUG("%s is to be used as extractor, creating ORB descriptor extractor as fallback.", descriptorType.c_str());
00154       return createDescriptorExtractor("ORB");
00155     }
00156     else {
00157       ROS_ERROR("No valid descriptor-matcher-type given: %s. Using ORB", descriptorType.c_str());
00158       return createDescriptorExtractor("ORB");
00159     }
00160     assert(extractor != 0 && "No extractor could be created");
00161     return extractor;
00162 }
00163 
00164 static inline int hamming_distance_orb32x8_popcountll(const uint64_t* v1, const uint64_t* v2) {
00165   return (__builtin_popcountll(v1[0] ^ v2[0]) + __builtin_popcountll(v1[1] ^ v2[1])) +
00166          (__builtin_popcountll(v1[2] ^ v2[2]) + __builtin_popcountll(v1[3] ^ v2[3]));
00167 }
00168 
00169 int bruteForceSearchORB(const uint64_t* v, const uint64_t* search_array, const unsigned int& size, int& result_index){
00170   //constexpr unsigned int howmany64bitwords = 4;//32*8/64;
00171   const unsigned int howmany64bitwords = 4;//32*8/64;
00172   assert(search_array && "Nullpointer in bruteForceSearchORB");
00173   result_index = -1;//impossible
00174   int min_distance = 1 + 256;//More than maximum distance
00175   for(unsigned int i = 0; i < size-1; i+=1, search_array+=4){
00176     int hamming_distance_i = hamming_distance_orb32x8_popcountll(v, search_array);
00177     if(hamming_distance_i < min_distance){
00178       min_distance = hamming_distance_i;
00179       result_index = i;
00180     }
00181   } 
00182   return min_distance;
00183 }


rgbdslam_v2
Author(s): Felix Endres, Juergen Hess, Nikolas Engelhard
autogenerated on Thu Jun 6 2019 21:49:45