Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpKltOpencv.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented
32  * with opencv.
33  */
34 
42 #ifndef _vpKltOpencv_h_
43 #define _vpKltOpencv_h_
44 
45 #include <visp3/core/vpColor.h>
46 #include <visp3/core/vpConfig.h>
47 #include <visp3/core/vpImage.h>
48 
49 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
50 
51 #include <opencv2/highgui/highgui.hpp>
52 #include <opencv2/imgproc/imgproc.hpp>
53 #include <opencv2/video/tracking.hpp>
54 
73 class VISP_EXPORT vpKltOpencv
74 {
75 public:
79  vpKltOpencv();
83  vpKltOpencv(const vpKltOpencv &copy);
87  virtual ~vpKltOpencv();
88 
94  void addFeature(const float &x, const float &y);
95 
106  void addFeature(const long &id, const float &x, const float &y);
107 
113  void addFeature(const cv::Point2f &f);
114 
122  void display(const vpImage<unsigned char> &I, const vpColor &color = vpColor::red, unsigned int thickness = 1);
131  static void display(const vpImage<unsigned char> &I, const std::vector<cv::Point2f> &features,
132  const vpColor &color = vpColor::green, unsigned int thickness = 1);
141  static void display(const vpImage<vpRGBa> &I, const std::vector<cv::Point2f> &features,
142  const vpColor &color = vpColor::green, unsigned int thickness = 1);
152  static void display(const vpImage<unsigned char> &I, const std::vector<cv::Point2f> &features,
153  const std::vector<long> &featuresid, const vpColor &color = vpColor::green,
154  unsigned int thickness = 1);
164  static void display(const vpImage<vpRGBa> &I, const std::vector<cv::Point2f> &features,
165  const std::vector<long> &featuresid, const vpColor &color = vpColor::green,
166  unsigned int thickness = 1);
167 
169  int getBlockSize() const { return m_blockSize; }
181  void getFeature(const int &index, long &id, float &x, float &y) const;
183  std::vector<cv::Point2f> getFeatures() const { return m_points[1]; }
184  // CvPoint2D32f* getFeatures() const {return features;}
186  std::vector<long> getFeaturesId() const { return m_points_id; }
187  // long* getFeaturesId() const {return featuresid;}
189  double getHarrisFreeParameter() const { return m_harris_k; }
191  // bool *getListOfLostFeature() const { return lostDuringTrack; }
193  int getMaxFeatures() const { return m_maxCount; }
196  double getMinDistance() const { return m_minDistance; }
198  int getNbFeatures() const { return (int)m_points[1].size(); }
200  int getNbPrevFeatures() const { return (int)m_points[0].size(); }
201  // void getPrevFeature(int index, int &id, float &x, float &y) const;
203  std::vector<cv::Point2f> getPrevFeatures() const { return m_points[0]; }
204  // CvPoint2D32f* getPrevFeatures() const {return prev_features;}
206  // long* getPrevFeaturesId() const {return prev_featuresid;}
208  int getPyramidLevels() const { return m_pyrMaxLevel; }
211  double getQuality() const { return m_qualityLevel; }
213  int getWindowSize() const { return m_winSize; }
214 
225  void initTracking(const cv::Mat &I, const cv::Mat &mask = cv::Mat());
226 
234  void initTracking(const cv::Mat &I, const std::vector<cv::Point2f> &pts);
235 
244  void initTracking(const cv::Mat &I, const std::vector<cv::Point2f> &pts, const std::vector<long> &ids);
245 
249  vpKltOpencv &operator=(const vpKltOpencv &copy);
250 
256  void track(const cv::Mat &I);
257 
267  void setBlockSize(int blockSize) { m_blockSize = blockSize; }
268 
275  void setHarrisFreeParameter(double harris_k) { m_harris_k = harris_k; }
276 
289  void setInitialGuess(const std::vector<cv::Point2f> &guess_pts);
290 
307  void setInitialGuess(const std::vector<cv::Point2f> &init_pts, const std::vector<cv::Point2f> &guess_pts,
308  const std::vector<long> &fid);
315  void setMaxFeatures(int maxCount) { m_maxCount = maxCount; }
316 
324  void setMinDistance(double minDistance) { m_minDistance = minDistance; }
325 
333  void setMinEigThreshold(double minEigThreshold) { m_minEigThreshold = minEigThreshold; }
334 
343  void setPyramidLevels(int pyrMaxLevel) { m_pyrMaxLevel = pyrMaxLevel; }
344 
356  void setQuality(double qualityLevel) { m_qualityLevel = qualityLevel; }
357 
360  void setTrackerId(int tid) { (void)tid; }
361 
368  void setUseHarris(int useHarrisDetector) { m_useHarrisDetector = useHarrisDetector; }
369 
377  void setWindowSize(int winSize) { m_winSize = winSize; }
378 
384  void suppressFeature(const int &index);
385 
386 protected:
387  cv::Mat m_gray;
388  cv::Mat m_prevGray;
389  std::vector<cv::Point2f> m_points[2];
390  std::vector<long> m_points_id;
392  cv::TermCriteria m_termcrit;
393  int m_winSize;
394  double m_qualityLevel;
395  double m_minDistance;
397  double m_harris_k;
403 };
404 END_VISP_NAMESPACE
405 #endif
406 #endif
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:157
static const vpColor red
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:220
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:74
std::vector< long > m_points_id
Keypoint id.
Definition: vpKltOpencv.h:390
std::vector< cv::Point2f > getPrevFeatures() const
Get the list of previous features.
Definition: vpKltOpencv.h:203
int m_useHarrisDetector
true to use Harris detector
Definition: vpKltOpencv.h:399
int m_maxCount
Max number of keypoints.
Definition: vpKltOpencv.h:391
double getQuality() const
Definition: vpKltOpencv.h:211
int getMaxFeatures() const
Get the list of lost feature.
Definition: vpKltOpencv.h:193
void setBlockSize(int blockSize)
Definition: vpKltOpencv.h:267
int m_blockSize
Block size.
Definition: vpKltOpencv.h:398
void setQuality(double qualityLevel)
Definition: vpKltOpencv.h:356
int getNbPrevFeatures() const
Get the number of previous features.
Definition: vpKltOpencv.h:200
void setTrackerId(int tid)
Definition: vpKltOpencv.h:360
int getWindowSize() const
Get the window size used to refine the corner locations.
Definition: vpKltOpencv.h:213
double m_minDistance
Mins distance between keypoints.
Definition: vpKltOpencv.h:395
int getNbFeatures() const
Get the number of current features.
Definition: vpKltOpencv.h:198
cv::TermCriteria m_termcrit
Term criteria.
Definition: vpKltOpencv.h:392
std::vector< cv::Point2f > getFeatures() const
Get the list of current features.
Definition: vpKltOpencv.h:183
double m_minEigThreshold
Min eigen threshold.
Definition: vpKltOpencv.h:396
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:275
int m_pyrMaxLevel
Pyramid max level.
Definition: vpKltOpencv.h:400
double getHarrisFreeParameter() const
Get the free parameter of the Harris detector.
Definition: vpKltOpencv.h:189
double m_qualityLevel
Quality level.
Definition: vpKltOpencv.h:394
bool m_initial_guess
true when initial guess is provided
Definition: vpKltOpencv.h:402
cv::Mat m_gray
Gray image.
Definition: vpKltOpencv.h:387
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:315
void setMinEigThreshold(double minEigThreshold)
Definition: vpKltOpencv.h:333
std::vector< long > getFeaturesId() const
Get the unique id of each feature.
Definition: vpKltOpencv.h:186
double m_harris_k
Harris parameter.
Definition: vpKltOpencv.h:397
double getMinDistance() const
Definition: vpKltOpencv.h:196
int m_winSize
Window criteria.
Definition: vpKltOpencv.h:393
long m_next_points_id
Id for the newt keypoint.
Definition: vpKltOpencv.h:401
cv::Mat m_prevGray
Previous gray image.
Definition: vpKltOpencv.h:388
void setMinDistance(double minDistance)
Definition: vpKltOpencv.h:324
int getBlockSize() const
Get the size of the averaging block used to track the features.
Definition: vpKltOpencv.h:169
int getPyramidLevels() const
Get the list of features id.
Definition: vpKltOpencv.h:208
void setUseHarris(int useHarrisDetector)
Definition: vpKltOpencv.h:368
void setWindowSize(int winSize)
Definition: vpKltOpencv.h:377
void setPyramidLevels(int pyrMaxLevel)
Definition: vpKltOpencv.h:343