MRPT  2.0.4
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-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 #include "vision-precomp.h" // Precompiled headers
11 
13 
14 // Universal include for all versions of OpenCV
15 #include <mrpt/3rdparty/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  // -----------------------------------------------------------------
50  // Select good features with subpixel accuracy (USING HARRIS OR KLT)
51  // -----------------------------------------------------------------
52  const bool use_harris = (options.featsType == featHarris);
53 
54  std::vector<cv::Point2f> points;
55  profiler.enter("extractFeaturesKLT.goodFeaturesToTrack");
56 
57  cv::goodFeaturesToTrack(
58  cGrey, points, nPts,
59  (double)options.harrisOptions.threshold, // for rejecting weak local
60  // maxima ( with min_eig <
61  // threshold*max(eig_image) )
62  (double)options.harrisOptions
63  .min_distance, // minimum distance between features
64  cv::noArray(), // mask
65  3, // blocksize
66  use_harris, /* harris */
67  options.harrisOptions.k);
68 
69  profiler.leave("extractFeaturesKLT.goodFeaturesToTrack");
70 
71  const unsigned int count = points.size();
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  const float W = options.patchSize * 0.5f;
101 
102  for (; i < limit; i++)
103  {
104  const int xBorderInf = (int)floor(points[i].x - W);
105  const int xBorderSup = (int)floor(points[i].x + W);
106  const int yBorderInf = (int)floor(points[i].y - W);
107  const int yBorderSup = (int)floor(points[i].y + W);
108 
109  if (options.patchSize != 0 &&
110  ((xBorderSup >= (int)imgW) || (xBorderInf < 0) ||
111  (yBorderSup >= (int)imgH) || (yBorderInf < 0)))
112  {
113  borderFeats++;
114  continue;
115  }
116 
117  CFeature ft;
118 
119  ft.type = featKLT;
120  ft.keypoint.pt.x = points[i].x; // X position
121  ft.keypoint.pt.y = points[i].y; // Y position
122  ft.track_status = status_TRACKED; // Feature Status
123  ft.response = 0.0; // A value proportional to the quality of the
124  // feature (unused yet)
125  ft.keypoint.ID = nCFeats++; // Feature ID into extraction
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(ft.keypoint.pt.x) - offset,
133  round(ft.keypoint.pt.y) - offset, options.patchSize,
134  options.patchSize); // Image patch surronding the feature
135  }
136 
137  feats.emplace_back(std::move(ft));
138 
139  } // end while
140 
141 #else
142  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
143 #endif
144 
145  MRPT_END
146 
147 } // end of function
#define MRPT_START
Definition: exceptions.h:241
#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
cv::Mat & asCvMatRef()
Get a reference to the internal cv::Mat, which can be resized, etc.
Definition: CImage.cpp:230
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:855
STL namespace.
TFeatureID ID
ID of the feature.
Definition: TKeyPoint.h:39
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: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
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:1172
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.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020