MRPT  1.9.9
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-2019, 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/otherlibs/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, const TImageROI& ROI)
34 {
35  MRPT_UNUSED_PARAM(ROI);
37  mrpt::system::CTimeLoggerEntry tle(profiler, "extractFeaturesAKAZE");
38 
39 #if MRPT_HAS_OPENCV
40 #if MRPT_OPENCV_VERSION_NUM < 0x300
41  THROW_EXCEPTION("This function requires OpenCV > 3.0.0");
42 #else
43 
44  using namespace cv;
45  vector<KeyPoint> cv_feats; // The opencv keypoint output vector
46  // Make sure we operate on a gray-scale version of the image:
47  const CImage inImg_gray(inImg, FAST_REF_OR_CONVERT_TO_GRAY);
48 
49 #if MRPT_OPENCV_VERSION_NUM >= 0x300
50 
51  Mat theImg = inImg_gray.asCvMat<Mat>(SHALLOW_COPY);
52  Ptr<AKAZE> akaze = AKAZE::create(
53 #if MRPT_OPENCV_VERSION_NUM >= 0x400
54  static_cast<cv::AKAZE::DescriptorType>(
55  options.AKAZEOptions.descriptor_type),
56 #else
57  options.AKAZEOptions.descriptor_type,
58 #endif
59  options.AKAZEOptions.descriptor_size,
60  options.AKAZEOptions.descriptor_channels,
61  options.AKAZEOptions.threshold, options.AKAZEOptions.nOctaves,
62  options.AKAZEOptions.nOctaveLayers,
63 #if MRPT_OPENCV_VERSION_NUM >= 0x400
64  static_cast<cv::KAZE::DiffusivityType>(options.AKAZEOptions.diffusivity)
65 #else
66  options.AKAZEOptions.diffusivity
67 #endif
68  );
69 
70  akaze->detect(theImg, cv_feats);
71 
72  // *All* the features have been extracted.
73  const size_t N = cv_feats.size();
74 
75 #endif
76  // sort the AKAZE features by line length
77  for (size_t i = 0; i < N; i++)
78  {
79  for (size_t j = i + 1; j < N; j++)
80  {
81  if (cv_feats.at(j).response > cv_feats.at(i).response)
82  {
83  KeyPoint temp_point = cv_feats.at(i);
84  cv_feats.at(i) = cv_feats.at(j);
85  cv_feats.at(j) = temp_point;
86  }
87  }
88  }
89 
90  unsigned int nMax =
91  (nDesiredFeatures != 0 && N > nDesiredFeatures) ? nDesiredFeatures : N;
92  const int offset = (int)this->options.patchSize / 2 + 1;
93  const size_t size_2 = options.patchSize / 2;
94  const size_t imgH = inImg.getHeight();
95  const size_t imgW = inImg.getWidth();
96  unsigned int i = 0;
97  unsigned int cont = 0;
98  TFeatureID nextID = init_ID;
99 
100  if (!options.addNewFeatures) feats.clear();
101 
102  while (cont != nMax && i != N)
103  {
104  // Take the next feature from the ordered list of good features:
105  const KeyPoint& kp = cv_feats[i];
106  i++;
107 
108  // Patch out of the image??
109  const int xBorderInf = (int)floor(kp.pt.x - size_2);
110  const int xBorderSup = (int)floor(kp.pt.x + size_2);
111  const int yBorderInf = (int)floor(kp.pt.y - size_2);
112  const int yBorderSup = (int)floor(kp.pt.y + size_2);
113 
114  if (!(xBorderSup < (int)imgW && xBorderInf > 0 &&
115  yBorderSup < (int)imgH && yBorderInf > 0))
116  continue; // nope, skip.
117 
118  // All tests passed: add new feature:
119  CFeature ft;
120  ft.type = featAKAZE;
121  ft.keypoint.ID = nextID++;
122  ft.keypoint.pt.x = kp.pt.x;
123  ft.keypoint.pt.y = kp.pt.y;
124  ft.response = kp.response;
125  ft.orientation = kp.angle;
126  ft.keypoint.octave = kp.octave;
127  ft.patchSize = options.patchSize; // The size of the feature patch
128 
129  if (options.patchSize > 0)
130  {
131  ft.patch.emplace();
132  inImg.extract_patch(
133  *ft.patch, round(kp.pt.x) - offset, round(kp.pt.y) - offset,
134  options.patchSize,
135  options.patchSize); // Image patch surronding the feature
136  }
137  feats.emplace_back(std::move(ft));
138  ++cont;
139  }
140 
141 #endif
142 #endif
143  MRPT_END
144 }
Shallow copy: the copied object is a reference to the original one.
Definition: img/CImage.h:74
#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
GLintptr offset
Definition: glext.h:3936
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:878
STL namespace.
TFeatureID ID
ID of the feature.
Definition: TKeyPoint.h:39
Definition: img/CImage.h:22
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:223
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:847
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:1184
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:147
#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: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019