00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2010 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef CFeatureExtraction_H 00029 #define CFeatureExtraction_H 00030 00031 #include <mrpt/utils/CImage.h> 00032 #include <mrpt/utils/CImageFloat.h> 00033 #include <mrpt/utils/CTicTac.h> 00034 #include <mrpt/vision/utils.h> 00035 #include <mrpt/vision/CFeature.h> 00036 00037 namespace mrpt 00038 { 00039 namespace vision 00040 { 00041 /** The central class from which images can be analyzed in search of different kinds of interest points and descriptors computed for them. 00042 * To extract features from an image, create an instance of CFeatureExtraction, 00043 * fill out its CFeatureExtraction::options field, including the algorithm to use (see 00044 * CFeatureExtraction::TMethodExtraction), and call CFeatureExtraction::detectFeatures. 00045 * This will return a set of features of the class mrpt::vision::CFeature, which include 00046 * details for each interest point as well as the desired descriptors and/or patches. 00047 * 00048 * By default, a 21x21 patch is extracted for each detected feature. If the patch is not needed, 00049 * set patchSize to 0 in CFeatureExtraction::options 00050 * 00051 * The implemented <b>detection</b> algorithms are (see CFeatureExtraction::TMethodExtraction): 00052 * - KLT (Kanade-Lucas-Tomasi): A detector (no descriptor vector). 00053 * - Harris: A detector (no descriptor vector). 00054 * - BCD (Binary Corner Detector): A detector (no descriptor vector) (Not implemented yet). 00055 * - SIFT: An implementation of the SIFT detector and descriptor. The implemention may be selected with CFeatureExtraction::TOptions::SIFTOptions::implementation. 00056 * - SURF: OpenCV's implementation of SURF detector and descriptor. 00057 * 00058 * Additionally, given a list of interest points onto an image, the following 00059 * <b>descriptors</b> can be computed for each point by calling CFeatureExtraction::computeDescriptors : 00060 * - SIFT descriptor (Lowe's descriptors). 00061 * - SURF descriptor (OpenCV's implementation - Requires OpenCV 1.1.0 from SVN or later). 00062 * - Intensity-domain spin images (SpinImage): Creates a vector descriptor with the 2D histogram as a single row. 00063 * - A circular patch in polar coordinates (Polar images): The matrix descriptor is a 2D polar image centered at the interest point. 00064 * - A log-polar image patch (Log-polar images): The matrix descriptor is the 2D log-polar image centered at the interest point. 00065 * 00066 * \note The descriptor "Intensity-domain spin images" is described in "A sparse texture representation using affine-invariant regions", S Lazebnik, C Schmid, J Ponce, 2003 IEEE Computer Society Conference on Computer Vision. 00067 * 00068 * \sa mrpt::vision::CFeature 00069 */ 00070 class VISION_IMPEXP CFeatureExtraction 00071 { 00072 public: 00073 enum TSIFTImplementation 00074 { 00075 LoweBinary = 0, 00076 CSBinary, 00077 VedaldiBinary, 00078 Hess, 00079 OpenCV 00080 }; 00081 00082 /** The set of parameters for all the detectors & descriptor algorithms */ 00083 struct VISION_IMPEXP TOptions : public utils::CLoadableOptions 00084 { 00085 /** Initalizer 00086 */ 00087 TOptions(); 00088 00089 /** See utils::CLoadableOptions 00090 */ 00091 void loadFromConfigFile( 00092 const mrpt::utils::CConfigFileBase &source, 00093 const std::string §ion); 00094 00095 /** See utils::CLoadableOptions 00096 */ 00097 void dumpToTextStream(CStream &out) const; 00098 00099 /** Type of the extracted features 00100 */ 00101 TFeatureType featsType; 00102 00103 /** Size of the patch to extract, or 0 if no patch is desired (default=21). 00104 */ 00105 unsigned int patchSize; 00106 00107 /** Indicates if subpixel accuracy is desired for the extracted points (only applicable to KLT and Harris features) 00108 */ 00109 bool FIND_SUBPIXEL; 00110 00111 struct VISION_IMPEXP TKLTOptions 00112 { 00113 /** KLT Options 00114 */ 00115 int radius; // size of the block of pixels used 00116 float threshold; // (default=0.1) for rejecting weak local maxima (with min_eig < threshold*max(eig_image)) 00117 float min_distance; // minimum distance between features 00118 bool tile_image; // splits the image into 8 tiles and search for the best points in all of them (distribute the features over all the image) 00119 } KLTOptions; 00120 00121 struct VISION_IMPEXP THarrisOptions 00122 { 00123 /** Harris Options 00124 */ 00125 float threshold; // (default=0.005) for rejecting weak local maxima (with min_eig < threshold*max(eig_image)) 00126 float k; // k factor for the Harris algorithm 00127 float sigma; // standard deviation for the gaussian smoothing function 00128 int radius; // size of the block of pixels used 00129 float min_distance; // minimum distance between features 00130 bool tile_image; // splits the image into 8 tiles and search for the best points in all of them (distribute the features over all the image) 00131 } harrisOptions; 00132 00133 struct VISION_IMPEXP TBCDOptions 00134 { 00135 /** BCD Options 00136 */ 00137 } BCDOptions; 00138 00139 struct VISION_IMPEXP TFASTOptions 00140 { 00141 /** FAST Options 00142 */ 00143 int threshold; //!< default= 20 00144 bool nonmax_suppression; //!< Default = true 00145 float min_distance; //!< (default=5) minimum distance between features (in pixels) 00146 bool use_KLT_response; //!< (default=false) If true, use CImage::KLT_response to compute the response at each point instead of the FAST "standard response". 00147 } FASTOptions; 00148 00149 struct VISION_IMPEXP TSIFTOptions 00150 { 00151 /** SIFT Options 00152 */ 00153 TSIFTImplementation implementation; 00154 } SIFTOptions; 00155 00156 struct VISION_IMPEXP TSURFOptions 00157 { 00158 /** SURF Options 00159 */ 00160 bool rotation_invariant; //!< Compute the rotation invariant SURF (dim=128) if set to true (default), or the smaller uSURF otherwise (dim=64) 00161 } SURFOptions; 00162 00163 struct VISION_IMPEXP TSpinImagesOptions 00164 { 00165 /** SpinImages Options 00166 */ 00167 unsigned int hist_size_intensity; //!< Number of bins in the "intensity" axis of the 2D histogram (default=10). 00168 unsigned int hist_size_distance; //!< Number of bins in the "distance" axis of the 2D histogram (default=10). 00169 float std_dist; //!< Standard deviation in "distance", used for the "soft histogram" (default=0.4 pixels) 00170 float std_intensity; //!< Standard deviation in "intensity", used for the "soft histogram" (default=20 units [0,255]) 00171 unsigned int radius; //!< Maximum radius of the area of which the histogram is built, in pixel units (default=20 pixels) 00172 } SpinImagesOptions; 00173 00174 /** PolarImagesOptions Options 00175 */ 00176 struct VISION_IMPEXP TPolarImagesOptions 00177 { 00178 unsigned int bins_angle; //!< Number of bins in the "angular" axis of the polar image (default=8). 00179 unsigned int bins_distance; //!< Number of bins in the "distance" axis of the polar image (default=6). 00180 unsigned int radius; //!< Maximum radius of the area of which the polar image is built, in pixel units (default=20 pixels) 00181 } PolarImagesOptions; 00182 00183 /** LogPolarImagesOptions Options 00184 */ 00185 struct VISION_IMPEXP TLogPolarImagesOptions 00186 { 00187 unsigned int radius; //!< Maximum radius of the area of which the log polar image is built, in pixel units (default=30 pixels) 00188 unsigned int num_angles; //!< (default=16) Log-Polar image patch will have dimensions WxH, with: W=num_angles, H= rho_scale * log(radius) 00189 double rho_scale; //!< (default=5) Log-Polar image patch will have dimensions WxH, with: W=num_angles, H= rho_scale * log(radius) 00190 } LogPolarImagesOptions; 00191 00192 }; 00193 00194 TOptions options; //!< Set all the parameters of the desired method here before calling "detectFeatures" 00195 00196 /** Constructor 00197 */ 00198 CFeatureExtraction(); 00199 00200 /** Virtual destructor. 00201 */ 00202 virtual ~CFeatureExtraction(); 00203 00204 /** Extract features from the image based on the method defined in TOptions. 00205 * \param img (input) The image from where to extract the images. 00206 * \param feats (output) A complete list of features (containing a patch for each one of them if options.patchsize > 0). 00207 * \param nDesiredFeatures (op. input) Number of features to be extracted. Default: all possible. 00208 * \param ROI (op. input) Region of Interest. Default: The whole image. 00209 * 00210 * \sa computeDescriptors 00211 */ 00212 void detectFeatures( const CImage &img, 00213 CFeatureList &feats, 00214 unsigned int init_ID = 0, 00215 unsigned int nDesiredFeatures = 0, 00216 const TImageROI &ROI = TImageROI()) const; 00217 00218 //void detectFeatures2( 00219 // const CImage &img, 00220 // std::vector<CFeature> &feats, 00221 // unsigned int init_ID = 0, 00222 // unsigned int nDesiredFeatures = 0, 00223 // const TImageROI &ROI = TImageROI()) const; 00224 00225 00226 /** Compute one (or more) descriptors for the given set of interest points onto the image, which may have been filled out manually or from \a detectFeatures 00227 * \param in_img (input) The image from where to compute the descriptors. 00228 * \param inout_features (input/output) The list of features whose descriptors are going to be computed. 00229 * \param in_descriptor_list (input) The bitwise OR of one or several descriptors defined in TDescriptorType. 00230 * 00231 * Each value in "in_descriptor_list" represents one descriptor to be computed, for example: 00232 * \code 00233 * // This call will compute both, SIFT and Spin-Image descriptors for a list of feature points lstFeats. 00234 * fext.computeDescriptors(img, lstFeats, descSIFT | descSpinImages ); 00235 * \endcode 00236 * 00237 * \note The SIFT descriptors for already located features can only be computed through the Hess and 00238 * CSBinary implementations which may be specified in CFeatureExtraction::TOptions::SIFTOptions. 00239 * 00240 * \note This call will also use additional parameters from \a options 00241 */ 00242 void computeDescriptors( 00243 const CImage &in_img, 00244 CFeatureList &inout_features, 00245 TDescriptorType in_descriptor_list) const; 00246 00247 /** Extract more features from the image (apart from the provided ones) based on the method defined in TOptions. 00248 * \param img (input) The image from where to extract the images. 00249 * \param inList (input) The actual features in the image. 00250 * \param outList (output) The list of new features (containing a patch for each one of them if options.patchsize > 0). 00251 * \param nDesiredFeatures (op. input) Number of features to be extracted. Default: all possible. 00252 */ 00253 void findMoreFeatures( const CImage &img, 00254 const CFeatureList &inList, 00255 CFeatureList &outList, 00256 unsigned int nDesiredFeats = 0) const; 00257 00258 private: 00259 /** Compute the SIFT descriptor of the provided features into the input image 00260 * \param in_img (input) The image from where to compute the descriptors. 00261 * \param in_features (input/output) The list of features whose descriptors are going to be computed. 00262 * 00263 * \note The SIFT descriptors for already located features can only be computed through the Hess and 00264 CSBinary implementations which may be specified in CFeatureExtraction::TOptions::SIFTOptions. 00265 */ 00266 void internal_computeSiftDescriptors( const CImage &in_img, 00267 CFeatureList &in_features) const; 00268 00269 00270 /** Compute the SURF descriptor of the provided features into the input image 00271 * \param in_img (input) The image from where to compute the descriptors. 00272 * \param in_features (input/output) The list of features whose descriptors are going to be computed. 00273 */ 00274 void internal_computeSurfDescriptors( const CImage &in_img, 00275 CFeatureList &in_features) const; 00276 00277 /** Compute the intensity-domain spin images descriptor of the provided features into the input image 00278 * \param in_img (input) The image from where to compute the descriptors. 00279 * \param in_features (input/output) The list of features whose descriptors are going to be computed. 00280 * 00281 * \note Additional parameters from CFeatureExtraction::TOptions::SpinImagesOptions are used in this method. 00282 */ 00283 void internal_computeSpinImageDescriptors( const CImage &in_img, 00284 CFeatureList &in_features) const; 00285 00286 /** Compute a polar-image descriptor of the provided features into the input image 00287 * \param in_img (input) The image from where to compute the descriptors. 00288 * \param in_features (input/output) The list of features whose descriptors are going to be computed. 00289 * 00290 * \note Additional parameters from CFeatureExtraction::TOptions::PolarImagesOptions are used in this method. 00291 */ 00292 void internal_computePolarImageDescriptors( const CImage &in_img, 00293 CFeatureList &in_features) const; 00294 00295 /** Compute a log-polar image descriptor of the provided features into the input image 00296 * \param in_img (input) The image from where to compute the descriptors. 00297 * \param in_features (input/output) The list of features whose descriptors are going to be computed. 00298 * 00299 * \note Additional parameters from CFeatureExtraction::TOptions::LogPolarImagesOptions are used in this method. 00300 */ 00301 void internal_computeLogPolarImageDescriptors( const CImage &in_img, 00302 CFeatureList &in_features) const; 00303 00304 /** Select good features using the openCV implementation of the KLT method. 00305 * \param img (input) The image from where to select extract the images. 00306 * \param feats (output) A complete list of features (containing a patch for each one of them if options.patchsize > 0). 00307 * \param nDesiredFeatures (op. input) Number of features to be extracted. Default: all possible. 00308 * \param omitPixels (op. input) A mask for determining the ROI. (0: do not omit this pixel, 1: omit this pixel) 00309 */ 00310 void selectGoodFeaturesKLT( 00311 const CImage &inImg, 00312 CFeatureList &feats, 00313 unsigned int init_ID = 0, 00314 unsigned int nDesiredFeatures = 0, 00315 void *mask_ = NULL) const; 00316 00317 /** Extract features from the image based on the KLT method. 00318 * \param img The image from where to extract the images. 00319 * \param feats The list of extracted features. 00320 * \param nDesiredFeatures Number of features to be extracted. Default: authomatic. 00321 * \param ROI (op. input) Region of Interest. Default: All the image. 00322 */ 00323 void extractFeaturesKLT( 00324 const CImage &img, 00325 CFeatureList &feats, 00326 unsigned int init_ID = 0, 00327 unsigned int nDesiredFeatures = 0, 00328 const TImageROI &ROI = TImageROI()) const; 00329 00330 // ------------------------------------------------------------------------------------ 00331 // BCD 00332 // ------------------------------------------------------------------------------------ 00333 /** Extract features from the image based on the BCD method. 00334 * \param img The image from where to extract the images. 00335 * \param feats The list of extracted features. 00336 * \param nDesiredFeatures Number of features to be extracted. Default: authomatic. 00337 * \param ROI (op. input) Region of Interest. Default: All the image. 00338 */ 00339 void extractFeaturesBCD( 00340 const CImage &img, 00341 CFeatureList &feats, 00342 unsigned int init_ID = 0, 00343 unsigned int nDesiredFeatures = 0, 00344 const TImageROI &ROI = TImageROI()) const; 00345 00346 // ------------------------------------------------------------------------------------ 00347 // SIFT 00348 // ------------------------------------------------------------------------------------ 00349 /** Extract features from the image based on the SIFT method. 00350 * \param img The image from where to extract the images. 00351 * \param feats The list of extracted features. 00352 * \param nDesiredFeatures Number of features to be extracted. Default: authomatic. 00353 * \param ROI (op. input) Region of Interest. Default: All the image. 00354 */ 00355 void extractFeaturesSIFT( 00356 const CImage &img, 00357 CFeatureList &feats, 00358 unsigned int init_ID = 0, 00359 unsigned int nDesiredFeatures = 0, 00360 const TImageROI &ROI = TImageROI()) const; 00361 00362 // ------------------------------------------------------------------------------------ 00363 // SURF 00364 // ------------------------------------------------------------------------------------ 00365 /** Extract features from the image based on the SURF method. 00366 * \param img The image from where to extract the images. 00367 * \param feats The list of extracted features. 00368 * \param nDesiredFeatures Number of features to be extracted. Default: authomatic. 00369 * \param ROI (op. input) Region of Interest. Default: All the image. 00370 */ 00371 void extractFeaturesSURF( 00372 const CImage &img, 00373 CFeatureList &feats, 00374 unsigned int init_ID = 0, 00375 unsigned int nDesiredFeatures = 0, 00376 const TImageROI &ROI = TImageROI()) const; 00377 00378 // ------------------------------------------------------------------------------------ 00379 // FAST 00380 // ------------------------------------------------------------------------------------ 00381 /** Extract features from the image based on the FAST method. 00382 * \param img The image from where to extract the images. 00383 * \param feats The list of extracted features. 00384 * \param nDesiredFeatures Number of features to be extracted. Default: authomatic. 00385 * \param ROI (op. input) Region of Interest. Default: All the image. 00386 */ 00387 void extractFeaturesFAST( 00388 const CImage &img, 00389 CFeatureList &feats, 00390 unsigned int init_ID = 0, 00391 unsigned int nDesiredFeatures = 0, 00392 const TImageROI &ROI = TImageROI()) const; 00393 00394 //void extractFeaturesFAST2( 00395 // const CImage &img, 00396 // std::vector<CFeature> &feats, 00397 // unsigned int init_ID = 0, 00398 // unsigned int nDesiredFeatures = 0, 00399 // const TImageROI &ROI = TImageROI()) const; 00400 00401 00402 // ------------------------------------------------------------------------------------ 00403 // my_scale_space_extrema 00404 // ------------------------------------------------------------------------------------ 00405 /** Computes extrema in the scale space. 00406 * \param dog_pyr Pyramid of images. 00407 * \param octvs Number of considered octaves. 00408 * \param intvls Number of intervales in octaves. 00409 */ 00410 void* my_scale_space_extrema( 00411 CFeatureList &featList, void* dog_pyr, 00412 int octvs, int intvls, double contr_thr, int curv_thr, 00413 void* storage ) const; 00414 00415 /** Adjust scale if the image was initially doubled. 00416 * \param features The sequence of features. 00417 */ 00418 void my_adjust_for_img_dbl( void* features ) const; 00419 00420 /** Gets the number of times that a point in the image is higher or lower than the surroundings in the image-scale space 00421 * \param dog_pyr Pyramid of images. 00422 * \param octvs Number of considered octaves. 00423 * \param intvls Number of intervales in octaves. 00424 * \param row The row of the feature in the original image. 00425 * \param col The column of the feature in the original image. 00426 * \param nMin [out]: Times that the feature is lower than the surroundings. 00427 * \param nMax [out]: Times that the feature is higher than the surroundings. 00428 */ 00429 void getTimesExtrema( void* dog_pyr, int octvs, int intvls, float row, float col, unsigned int &nMin, unsigned int &nMax ) const; 00430 00431 /** Computes the Laplacian value of the feature in the corresponing image in the pyramid. 00432 * \param dog_pyr Pyramid of images. 00433 * \param octvs Number of considered octaves. 00434 * \param intvls Number of intervales in octaves. 00435 * \param row The row of the feature in the original image. 00436 * \param col The column of the feature in the original image. 00437 */ 00438 double getLaplacianValue( void* dog_pyr, int octvs, int intvls, float row, float col ) const; 00439 00440 /** Append a sequence of openCV features into an MRPT feature list. 00441 * \param features The sequence of features. 00442 * \param list [in-out] The list of MRPT features. 00443 * \param init_ID [in] The initial ID for the new features. 00444 */ 00445 void insertCvSeqInCFeatureList( void* features, CFeatureList &list, unsigned int init_ID = 0 ) const; 00446 00447 /** Converts a sequence of openCV features into an MRPT feature list. 00448 * \param features The sequence of features. 00449 * \param list [in-out] The list of MRPT features. 00450 * \param init_ID [in][optional] The initial ID for the features (default = 0). 00451 * \param ROI [in][optional] The initial ID for the features (default = empty ROI -> not used). 00452 */ 00453 void convertCvSeqInCFeatureList( void* features, CFeatureList &list, unsigned int init_ID = 0, const TImageROI &ROI = TImageROI() ) const; 00454 00455 }; // end of class 00456 } // end of namespace 00457 } // end of namespace 00458 #endif
| Page generated by Doxygen 1.7.1 for MRPT 0.9.1 SVN:exported at Fri Aug 6 10:31:35 UTC 2010 |
