Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
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
00047
00048
00049 int min = params->get<int>("max_keypoints");
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
00064 FeatureDetector* createDetector(const std::string& detectorName){
00065
00066 DetectorAdjuster* detAdj = NULL;
00067
00068 ROS_INFO_STREAM("Using " << detectorName << " keypoint detector.");
00069 if( detectorName == "SIFTGPU" ) {
00070 return NULL;
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;
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();
00125 }
00126 else if(descriptorType == "SURF") {
00127 extractor = new SurfDescriptorExtractor();
00128 }
00129 else if(descriptorType == "SURF128") {
00130 extractor = new SurfDescriptorExtractor();
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();
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
00171 const unsigned int howmany64bitwords = 4;
00172 assert(search_array && "Nullpointer in bruteForceSearchORB");
00173 result_index = -1;
00174 int min_distance = 1 + 256;
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 }