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-2018, 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
22 #include <mrpt/io/CMemoryStream.h>
23 
24 using namespace mrpt::vision;
25 using namespace mrpt::img;
26 using namespace mrpt::math;
27 using namespace mrpt::img;
28 using namespace mrpt;
29 using namespace std;
30 
32  const mrpt::img::CImage& inImg, CFeatureList& feats, unsigned int init_ID,
33  unsigned int nDesiredFeatures, const TImageROI& ROI) const
34 {
35  MRPT_UNUSED_PARAM(ROI);
37 #if MRPT_HAS_OPENCV
38 #if MRPT_OPENCV_VERSION_NUM < 0x300
39  THROW_EXCEPTION("This function requires OpenCV > 3.0.0");
40 #else
41 
42  using namespace cv;
43  vector<KeyPoint> cv_feats; // The opencv keypoint output vector
44  // Make sure we operate on a gray-scale version of the image:
45  const CImage inImg_gray(inImg, FAST_REF_OR_CONVERT_TO_GRAY);
46 
47 #if MRPT_OPENCV_VERSION_NUM >= 0x300
48 
49  const Mat theImg = cvarrToMat(inImg_gray.getAs<IplImage>());
50  Ptr<AKAZE> akaze = AKAZE::create(
51  options.AKAZEOptions.descriptor_type,
52  options.AKAZEOptions.descriptor_size,
53  options.AKAZEOptions.descriptor_channels,
54  options.AKAZEOptions.threshold, options.AKAZEOptions.nOctaves,
55  options.AKAZEOptions.nOctaveLayers, options.AKAZEOptions.diffusivity);
56 
57  akaze->detect(theImg, cv_feats);
58 
59  // *All* the features have been extracted.
60  const size_t N = cv_feats.size();
61 
62 #endif
63  // sort the AKAZE features by line length
64  for (size_t i = 0; i < N; i++)
65  {
66  for (size_t j = i + 1; j < N; j++)
67  {
68  if (cv_feats.at(j).response > cv_feats.at(i).response)
69  {
70  KeyPoint temp_point = cv_feats.at(i);
71  cv_feats.at(i) = cv_feats.at(j);
72  cv_feats.at(j) = temp_point;
73  }
74  }
75  }
76 
77  unsigned int nMax =
78  (nDesiredFeatures != 0 && N > nDesiredFeatures) ? nDesiredFeatures : N;
79  const int offset = (int)this->options.patchSize / 2 + 1;
80  const size_t size_2 = options.patchSize / 2;
81  const size_t imgH = inImg.getHeight();
82  const size_t imgW = inImg.getWidth();
83  unsigned int i = 0;
84  unsigned int cont = 0;
85  TFeatureID nextID = init_ID;
86 
87  if (!options.addNewFeatures) feats.clear();
88 
89  while (cont != nMax && i != N)
90  {
91  // Take the next feature from the ordered list of good features:
92  const KeyPoint& kp = cv_feats[i];
93  i++;
94 
95  // Patch out of the image??
96  const int xBorderInf = (int)floor(kp.pt.x - size_2);
97  const int xBorderSup = (int)floor(kp.pt.x + size_2);
98  const int yBorderInf = (int)floor(kp.pt.y - size_2);
99  const int yBorderSup = (int)floor(kp.pt.y + size_2);
100 
101  if (!(xBorderSup < (int)imgW && xBorderInf > 0 &&
102  yBorderSup < (int)imgH && yBorderInf > 0))
103  continue; // nope, skip.
104 
105  // All tests passed: add new feature:
106  CFeature::Ptr ft = std::make_shared<CFeature>();
107  ft->type = featAKAZE;
108  ft->ID = nextID++;
109  ft->x = kp.pt.x;
110  ft->y = kp.pt.y;
111  ft->response = kp.response;
112  ft->orientation = kp.angle;
113  ft->scale = kp.octave;
114  ft->patchSize = options.patchSize; // The size of the feature patch
115 
116  if (options.patchSize > 0)
117  {
118  inImg.extract_patch(
119  ft->patch, round(ft->x) - offset, round(ft->y) - offset,
120  options.patchSize,
121  options.patchSize); // Image patch surronding the feature
122  }
123  feats.push_back(ft);
124  ++cont;
125  // cout << ft->x << " " << ft->y << endl;
126  }
127 
128 #endif
129 #endif
130  MRPT_END
131 }
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: img/CImage.h:599
#define MRPT_START
Definition: exceptions.h:262
uint64_t TFeatureID
Definition of a feature ID.
void extractFeaturesAKAZE(const mrpt::img::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.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
GLintptr offset
Definition: glext.h:3925
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:892
STL namespace.
A structure for defining a ROI within an image.
This base provides a set of functions for maths stuff.
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:864
Classes for computer vision, detectors, features, etc.
Definition: CCamModel.h:18
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
Definition: CFeature.h:304
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define MRPT_END
Definition: exceptions.h:266
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:1359
AKAZE detector, OpenCV&#39;s implementation.
void push_back(const CFeature::Ptr &f)
Definition: CFeature.h:398
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020