ViSP  2.10.0
vpKltOpencv.h
1 /****************************************************************************
2  *
3  * $Id: vpKltOpencv.h 5148 2015-01-12 14:31:30Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented
36  * with opencv.
37  *
38  * Authors:
39  * Fabien Servant
40  * Fabien Spindler
41  *
42  *****************************************************************************/
43 
51 #ifndef vpKltOpencv_h
52 #define vpKltOpencv_h
53 
54 #include <visp/vpConfig.h>
55 #include <visp/vpColor.h>
56 #include <visp/vpImage.h>
57 
58 #if (defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408))
59 
60 #include <opencv2/video/tracking.hpp>
61 #include <opencv2/imgproc/imgproc.hpp>
62 #include <opencv2/highgui/highgui.hpp>
63 
79 class VISP_EXPORT vpKltOpencv
80 {
81 public:
82  vpKltOpencv();
83  vpKltOpencv(const vpKltOpencv& copy);
84  virtual ~vpKltOpencv();
85 
86  void addFeature(const float &x, const float &y);
87  void addFeature(const long &id, const float &x, const float &y);
88  void addFeature(const cv::Point2f &f);
89 
90  void display(const vpImage<unsigned char> &I,
91  const vpColor &color = vpColor::red, unsigned int thickness=1);
92  static void display(const vpImage<unsigned char> &I, const std::vector<cv::Point2f> &features,
93  const vpColor &color = vpColor::green, unsigned int thickness=1);
94  static void display(const vpImage<vpRGBa> &I, const std::vector<cv::Point2f> &features,
95  const vpColor &color = vpColor::green, unsigned int thickness=1);
96  static void display(const vpImage<unsigned char> &I, const std::vector<cv::Point2f> &features,
97  const std::vector<long> &featuresid,
98  const vpColor &color = vpColor::green, unsigned int thickness=1);
99  static void display(const vpImage<vpRGBa> &I, const std::vector<cv::Point2f> &features,
100  const std::vector<long> &featuresid,
101  const vpColor &color = vpColor::green, unsigned int thickness=1);
102 
104  int getBlockSize() const {return m_blockSize;}
105  void getFeature(const int &index, int &id, float &x, float &y) const;
107  std::vector<cv::Point2f> getFeatures() const {return m_points[1];}
108  //CvPoint2D32f* getFeatures() const {return features;}
110  std::vector<long> getFeaturesId() const {return m_points_id;}
111  //long* getFeaturesId() const {return featuresid;}
113  double getHarrisFreeParameter() const {return m_harris_k;}
115  //bool *getListOfLostFeature() const { return lostDuringTrack; }
117  int getMaxFeatures() const {return m_maxCount;}
119  double getMinDistance() const {return m_minDistance;}
121  int getNbFeatures() const { return (int)m_points[1].size(); }
123  int getNbPrevFeatures() const { return (int)m_points[0].size(); }
124  //void getPrevFeature(int index, int &id, float &x, float &y) const;
126  std::vector<cv::Point2f> getPrevFeatures() const {return m_points[0];}
127  //CvPoint2D32f* getPrevFeatures() const {return prev_features;}
129  //long* getPrevFeaturesId() const {return prev_featuresid;}
131  int getPyramidLevels() const {return m_pyrMaxLevel;}
133  double getQuality() const {return m_qualityLevel;}
135  int getWindowSize() const {return m_winSize;}
136 
137  void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat());
138  void initTracking(const cv::Mat &I, const std::vector<cv::Point2f> &pts);
139  void initTracking(const cv::Mat &I, const std::vector<cv::Point2f> &pts, const std::vector<long> &ids);
140 
141  vpKltOpencv & operator=(const vpKltOpencv& copy);
142  void track(const cv::Mat &I);
143  void setBlockSize(const int blockSize);
144  void setHarrisFreeParameter(double harris_k);
145  void setInitialGuess(const std::vector<cv::Point2f> &guess_pts);
146  void setMaxFeatures(const int maxCount);
147  void setMinDistance(double minDistance);
148  void setMinEigThreshold(double minEigThreshold);
149  void setPyramidLevels(const int pyrMaxLevel);
150  void setQuality(double qualityLevel);
152  void setTrackerId(int tid) {(void)tid;}
153  void setUseHarris(const int useHarrisDetector);
154  void setWindowSize(const int winSize);
155  void suppressFeature(const int &index);
156 
157 protected:
158  cv::Mat m_gray, m_prevGray;
159  std::vector<cv::Point2f> m_points[2];
160  std::vector<long> m_points_id;
162  cv::TermCriteria m_termcrit;
167  double m_harris_k;
173 
174 };
175 
176 #elif defined(VISP_HAVE_OPENCV)
177 #ifdef _CH_
178 #pragma package <opencv>
179 #endif
180 
181 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
182 # include <opencv2/imgproc/imgproc.hpp>
183 # include <opencv2/video/tracking.hpp>
184 # include <opencv2/legacy/legacy.hpp>
185 # include <opencv2/highgui/highgui.hpp>
186 #else
187 # ifndef _EiC
188 # include <cv.h>
189 # include <highgui.h>
190 # include <stdio.h>
191 # include <ctype.h>
192 # endif
193 #endif
194 
195 #include <visp/vpConfig.h>
196 #include <visp/vpImage.h>
197 #include <visp/vpImageIo.h>
198 #include <visp/vpDisplay.h>
199 #include <visp/vpDebug.h>
200 #include <visp/vpException.h>
201 #include <visp/vpTrackingException.h>
202 
203 typedef int (*funccheck)(int,double,double);
204 typedef void (*funcinfo)(int,int,int,double,double);
205 typedef void (*funcevent)(int);
206 
222 class VISP_EXPORT vpKltOpencv
223 {
224 private:
225  int initialized; //Is the tracker ready ?
226 
227  int maxFeatures; //Maximum number of features to track (Default 50)
228  int globalcountFeatures; //Keep over time for ID
229 
230 
231 
232  int win_size; //Size of search window for tracker (default 10)
233  double quality; //Multiplier for the maxmin eigenvalue; specifies minimal accepted quality of image corners (default 0.01)
234  double min_distance; //Limit, specifying minimum possible distance between returned corners; Euclidian distance is used. (default 10)
235  double harris_free_parameter; //Harris detector free parameter. (default 0.04)
236  int block_size; //Size of the averaging block used by the corner detector (default 3)
237  int use_harris; //0 use a simple Minimum EigenValue Detector, != 0 use Harris (default 1)
238  int pyramid_level; //Number of level for the tracker's gaussian pyramid data (default 3)
239  int _tid; //tracker id for multiple trackers
240 
241  IplImage *image; //Image buffer
242  IplImage *prev_image; //Image buffer for the previous iteration
243  IplImage *pyramid; //Gaussian pyramid data
244  IplImage *prev_pyramid; //Gaussian pyramid data for the previous iteration
245  IplImage *swap_temp; //Internal
246 
247  int countFeatures; //Currently tracked features
248  int countPrevFeatures; //Previously tracked features
249 
250  CvPoint2D32f *features; //List of features
251  CvPoint2D32f *prev_features; //List of features for previous iteration
252 
253  long *featuresid; //Array of ids for features
254  long *prev_featuresid; //Array of ids for previous features
255 
256  int flags; //Flags for tracking (internal)
257 
258  bool initial_guess; //Bool to precise if the next call to track() uses an initial guess
259 
260  bool *lostDuringTrack; // Result of the tracker for every feature : 1 = lost, 0 = still present
261  char *status; //Result of the tracker for every features : 0 = lost, 1 = found
262 
263 
264 
265  //EVENT FUNCTION POINTERS
266  funcevent OnInitialize;
267  funcinfo OnFeatureLost;
268  funcinfo OnNewFeature;
269  funcinfo OnMeasureFeature;
270  funccheck IsFeatureValid;
271 
272 private:
273 
274  //Internal
275  void clean();
276  void cleanAll();
277  void reset();
278 
279 public:
280  vpKltOpencv();
281  vpKltOpencv(const vpKltOpencv& copy);
282  virtual ~vpKltOpencv();
283 
284  void addFeature(const int &id, const float &x, const float &y);
285 
286  //Draw the tracked features on the given image
287  void display(const vpImage<unsigned char> &I,
288  vpColor color = vpColor::red, unsigned int thickness=1);
289 
291  int getBlockSize() const {return block_size;}
292  void getFeature(int index, int &id, float &x, float &y) const;
294  CvPoint2D32f* getFeatures() const {return features;}
296  long* getFeaturesId() const {return featuresid;}
298  double getHarrisFreeParameter() const {return harris_free_parameter;}
300  bool *getListOfLostFeature() const { return lostDuringTrack; }
302  int getMaxFeatures() const {return maxFeatures;}
304  double getMinDistance() const {return min_distance;}
306  int getNbFeatures() const { return countFeatures; }
308  int getNbPrevFeatures() const { return countPrevFeatures; }
309  void getPrevFeature(int index, int &id, float &x, float &y) const;
311  CvPoint2D32f* getPrevFeatures() const {return prev_features;}
313  long* getPrevFeaturesId() const {return prev_featuresid;}
315  int getPyramidLevels() const {return pyramid_level;}
317  double getQuality() const {return quality;}
319  int getWindowSize() const {return win_size;}
320 
321  //Detect corners in the image. Initialize the tracker
322  void initTracking(const IplImage *I, const IplImage *mask = NULL);
323  void initTracking(const IplImage *I, CvPoint2D32f *pts, int size);
324  void initTracking(const IplImage *I, CvPoint2D32f *pts, long *fid, int size);
325  vpKltOpencv & operator=(const vpKltOpencv& copy);
326  //Track !
327  void track(const IplImage *I);
328 
329 
330  //Seters
339  void setBlockSize(const int input) {initialized = 0; block_size=input;}
347  void setHarrisFreeParameter(double input) {initialized = 0; harris_free_parameter=input;}
348  void setInitialGuess(CvPoint2D32f **guess_pts);
352  void setIsFeatureValid(funccheck input) {IsFeatureValid = input;}
353 
354  /* Should be used only before initTracking */
355  void setMaxFeatures(const int input);
363  void setMinDistance(double input) {initialized = 0; min_distance=input;}
364 
365  //Functors
366 
367  //Event when tracker is initialized -> event(id_tracker)
368  void setOnInitialize(funcevent input) {OnInitialize = input;}
369  //Event when a feature is lost -> event(id_tracker, index, uid, x, y)
370  void setOnFeatureLost(funcinfo input) {OnFeatureLost = input;}
371  //Event when a new feature is found -> event(id_tracker, index, uid, x, y)
372  void setOnNewFeature(funcinfo input) {OnNewFeature = input;}
373  //Event when a feature is found while tracking -> event(id_tracker, index, uid, x, y)
374  void setOnMeasureFeature(funcinfo input) {OnMeasureFeature = input;}
383  void setPyramidLevels(const int input) {initialized = 0; pyramid_level=input;}
384  void setQuality(double input) {initialized = 0; quality=input;}
385  void setTrackerId(int tid) {_tid = tid;}
393  void setUseHarris(const int input) {initialized = 0; use_harris=input;}
394  void setWindowSize(const int input) {initialized = 0; win_size=input;}
395 
396  void suppressFeature(int index);
397 
398  //Static Functions
399 public:
400  static void display(const vpImage<unsigned char>& I, const CvPoint2D32f* features_list,
401  const int &nbFeatures, vpColor color = vpColor::green,
402  unsigned int thickness=1);
403  static void display(const vpImage<vpRGBa>& I, const CvPoint2D32f* features_list,
404  const int &nbFeatures, vpColor color = vpColor::green,
405  unsigned int thickness=1);
406 
407  static void display(const vpImage<unsigned char>& I, const CvPoint2D32f* features_list,
408  const long *featuresid_list, const int &nbFeatures,
409  vpColor color = vpColor::green, unsigned int thickness=1);
410  static void display(const vpImage<vpRGBa>& I, const CvPoint2D32f* features_list,
411  const long *featuresid_list, const int &nbFeatures,
412  vpColor color = vpColor::green, unsigned int thickness=1);
413 
414 };
415 
416 #endif
417 #endif
int getPyramidLevels() const
Get the list of features id.
Definition: vpKltOpencv.h:131
double m_harris_k
Definition: vpKltOpencv.h:167
int getMaxFeatures() const
Get the list of lost feature.
Definition: vpKltOpencv.h:117
std::vector< cv::Point2f > getFeatures() const
Get the list of current features.
Definition: vpKltOpencv.h:107
double getHarrisFreeParameter() const
Get the free parameter of the Harris detector.
Definition: vpKltOpencv.h:113
cv::Mat m_prevGray
Definition: vpKltOpencv.h:158
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
cv::TermCriteria m_termcrit
Definition: vpKltOpencv.h:162
static const vpColor green
Definition: vpColor.h:170
static const vpColor red
Definition: vpColor.h:167
bool m_initial_guess
Definition: vpKltOpencv.h:172
std::vector< long > m_points_id
Keypoint id.
Definition: vpKltOpencv.h:160
double getQuality() const
Get the parameter characterizing the minimal accepted quality of image corners.
Definition: vpKltOpencv.h:133
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:152
int getBlockSize() const
Get the size of the averaging block used to track the features.
Definition: vpKltOpencv.h:104
double m_minDistance
Definition: vpKltOpencv.h:165
double m_minEigThreshold
Definition: vpKltOpencv.h:166
std::vector< cv::Point2f > getPrevFeatures() const
Get the list of previous features.
Definition: vpKltOpencv.h:126
double m_qualityLevel
Definition: vpKltOpencv.h:164
int m_useHarrisDetector
Definition: vpKltOpencv.h:169
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV.
Definition: vpKltOpencv.h:79
int getNbFeatures() const
Get the number of current features.
Definition: vpKltOpencv.h:121
long m_next_points_id
Definition: vpKltOpencv.h:171
double getMinDistance() const
Get the minimal Euclidean distance between detected corners during initialization.
Definition: vpKltOpencv.h:119
int getNbPrevFeatures() const
Get the number of previous features.
Definition: vpKltOpencv.h:123
int m_pyrMaxLevel
Definition: vpKltOpencv.h:170
int getWindowSize() const
Get the window size used to refine the corner locations.
Definition: vpKltOpencv.h:135
std::vector< long > getFeaturesId() const
Get the unique id of each feature.
Definition: vpKltOpencv.h:110