Main MRPT website > C++ reference for MRPT 1.9.9
CFeatureExtraction_AKAZE.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 /*---------------------------------------------------------------
11  APPLICATION: CFeatureExtraction
12  FILE: CFeatureExtraction_AKAZE.cpp
13  AUTHOR: Raghavender Sahdev <raghavendersahdev@gmail.com>
14  ---------------------------------------------------------------*/
15 
16 #include "vision-precomp.h" // Precompiled headers
17 // Universal include for all versions of OpenCV
18 #include <mrpt/otherlibs/do_opencv_includes.h>
19 
20 #include <mrpt/system/os.h>
21 #include <mrpt/vision/CFeatureExtraction.h> // important import
23 
24 using namespace mrpt::vision;
25 using namespace mrpt::utils;
26 using namespace mrpt::math;
27 using namespace mrpt;
28 using namespace std;
29 
31  const mrpt::utils::CImage& inImg, CFeatureList& feats, unsigned int init_ID,
32  unsigned int nDesiredFeatures, const TImageROI& ROI) const
33 {
34  MRPT_UNUSED_PARAM(ROI);
36 #if MRPT_HAS_OPENCV
37 #if MRPT_OPENCV_VERSION_NUM < 0x300
38  THROW_EXCEPTION("This function requires OpenCV > 3.0.0")
39 #else
40 
41  using namespace cv;
42  vector<KeyPoint> cv_feats; // The opencv keypoint output vector
43  // Make sure we operate on a gray-scale version of the image:
44  const CImage inImg_gray(inImg, FAST_REF_OR_CONVERT_TO_GRAY);
45 
46 #if MRPT_OPENCV_VERSION_NUM >= 0x300
47 
48  const Mat theImg = cvarrToMat(inImg_gray.getAs<IplImage>());
49  Ptr<AKAZE> akaze = AKAZE::create(
50  options.AKAZEOptions.descriptor_type,
51  options.AKAZEOptions.descriptor_size,
52  options.AKAZEOptions.descriptor_channels,
53  options.AKAZEOptions.threshold, options.AKAZEOptions.nOctaves,
54  options.AKAZEOptions.nOctaveLayers, options.AKAZEOptions.diffusivity);
55 
56  akaze->detect(theImg, cv_feats);
57 
58  // *All* the features have been extracted.
59  const size_t N = cv_feats.size();
60 
61 #endif
62  // sort the AKAZE features by line length
63  for (size_t i = 0; i < N; i++)
64  {
65  for (size_t j = i + 1; j < N; j++)
66  {
67  if (cv_feats.at(j).response > cv_feats.at(i).response)
68  {
69  KeyPoint temp_point = cv_feats.at(i);
70  cv_feats.at(i) = cv_feats.at(j);
71  cv_feats.at(j) = temp_point;
72  }
73  }
74  }
75 
76  unsigned int nMax =
77  (nDesiredFeatures != 0 && N > nDesiredFeatures) ? nDesiredFeatures : N;
78  const int offset = (int)this->options.patchSize / 2 + 1;
79  const size_t size_2 = options.patchSize / 2;
80  const size_t imgH = inImg.getHeight();
81  const size_t imgW = inImg.getWidth();
82  unsigned int i = 0;
83  unsigned int cont = 0;
84  TFeatureID nextID = init_ID;
85 
86  if (!options.addNewFeatures) feats.clear();
87 
88  while (cont != nMax && i != N)
89  {
90  // Take the next feature from the ordered list of good features:
91  const KeyPoint& kp = cv_feats[i];
92  i++;
93 
94  // Patch out of the image??
95  const int xBorderInf = (int)floor(kp.pt.x - size_2);
96  const int xBorderSup = (int)floor(kp.pt.x + size_2);
97  const int yBorderInf = (int)floor(kp.pt.y - size_2);
98  const int yBorderSup = (int)floor(kp.pt.y + size_2);
99 
100  if (!(xBorderSup < (int)imgW && xBorderInf > 0 &&
101  yBorderSup < (int)imgH && yBorderInf > 0))
102  continue; // nope, skip.
103 
104  // All tests passed: add new feature:
105  CFeature::Ptr ft = std::make_shared<CFeature>();
106  ft->type = featAKAZE;
107  ft->ID = nextID++;
108  ft->x = kp.pt.x;
109  ft->y = kp.pt.y;
110  ft->response = kp.response;
111  ft->orientation = kp.angle;
112  ft->scale = kp.octave;
113  ft->patchSize = options.patchSize; // The size of the feature patch
114 
115  if (options.patchSize > 0)
116  {
117  inImg.extract_patch(
118  ft->patch, round(ft->x) - offset, round(ft->y) - offset,
119  options.patchSize,
120  options.patchSize); // Image patch surronding the feature
121  }
122  feats.push_back(ft);
123  ++cont;
124  // cout << ft->x << " " << ft->y << endl;
125  }
126 
127 #endif
128 #endif
129  MRPT_END
130 }
std::shared_ptr< CFeature > Ptr
Definition: CFeature.h:58
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
void extractFeaturesAKAZE(const mrpt::utils::CImage &inImg, CFeatureList &feats, unsigned int init_ID, unsigned int nDesiredFeatures, const TImageROI &ROI=TImageROI()) const
Extract features from the image based on the AKAZE method.
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:118
#define THROW_EXCEPTION(msg)
GLintptr offset
Definition: glext.h:3925
STL namespace.
const T * getAs() const
Returns a pointer to a const T* containing the image - the idea is to call like "img.getAs<IplImage>()" so we can avoid here including OpenCV&#39;s headers.
Definition: CImage.h:587
A structure for defining a ROI within an image.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:869
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:897
Classes for computer vision, detectors, features, etc.
uint64_t TFeatureID
Definition of a feature ID.
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
Definition: CFeature.h:305
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void extract_patch(CImage &patch, const unsigned int col=0, const unsigned int row=0, const unsigned int width=1, const unsigned int height=1) const
Extract a patch from this image, saveing it into "patch" (its previous contents will be overwritten)...
Definition: CImage.cpp:1367
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:25
AKAZE detector, OpenCV&#39;s implementation.
void push_back(const CFeature::Ptr &f)
Definition: CFeature.h:399



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019