ViSP  2.7.0
vpKltOpencv.h
1 /****************************************************************************
2  *
3  * $Id: vpKltOpencv.h 4101 2013-02-05 16:26:17Z ayol $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
141 class VISP_EXPORT vpKltOpencv
142 {
143  private:
144  int initialized; //Is the tracker ready ?
145 
146  int maxFeatures; //Maximum number of features to track (Default 50)
147  int globalcountFeatures; //Keep over time for ID
148 
149 
150 
151  int win_size; //Size of search window for tracker (default 10)
152  double quality; //Multiplier for the maxmin eigenvalue; specifies minimal accepted quality of image corners (default 0.01)
153  double min_distance; //Limit, specifying minimum possible distance between returned corners; Euclidian distance is used. (default 10)
154  double harris_free_parameter; //Harris detector free parameter. (default 0.04)
155  int block_size; //Size of the averaging block used by the corner detector (default 3)
156  int use_harris; //0 use a simple Minimum EigenValue Detector, != 0 use Harris (default 1)
157  int pyramid_level; //Number of level for the tracker's gaussian pyramid data (default 3)
158  int _tid; //tracker id for multiple trackers
159 
160  IplImage *image; //Image buffer
161  IplImage *prev_image; //Image buffer for the previous iteration
162  IplImage *pyramid; //Gaussian pyramid data
163  IplImage *prev_pyramid; //Gaussian pyramid data for the previous iteration
164  IplImage *swap_temp; //Internal
165 
166  int countFeatures; //Currently tracked features
167  int countPrevFeatures; //Previously tracked features
168 
169  CvPoint2D32f *features; //List of features
170  CvPoint2D32f *prev_features; //List of features for previous iteration
171 
172  long *featuresid; //Array of ids for features
173  long *prev_featuresid; //Array of ids for previous features
174 
175  int flags; //Flags for tracking (internal)
176 
177  bool initial_guess; //Bool to precise if the next call to track() uses an initial guess
178 
179  bool *lostDuringTrack; // Result of the tracker for every feature : 1 = lost, 0 = still present
180  char *status; //Result of the tracker for every features : 0 = lost, 1 = found
181 
182 
183 
184  //EVENT FUNCTION POINTERS
185  funcevent OnInitialize;
186  funcinfo OnFeatureLost;
187  funcinfo OnNewFeature;
188  funcinfo OnMeasureFeature;
189  funccheck IsFeatureValid;
190 
191  private:
192 
193  //Internal
194  void clean();
195  void cleanAll();
196  void reset();
197 
198  public:
199  vpKltOpencv();
200  vpKltOpencv(const vpKltOpencv& copy);
201  virtual ~vpKltOpencv();
202 
203  //Detect corners in the image. Initialize the tracker
204  void initTracking(const IplImage *I, const IplImage *mask = NULL);
205 
206  //Track !
207  void track(const IplImage *I);
208 
209  //Draw the tracked features on the given image
210  void display(const vpImage<unsigned char> &I,
211  vpColor color = vpColor::red);
212 
213  //Seters
214  void setInitialGuess(CvPoint2D32f **guess_pts);
215 
216  /* Should be used only before initTracking */
217  void setMaxFeatures(const int input);
218 
226  void setWindowSize(const int input) {initialized = 0; win_size=input;}
227  void setQuality(double input) {initialized = 0; quality=input;}
228 
236  void setMinDistance(double input) {initialized = 0; min_distance=input;}
237 
245  void setHarrisFreeParameter(double input) {initialized = 0; harris_free_parameter=input;}
246 
255  void setBlockSize(const int input) {initialized = 0; block_size=input;}
256  void setUseHarris(const int input) {initialized = 0; use_harris=input;}
257 
266  void setPyramidLevels(const int input) {initialized = 0; pyramid_level=input;}
267  void setTrackerId(int tid) {_tid = tid;}
268 
269  //Functors
270 
271  //Event when tracker is initialized -> event(id_tracker)
272  void setOnInitialize(funcevent input) {OnInitialize = input;}
273  //Event when a feature is lost -> event(id_tracker, index, uid, x, y)
274  void setOnFeatureLost(funcinfo input) {OnFeatureLost = input;}
275  //Event when a new feature is found -> event(id_tracker, index, uid, x, y)
276  void setOnNewFeature(funcinfo input) {OnNewFeature = input;}
277  //Event when a feature is found while tracking -> event(id_tracker, index, uid, x, y)
278  void setOnMeasureFeature(funcinfo input) {OnMeasureFeature = input;}
279  //Is a feature valid (e.g. : test if not too close to borders) -> event(id_tracker, x, y)
280  void setIsFeatureValid(funccheck input) {IsFeatureValid = input;}
281 
283  int getBlockSize() const {return block_size;}
285  CvPoint2D32f* getFeatures() const {return features;}
287  long* getFeaturesId() const {return featuresid;}
289  double getHarrisFreeParameter() const {return harris_free_parameter;}
291  bool *getListOfLostFeature() const { return lostDuringTrack; }
293  int getMaxFeatures() const {return maxFeatures;}
295  double getMinDistance() const {return min_distance;}
297  int getNbFeatures() const { return countFeatures; }
299  int getNbPrevFeatures() const { return countPrevFeatures; }
301  CvPoint2D32f* getPrevFeatures() const {return prev_features;}
303  long* getPrevFeaturesId() const {return prev_featuresid;}
305  int getPyramidLevels() const {return pyramid_level;}
307  double getQuality() const {return quality;}
309  int getWindowSize() const {return win_size;}
310 
311  void getFeature(int index, int &id, float &x, float &y) const;
312  void getPrevFeature(int index, int &id, float &x, float &y) const;
313  void addFeature(const int &id, const float &x, const float &y);
314  void suppressFeature(int index);
315 
316 //Static Functions
317 public:
318  static void display(const vpImage<unsigned char>& I, const CvPoint2D32f* features_list,
319  const int &nbFeatures, vpColor color = vpColor::green,
320  unsigned int thickness=1);
321  static void display(const vpImage<vpRGBa>& I, const CvPoint2D32f* features_list,
322  const int &nbFeatures, vpColor color = vpColor::green,
323  unsigned int thickness=1);
324 
325  static void display(const vpImage<unsigned char>& I, const CvPoint2D32f* features_list,
326  const long *featuresid_list, const int &nbFeatures,
327  vpColor color = vpColor::green, unsigned int thickness=1);
328  static void display(const vpImage<vpRGBa>& I, const CvPoint2D32f* features_list,
329  const long *featuresid_list, const int &nbFeatures,
330  vpColor color = vpColor::green, unsigned int thickness=1);
331 
332 };
333 
334 #endif
335 #endif
void setOnInitialize(funcevent input)
Definition: vpKltOpencv.h:272
int getPyramidLevels() const
Get the number of pyramid levels.
Definition: vpKltOpencv.h:305
void setQuality(double input)
Definition: vpKltOpencv.h:227
void setOnFeatureLost(funcinfo input)
Definition: vpKltOpencv.h:274
int getMaxFeatures() const
Get Max number of features.
Definition: vpKltOpencv.h:293
CvPoint2D32f * getFeatures() const
Get the list of features.
Definition: vpKltOpencv.h:285
void setOnNewFeature(funcinfo input)
Definition: vpKltOpencv.h:276
double getHarrisFreeParameter() const
Get Harris free parameter.
Definition: vpKltOpencv.h:289
Class to define colors available for display functionnalities.
Definition: vpColor.h:123
void setUseHarris(const int input)
Definition: vpKltOpencv.h:256
void setOnMeasureFeature(funcinfo input)
Definition: vpKltOpencv.h:278
void setPyramidLevels(const int input)
Definition: vpKltOpencv.h:266
void setBlockSize(const int input)
Definition: vpKltOpencv.h:255
CvPoint2D32f * getPrevFeatures() const
Get the list of features.
Definition: vpKltOpencv.h:301
static const vpColor green
Definition: vpColor.h:168
static const vpColor red
Definition: vpColor.h:165
long * getFeaturesId() const
Get the list of features id.
Definition: vpKltOpencv.h:287
void setWindowSize(const int input)
Definition: vpKltOpencv.h:226
double getQuality() const
Get the quality of the tracker.
Definition: vpKltOpencv.h:307
void setTrackerId(int tid)
Definition: vpKltOpencv.h:267
int getBlockSize() const
Get the block size.
Definition: vpKltOpencv.h:283
void setIsFeatureValid(funccheck input)
Definition: vpKltOpencv.h:280
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented with OpenCV.
Definition: vpKltOpencv.h:141
int getNbFeatures() const
Get the current number of features.
Definition: vpKltOpencv.h:297
long * getPrevFeaturesId() const
Get the list of features id.
Definition: vpKltOpencv.h:303
double getMinDistance() const
Get Min Distance.
Definition: vpKltOpencv.h:295
int getNbPrevFeatures() const
Get the previous number of features.
Definition: vpKltOpencv.h:299
void setMinDistance(double input)
Definition: vpKltOpencv.h:236
bool * getListOfLostFeature() const
Get the list of lost feature.
Definition: vpKltOpencv.h:291
int getWindowSize() const
Get Max number of features.
Definition: vpKltOpencv.h:309
void setHarrisFreeParameter(double input)
Definition: vpKltOpencv.h:245