MRPT  1.9.9
CFeatureExtraction_harris_KLT.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 #include "vision-precomp.h" // Precompiled headers
11 
13 
14 // Universal include for all versions of OpenCV
15 #include <mrpt/otherlibs/do_opencv_includes.h>
16 
17 using namespace mrpt;
18 using namespace mrpt::img;
19 using namespace mrpt::vision;
20 using namespace mrpt::img;
21 using namespace mrpt::system;
22 using namespace std;
23 
24 /************************************************************************************************
25  * extractFeaturesKLT
26  ************************************************************************************************/
27 void CFeatureExtraction::extractFeaturesKLT(
28  const mrpt::img::CImage& inImg, CFeatureList& feats, unsigned int init_ID,
29  unsigned int nDesiredFeatures, const TImageROI& ROI)
30 {
32  CTimeLoggerEntry tle(profiler, "extractFeaturesKLT");
33 
34 #if MRPT_HAS_OPENCV
35  const unsigned int MAX_COUNT = 300;
36 
37  // -----------------------------------------------------------------
38  // Create OpenCV Local Variables
39  // -----------------------------------------------------------------
40  profiler.enter("extractFeaturesKLT.img2gray");
41 
42  const CImage inImg_gray(inImg, FAST_REF_OR_CONVERT_TO_GRAY);
43  const cv::Mat& cGrey = inImg_gray.asCvMatRef();
44 
45  profiler.leave("extractFeaturesKLT.img2gray");
46 
47  const auto nPts = (nDesiredFeatures <= 0) ? MAX_COUNT : nDesiredFeatures;
48 
49  const auto count = nPts; // Number of points to find
50 
51  // -----------------------------------------------------------------
52  // Select good features with subpixel accuracy (USING HARRIS OR KLT)
53  // -----------------------------------------------------------------
54  const bool use_harris = (options.featsType == featHarris);
55 
56  std::vector<cv::Point2f> points;
57  profiler.enter("extractFeaturesKLT.goodFeaturesToTrack");
58 
59  cv::goodFeaturesToTrack(
60  cGrey, points, nPts,
61  (double)options.harrisOptions.threshold, // for rejecting weak local
62  // maxima ( with min_eig <
63  // threshold*max(eig_image) )
64  (double)options.harrisOptions
65  .min_distance, // minimum distance between features
66  cv::noArray(), // mask
67  3, // blocksize
68  use_harris, /* harris */
69  options.harrisOptions.k);
70 
71  profiler.leave("extractFeaturesKLT.goodFeaturesToTrack");
72 
73  if (nDesiredFeatures > 0 && count < nPts)
74  cout << "\n[WARNING][selectGoodFeaturesKLT]: Only " << count << " of "
75  << nDesiredFeatures << " points could be extracted in the image."
76  << endl;
77 
78  if (options.FIND_SUBPIXEL && !points.empty())
79  {
80  profiler.enter("extractFeaturesKLT.cornerSubPix");
81  // Subpixel interpolation
82  cv::cornerSubPix(
83  cGrey, points, cv::Size(3, 3), cv::Size(-1, -1),
84  cv::TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, 0.05));
85 
86  profiler.leave("extractFeaturesKLT.cornerSubPix");
87  }
88 
89  CTimeLoggerEntry tle2(profiler, "extractFeaturesKLT.fillFeatsStruct");
90 
91  feats.clear();
92  unsigned int borderFeats = 0;
93  unsigned int nCFeats = init_ID;
94  int i = 0;
95  const int limit = min(nPts, count);
96  int offset = (int)this->options.patchSize / 2 + 1;
97  unsigned int imgH = inImg.getHeight();
98  unsigned int imgW = inImg.getWidth();
99 
100  while (i < limit)
101  {
102  const int xBorderInf = (int)floor(points[i].x - options.patchSize / 2);
103  const int xBorderSup = (int)floor(points[i].x + options.patchSize / 2);
104  const int yBorderInf = (int)floor(points[i].y - options.patchSize / 2);
105  const int yBorderSup = (int)floor(points[i].y + options.patchSize / 2);
106 
107  if (options.patchSize == 0 ||
108  ((xBorderSup < (int)imgW) && (xBorderInf > 0) &&
109  (yBorderSup < (int)imgH) && (yBorderInf > 0)))
110  {
111  CFeature ft;
112 
113  ft.type = featKLT;
114  ft.keypoint.pt.x = points[i].x; // X position
115  ft.keypoint.pt.y = points[i].y; // Y position
116  ft.track_status = status_TRACKED; // Feature Status
117  ft.response = 0.0; // A value proportional to the quality of the
118  // feature (unused yet)
119  ft.keypoint.ID = nCFeats++; // Feature ID into extraction
120  ft.patchSize = options.patchSize; // The size of the feature patch
121 
122  if (options.patchSize > 0)
123  {
124  ft.patch.emplace();
125  inImg.extract_patch(
126  *ft.patch, round(ft.keypoint.pt.x) - offset,
127  round(ft.keypoint.pt.y) - offset, options.patchSize,
128  options.patchSize); // Image patch surronding the feature
129  }
130 
131  feats.emplace_back(std::move(ft));
132 
133  } // end if
134  else
135  borderFeats++;
136 
137  i++;
138  } // end while
139 
140 #else
141  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
142 #endif
143 
144  MRPT_END
145 
146 } // end of function
GLuint GLuint GLsizei count
Definition: glext.h:3532
#define MRPT_START
Definition: exceptions.h:241
#define min(a, b)
#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
TFeatureTrackStatus track_status
Status of the feature tracking process.
Definition: CFeature.h:76
GLintptr offset
Definition: glext.h:3936
cv::Mat & asCvMatRef()
Get a reference to the internal cv::Mat, which can be resized, etc.
Definition: CImage.cpp:233
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
GLint limit
Definition: glext.h:8342
GLsizei const GLfloat * points
Definition: glext.h:5414
A structure for defining a ROI within an image.
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
float response
A measure of the "goodness" of the feature.
Definition: CFeature.h:79
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Harris border and corner detector [HARRIS].
#define MRPT_END
Definition: exceptions.h:245
Kanade-Lucas-Tomasi feature [SHI&#39;94].
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
GLenum GLint GLint y
Definition: glext.h:3542
GLenum GLint x
Definition: glext.h:3542
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
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