Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpMbKltTracker.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  * Model based tracker using only KLT
32  *
33  * Authors:
34  * Romain Tallonneau
35  * Aurelien Yol
36  *
37  *****************************************************************************/
43 #ifndef vpMbKltTracker_h
44 #define vpMbKltTracker_h
45 
46 #include <visp3/core/vpConfig.h>
47 
48 #if defined(VISP_HAVE_MODULE_KLT) && (defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100))
49 
50 #include <visp3/mbt/vpMbTracker.h>
51 #include <visp3/klt/vpKltOpencv.h>
52 #include <visp3/core/vpMeterPixelConversion.h>
53 #include <visp3/core/vpPixelMeterConversion.h>
54 #include <visp3/mbt/vpMbtKltXmlParser.h>
55 #include <visp3/vision/vpHomography.h>
56 #include <visp3/core/vpRobust.h>
57 #include <visp3/core/vpSubColVector.h>
58 #include <visp3/core/vpSubMatrix.h>
59 #include <visp3/core/vpExponentialMap.h>
60 #include <visp3/mbt/vpMbtDistanceKltPoints.h>
61 #include <visp3/mbt/vpMbtDistanceCircle.h>
62 #include <visp3/mbt/vpMbtDistanceKltCylinder.h>
63 
226 class VISP_EXPORT vpMbKltTracker: virtual public vpMbTracker
227 {
228  friend class vpMbKltMultiTracker;
230 
231 protected:
233 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
234  cv::Mat cur;
235 #else
236  IplImage *cur;
237 #endif
245  unsigned int maskBorder;
247  double lambda;
249  unsigned int maxIter;
253  double percentGood;
259  std::list<vpMbtDistanceKltPoints*> kltPolygons;
261  std::list<vpMbtDistanceKltCylinder*> kltCylinders;
263  std::list<vpMbtDistanceCircle*> circles_disp;
264 
265 public:
266  vpMbKltTracker();
267  virtual ~vpMbKltTracker();
268 
271 
272  void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, const double r, const std::string &name="");
273  virtual void display(const vpImage<unsigned char>& I, const vpHomogeneousMatrix &cMo,
274  const vpCameraParameters &cam, const vpColor& col, const unsigned int thickness=1,
275  const bool displayFullModel = false);
276  virtual void display(const vpImage<vpRGBa>& I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
277  const vpColor& col, const unsigned int thickness=1, const bool displayFullModel = false);
278 
280  virtual std::list<vpMbtDistanceCircle*> &getFeaturesCircle() { return circles_disp; }
282  virtual std::list<vpMbtDistanceKltCylinder*> &getFeaturesKltCylinder() { return kltCylinders; }
284  virtual std::list<vpMbtDistanceKltPoints*> &getFeaturesKlt() { return kltPolygons; }
285 
291 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
292  inline std::vector<cv::Point2f> getKltPoints() const {return tracker.getFeatures();}
293 #else
294  inline CvPoint2D32f* getKltPoints() {return tracker.getFeatures();}
295 #endif
296 
297  std::vector<vpImagePoint> getKltImagePoints() const;
298 
299  std::map<int, vpImagePoint> getKltImagePointsWithId() const;
300 
306  inline vpKltOpencv getKltOpencv() const { return tracker; }
307 
313  virtual inline double getLambda() const {return lambda;}
314 
320  inline unsigned int getMaskBorder() const { return maskBorder; }
321 
327  virtual inline unsigned int getMaxIter() const {return maxIter;}
328 
334  inline int getNbKltPoints() const {return tracker.getNbFeatures();}
335 
341  inline double getThresholdAcceptation() const { return threshold_outlier;}
342 
343  virtual void loadConfigFile(const std::string& configFile);
344  void loadConfigFile(const char* configFile);
345 
346  virtual void reInitModel(const vpImage<unsigned char>& I, const std::string &cad_name, const vpHomogeneousMatrix& cMo_,
347  const bool verbose=false);
348  void reInitModel(const vpImage<unsigned char>& I, const char* cad_name, const vpHomogeneousMatrix& cMo,
349  const bool verbose=false);
350  void resetTracker();
351 
353 
354  virtual void setKltOpencv(const vpKltOpencv& t);
355 
361  virtual inline void setLambda(const double gain) {this->lambda = gain;}
362 
368  inline void setMaskBorder(const unsigned int &e)
369  {
370  maskBorder = e;
371  //if(useScanLine)
372  faces.getMbScanLineRenderer().setMaskBorder(maskBorder);
373  }
374 
380  virtual inline void setMaxIter(const unsigned int max) {maxIter = max;}
381 
389  virtual void setOgreVisibilityTest(const bool &v){
391 #ifdef VISP_HAVE_OGRE
392  faces.getOgreContext()->setWindowName("MBT Klt");
393 #endif
394  }
395 
401  virtual void setScanLineVisibilityTest(const bool &v){
403 
404  for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=kltPolygons.begin(); it!=kltPolygons.end(); ++it)
405  (*it)->useScanLine = v;
406  }
407 
408  virtual void setPose(const vpImage<unsigned char> &I, const vpHomogeneousMatrix& cdMo);
409 
415  virtual void setProjectionErrorComputation(const bool &flag) {
416  if(flag)
417  vpCTRACE << "This option is not yet implemented in vpMbKltTracker, projection error computation set to false." << std::endl ; }
418 
424  inline void setThresholdAcceptation(const double th) {threshold_outlier = th;}
425 
426  void setUseKltTracking(const std::string &name, const bool &useKltTracking);
427 
428  virtual void testTracking();
429  virtual void track(const vpImage<unsigned char>& I);
430 
431 protected:
434  void computeVVS(const unsigned int &nbInfos, vpColVector &w);
435  void computeVVSCheckLevenbergMarquardtKlt(const unsigned int iter, const unsigned int nbInfos,
436  const vpHomogeneousMatrix &cMoPrev, const vpColVector &error_prev, const vpHomogeneousMatrix &ctTc0_Prev,
437  double &mu, bool &reStartFromLastIncrement);
438  void computeVVSCovariance(const vpColVector &w_true, const vpHomogeneousMatrix &cMoPrev,
439  const vpMatrix &L_true, const vpMatrix &LVJ_true);
440  void computeVVSInteractionMatrixAndResidu(unsigned int shift, vpColVector &R, vpMatrix &L, vpHomography &H,
441  std::list<vpMbtDistanceKltPoints*> &kltPolygons_, std::list<vpMbtDistanceKltCylinder*> &kltCylinders_,
442  const vpHomogeneousMatrix &ctTc0_);
443  void computeVVSPoseEstimation(const unsigned int iter, vpMatrix &L,
444  const vpColVector &w, vpMatrix &L_true, vpMatrix &LVJ_true, double &normRes, double &normRes_1, vpColVector &w_true,
445  vpColVector &R, vpMatrix &LTL, vpColVector &LTR, vpColVector &error_prev, vpColVector &v, double &mu,
446  vpHomogeneousMatrix &cMoPrev, vpHomogeneousMatrix &ctTc0_Prev);
447  void computeVVSWeights(const unsigned int iter, const unsigned int nbInfos, const vpColVector &R,
448  vpColVector &w_true, vpColVector &w, vpRobust &robust);
449 
450  virtual void init(const vpImage<unsigned char>& I);
451  virtual void initFaceFromCorners(vpMbtPolygon &polygon);
452  virtual void initFaceFromLines(vpMbtPolygon &polygon);
453  virtual void initCircle(const vpPoint&, const vpPoint &, const vpPoint &, const double, const int,
454  const std::string &name="");
455  virtual void initCylinder(const vpPoint&, const vpPoint &, const double, const int,
456  const std::string &name="");
457 
458  void preTracking(const vpImage<unsigned char>& I, unsigned int &nbInfos, unsigned int &nbFaceUsed);
460  virtual void reinit(const vpImage<unsigned char>& I);
462 };
463 
464 #endif
465 #endif // VISP_HAVE_OPENCV
bool compute_interaction
If true, compute the interaction matrix at each iteration of the minimization. Otherwise, compute it only on the first iteration.
virtual std::list< vpMbtDistanceCircle * > & getFeaturesCircle()
void setWindowName(const Ogre::String &n)
Definition: vpAROgre.h:266
std::list< vpMbtDistanceKltPoints * > kltPolygons
void setThresholdAcceptation(const double th)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
virtual ~vpMbKltTracker()
double lambda
The gain of the virtual visual servoing stage.
virtual void setScanLineVisibilityTest(const bool &v)
virtual void track(const vpImage< unsigned char > &I)=0
void computeVVSPoseEstimation(const unsigned int iter, vpMatrix &L, const vpColVector &w, vpMatrix &L_true, vpMatrix &LVJ_true, double &normRes, double &normRes_1, vpColVector &w_true, vpColVector &R, vpMatrix &LTL, vpColVector &LTR, vpColVector &error_prev, vpColVector &v, double &mu, vpHomogeneousMatrix &cMoPrev, vpHomogeneousMatrix &ctTc0_Prev)
virtual void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, const double r, const std::string &name="")
virtual void setUseKltTracking(const std::string &name, const bool &useKltTracking)
virtual void computeVVS(std::map< std::string, const vpImage< unsigned char > * > &mapOfImages, std::map< std::string, unsigned int > &mapOfNumberOfRows, std::map< std::string, unsigned int > &mapOfNbInfos, vpColVector &w_mbt, vpColVector &w_klt, const unsigned int lvl=0)
vpMbHiddenFaces< vpMbtPolygon > faces
Set of faces describing the object.
Definition: vpMbTracker.h:144
virtual void setLambda(const double gain)
unsigned int maxIter
The maximum iteration of the virtual visual servoing stage.
virtual void computeVVSWeights(const unsigned int iter, const unsigned int nbInfos, std::map< std::string, unsigned int > &mapOfNbInfos, vpColVector &R, vpColVector &w_true, vpColVector &w, std::map< std::string, vpRobust > &mapOfRobusts, double threshold)
std::vector< cv::Point2f > getFeatures() const
Get the list of current features.
Definition: vpKltOpencv.h:104
Implementation of an homogeneous matrix and operations on such kind of matrices.
virtual void setProjectionErrorComputation(const bool &flag)
virtual void preTracking(std::map< std::string, const vpImage< unsigned char > * > &mapOfImages, std::map< std::string, unsigned int > &mapOfNbInfos, std::map< std::string, unsigned int > &mapOfNbFaceUsed)
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
double percentGood
Percentage of good points, according to the initial number, that must have the tracker.
vpHomogeneousMatrix cMo
The current pose.
Definition: vpMbTracker.h:115
virtual void initCircle(const vpPoint &p1, const vpPoint &p2, const vpPoint &p3, const double radius, const int idFace=0, const std::string &name="")=0
virtual double getLambda() const
vpMbScanLine & getMbScanLineRenderer()
Hybrid stereo (or more) tracker based on moving-edges and keypoints tracked using KLT tracker...
void computeVVSCheckLevenbergMarquardtKlt(const unsigned int iter, const unsigned int nbInfos, const vpHomogeneousMatrix &cMoPrev, const vpColVector &error_prev, const vpHomogeneousMatrix &ctTc0_Prev, double &mu, bool &reStartFromLastIncrement)
bool firstInitialisation
Flag to specify whether the init method is called the first or not (specific calls to realize in this...
virtual void resetTracker()=0
Class that defines what is a point.
Definition: vpPoint.h:59
vpCameraParameters cam
The camera parameters.
Definition: vpMbTracker.h:113
vpHomogeneousMatrix ctTc0
The estimated displacement of the pose between the current instant and the initial position...
virtual void init(const vpImage< unsigned char > &I)=0
unsigned int getMaskBorder() const
std::vector< cv::Point2f > getKltPoints() const
virtual std::map< std::string, std::vector< vpImagePoint > > getKltImagePoints() const
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:179
vpAROgre * getOgreContext()
void computeVVSInteractionMatrixAndResidu(unsigned int shift, vpColVector &R, vpMatrix &L, vpHomography &H, std::list< vpMbtDistanceKltPoints * > &kltPolygons_, std::list< vpMbtDistanceKltCylinder * > &kltCylinders_, const vpHomogeneousMatrix &ctTc0_)
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, const unsigned int thickness=1, const bool displayFullModel=false)=0
Implementation of a polygon of the model used by the model-based tracker.
Definition: vpMbtPolygon.h:64
virtual void setCameraParameters(const vpCameraParameters &camera)
Definition: vpMbTracker.h:389
virtual std::map< std::string, std::vector< cv::Point2f > > getKltPoints() const
virtual void reInitModel(const vpImage< unsigned char > &I, const std::string &cad_name, const vpHomogeneousMatrix &cMo_, const bool verbose=false)
unsigned int maskBorder
Erosion of the mask.
virtual void initFaceFromCorners(vpMbtPolygon &polygon)=0
Generic class defining intrinsic camera parameters.
Main methods for a model-based tracker.
Definition: vpMbTracker.h:103
virtual std::list< vpMbtDistanceKltPoints * > & getFeaturesKlt()
Model based tracker using only KLT.
virtual void loadConfigFile(const std::string &configFile)=0
vpKltOpencv tracker
Points tracker.
std::list< vpMbtDistanceKltCylinder * > kltCylinders
virtual std::list< vpMbtDistanceKltCylinder * > & getFeaturesKltCylinder()
double threshold_outlier
Threshold below which the weight associated to a point to consider this one as an outlier...
virtual void setMaxIter(const unsigned int max)
virtual void setKltOpencv(const vpKltOpencv &t)
virtual std::map< std::string, std::map< int, vpImagePoint > > getKltImagePointsWithId() const
cv::Mat cur
Temporary OpenCV image for fast conversion.
double lambda
The gain of the virtual visual servoing stage.
int getNbKltPoints() const
vpKltOpencv getKltOpencv() const
std::list< vpMbtDistanceCircle * > circles_disp
Vector of the circles used here only to display the full model.
virtual void setOgreVisibilityTest(const bool &v)
void setMaskBorder(const unsigned int &e)
#define vpCTRACE
Definition: vpDebug.h:337
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
virtual void initFaceFromLines(vpMbtPolygon &polygon)=0
virtual void setOgreVisibilityTest(const bool &v)
virtual void testTracking()=0
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
Contains an M-Estimator and various influence function.
Definition: vpRobust.h:60
double getThresholdAcceptation() const
virtual void postTracking(std::map< std::string, const vpImage< unsigned char > * > &mapOfImages, vpColVector &w_mbt, vpColVector &w_klt, std::map< std::string, unsigned int > &mapOfNumberOfRows, std::map< std::string, unsigned int > &mapOfNbInfos, const unsigned int lvl)
unsigned int maxIter
The maximum iteration of the virtual visual servoing stage.
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)=0
virtual void setScanLineVisibilityTest(const bool &v)
Definition: vpMbTracker.h:439
void computeVVSCovariance(const vpColVector &w_true, const vpHomogeneousMatrix &cMoPrev, const vpMatrix &L_true, const vpMatrix &LVJ_true)
virtual unsigned int getMaxIter() const
Model based stereo (or more) tracker using only KLT.
vpHomogeneousMatrix c0Mo
Initial pose.
virtual void initCylinder(const vpPoint &p1, const vpPoint &p2, const double radius, const int idFace=0, const std::string &name="")=0