Visual Servoing Platform  version 3.0.0
vpKltOpencv.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
35  * Fabien Servant
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
47 #ifndef vpKltOpencv_h
48 #define vpKltOpencv_h
49 
50 #include <visp3/core/vpConfig.h>
51 #include <visp3/core/vpColor.h>
52 #include <visp3/core/vpImage.h>
53 
54 #if (defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408))
55 
56 #include <opencv2/video/tracking.hpp>
57 #include <opencv2/imgproc/imgproc.hpp>
58 #include <opencv2/highgui/highgui.hpp>
59 
75 class VISP_EXPORT vpKltOpencv
76 {
77 public:
78  vpKltOpencv();
79  vpKltOpencv(const vpKltOpencv& copy);
80  virtual ~vpKltOpencv();
81 
82  void addFeature(const float &x, const float &y);
83  void addFeature(const long &id, const float &x, const float &y);
84  void addFeature(const cv::Point2f &f);
85 
86  void display(const vpImage<unsigned char> &I,
87  const vpColor &color = vpColor::red, unsigned int thickness=1);
88  static void display(const vpImage<unsigned char> &I, const std::vector<cv::Point2f> &features,
89  const vpColor &color = vpColor::green, unsigned int thickness=1);
90  static void display(const vpImage<vpRGBa> &I, const std::vector<cv::Point2f> &features,
91  const vpColor &color = vpColor::green, unsigned int thickness=1);
92  static void display(const vpImage<unsigned char> &I, const std::vector<cv::Point2f> &features,
93  const std::vector<long> &featuresid,
94  const vpColor &color = vpColor::green, unsigned int thickness=1);
95  static void display(const vpImage<vpRGBa> &I, const std::vector<cv::Point2f> &features,
96  const std::vector<long> &featuresid,
97  const vpColor &color = vpColor::green, unsigned int thickness=1);
98 
100  int getBlockSize() const {return m_blockSize;}
101  void getFeature(const int &index, int &id, float &x, float &y) const;
103  std::vector<cv::Point2f> getFeatures() const {return m_points[1];}
104  //CvPoint2D32f* getFeatures() const {return features;}
106  std::vector<long> getFeaturesId() const {return m_points_id;}
107  //long* getFeaturesId() const {return featuresid;}
109  double getHarrisFreeParameter() const {return m_harris_k;}
111  //bool *getListOfLostFeature() const { return lostDuringTrack; }
113  int getMaxFeatures() const {return m_maxCount;}
115  double getMinDistance() const {return m_minDistance;}
117  int getNbFeatures() const { return (int)m_points[1].size(); }
119  int getNbPrevFeatures() const { return (int)m_points[0].size(); }
120  //void getPrevFeature(int index, int &id, float &x, float &y) const;
122  std::vector<cv::Point2f> getPrevFeatures() const {return m_points[0];}
123  //CvPoint2D32f* getPrevFeatures() const {return prev_features;}
125  //long* getPrevFeaturesId() const {return prev_featuresid;}
127  int getPyramidLevels() const {return m_pyrMaxLevel;}
129  double getQuality() const {return m_qualityLevel;}
131  int getWindowSize() const {return m_winSize;}
132 
133  void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat());
134  void initTracking(const cv::Mat &I, const std::vector<cv::Point2f> &pts);
135  void initTracking(const cv::Mat &I, const std::vector<cv::Point2f> &pts, const std::vector<long> &ids);
136 
137  vpKltOpencv & operator=(const vpKltOpencv& copy);
138  void track(const cv::Mat &I);
139  void setBlockSize(const int blockSize);
140  void setHarrisFreeParameter(double harris_k);
141  void setInitialGuess(const std::vector<cv::Point2f> &guess_pts);
142  void setInitialGuess(const std::vector<cv::Point2f> &init_pts, const std::vector<cv::Point2f> &guess_pts, const std::vector<long> &fid);
143  void setMaxFeatures(const int maxCount);
144  void setMinDistance(double minDistance);
145  void setMinEigThreshold(double minEigThreshold);
146  void setPyramidLevels(const int pyrMaxLevel);
147  void setQuality(double qualityLevel);
149  void setTrackerId(int tid) {(void)tid;}
150  void setUseHarris(const int useHarrisDetector);
151  void setWindowSize(const int winSize);
152  void suppressFeature(const int &index);
153 
154 protected:
155  cv::Mat m_gray, m_prevGray;
156  std::vector<cv::Point2f> m_points[2];
157  std::vector<long> m_points_id;
159  cv::TermCriteria m_termcrit;
164  double m_harris_k;
170 
171 };
172 
173 #elif defined(VISP_HAVE_OPENCV)
174 #ifdef _CH_
175 #pragma package <opencv>
176 #endif
177 
178 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
179 # include <opencv2/imgproc/imgproc.hpp>
180 # include <opencv2/video/tracking.hpp>
181 # include <opencv2/legacy/legacy.hpp>
182 # include <opencv2/highgui/highgui.hpp>
183 #else
184 # ifndef _EiC
185 # include <cv.h>
186 # include <highgui.h>
187 # include <stdio.h>
188 # include <ctype.h>
189 # endif
190 #endif
191 
192 #include <visp3/core/vpConfig.h>
193 #include <visp3/core/vpImage.h>
194 #include <visp3/core/vpDisplay.h>
195 #include <visp3/core/vpDebug.h>
196 #include <visp3/core/vpException.h>
197 #include <visp3/core/vpTrackingException.h>
198 
199 typedef int (*funccheck)(int,double,double);
200 typedef void (*funcinfo)(int,int,int,double,double);
201 typedef void (*funcevent)(int);
202 
218 class VISP_EXPORT vpKltOpencv
219 {
220 private:
221  int initialized; //Is the tracker ready ?
222 
223  int maxFeatures; //Maximum number of features to track (Default 50)
224  int globalcountFeatures; //Keep over time for ID
225 
226  int win_size; //Size of search window for tracker (default 10)
227  double quality; //Multiplier for the maxmin eigenvalue; specifies minimal accepted quality of image corners (default 0.01)
228  double min_distance; //Limit, specifying minimum possible distance between returned corners; Euclidian distance is used. (default 10)
229  double harris_free_parameter; //Harris detector free parameter. (default 0.04)
230  int block_size; //Size of the averaging block used by the corner detector (default 3)
231  int use_harris; //0 use a simple Minimum EigenValue Detector, != 0 use Harris (default 1)
232  int pyramid_level; //Number of level for the tracker's gaussian pyramid data (default 3)
233  int _tid; //tracker id for multiple trackers
234 
235  IplImage *image; //Image buffer
236  IplImage *prev_image; //Image buffer for the previous iteration
237  IplImage *pyramid; //Gaussian pyramid data
238  IplImage *prev_pyramid; //Gaussian pyramid data for the previous iteration
239  IplImage *swap_temp; //Internal
240 
241  int countFeatures; //Currently tracked features
242  int countPrevFeatures; //Previously tracked features
243 
244  CvPoint2D32f *features; //List of features
245  CvPoint2D32f *prev_features; //List of features for previous iteration
246 
247  long *featuresid; //Array of ids for features
248  long *prev_featuresid; //Array of ids for previous features
249 
250  int flags; //Flags for tracking (internal)
251 
252  bool initial_guess; //Bool to precise if the next call to track() uses an initial guess
253 
254  bool *lostDuringTrack; // Result of the tracker for every feature : 1 = lost, 0 = still present
255  char *status; //Result of the tracker for every features : 0 = lost, 1 = found
256 
257 
258 
259  //EVENT FUNCTION POINTERS
260  funcevent OnInitialize;
261  funcinfo OnFeatureLost;
262  funcinfo OnNewFeature;
263  funcinfo OnMeasureFeature;
264  funccheck IsFeatureValid;
265 
266 private:
267 
268  //Internal
269  void clean();
270  void cleanAll();
271  void reset();
272 
273 public:
274  vpKltOpencv();
275  vpKltOpencv(const vpKltOpencv& copy);
276  virtual ~vpKltOpencv();
277 
278  void addFeature(const int &id, const float &x, const float &y);
279 
280  //Draw the tracked features on the given image
281  void display(const vpImage<unsigned char> &I,
282  vpColor color = vpColor::red, unsigned int thickness=1);
283 
285  int getBlockSize() const {return block_size;}
286  void getFeature(int index, int &id, float &x, float &y) const;
288  CvPoint2D32f* getFeatures() const {return features;}
290  long* getFeaturesId() const {return featuresid;}
292  double getHarrisFreeParameter() const {return harris_free_parameter;}
294  bool *getListOfLostFeature() const { return lostDuringTrack; }
296  int getMaxFeatures() const {return maxFeatures;}
298  double getMinDistance() const {return min_distance;}
300  int getNbFeatures() const { return countFeatures; }
302  int getNbPrevFeatures() const { return countPrevFeatures; }
303  void getPrevFeature(int index, int &id, float &x, float &y) const;
305  CvPoint2D32f* getPrevFeatures() const {return prev_features;}
307  long* getPrevFeaturesId() const {return prev_featuresid;}
309  int getPyramidLevels() const {return pyramid_level;}
311  double getQuality() const {return quality;}
313  int getWindowSize() const {return win_size;}
314 
315  //Detect corners in the image. Initialize the tracker
316  void initTracking(const IplImage *I, const IplImage *mask = NULL);
317  void initTracking(const IplImage *I, CvPoint2D32f *pts, int size);
318  void initTracking(const IplImage *I, CvPoint2D32f *pts, long *fid, int size);
319  vpKltOpencv & operator=(const vpKltOpencv& copy);
320  //Track !
321  void track(const IplImage *I);
322 
323 
324  //Seters
333  void setBlockSize(const int input) {initialized = 0; block_size=input;}
341  void setHarrisFreeParameter(double input) {initialized = 0; harris_free_parameter=input;}
342  void setInitialGuess(CvPoint2D32f **guess_pts);
343  void setInitialGuess(CvPoint2D32f **init_pts, CvPoint2D32f **guess_pts, long *fid, int size);
347  void setIsFeatureValid(funccheck input) {IsFeatureValid = input;}
348 
349  /* Should be used only before initTracking */
350  void setMaxFeatures(const int input);
358  void setMinDistance(double input) {initialized = 0; min_distance=input;}
359 
360  //Functors
361 
362  //Event when tracker is initialized -> event(id_tracker)
363  void setOnInitialize(funcevent input) {OnInitialize = input;}
364  //Event when a feature is lost -> event(id_tracker, index, uid, x, y)
365  void setOnFeatureLost(funcinfo input) {OnFeatureLost = input;}
366  //Event when a new feature is found -> event(id_tracker, index, uid, x, y)
367  void setOnNewFeature(funcinfo input) {OnNewFeature = input;}
368  //Event when a feature is found while tracking -> event(id_tracker, index, uid, x, y)
369  void setOnMeasureFeature(funcinfo input) {OnMeasureFeature = input;}
378  void setPyramidLevels(const int input) {initialized = 0; pyramid_level=input;}
379  void setQuality(double input) {initialized = 0; quality=input;}
380  void setTrackerId(int tid) {_tid = tid;}
388  void setUseHarris(const int input) {initialized = 0; use_harris=input;}
389  void setWindowSize(const int input) {initialized = 0; win_size=input;}
390 
391  void suppressFeature(int index);
392 
393  //Static Functions
394 public:
395  static void display(const vpImage<unsigned char>& I, const CvPoint2D32f* features_list,
396  const int &nbFeatures, vpColor color = vpColor::green,
397  unsigned int thickness=1);
398  static void display(const vpImage<vpRGBa>& I, const CvPoint2D32f* features_list,
399  const int &nbFeatures, vpColor color = vpColor::green,
400  unsigned int thickness=1);
401 
402  static void display(const vpImage<unsigned char>& I, const CvPoint2D32f* features_list,
403  const long *featuresid_list, const int &nbFeatures,
404  vpColor color = vpColor::green, unsigned int thickness=1);
405  static void display(const vpImage<vpRGBa>& I, const CvPoint2D32f* features_list,
406  const long *featuresid_list, const int &nbFeatures,
407  vpColor color = vpColor::green, unsigned int thickness=1);
408 
409 };
410 
411 #endif
412 #endif
int getPyramidLevels() const
Get the list of features id.
Definition: vpKltOpencv.h:127
double m_harris_k
Definition: vpKltOpencv.h:164
int getMaxFeatures() const
Get the list of lost feature.
Definition: vpKltOpencv.h:113
std::vector< cv::Point2f > getFeatures() const
Get the list of current features.
Definition: vpKltOpencv.h:103
double getHarrisFreeParameter() const
Get the free parameter of the Harris detector.
Definition: vpKltOpencv.h:109
cv::Mat m_prevGray
Definition: vpKltOpencv.h:155
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
cv::TermCriteria m_termcrit
Definition: vpKltOpencv.h:159
static const vpColor green
Definition: vpColor.h:166
static const vpColor red
Definition: vpColor.h:163
bool m_initial_guess
Definition: vpKltOpencv.h:169
std::vector< long > m_points_id
Keypoint id.
Definition: vpKltOpencv.h:157
double getQuality() const
Get the parameter characterizing the minimal accepted quality of image corners.
Definition: vpKltOpencv.h:129
void setTrackerId(int tid)
Does nothing. Just here for compat with previous releases that use OpenCV C api to do the tracking...
Definition: vpKltOpencv.h:149
int getBlockSize() const
Get the size of the averaging block used to track the features.
Definition: vpKltOpencv.h:100
double m_minDistance
Definition: vpKltOpencv.h:162
double m_minEigThreshold
Definition: vpKltOpencv.h:163
std::vector< cv::Point2f > getPrevFeatures() const
Get the list of previous features.
Definition: vpKltOpencv.h:122
double m_qualityLevel
Definition: vpKltOpencv.h:161
int m_useHarrisDetector
Definition: vpKltOpencv.h:166
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV.
Definition: vpKltOpencv.h:75
int getNbFeatures() const
Get the number of current features.
Definition: vpKltOpencv.h:117
long m_next_points_id
Definition: vpKltOpencv.h:168
double getMinDistance() const
Get the minimal Euclidean distance between detected corners during initialization.
Definition: vpKltOpencv.h:115
int getNbPrevFeatures() const
Get the number of previous features.
Definition: vpKltOpencv.h:119
int m_pyrMaxLevel
Definition: vpKltOpencv.h:167
int getWindowSize() const
Get the window size used to refine the corner locations.
Definition: vpKltOpencv.h:131
std::vector< long > getFeaturesId() const
Get the unique id of each feature.
Definition: vpKltOpencv.h:106