Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpKltOpencv.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 
76 class VISP_EXPORT vpKltOpencv
77 {
78 public:
79  vpKltOpencv();
80  vpKltOpencv(const vpKltOpencv& copy);
81  virtual ~vpKltOpencv();
82 
83  void addFeature(const float &x, const float &y);
84  void addFeature(const long &id, const float &x, const float &y);
85  void addFeature(const cv::Point2f &f);
86 
87  void display(const vpImage<unsigned char> &I,
88  const vpColor &color = vpColor::red, unsigned int thickness=1);
89  static void display(const vpImage<unsigned char> &I, const std::vector<cv::Point2f> &features,
90  const vpColor &color = vpColor::green, unsigned int thickness=1);
91  static void display(const vpImage<vpRGBa> &I, const std::vector<cv::Point2f> &features,
92  const vpColor &color = vpColor::green, unsigned int thickness=1);
93  static void display(const vpImage<unsigned char> &I, const std::vector<cv::Point2f> &features,
94  const std::vector<long> &featuresid,
95  const vpColor &color = vpColor::green, unsigned int thickness=1);
96  static void display(const vpImage<vpRGBa> &I, const std::vector<cv::Point2f> &features,
97  const std::vector<long> &featuresid,
98  const vpColor &color = vpColor::green, unsigned int thickness=1);
99 
101  int getBlockSize() const {return m_blockSize;}
102  void getFeature(const int &index, long &id, float &x, float &y) const;
104  std::vector<cv::Point2f> getFeatures() const {return m_points[1];}
105  //CvPoint2D32f* getFeatures() const {return features;}
107  std::vector<long> getFeaturesId() const {return m_points_id;}
108  //long* getFeaturesId() const {return featuresid;}
110  double getHarrisFreeParameter() const {return m_harris_k;}
112  //bool *getListOfLostFeature() const { return lostDuringTrack; }
114  int getMaxFeatures() const {return m_maxCount;}
116  double getMinDistance() const {return m_minDistance;}
118  int getNbFeatures() const { return (int)m_points[1].size(); }
120  int getNbPrevFeatures() const { return (int)m_points[0].size(); }
121  //void getPrevFeature(int index, int &id, float &x, float &y) const;
123  std::vector<cv::Point2f> getPrevFeatures() const {return m_points[0];}
124  //CvPoint2D32f* getPrevFeatures() const {return prev_features;}
126  //long* getPrevFeaturesId() const {return prev_featuresid;}
128  int getPyramidLevels() const {return m_pyrMaxLevel;}
130  double getQuality() const {return m_qualityLevel;}
132  int getWindowSize() const {return m_winSize;}
133 
134  void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat());
135  void initTracking(const cv::Mat &I, const std::vector<cv::Point2f> &pts);
136  void initTracking(const cv::Mat &I, const std::vector<cv::Point2f> &pts, const std::vector<long> &ids);
137 
138  vpKltOpencv & operator=(const vpKltOpencv& copy);
139  void track(const cv::Mat &I);
140  void setBlockSize(const int blockSize);
141  void setHarrisFreeParameter(double harris_k);
142  void setInitialGuess(const std::vector<cv::Point2f> &guess_pts);
143  void setInitialGuess(const std::vector<cv::Point2f> &init_pts, const std::vector<cv::Point2f> &guess_pts, const std::vector<long> &fid);
144  void setMaxFeatures(const int maxCount);
145  void setMinDistance(double minDistance);
146  void setMinEigThreshold(double minEigThreshold);
147  void setPyramidLevels(const int pyrMaxLevel);
148  void setQuality(double qualityLevel);
150  void setTrackerId(int tid) {(void)tid;}
151  void setUseHarris(const int useHarrisDetector);
152  void setWindowSize(const int winSize);
153  void suppressFeature(const int &index);
154 
155 protected:
156  cv::Mat m_gray, m_prevGray;
157  std::vector<cv::Point2f> m_points[2];
158  std::vector<long> m_points_id;
160  cv::TermCriteria m_termcrit;
165  double m_harris_k;
171 
172 };
173 
174 #elif defined(VISP_HAVE_OPENCV)
175 #ifdef _CH_
176 #pragma package <opencv>
177 #endif
178 
179 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
180 # include <opencv2/imgproc/imgproc.hpp>
181 # include <opencv2/video/tracking.hpp>
182 # include <opencv2/legacy/legacy.hpp>
183 # include <opencv2/highgui/highgui.hpp>
184 #else
185 # ifndef _EiC
186 # include <cv.h>
187 # include <highgui.h>
188 # include <stdio.h>
189 # include <ctype.h>
190 # endif
191 #endif
192 
193 #include <visp3/core/vpConfig.h>
194 #include <visp3/core/vpImage.h>
195 #include <visp3/core/vpDisplay.h>
196 #include <visp3/core/vpDebug.h>
197 #include <visp3/core/vpException.h>
198 #include <visp3/core/vpTrackingException.h>
199 
200 typedef int (*funccheck)(int,double,double);
201 typedef void (*funcinfo)(int,int,int,double,double);
202 typedef void (*funcevent)(int);
203 
219 class VISP_EXPORT vpKltOpencv
220 {
221 private:
222  int initialized; //Is the tracker ready ?
223 
224  int maxFeatures; //Maximum number of features to track (Default 50)
225  int globalcountFeatures; //Keep over time for ID
226 
227  int win_size; //Size of search window for tracker (default 10)
228  double quality; //Multiplier for the maxmin eigenvalue; specifies minimal accepted quality of image corners (default 0.01)
229  double min_distance; //Limit, specifying minimum possible distance between returned corners; Euclidian distance is used. (default 10)
230  double harris_free_parameter; //Harris detector free parameter. (default 0.04)
231  int block_size; //Size of the averaging block used by the corner detector (default 3)
232  int use_harris; //0 use a simple Minimum EigenValue Detector, != 0 use Harris (default 1)
233  int pyramid_level; //Number of level for the tracker's gaussian pyramid data (default 3)
234  int _tid; //tracker id for multiple trackers
235 
236  IplImage *image; //Image buffer
237  IplImage *prev_image; //Image buffer for the previous iteration
238  IplImage *pyramid; //Gaussian pyramid data
239  IplImage *prev_pyramid; //Gaussian pyramid data for the previous iteration
240  IplImage *swap_temp; //Internal
241 
242  int countFeatures; //Currently tracked features
243  int countPrevFeatures; //Previously tracked features
244 
245  CvPoint2D32f *features; //List of features
246  CvPoint2D32f *prev_features; //List of features for previous iteration
247 
248  long *featuresid; //Array of ids for features
249  long *prev_featuresid; //Array of ids for previous features
250 
251  int flags; //Flags for tracking (internal)
252 
253  bool initial_guess; //Bool to precise if the next call to track() uses an initial guess
254 
255  bool *lostDuringTrack; // Result of the tracker for every feature : 1 = lost, 0 = still present
256  char *status; //Result of the tracker for every features : 0 = lost, 1 = found
257 
258 
259 
260  //EVENT FUNCTION POINTERS
261  funcevent OnInitialize;
262  funcinfo OnFeatureLost;
263  funcinfo OnNewFeature;
264  funcinfo OnMeasureFeature;
265  funccheck IsFeatureValid;
266 
267 private:
268 
269  //Internal
270  void clean();
271  void cleanAll();
272  void reset();
273 
274 public:
275  vpKltOpencv();
276  vpKltOpencv(const vpKltOpencv& copy);
277  virtual ~vpKltOpencv();
278 
279  void addFeature(const int &id, const float &x, const float &y);
280 
281  //Draw the tracked features on the given image
282  void display(const vpImage<unsigned char> &I,
283  vpColor color = vpColor::red, unsigned int thickness=1);
284 
286  int getBlockSize() const {return block_size;}
287  void getFeature(int index, long &id, float &x, float &y) const;
289  CvPoint2D32f* getFeatures() const {return features;}
291  long* getFeaturesId() const {return featuresid;}
293  double getHarrisFreeParameter() const {return harris_free_parameter;}
295  bool *getListOfLostFeature() const { return lostDuringTrack; }
297  int getMaxFeatures() const {return maxFeatures;}
299  double getMinDistance() const {return min_distance;}
301  int getNbFeatures() const { return countFeatures; }
303  int getNbPrevFeatures() const { return countPrevFeatures; }
304  void getPrevFeature(int index, int &id, float &x, float &y) const;
306  CvPoint2D32f* getPrevFeatures() const {return prev_features;}
308  long* getPrevFeaturesId() const {return prev_featuresid;}
310  int getPyramidLevels() const {return pyramid_level;}
312  double getQuality() const {return quality;}
314  int getWindowSize() const {return win_size;}
315 
316  //Detect corners in the image. Initialize the tracker
317  void initTracking(const IplImage *I, const IplImage *mask = NULL);
318  void initTracking(const IplImage *I, CvPoint2D32f *pts, int size);
319  void initTracking(const IplImage *I, CvPoint2D32f *pts, long *fid, int size);
320  vpKltOpencv & operator=(const vpKltOpencv& copy);
321  //Track !
322  void track(const IplImage *I);
323 
324 
325  //Seters
334  void setBlockSize(const int input) {initialized = 0; block_size=input;}
342  void setHarrisFreeParameter(double input) {initialized = 0; harris_free_parameter=input;}
343  void setInitialGuess(CvPoint2D32f **guess_pts);
344  void setInitialGuess(CvPoint2D32f **init_pts, CvPoint2D32f **guess_pts, long *fid, int size);
348  void setIsFeatureValid(funccheck input) {IsFeatureValid = input;}
349 
350  /* Should be used only before initTracking */
351  void setMaxFeatures(const int input);
359  void setMinDistance(double input) {initialized = 0; min_distance=input;}
360 
361  //Functors
362 
363  //Event when tracker is initialized -> event(id_tracker)
364  void setOnInitialize(funcevent input) {OnInitialize = input;}
365  //Event when a feature is lost -> event(id_tracker, index, uid, x, y)
366  void setOnFeatureLost(funcinfo input) {OnFeatureLost = input;}
367  //Event when a new feature is found -> event(id_tracker, index, uid, x, y)
368  void setOnNewFeature(funcinfo input) {OnNewFeature = input;}
369  //Event when a feature is found while tracking -> event(id_tracker, index, uid, x, y)
370  void setOnMeasureFeature(funcinfo input) {OnMeasureFeature = input;}
379  void setPyramidLevels(const int input) {initialized = 0; pyramid_level=input;}
380  void setQuality(double input) {initialized = 0; quality=input;}
381  void setTrackerId(int tid) {_tid = tid;}
389  void setUseHarris(const int input) {initialized = 0; use_harris=input;}
390  void setWindowSize(const int input) {initialized = 0; win_size=input;}
391 
392  void suppressFeature(int index);
393 
394  //Static Functions
395 public:
396  static void display(const vpImage<unsigned char>& I, const CvPoint2D32f* features_list,
397  const int &nbFeatures, vpColor color = vpColor::green,
398  unsigned int thickness=1);
399  static void display(const vpImage<vpRGBa>& I, const CvPoint2D32f* features_list,
400  const int &nbFeatures, vpColor color = vpColor::green,
401  unsigned int thickness=1);
402 
403  static void display(const vpImage<unsigned char>& I, const CvPoint2D32f* features_list,
404  const long *featuresid_list, const int &nbFeatures,
405  vpColor color = vpColor::green, unsigned int thickness=1);
406  static void display(const vpImage<vpRGBa>& I, const CvPoint2D32f* features_list,
407  const long *featuresid_list, const int &nbFeatures,
408  vpColor color = vpColor::green, unsigned int thickness=1);
409 
410 };
411 
412 #endif
413 #endif
int getPyramidLevels() const
Get the list of features id.
Definition: vpKltOpencv.h:128
double m_harris_k
Definition: vpKltOpencv.h:165
int getMaxFeatures() const
Get the list of lost feature.
Definition: vpKltOpencv.h:114
std::vector< cv::Point2f > getFeatures() const
Get the list of current features.
Definition: vpKltOpencv.h:104
double getHarrisFreeParameter() const
Get the free parameter of the Harris detector.
Definition: vpKltOpencv.h:110
cv::Mat m_prevGray
Definition: vpKltOpencv.h:156
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
cv::TermCriteria m_termcrit
Definition: vpKltOpencv.h:160
static const vpColor green
Definition: vpColor.h:166
static const vpColor red
Definition: vpColor.h:163
bool m_initial_guess
Definition: vpKltOpencv.h:170
std::vector< long > m_points_id
Keypoint id.
Definition: vpKltOpencv.h:158
double getQuality() const
Get the parameter characterizing the minimal accepted quality of image corners.
Definition: vpKltOpencv.h:130
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:150
int getBlockSize() const
Get the size of the averaging block used to track the features.
Definition: vpKltOpencv.h:101
double m_minDistance
Definition: vpKltOpencv.h:163
double m_minEigThreshold
Definition: vpKltOpencv.h:164
std::vector< cv::Point2f > getPrevFeatures() const
Get the list of previous features.
Definition: vpKltOpencv.h:123
double m_qualityLevel
Definition: vpKltOpencv.h:162
int m_useHarrisDetector
Definition: vpKltOpencv.h:167
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:76
int getNbFeatures() const
Get the number of current features.
Definition: vpKltOpencv.h:118
long m_next_points_id
Definition: vpKltOpencv.h:169
double getMinDistance() const
Get the minimal Euclidean distance between detected corners during initialization.
Definition: vpKltOpencv.h:116
int getNbPrevFeatures() const
Get the number of previous features.
Definition: vpKltOpencv.h:120
int m_pyrMaxLevel
Definition: vpKltOpencv.h:168
int getWindowSize() const
Get the window size used to refine the corner locations.
Definition: vpKltOpencv.h:132
std::vector< long > getFeaturesId() const
Get the unique id of each feature.
Definition: vpKltOpencv.h:107