MRPT  2.0.1
CFeatureExtraction_AKAZE.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://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/3rdparty/do_opencv_includes.h>
19 
20 #include <mrpt/io/CMemoryStream.h>
21 #include <mrpt/system/os.h>
22 #include <mrpt/vision/CFeatureExtraction.h> // important import
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, [[maybe_unused]] const TImageROI& ROI)
34 {
36  mrpt::system::CTimeLoggerEntry tle(profiler, "extractFeaturesAKAZE");
37 
38 #if MRPT_HAS_OPENCV
39 #if MRPT_OPENCV_VERSION_NUM < 0x300
40  THROW_EXCEPTION("This function requires OpenCV > 3.0.0");
41 #else
42 
43  using namespace cv;
44  vector<KeyPoint> cv_feats; // The opencv keypoint output vector
45  // Make sure we operate on a gray-scale version of the image:
46  const CImage inImg_gray(inImg, FAST_REF_OR_CONVERT_TO_GRAY);
47 
48 #if MRPT_OPENCV_VERSION_NUM >= 0x300
49 
50  Mat theImg = inImg_gray.asCvMat<Mat>(SHALLOW_COPY);
51  Ptr<AKAZE> akaze = AKAZE::create(
52 #if MRPT_OPENCV_VERSION_NUM >= 0x400
53  static_cast<cv::AKAZE::DescriptorType>(
54  options.AKAZEOptions.descriptor_type),
55 #else
56  options.AKAZEOptions.descriptor_type,
57 #endif
58  options.AKAZEOptions.descriptor_size,
59  options.AKAZEOptions.descriptor_channels,
60  options.AKAZEOptions.threshold, options.AKAZEOptions.nOctaves,
61  options.AKAZEOptions.nOctaveLayers,
62 #if MRPT_OPENCV_VERSION_NUM >= 0x400
63  static_cast<cv::KAZE::DiffusivityType>(options.AKAZEOptions.diffusivity)
64 #else
65  options.AKAZEOptions.diffusivity
66 #endif
67  );
68 
69  akaze->detect(theImg, cv_feats);
70 
71  // *All* the features have been extracted.
72  const size_t N = cv_feats.size();
73 
74 #endif
75  // sort the AKAZE features by line length
76  for (size_t i = 0; i < N; i++)
77  {
78  for (size_t j = i + 1; j < N; j++)
79  {
80  if (cv_feats.at(j).response > cv_feats.at(i).response)
81  {
82  KeyPoint temp_point = cv_feats.at(i);
83  cv_feats.at(i) = cv_feats.at(j);
84  cv_feats.at(j) = temp_point;
85  }
86  }
87  }
88 
89  unsigned int nMax =
90  (nDesiredFeatures != 0 && N > nDesiredFeatures) ? nDesiredFeatures : N;
91  const int offset = (int)this->options.patchSize / 2 + 1;
92  const size_t size_2 = options.patchSize / 2;
93  const size_t imgH = inImg.getHeight();
94  const size_t imgW = inImg.getWidth();
95  unsigned int i = 0;
96  unsigned int cont = 0;
97  TFeatureID nextID = init_ID;
98 
99  if (!options.addNewFeatures) feats.clear();
100 
101  while (cont != nMax && i != N)
102  {
103  // Take the next feature from the ordered list of good features:
104  const KeyPoint& kp = cv_feats[i];
105  i++;
106 
107  // Patch out of the image??
108  const int xBorderInf = (int)floor(kp.pt.x - size_2);
109  const int xBorderSup = (int)floor(kp.pt.x + size_2);
110  const int yBorderInf = (int)floor(kp.pt.y - size_2);
111  const int yBorderSup = (int)floor(kp.pt.y + size_2);
112 
113  if (!(xBorderSup < (int)imgW && xBorderInf > 0 &&
114  yBorderSup < (int)imgH && yBorderInf > 0))
115  continue; // nope, skip.
116 
117  // All tests passed: add new feature:
118  CFeature ft;
119  ft.type = featAKAZE;
120  ft.keypoint.ID = nextID++;
121  ft.keypoint.pt.x = kp.pt.x;
122  ft.keypoint.pt.y = kp.pt.y;
123  ft.response = kp.response;
124  ft.orientation = kp.angle;
125  ft.keypoint.octave = kp.octave;
126  ft.patchSize = options.patchSize; // The size of the feature patch
127 
128  if (options.patchSize > 0)
129  {
130  ft.patch.emplace();
131  inImg.extract_patch(
132  *ft.patch, round(kp.pt.x) - offset, round(kp.pt.y) - offset,
133  options.patchSize,
134  options.patchSize); // Image patch surronding the feature
135  }
136  feats.emplace_back(std::move(ft));
137  ++cont;
138  }
139 
140 #endif
141 #endif
142  MRPT_END
143 }
Shallow copy: the copied object is a reference to the original one.
Definition: img/CImage.h:75
#define MRPT_START
Definition: exceptions.h:241
uint64_t TFeatureID
Definition of a feature ID.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
A safe way to call enter() and leave() of a mrpt::system::CTimeLogger upon construction and destructi...
TKeyPointMethod type
Keypoint method used to detect this feature.
Definition: CFeature.h:73
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:849
STL namespace.
TFeatureID ID
ID of the feature.
Definition: TKeyPoint.h:39
Definition: img/CImage.h:23
void asCvMat(cv::Mat &out_img, copy_type_t copy_type) const
Makes a shallow or deep copy of this image into the provided cv::Mat.
Definition: CImage.cpp:217
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:818
TKeyPointf keypoint
Definition: CFeature.h:64
std::optional< mrpt::img::CImage > patch
A patch of the image surrounding the feature.
Definition: CFeature.h:67
Classes for computer vision, detectors, features, etc.
Definition: CDifodo.h:17
A generic 2D feature from an image, extracted with CFeatureExtraction Each feature may have one or mo...
Definition: CFeature.h:53
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
Definition: CFeature.h:275
void extractFeaturesAKAZE(const mrpt::img::CImage &inImg, CFeatureList &feats, unsigned int init_ID, unsigned int nDesiredFeatures, const TImageROI &ROI=TImageROI())
Extract features from the image based on the AKAZE method.
float response
A measure of the "goodness" of the feature.
Definition: CFeature.h:79
AKAZE detector, OpenCV&#39;s implementation.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define MRPT_END
Definition: exceptions.h:245
uint8_t octave
The image octave the image was found in: 0=original image, 1=1/2 image, 2=1/4 image, etc.
Definition: TKeyPoint.h:49
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:1166
float orientation
Main orientation of the feature.
Definition: CFeature.h:81
uint16_t patchSize
Size of the patch (patchSize x patchSize) (it must be an odd number)
Definition: CFeature.h:70
void emplace_back(CFeature &&f)
Definition: CFeature.h:364
pixel_coords_t pt
Coordinates in the image.
Definition: TKeyPoint.h:36
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:148
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:24



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020