Visual Servoing Platform  version 3.6.1 under development (2024-05-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 
72 class VISP_EXPORT vpKltOpencv
73 {
74 public:
78  vpKltOpencv();
82  vpKltOpencv(const vpKltOpencv &copy);
86  virtual ~vpKltOpencv();
87 
93  void addFeature(const float &x, const float &y);
94 
105  void addFeature(const long &id, const float &x, const float &y);
106 
112  void addFeature(const cv::Point2f &f);
113 
121  void display(const vpImage<unsigned char> &I, const vpColor &color = vpColor::red, unsigned int thickness = 1);
130  static void display(const vpImage<unsigned char> &I, const std::vector<cv::Point2f> &features,
131  const vpColor &color = vpColor::green, unsigned int thickness = 1);
140  static void display(const vpImage<vpRGBa> &I, const std::vector<cv::Point2f> &features,
141  const vpColor &color = vpColor::green, unsigned int thickness = 1);
151  static void display(const vpImage<unsigned char> &I, const std::vector<cv::Point2f> &features,
152  const std::vector<long> &featuresid, const vpColor &color = vpColor::green,
153  unsigned int thickness = 1);
163  static void display(const vpImage<vpRGBa> &I, const std::vector<cv::Point2f> &features,
164  const std::vector<long> &featuresid, const vpColor &color = vpColor::green,
165  unsigned int thickness = 1);
166 
168  int getBlockSize() const { return m_blockSize; }
180  void getFeature(const int &index, long &id, float &x, float &y) const;
182  std::vector<cv::Point2f> getFeatures() const { return m_points[1]; }
183  // CvPoint2D32f* getFeatures() const {return features;}
185  std::vector<long> getFeaturesId() const { return m_points_id; }
186  // long* getFeaturesId() const {return featuresid;}
188  double getHarrisFreeParameter() const { return m_harris_k; }
190  // bool *getListOfLostFeature() const { return lostDuringTrack; }
192  int getMaxFeatures() const { return m_maxCount; }
195  double getMinDistance() const { return m_minDistance; }
197  int getNbFeatures() const { return (int)m_points[1].size(); }
199  int getNbPrevFeatures() const { return (int)m_points[0].size(); }
200  // void getPrevFeature(int index, int &id, float &x, float &y) const;
202  std::vector<cv::Point2f> getPrevFeatures() const { return m_points[0]; }
203  // CvPoint2D32f* getPrevFeatures() const {return prev_features;}
205  // long* getPrevFeaturesId() const {return prev_featuresid;}
207  int getPyramidLevels() const { return m_pyrMaxLevel; }
210  double getQuality() const { return m_qualityLevel; }
212  int getWindowSize() const { return m_winSize; }
213 
224  void initTracking(const cv::Mat &I, const cv::Mat &mask = cv::Mat());
225 
233  void initTracking(const cv::Mat &I, const std::vector<cv::Point2f> &pts);
234 
243  void initTracking(const cv::Mat &I, const std::vector<cv::Point2f> &pts, const std::vector<long> &ids);
244 
248  vpKltOpencv &operator=(const vpKltOpencv &copy);
249 
255  void track(const cv::Mat &I);
256 
266  void setBlockSize(int blockSize) { m_blockSize = blockSize; }
267 
274  void setHarrisFreeParameter(double harris_k) { m_harris_k = harris_k; }
275 
288  void setInitialGuess(const std::vector<cv::Point2f> &guess_pts);
289 
306  void setInitialGuess(const std::vector<cv::Point2f> &init_pts, const std::vector<cv::Point2f> &guess_pts,
307  const std::vector<long> &fid);
314  void setMaxFeatures(int maxCount) { m_maxCount = maxCount; }
315 
323  void setMinDistance(double minDistance) { m_minDistance = minDistance; }
324 
332  void setMinEigThreshold(double minEigThreshold) { m_minEigThreshold = minEigThreshold; }
333 
342  void setPyramidLevels(int pyrMaxLevel) { m_pyrMaxLevel = pyrMaxLevel; }
343 
355  void setQuality(double qualityLevel) { m_qualityLevel = qualityLevel; }
356 
359  void setTrackerId(int tid) { (void)tid; }
360 
367  void setUseHarris(int useHarrisDetector) { m_useHarrisDetector = useHarrisDetector; }
368 
376  void setWindowSize(int winSize) { m_winSize = winSize; }
377 
383  void suppressFeature(const int &index);
384 
385 protected:
386  cv::Mat m_gray;
387  cv::Mat m_prevGray;
388  std::vector<cv::Point2f> m_points[2];
389  std::vector<long> m_points_id;
391  cv::TermCriteria m_termcrit;
392  int m_winSize;
393  double m_qualityLevel;
394  double m_minDistance;
396  double m_harris_k;
402 };
403 
404 #endif
405 #endif
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
static const vpColor red
Definition: vpColor.h:211
static const vpColor green
Definition: vpColor.h:214
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:73
std::vector< long > m_points_id
Keypoint id.
Definition: vpKltOpencv.h:389
std::vector< cv::Point2f > getPrevFeatures() const
Get the list of previous features.
Definition: vpKltOpencv.h:202
int m_useHarrisDetector
true to use Harris detector
Definition: vpKltOpencv.h:398
int m_maxCount
Max number of keypoints.
Definition: vpKltOpencv.h:390
double getQuality() const
Definition: vpKltOpencv.h:210
int getMaxFeatures() const
Get the list of lost feature.
Definition: vpKltOpencv.h:192
void setBlockSize(int blockSize)
Definition: vpKltOpencv.h:266
int m_blockSize
Block size.
Definition: vpKltOpencv.h:397
void setQuality(double qualityLevel)
Definition: vpKltOpencv.h:355
int getNbPrevFeatures() const
Get the number of previous features.
Definition: vpKltOpencv.h:199
void setTrackerId(int tid)
Definition: vpKltOpencv.h:359
int getWindowSize() const
Get the window size used to refine the corner locations.
Definition: vpKltOpencv.h:212
double m_minDistance
Mins distance between keypoints.
Definition: vpKltOpencv.h:394
int getNbFeatures() const
Get the number of current features.
Definition: vpKltOpencv.h:197
cv::TermCriteria m_termcrit
Term criteria.
Definition: vpKltOpencv.h:391
std::vector< cv::Point2f > getFeatures() const
Get the list of current features.
Definition: vpKltOpencv.h:182
double m_minEigThreshold
Min eigen threshold.
Definition: vpKltOpencv.h:395
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:274
int m_pyrMaxLevel
Pyramid max level.
Definition: vpKltOpencv.h:399
double getHarrisFreeParameter() const
Get the free parameter of the Harris detector.
Definition: vpKltOpencv.h:188
double m_qualityLevel
Quality level.
Definition: vpKltOpencv.h:393
bool m_initial_guess
true when initial guess is provided
Definition: vpKltOpencv.h:401
cv::Mat m_gray
Gray image.
Definition: vpKltOpencv.h:386
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:314
void setMinEigThreshold(double minEigThreshold)
Definition: vpKltOpencv.h:332
std::vector< long > getFeaturesId() const
Get the unique id of each feature.
Definition: vpKltOpencv.h:185
double m_harris_k
Harris parameter.
Definition: vpKltOpencv.h:396
double getMinDistance() const
Definition: vpKltOpencv.h:195
int m_winSize
Window criteria.
Definition: vpKltOpencv.h:392
long m_next_points_id
Id for the newt keypoint.
Definition: vpKltOpencv.h:400
cv::Mat m_prevGray
Previous gray image.
Definition: vpKltOpencv.h:387
void setMinDistance(double minDistance)
Definition: vpKltOpencv.h:323
int getBlockSize() const
Get the size of the averaging block used to track the features.
Definition: vpKltOpencv.h:168
int getPyramidLevels() const
Get the list of features id.
Definition: vpKltOpencv.h:207
void setUseHarris(int useHarrisDetector)
Definition: vpKltOpencv.h:367
void setWindowSize(int winSize)
Definition: vpKltOpencv.h:376
void setPyramidLevels(int pyrMaxLevel)
Definition: vpKltOpencv.h:342