15 #include <mrpt/3rdparty/do_opencv_includes.h>
27 void CFeatureExtraction::extractFeaturesKLT(
29 unsigned int nDesiredFeatures,
const TImageROI& ROI)
35 const unsigned int MAX_COUNT = 300;
40 profiler.enter(
"extractFeaturesKLT.img2gray");
43 const cv::Mat& cGrey = inImg_gray.
asCvMatRef();
45 profiler.leave(
"extractFeaturesKLT.img2gray");
47 const auto nPts = (nDesiredFeatures <= 0) ? MAX_COUNT : nDesiredFeatures;
52 const bool use_harris = (options.featsType ==
featHarris);
54 std::vector<cv::Point2f> points;
55 profiler.enter(
"extractFeaturesKLT.goodFeaturesToTrack");
57 cv::goodFeaturesToTrack(
59 (
double)options.harrisOptions.threshold,
62 (
double)options.harrisOptions
67 options.harrisOptions.k);
69 profiler.leave(
"extractFeaturesKLT.goodFeaturesToTrack");
71 const unsigned int count = points.size();
73 if (nDesiredFeatures > 0 && count < nPts)
74 cout <<
"\n[WARNING][selectGoodFeaturesKLT]: Only " << count <<
" of "
75 << nDesiredFeatures <<
" points could be extracted in the image."
78 if (options.FIND_SUBPIXEL && !points.empty())
80 profiler.enter(
"extractFeaturesKLT.cornerSubPix");
83 cGrey, points, cv::Size(3, 3), cv::Size(-1, -1),
84 cv::TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, 0.05));
86 profiler.leave(
"extractFeaturesKLT.cornerSubPix");
92 unsigned int borderFeats = 0;
93 unsigned int nCFeats = init_ID;
95 const int limit = min(nPts, count);
96 int offset = (int)this->options.patchSize / 2 + 1;
98 unsigned int imgW = inImg.
getWidth();
100 const float W = options.patchSize * 0.5f;
102 for (; i < limit; i++)
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);
109 if (options.patchSize != 0 &&
110 ((xBorderSup >= (
int)imgW) || (xBorderInf < 0) ||
111 (yBorderSup >= (int)imgH) || (yBorderInf < 0)))
128 if (options.patchSize > 0)
142 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");