ViSP  2.6.2
vpKltOpencv.h
1 /****************************************************************************
2  *
3  * $Id: vpKltOpencv.h 3797 2012-06-21 07:44:05Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 countFeatures; //Currently tracked features
148  int countPrevFeatures; //Previously tracked features
149  int globalcountFeatures; //Keep over time for ID
150 
151  int flags; //Flags for tracking (internal)
152 
153  int win_size; //Size of search window for tracker (default 10)
154  double quality; //Multiplier for the maxmin eigenvalue; specifies minimal accepted quality of image corners (default 0.01)
155  double min_distance; //Limit, specifying minimum possible distance between returned corners; Euclidian distance is used. (default 10)
156  double harris_free_parameter; //Harris detector free parameter. (default 0.04)
157  int block_size; //Size of the averaging block used by the corner detector (default 3)
158  int use_harris; //0 use a simple Minimum EigenValue Detector, != 0 use Harris (default 1)
159  int pyramid_level; //Number of level for the tracker's gaussian pyramid data (default 3)
160  int _tid; //tracker id for multiple trackers
161 
162  IplImage *image; //Image buffer
163  IplImage *prev_image; //Image buffer for the previous iteration
164  IplImage *pyramid; //Gaussian pyramid data
165  IplImage *prev_pyramid; //Gaussian pyramid data for the previous iteration
166  IplImage *swap_temp; //Internal
167 
168  CvPoint2D32f *features; //List of features
169  CvPoint2D32f *prev_features; //List of features for previous iteration
170 
171  long *featuresid; //Array of ids for features
172  long *prev_featuresid; //Array of ids for previous features
173  bool *lostDuringTrack; // Result of the tracker for every feature : 1 = lost, 0 = still present
174  char *status; //Result of the tracker for every features : 0 = lost, 1 = found
175 
176 
177 
178  //EVENT FUNCTION POINTERS
179  funcevent OnInitialize;
180  funcinfo OnFeatureLost;
181  funcinfo OnNewFeature;
182  funcinfo OnMeasureFeature;
183  funccheck IsFeatureValid;
184 
185  private:
186 
187  //Internal
188  void clean();
189  void cleanAll();
190  void reset();
191 
192  public:
193  vpKltOpencv();
194  vpKltOpencv(const vpKltOpencv& copy);
195  virtual ~vpKltOpencv();
196 
197  //Detect corners in the image. Initialize the tracker
198  void initTracking(const IplImage *I, const IplImage *mask = NULL);
199 
200  //Track !
201  void track(const IplImage *I);
202 
203  //Draw the tracked features on the given image
204  void display(const vpImage<unsigned char> &I,
205  vpColor color = vpColor::red);
206 
207  //Seters
208 
209  /* Should be used only before initTracking */
210  void setMaxFeatures(const int input);
211 
219  void setWindowSize(const int input) {initialized = 0; win_size=input;}
220  void setQuality(double input) {initialized = 0; quality=input;}
221 
229  void setMinDistance(double input) {initialized = 0; min_distance=input;}
230 
238  void setHarrisFreeParameter(double input) {initialized = 0; harris_free_parameter=input;}
239 
248  void setBlockSize(const int input) {initialized = 0; block_size=input;}
249  void setUseHarris(const int input) {initialized = 0; use_harris=input;}
250 
259  void setPyramidLevels(const int input) {initialized = 0; pyramid_level=input;}
260  void setTrackerId(int tid) {_tid = tid;}
261 
262  //Functors
263 
264  //Event when tracker is initialized -> event(id_tracker)
265  void setOnInitialize(funcevent input) {OnInitialize = input;}
266  //Event when a feature is lost -> event(id_tracker, index, uid, x, y)
267  void setOnFeatureLost(funcinfo input) {OnFeatureLost = input;}
268  //Event when a new feature is found -> event(id_tracker, index, uid, x, y)
269  void setOnNewFeature(funcinfo input) {OnNewFeature = input;}
270  //Event when a feature is found while tracking -> event(id_tracker, index, uid, x, y)
271  void setOnMeasureFeature(funcinfo input) {OnMeasureFeature = input;}
272  //Is a feature valid (e.g. : test if not too close to borders) -> event(id_tracker, x, y)
273  void setIsFeatureValid(funccheck input) {IsFeatureValid = input;}
274 
276  bool *getListOfLostFeature() const { return lostDuringTrack; }
278  CvPoint2D32f* getFeatures() const {return features;}
280  long* getFeaturesId() const {return featuresid;}
282  CvPoint2D32f* getPrevFeatures() const {return prev_features;}
284  long* getPrevFeaturesId() const {return prev_featuresid;}
286  int getNbFeatures() const { return countFeatures; }
288  int getNbPrevFeatures() const { return countPrevFeatures; }
290  int getMaxFeatures() const {return maxFeatures;}
291 
292  void getFeature(int index, int &id, float &x, float &y) const;
293  void getPrevFeature(int index, int &id, float &x, float &y) const;
294  void addFeature(const int &id, const float &x, const float &y);
295  void suppressFeature(int index);
296 
297 //Static Functions
298 public:
299  static void display(const vpImage<unsigned char>& I,const CvPoint2D32f* features_list,
300  const int &nbFeatures, vpColor color = vpColor::green,
301  unsigned int thickness=1);
302 
303  static void display(const vpImage<unsigned char>& I,const CvPoint2D32f* features_list,
304  const long *featuresid_list, const int &nbFeatures,
305  vpColor color = vpColor::green, unsigned int thickness=1);
306 
307 };
308 
309 #endif
310 #endif
void setOnInitialize(funcevent input)
Definition: vpKltOpencv.h:265
void setQuality(double input)
Definition: vpKltOpencv.h:220
void setOnFeatureLost(funcinfo input)
Definition: vpKltOpencv.h:267
int getMaxFeatures() const
Get Max number of features.
Definition: vpKltOpencv.h:290
CvPoint2D32f * getFeatures() const
Get the list of features.
Definition: vpKltOpencv.h:278
void setOnNewFeature(funcinfo input)
Definition: vpKltOpencv.h:269
Class to define colors available for display functionnalities.
Definition: vpColor.h:123
void setUseHarris(const int input)
Definition: vpKltOpencv.h:249
void setOnMeasureFeature(funcinfo input)
Definition: vpKltOpencv.h:271
void setPyramidLevels(const int input)
Definition: vpKltOpencv.h:259
void setBlockSize(const int input)
Definition: vpKltOpencv.h:248
CvPoint2D32f * getPrevFeatures() const
Get the list of features.
Definition: vpKltOpencv.h:282
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:280
void setWindowSize(const int input)
Definition: vpKltOpencv.h:219
void setTrackerId(int tid)
Definition: vpKltOpencv.h:260
void setIsFeatureValid(funccheck input)
Definition: vpKltOpencv.h:273
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:286
long * getPrevFeaturesId() const
Get the list of features id.
Definition: vpKltOpencv.h:284
int getNbPrevFeatures() const
Get the previous number of features.
Definition: vpKltOpencv.h:288
void setMinDistance(double input)
Definition: vpKltOpencv.h:229
bool * getListOfLostFeature() const
Get the list of lost feature.
Definition: vpKltOpencv.h:276
void setHarrisFreeParameter(double input)
Definition: vpKltOpencv.h:238