ViSP  2.9.0
vpKltOpencv.h
1 /****************************************************************************
2  *
3  * $Id: vpKltOpencv.h 4632 2014-02-03 17:06:40Z 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 
56 #ifdef VISP_HAVE_OPENCV
57 
58 #ifdef _CH_
59 #pragma package <opencv>
60 #endif
61 
62 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
63 # include <opencv2/imgproc/imgproc.hpp>
64 # include <opencv2/video/tracking.hpp>
65 # include <opencv2/legacy/legacy.hpp>
66 # include <opencv2/highgui/highgui.hpp>
67 #else
68 # ifndef _EiC
69 # include <cv.h>
70 # include <highgui.h>
71 # include <stdio.h>
72 # include <ctype.h>
73 # endif
74 #endif
75 
76 #include <visp/vpConfig.h>
77 #include <visp/vpImage.h>
78 #include <visp/vpImageIo.h>
79 #include <visp/vpDisplay.h>
80 #include <visp/vpDebug.h>
81 #include <visp/vpException.h>
82 #include <visp/vpTrackingException.h>
83 
84 typedef int (*funccheck)(int,double,double);
85 typedef void (*funcinfo)(int,int,int,double,double);
86 typedef void (*funcevent)(int);
87 
103 class VISP_EXPORT vpKltOpencv
104 {
105  private:
106  int initialized; //Is the tracker ready ?
107 
108  int maxFeatures; //Maximum number of features to track (Default 50)
109  int globalcountFeatures; //Keep over time for ID
110 
111 
112 
113  int win_size; //Size of search window for tracker (default 10)
114  double quality; //Multiplier for the maxmin eigenvalue; specifies minimal accepted quality of image corners (default 0.01)
115  double min_distance; //Limit, specifying minimum possible distance between returned corners; Euclidian distance is used. (default 10)
116  double harris_free_parameter; //Harris detector free parameter. (default 0.04)
117  int block_size; //Size of the averaging block used by the corner detector (default 3)
118  int use_harris; //0 use a simple Minimum EigenValue Detector, != 0 use Harris (default 1)
119  int pyramid_level; //Number of level for the tracker's gaussian pyramid data (default 3)
120  int _tid; //tracker id for multiple trackers
121 
122  IplImage *image; //Image buffer
123  IplImage *prev_image; //Image buffer for the previous iteration
124  IplImage *pyramid; //Gaussian pyramid data
125  IplImage *prev_pyramid; //Gaussian pyramid data for the previous iteration
126  IplImage *swap_temp; //Internal
127 
128  int countFeatures; //Currently tracked features
129  int countPrevFeatures; //Previously tracked features
130 
131  CvPoint2D32f *features; //List of features
132  CvPoint2D32f *prev_features; //List of features for previous iteration
133 
134  long *featuresid; //Array of ids for features
135  long *prev_featuresid; //Array of ids for previous features
136 
137  int flags; //Flags for tracking (internal)
138 
139  bool initial_guess; //Bool to precise if the next call to track() uses an initial guess
140 
141  bool *lostDuringTrack; // Result of the tracker for every feature : 1 = lost, 0 = still present
142  char *status; //Result of the tracker for every features : 0 = lost, 1 = found
143 
144 
145 
146  //EVENT FUNCTION POINTERS
147  funcevent OnInitialize;
148  funcinfo OnFeatureLost;
149  funcinfo OnNewFeature;
150  funcinfo OnMeasureFeature;
151  funccheck IsFeatureValid;
152 
153  private:
154 
155  //Internal
156  void clean();
157  void cleanAll();
158  void reset();
159 
160  public:
161  vpKltOpencv();
162  vpKltOpencv(const vpKltOpencv& copy);
163  virtual ~vpKltOpencv();
164 
165  void addFeature(const int &id, const float &x, const float &y);
166 
167  //Draw the tracked features on the given image
168  void display(const vpImage<unsigned char> &I,
169  vpColor color = vpColor::red, unsigned int thickness=1);
170 
172  int getBlockSize() const {return block_size;}
173  void getFeature(int index, int &id, float &x, float &y) const;
175  CvPoint2D32f* getFeatures() const {return features;}
177  long* getFeaturesId() const {return featuresid;}
179  double getHarrisFreeParameter() const {return harris_free_parameter;}
181  bool *getListOfLostFeature() const { return lostDuringTrack; }
183  int getMaxFeatures() const {return maxFeatures;}
185  double getMinDistance() const {return min_distance;}
187  int getNbFeatures() const { return countFeatures; }
189  int getNbPrevFeatures() const { return countPrevFeatures; }
190  void getPrevFeature(int index, int &id, float &x, float &y) const;
192  CvPoint2D32f* getPrevFeatures() const {return prev_features;}
194  long* getPrevFeaturesId() const {return prev_featuresid;}
196  int getPyramidLevels() const {return pyramid_level;}
198  double getQuality() const {return quality;}
200  int getWindowSize() const {return win_size;}
201 
202  //Detect corners in the image. Initialize the tracker
203  void initTracking(const IplImage *I, const IplImage *mask = NULL);
204 
205  vpKltOpencv & operator=(const vpKltOpencv& copy);
206  //Track !
207  void track(const IplImage *I);
208 
209 
210  //Seters
219  void setBlockSize(const int input) {initialized = 0; block_size=input;}
227  void setHarrisFreeParameter(double input) {initialized = 0; harris_free_parameter=input;}
228  void setInitialGuess(CvPoint2D32f **guess_pts);
232  void setIsFeatureValid(funccheck input) {IsFeatureValid = input;}
233 
234  /* Should be used only before initTracking */
235  void setMaxFeatures(const int input);
243  void setMinDistance(double input) {initialized = 0; min_distance=input;}
244 
245  //Functors
246 
247  //Event when tracker is initialized -> event(id_tracker)
248  void setOnInitialize(funcevent input) {OnInitialize = input;}
249  //Event when a feature is lost -> event(id_tracker, index, uid, x, y)
250  void setOnFeatureLost(funcinfo input) {OnFeatureLost = input;}
251  //Event when a new feature is found -> event(id_tracker, index, uid, x, y)
252  void setOnNewFeature(funcinfo input) {OnNewFeature = input;}
253  //Event when a feature is found while tracking -> event(id_tracker, index, uid, x, y)
254  void setOnMeasureFeature(funcinfo input) {OnMeasureFeature = input;}
263  void setPyramidLevels(const int input) {initialized = 0; pyramid_level=input;}
264  void setQuality(double input) {initialized = 0; quality=input;}
265  void setTrackerId(int tid) {_tid = tid;}
273  void setUseHarris(const int input) {initialized = 0; use_harris=input;}
274  void setWindowSize(const int input) {initialized = 0; win_size=input;}
275 
276  void suppressFeature(int index);
277 
278 //Static Functions
279 public:
280  static void display(const vpImage<unsigned char>& I, const CvPoint2D32f* features_list,
281  const int &nbFeatures, vpColor color = vpColor::green,
282  unsigned int thickness=1);
283  static void display(const vpImage<vpRGBa>& I, const CvPoint2D32f* features_list,
284  const int &nbFeatures, vpColor color = vpColor::green,
285  unsigned int thickness=1);
286 
287  static void display(const vpImage<unsigned char>& I, const CvPoint2D32f* features_list,
288  const long *featuresid_list, const int &nbFeatures,
289  vpColor color = vpColor::green, unsigned int thickness=1);
290  static void display(const vpImage<vpRGBa>& I, const CvPoint2D32f* features_list,
291  const long *featuresid_list, const int &nbFeatures,
292  vpColor color = vpColor::green, unsigned int thickness=1);
293 
294 };
295 
296 #endif
297 #endif
void setOnInitialize(funcevent input)
Definition: vpKltOpencv.h:248
int getPyramidLevels() const
Get the number of pyramid levels.
Definition: vpKltOpencv.h:196
void setQuality(double input)
Definition: vpKltOpencv.h:264
void setOnFeatureLost(funcinfo input)
Definition: vpKltOpencv.h:250
int getMaxFeatures() const
Get Max number of features.
Definition: vpKltOpencv.h:183
CvPoint2D32f * getFeatures() const
Get the list of features.
Definition: vpKltOpencv.h:175
void setOnNewFeature(funcinfo input)
Definition: vpKltOpencv.h:252
double getHarrisFreeParameter() const
Get Harris free parameter.
Definition: vpKltOpencv.h:179
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
void setUseHarris(const int input)
Definition: vpKltOpencv.h:273
void setOnMeasureFeature(funcinfo input)
Definition: vpKltOpencv.h:254
void setPyramidLevels(const int input)
Definition: vpKltOpencv.h:263
void setBlockSize(const int input)
Definition: vpKltOpencv.h:219
CvPoint2D32f * getPrevFeatures() const
Get the list of features.
Definition: vpKltOpencv.h:192
static const vpColor green
Definition: vpColor.h:170
static const vpColor red
Definition: vpColor.h:167
long * getFeaturesId() const
Get the list of features id.
Definition: vpKltOpencv.h:177
void setWindowSize(const int input)
Definition: vpKltOpencv.h:274
double getQuality() const
Get the quality of the tracker.
Definition: vpKltOpencv.h:198
void setTrackerId(int tid)
Definition: vpKltOpencv.h:265
int getBlockSize() const
Get the block size.
Definition: vpKltOpencv.h:172
void setIsFeatureValid(funccheck input)
Definition: vpKltOpencv.h:232
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV.
Definition: vpKltOpencv.h:103
int getNbFeatures() const
Get the current number of features.
Definition: vpKltOpencv.h:187
long * getPrevFeaturesId() const
Get the list of features id.
Definition: vpKltOpencv.h:194
double getMinDistance() const
Get Min Distance.
Definition: vpKltOpencv.h:185
int getNbPrevFeatures() const
Get the previous number of features.
Definition: vpKltOpencv.h:189
void setMinDistance(double input)
Definition: vpKltOpencv.h:243
bool * getListOfLostFeature() const
Get the list of lost feature.
Definition: vpKltOpencv.h:181
int getWindowSize() const
Get Max number of features.
Definition: vpKltOpencv.h:200
void setHarrisFreeParameter(double input)
Definition: vpKltOpencv.h:227