ViSP  2.10.0
vpMbEdgeKltTracker.cpp
1 /****************************************************************************
2  *
3  * $Id: vpMbEdgeKltTracker.cpp 5189 2015-01-21 16:42:18Z ayol $
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  * Description:
34  * Hybrid tracker based on edges (vpMbt) and points of interests (KLT)
35  *
36  * Authors:
37  * Romain Tallonneau
38  * Aurelien Yol
39  *
40  *****************************************************************************/
41 
42 //#define VP_DEBUG_MODE 1 // Activate debug level 1
43 
44 #include <visp/vpDebug.h>
45 #include <visp/vpMbEdgeKltTracker.h>
46 #include <visp/vpTrackingException.h>
47 #include <visp/vpVelocityTwistMatrix.h>
48 
49 #if (defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100))
50 
52  : compute_interaction(true), lambda(0.8), thresholdKLT(2.), thresholdMBT(2.), maxIter(200)
53 {
54  computeCovariance = false;
55 
57 
60 
61 #ifdef VISP_HAVE_OGRE
62  faces.getOgreContext()->setWindowName("MBT Hybrid");
63 #endif
64 }
65 
71 {
72 }
73 
80 void
82 {
84 
86 
87  unsigned int i = (unsigned int)scales.size();
88  do {
89  i--;
90  if(scales[i]){
91  downScale(i);
93  upScale(i);
94  }
95  } while(i != 0);
96 
98 }
99 
109 void
111 {
112  vpMbKltTracker::setPose(I, cdMo);
113 
114  if (! lines[scaleLevel].empty()) {
115  lines[scaleLevel].front() ;
116  for(std::list<vpMbtDistanceLine*>::const_iterator it=lines[scaleLevel].begin(); it!=lines[scaleLevel].end(); ++it){
117  if((*it)->meline != NULL){
118  delete (*it)->meline;
119  (*it)->meline = NULL;
120  }
121  }
122  }
123 
124  if (! cylinders[scaleLevel].empty()) {
125  cylinders[scaleLevel].front() ;
126  for(std::list<vpMbtDistanceCylinder*>::const_iterator it=cylinders[scaleLevel].begin(); it!=cylinders[scaleLevel].end(); ++it){
127  if((*it)->meline1 != NULL){
128  delete (*it)->meline1;
129  (*it)->meline1 = NULL;
130  }
131  if((*it)->meline2 != NULL){
132  delete (*it)->meline2;
133  (*it)->meline2 = NULL;
134  }
135  }
136  }
137 
138  if (! circles[scaleLevel].empty()) {
139  circles[scaleLevel].front() ;
140  for(std::list<vpMbtDistanceCircle*>::const_iterator it=circles[scaleLevel].begin(); it!=circles[scaleLevel].end(); ++it){
141  if((*it)->meEllipse != NULL){
142  delete (*it)->meEllipse;
143  (*it)->meEllipse = NULL;
144  }
145  }
146  }
147 
148  initPyramid(I, Ipyramid);
149 
150  unsigned int i = (unsigned int)scales.size();
151  do {
152  i--;
153  if(scales[i]){
154  downScale(i);
156  upScale(i);
157  }
158  } while(i != 0);
159 
161 }
162 
167 void
169 {
172 }
173 
174 unsigned int
175 vpMbEdgeKltTracker::initMbtTracking(const unsigned int lvl)
176 {
177  vpMbtDistanceLine *l ;
179  vpMbtDistanceCircle *ci ;
180 
181  if(lvl >= scales.size() || !scales[lvl]){
182  throw vpException(vpException::dimensionError, "lvl not used.");
183  }
184 
185  unsigned int nbrow = 0;
186  for(std::list<vpMbtDistanceLine*>::iterator it=lines[lvl].begin(); it!=lines[lvl].end(); ++it){
187  l = *it;
188  nbrow += l->nbFeature ;
190  }
191 
192  for(std::list<vpMbtDistanceCylinder*>::const_iterator it=cylinders[lvl].begin(); it!=cylinders[lvl].end(); ++it){
193  cy = *it;
194  nbrow += cy->nbFeature ;
196  }
197 
198  for(std::list<vpMbtDistanceCircle*>::const_iterator it=circles[lvl].begin(); it!=circles[lvl].end(); ++it){
199  ci = *it;
200  nbrow += ci->nbFeature ;
202  }
203 
204  return nbrow;
205 }
206 
218 void
219 vpMbEdgeKltTracker::loadConfigFile(const std::string& configFile)
220 {
221  vpMbEdgeKltTracker::loadConfigFile(configFile.c_str());
222 }
223 
288 void
289 vpMbEdgeKltTracker::loadConfigFile(const char* configFile)
290 {
291 #ifdef VISP_HAVE_XML2
293 
294  xmlp.setCameraParameters(cam);
297 
298  xmlp.setMovingEdge(me);
299 
300  xmlp.setMaxFeatures(10000);
301  xmlp.setWindowSize(5);
302  xmlp.setQuality(0.01);
303  xmlp.setMinDistance(5);
304  xmlp.setHarrisParam(0.01);
305  xmlp.setBlockSize(3);
306  xmlp.setPyramidLevels(3);
308 
309  try{
310  std::cout << " *********** Parsing XML for Mb Edge Tracker ************ " << std::endl;
311  xmlp.parse(configFile);
312  }
313  catch(...){
314  vpERROR_TRACE("Can't open XML file \"%s\"\n ", configFile);
315  throw vpException(vpException::ioError, "problem to parse configuration file.");
316  }
317 
318  vpCameraParameters camera;
319  xmlp.getCameraParameters(camera);
320  setCameraParameters(camera);
321 
324 
325  if(xmlp.hasNearClippingDistance())
327 
328  if(xmlp.hasFarClippingDistance())
330 
331  if(xmlp.getFovClipping()){
333  }
334 
335  vpMe meParser;
336  xmlp.getMe(meParser);
338 
340  tracker.setWindowSize((int)xmlp.getWindowSize());
341  tracker.setQuality(xmlp.getQuality());
344  tracker.setBlockSize((int)xmlp.getBlockSize());
346  maskBorder = xmlp.getMaskBorder();
347 #else
348  vpTRACE("You need the libXML2 to read the config file %s", configFile);
349 #endif
350 }
351 
355 bool
357  const unsigned int lvl)
358 {
359  bool reInit = vpMbKltTracker::postTracking(I, w_klt);
360 
361  postTrackingMbt(w_mbt,lvl);
362 
363  if (displayFeatures)
364  {
365  if(lvl == 0){
367  for(std::list<vpMbtDistanceLine*>::const_iterator it=lines[lvl].begin(); it!=lines[lvl].end(); ++it){
368  l = *it;
369  if (l->isVisible()){
370  l->displayMovingEdges(I);
371  }
372  }
373 
375  for(std::list<vpMbtDistanceCylinder*>::const_iterator it=cylinders[lvl].begin(); it!=cylinders[lvl].end(); ++it){
376  cy = *it;
377  // A cylinder is always visible
378  cy->displayMovingEdges(I);
379  }
380 
381  vpMbtDistanceCircle *ci ;
382  for(std::list<vpMbtDistanceCircle*>::const_iterator it=circles[lvl].begin(); it!=circles[lvl].end(); ++it){
383  ci = *it;
384  if (ci->isVisible()){
385  ci->displayMovingEdges(I);
386  }
387  }
388  }
389  }
390 
391  if(reInit)
392  return true;
393 
395 
398 
399  return false;
400 }
401 
412 void
414 {
415  if(lvl >= scales.size() || !scales[lvl]){
416  throw vpException(vpException::dimensionError, "_lvl not used.");
417  }
418  unsigned int n =0 ;
420  for(std::list<vpMbtDistanceLine*>::const_iterator it=lines[lvl].begin(); it!=lines[lvl].end(); ++it){
421  l = *it;
422  {
423  double wmean = 0 ;
424  std::list<vpMeSite>::iterator itListLine;
425  if (l->nbFeature > 0) itListLine = l->meline->getMeList().begin();
426 
427  for (unsigned int i=0 ; i < l->nbFeature ; i++){
428  wmean += w[n+i] ;
429  vpMeSite p = *itListLine;
430  if (w[n+i] < 0.5){
432 
433  *itListLine = p;
434  }
435 
436  ++itListLine;
437  }
438  n+= l->nbFeature ;
439 
440  if (l->nbFeature!=0)
441  wmean /= l->nbFeature ;
442  else
443  wmean = 1;
444 
445  l->setMeanWeight(wmean);
446 
447  if (wmean < 0.8)
448  l->Reinit = true;
449  }
450  }
451 
452  // Same thing with cylinders as with lines
454  for(std::list<vpMbtDistanceCylinder*>::const_iterator it=cylinders[lvl].begin(); it!=cylinders[lvl].end(); ++it){
455  cy = *it;
456  double wmean = 0 ;
457  std::list<vpMeSite>::iterator itListCyl1;
458  std::list<vpMeSite>::iterator itListCyl2;
459  if (cy->nbFeature > 0){
460  itListCyl1 = cy->meline1->getMeList().begin();
461  itListCyl2 = cy->meline2->getMeList().begin();
462  }
463 
464  wmean = 0;
465  for(unsigned int i=0 ; i < cy->nbFeaturel1 ; i++){
466  wmean += w[n+i] ;
467  vpMeSite p = *itListCyl1;
468  if (w[n+i] < 0.5){
470 
471  *itListCyl1 = p;
472  }
473 
474  ++itListCyl1;
475  }
476 
477  if (cy->nbFeaturel1!=0)
478  wmean /= cy->nbFeaturel1 ;
479  else
480  wmean = 1;
481 
482  cy->setMeanWeight1(wmean);
483 
484  if (wmean < 0.8){
485  cy->Reinit = true;
486  }
487 
488  wmean = 0;
489  for(unsigned int i=cy->nbFeaturel1 ; i < cy->nbFeature ; i++){
490  wmean += w[n+i] ;
491  vpMeSite p = *itListCyl2;
492  if (w[n+i] < 0.5){
494 
495  *itListCyl2 = p;
496  }
497 
498  ++itListCyl2;
499  }
500 
501  if (cy->nbFeaturel2!=0)
502  wmean /= cy->nbFeaturel2 ;
503  else
504  wmean = 1;
505 
506  cy->setMeanWeight2(wmean);
507 
508  if (wmean < 0.8){
509  cy->Reinit = true;
510  }
511 
512  n+= cy->nbFeature ;
513  }
514 
515  // Same thing with circles as with lines
517  for(std::list<vpMbtDistanceCircle*>::const_iterator it=circles[scaleLevel].begin(); it!=circles[scaleLevel].end(); ++it){
518  ci = *it;
519  double wmean = 0 ;
520  std::list<vpMeSite>::iterator itListCir;
521 
522  if (ci->nbFeature > 0){
523  itListCir = ci->meEllipse->getMeList().begin();
524  }
525 
526  wmean = 0;
527  for(unsigned int i=0 ; i < ci->nbFeature ; i++){
528  wmean += w[n+i] ;
529  vpMeSite p = *itListCir;
530  if (w[n+i] < 0.5){
532 
533  *itListCir = p;
534  }
535 
536  ++itListCir;
537  }
538 
539  if (ci->nbFeature!=0)
540  wmean /= ci->nbFeature ;
541  else
542  wmean = 1;
543 
544  ci->setMeanWeight(wmean);
545 
546  if (wmean < 0.8){
547  ci->Reinit = true;
548  }
549 
550  n+= ci->nbFeature ;
551  }
552 }
553 
563 void
564 vpMbEdgeKltTracker::computeVVS(const vpImage<unsigned char>& I, const unsigned int &nbInfos, vpColVector &w_mbt, vpColVector &w_klt, const unsigned int lvl)
565 {
566  vpColVector factor;
567  unsigned int nbrow = trackFirstLoop(I, factor, lvl);
568 
569  if(nbrow < 4 && nbInfos < 4){
570  vpERROR_TRACE("\n\t\t Error-> not enough data") ;
571  throw vpTrackingException(vpTrackingException::notEnoughPointError, "\n\t\t Error-> not enough data");
572  }
573  else if(nbrow < 4)
574  nbrow = 0;
575 
576  double residu = 0;
577  double residu_1 = -1;
578  unsigned int iter = 0;
579 
580  vpMatrix *L;
581  vpMatrix L_mbt, L_klt; // interaction matrix
582  vpColVector *R;
583  vpColVector R_mbt, R_klt; // residu
584  vpMatrix L_true;
585  vpMatrix LVJ_true;
586  //vpColVector R_true;
587  vpColVector w_true;
588 
589  if(nbrow != 0){
590  L_mbt.resize(nbrow,6);
591  R_mbt.resize(nbrow);
592  }
593 
594  if(nbInfos != 0){
595  L_klt.resize(2*nbInfos,6);
596  R_klt.resize(2*nbInfos);
597  }
598 
599  //vpColVector w; // weight from MEstimator
600  vpColVector v; // "speed" for VVS
601  vpRobust robust_mbt(0), robust_klt(0);
602  vpHomography H;
603 
604  vpMatrix LTL, LTR;
605 
606  double factorMBT = 1.0;
607  double factorKLT = 1.0;
608 
609  //More efficient weight repartition for hybrid tracker should come soon...
610 // factorMBT = 1.0 - (double)nbrow / (double)(nbrow + nbInfos);
611 // factorKLT = 1.0 - factorMBT;
612  factorMBT = 0.35;
613  factorKLT = 0.65;
614 
615  if (nbrow < 4)
616  factorKLT = 1.;
617  if (nbInfos < 4)
618  factorMBT = 1.;
619 
620  double residuMBT = 0;
621  double residuKLT = 0;
622 
623  vpHomogeneousMatrix cMoPrev;
624 
625  while( ((int)((residu - residu_1)*1e8) !=0 ) && (iter<maxIter) ){
626  L = new vpMatrix();
627  R = new vpColVector();
628 
629  if(nbrow >= 4)
630  trackSecondLoop(I,L_mbt,R_mbt,cMo,lvl);
631 
632  if(nbInfos >= 4){
633  unsigned int shift = 0;
634  vpMbtDistanceKltPoints *kltpoly;
635  // for (unsigned int i = 0; i < faces.size(); i += 1){
636  for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=vpMbKltTracker::kltPolygons.begin(); it!=vpMbKltTracker::kltPolygons.end(); ++it){
637  kltpoly = *it;
638  if(kltpoly->polygon->isVisible() && kltpoly->hasEnoughPoints()){
639  vpSubColVector subR(R_klt, shift, 2*kltpoly->getCurrentNumberPoints());
640  vpSubMatrix subL(L_klt, shift, 0, 2*kltpoly->getCurrentNumberPoints(), 6);
641  kltpoly->computeHomography(ctTc0, H);
642  kltpoly->computeInteractionMatrixAndResidu(subR, subL);
643  shift += 2*kltpoly->getCurrentNumberPoints();
644  }
645  }
646  }
647 
648  if(iter == 0){
649  m_w.resize(nbrow + 2*nbInfos);
650  m_w=1;
651 
652  if(nbrow != 0){
653  w_mbt.resize(nbrow);
654  w_mbt = 1;
655  robust_mbt.resize(nbrow);
656  }
657 
658  if(nbInfos != 0){
659  w_klt.resize(2*nbInfos);
660  w_klt = 1;
661  robust_klt.resize(2*nbInfos);
662  }
663 
664  w_true.resize(nbrow + 2*nbInfos);
665  }
666 
667  /* robust */
668  if(nbrow > 3){
669  residuMBT = 0;
670  for(unsigned int i = 0; i < R_mbt.getRows(); i++)
671  residuMBT += fabs(R_mbt[i]);
672  residuMBT /= R_mbt.getRows();
673 
674  robust_mbt.setIteration(iter);
675  robust_mbt.setThreshold(thresholdMBT/cam.get_px());
676  robust_mbt.MEstimator( vpRobust::TUKEY, R_mbt, w_mbt);
677  L->stackMatrices(L_mbt);
678  R->stackMatrices(R_mbt);
679  }
680 
681  if(nbInfos > 3){
682  residuKLT = 0;
683  for(unsigned int i = 0; i < R_klt.getRows(); i++)
684  residuKLT += fabs(R_klt[i]);
685  residuKLT /= R_klt.getRows();
686 
687  robust_klt.setIteration(iter);
688  robust_klt.setThreshold(thresholdKLT/cam.get_px());
689  robust_klt.MEstimator( vpRobust::TUKEY, R_klt, w_klt);
690 
691  L->stackMatrices(L_klt);
692  R->stackMatrices(R_klt);
693  }
694 
695  unsigned int cpt = 0;
696  while(cpt< (nbrow+2*nbInfos)){
697  if(cpt<(unsigned)nbrow){
698  m_w[cpt] = ((w_mbt[cpt] * factor[cpt]) * factorMBT) ;
699  }
700  else
701  m_w[cpt] = (w_klt[cpt-nbrow] * factorKLT);
702  cpt++;
703  }
704 
705  m_error = (*R);
706  if(computeCovariance){
707  L_true = (*L);
708  if(!isoJoIdentity){
710  cVo.buildFrom(cMo);
711  LVJ_true = ((*L)*cVo*oJo);
712  }
713  }
714 
715  residu_1 = residu;
716  residu = 0;
717  double num = 0;
718  double den = 0;
719  for (unsigned int i = 0; i < static_cast<unsigned int>(R->getRows()); i++){
720  num += m_w[i]*vpMath::sqr((*R)[i]);
721  den += m_w[i];
722 
723  w_true[i] = m_w[i];
724  (*R)[i] *= m_w[i];
726  for (unsigned int j = 0; j < 6; j += 1){
727  (*L)[i][j] *= m_w[i];
728  }
729  }
730  }
731 
732  residu = sqrt(num/den);
733 
734  if(isoJoIdentity){
735  LTL = L->AtA();
736  computeJTR(*L, *R, LTR);
737  v = -lambda * LTL.pseudoInverse() * LTR;
738  }
739  else{
741  cVo.buildFrom(cMo);
742  vpMatrix LVJ = ((*L)*cVo*oJo);
743  vpMatrix LVJTLVJ = (LVJ).AtA();
744  vpMatrix LVJTR;
745  computeJTR(LVJ, *R, LVJTR);
746  v = -lambda*LVJTLVJ.pseudoInverse(1e-16)*LVJTR;
747  v = cVo * v;
748  }
749 
750  cMoPrev = cMo;
753 
754  iter++;
755 
756  delete L;
757  delete R;
758  }
759 
760  if(computeCovariance){
761  vpMatrix D;
762  D.diag(w_true);
763 
764  // Note that here the covariance is computed on cMoPrev for time computation efficiency
765  if(isoJoIdentity){
766  computeCovarianceMatrix(cMoPrev,m_error,L_true,D);
767  }
768  else{
769  computeCovarianceMatrix(cMoPrev,m_error,LVJ_true,D);
770  }
771  }
772 }
773 
781 void
783 {
784  unsigned int nbInfos = 0;
785  unsigned int nbFaceUsed = 0;
786  vpColVector w_klt;
787 
788  try{
789  vpMbKltTracker::preTracking(I, nbInfos, nbFaceUsed);
790  }
791  catch(...){}
792 
793  if(nbInfos >= 4)
794  vpMbKltTracker::computeVVS(nbInfos, w_klt);
795  else{
796  nbInfos = 0;
797  // std::cout << "[Warning] Unable to init with KLT" << std::endl;
798  }
799 
801 
802  vpColVector w_mbt;
803  computeVVS(I, nbInfos, w_mbt, w_klt);
804 
805  if(postTracking(I, w_mbt, w_klt)){
807 
808  initPyramid(I, Ipyramid);
809 
810  unsigned int i = (unsigned int)scales.size();
811  do {
812  i--;
813  if(scales[i]){
814  downScale(i);
816  upScale(i);
817  }
818  } while(i != 0);
819 
821  }
822 }
823 
824 unsigned int
826 {
827  vpMbtDistanceLine *l ;
829  vpMbtDistanceCircle *ci ;
830 
831  if(lvl >= scales.size() || !scales[lvl]){
832  throw vpException(vpException::dimensionError, "_lvl not used.");
833  }
834 
835  unsigned int nbrow = initMbtTracking(lvl);
836 
837  if (nbrow==0){
838 // vpERROR_TRACE("\n\t\t Error-> not enough data in the interaction matrix...") ;
839 // throw vpTrackingException(vpTrackingException::notEnoughPointError, "\n\t\t Error-> not enough data in the interaction matrix...");
840  return nbrow;
841  }
842 
843  factor.resize(nbrow);
844  factor = 1;
845 
846  unsigned int n = 0;
847  for(std::list<vpMbtDistanceLine*>::const_iterator it=lines[lvl].begin(); it!=lines[lvl].end(); ++it){
848  l = *it;
850 
851  double fac = 1;
852  for(std::list<int>::const_iterator itindex = l->Lindex_polygon.begin(); itindex!=l->Lindex_polygon.end(); ++itindex){
853  int index = *itindex;
854  if (l->hiddenface->isAppearing((unsigned int)index)) {
855  fac = 0.2;
856  break;
857  }
858  if(l->closeToImageBorder(I, 10)){
859  fac = 0.1;
860  break;
861  }
862  }
863 
864  std::list<vpMeSite>::const_iterator itListLine;
865  if (l->meline != NULL)
866  itListLine = l->meline->getMeList().begin();
867 
868  for (unsigned int i=0 ; i < l->nbFeature ; i++){
869  factor[n+i] = fac;
870  vpMeSite site = *itListLine;
871  if (site.getState() != vpMeSite::NO_SUPPRESSION) factor[n+i] = 0.2;
872  ++itListLine;
873  }
874  n+= l->nbFeature ;
875  }
876 
877  for(std::list<vpMbtDistanceCylinder*>::const_iterator it=cylinders[lvl].begin(); it!=cylinders[lvl].end(); ++it){
878  cy = *it;
880  double fac = 1.0;
881 
882  std::list<vpMeSite>::const_iterator itCyl1;
883  std::list<vpMeSite>::const_iterator itCyl2;
884  if ((cy->meline1 != NULL || cy->meline2 != NULL)){
885  itCyl1 = cy->meline1->getMeList().begin();
886  itCyl2 = cy->meline2->getMeList().begin();
887  }
888 
889  for(unsigned int i=0 ; i < cy->nbFeature ; i++){
890  factor[n+i] = fac;
891  vpMeSite site;
892  if(i<cy->nbFeaturel1) {
893  site= *itCyl1;
894  ++itCyl1;
895  }
896  else{
897  site= *itCyl2;
898  ++itCyl2;
899  }
900  if (site.getState() != vpMeSite::NO_SUPPRESSION) factor[n+i] = 0.2;
901  }
902 
903  n+= cy->nbFeature ;
904  }
905 
906  for(std::list<vpMbtDistanceCircle*>::const_iterator it=circles[scaleLevel].begin(); it!=circles[scaleLevel].end(); ++it){
907  ci = *it;
909  double fac = 1.0;
910 
911  std::list<vpMeSite>::const_iterator itCir;
912  if (ci->meEllipse != NULL) {
913  itCir = ci->meEllipse->getMeList().begin();
914  }
915 
916  for(unsigned int i=0 ; i < ci->nbFeature ; i++){
917  factor[n+i] = fac;
918  vpMeSite site = *itCir;
919  if (site.getState() != vpMeSite::NO_SUPPRESSION) factor[n+i] = 0.2;
920  ++itCir;
921  }
922 
923  n+= ci->nbFeature ;
924  }
925 
926  return nbrow;
927 }
928 
929 void
931  vpHomogeneousMatrix& cMo_, const unsigned int lvl)
932 {
935  vpMbtDistanceCircle *ci ;
936 
937  unsigned int n = 0 ;
938  for(std::list<vpMbtDistanceLine*>::const_iterator it=lines[lvl].begin(); it!=lines[lvl].end(); ++it){
939  l = *it;
941  for (unsigned int i=0 ; i < l->nbFeature ; i++){
942  for (unsigned int j=0; j < 6 ; j++){
943  L[n+i][j] = l->L[i][j];
944  error[n+i] = l->error[i];
945  }
946  }
947  n+= l->nbFeature;
948  }
949 
950  for(std::list<vpMbtDistanceCylinder*>::const_iterator it=cylinders[lvl].begin(); it!=cylinders[lvl].end(); ++it){
951  cy = *it;
952  cy->computeInteractionMatrixError(cMo_, I) ;
953  for(unsigned int i=0 ; i < cy->nbFeature ; i++){
954  for(unsigned int j=0; j < 6 ; j++){
955  L[n+i][j] = cy->L[i][j];
956  error[n+i] = cy->error[i];
957  }
958  }
959  n+= cy->nbFeature ;
960  }
961  for(std::list<vpMbtDistanceCircle*>::const_iterator it=circles[scaleLevel].begin(); it!=circles[scaleLevel].end(); ++it){
962  ci = *it;
964  for(unsigned int i=0 ; i < ci->nbFeature ; i++){
965  for(unsigned int j=0; j < 6 ; j++){
966  L[n+i][j] = ci->L[i][j];
967  error[n+i] = ci->error[i];
968  }
969  }
970 
971  n+= ci->nbFeature ;
972  }
973 }
974 
980 void
982 {
983  this->cam = camera;
984 
987 }
988 
994 void
996 {
999 }
1005 void
1007 {
1010 }
1011 
1023 void
1024 vpMbEdgeKltTracker::initCircle(const vpPoint& p1, const vpPoint &p2, const vpPoint &p3, const double radius,
1025  const int idFace, const std::string &name)
1026 {
1027  vpMbEdgeTracker::initCircle(p1, p2, p3, radius, idFace, name);
1028 }
1029 
1040 void
1041 vpMbEdgeKltTracker::initCylinder(const vpPoint& p1, const vpPoint &p2, const double radius, const int idFace,
1042  const std::string &name)
1043 {
1044  vpMbEdgeTracker::initCylinder(p1, p2, radius, idFace, name);
1045 }
1046 
1057 void
1059  const vpColor& col, const unsigned int thickness, const bool displayFullModel)
1060 {
1061  for (unsigned int i = 0; i < scales.size(); i += 1){
1062  if(scales[i]){
1063  for(std::list<vpMbtDistanceLine*>::const_iterator it=lines[scaleLevel].begin(); it!=lines[scaleLevel].end(); ++it){
1064  (*it)->display(I,cMo_, camera, col, thickness, displayFullModel);
1065  }
1066 
1067  for(std::list<vpMbtDistanceCylinder*>::const_iterator it=cylinders[scaleLevel].begin(); it!=cylinders[scaleLevel].end(); ++it){
1068  (*it)->display(I, cMo_, camera, col, thickness, displayFullModel);
1069  }
1070 
1071  for(std::list<vpMbtDistanceCircle*>::const_iterator it=circles[scaleLevel].begin(); it!=circles[scaleLevel].end(); ++it){
1072  (*it)->display(I, cMo_, camera, col, thickness, displayFullModel);
1073  }
1074 
1075  break ; //displaying model on one scale only
1076  }
1077  }
1078 
1079  vpMbtDistanceKltPoints *kltpoly;
1080  for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=kltPolygons.begin(); it!=kltPolygons.end(); ++it){
1081  kltpoly = *it;
1082  if(displayFeatures && kltpoly->hasEnoughPoints() && kltpoly->polygon->isVisible()) {
1083  kltpoly->displayPrimitive(I);
1084  }
1085  }
1086 
1087 #ifdef VISP_HAVE_OGRE
1088  if(useOgre)
1089  faces.displayOgre(cMo_);
1090 #endif
1091 }
1092 
1103 void
1105  const vpColor& col , const unsigned int thickness, const bool displayFullModel)
1106 {
1107  for (unsigned int i = 0; i < scales.size(); i += 1){
1108  if(scales[i]){
1109  for(std::list<vpMbtDistanceLine*>::const_iterator it=lines[scaleLevel].begin(); it!=lines[scaleLevel].end(); ++it){
1110  (*it)->display(I,cMo_, camera, col, thickness, displayFullModel);
1111  }
1112 
1113  for(std::list<vpMbtDistanceCylinder*>::const_iterator it=cylinders[scaleLevel].begin(); it!=cylinders[scaleLevel].end(); ++it){
1114  (*it)->display(I, cMo_, camera, col, thickness, displayFullModel);
1115  }
1116 
1117  for(std::list<vpMbtDistanceCircle*>::const_iterator it=circles[scaleLevel].begin(); it!=circles[scaleLevel].end(); ++it){
1118  (*it)->display(I, cMo_, camera, col, thickness, displayFullModel);
1119  }
1120 
1121  break ; //displaying model on one scale only
1122  }
1123  }
1124 
1125  vpMbtDistanceKltPoints *kltpoly;
1126  for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=kltPolygons.begin(); it!=kltPolygons.end(); ++it){
1127  kltpoly = *it;
1128  if(displayFeatures && kltpoly->hasEnoughPoints() && kltpoly->polygon->isVisible()) {
1129  kltpoly->displayPrimitive(I);
1130  }
1131  }
1132 
1133 #ifdef VISP_HAVE_OGRE
1134  if(useOgre)
1135  faces.displayOgre(cMo_);
1136 #endif
1137 }
1138 
1148 void
1149 vpMbEdgeKltTracker::reInitModel(const vpImage<unsigned char>& I, const std::string &cad_name,
1150  const vpHomogeneousMatrix& cMo_, const bool verbose)
1151 {
1152  reInitModel(I, cad_name.c_str(), cMo_, verbose);
1153 }
1154 
1164 void
1166  const vpHomogeneousMatrix& cMo_, const bool verbose)
1167 {
1168  vpMbKltTracker::reInitModel(I, cad_name, cMo_, verbose);
1169  vpMbEdgeTracker::reInitModel(I, cad_name, cMo_, verbose);
1170 }
1171 
1172 #endif //VISP_HAVE_OPENCV
void setWindowName(const Ogre::String &n)
Definition: vpAROgre.h:268
void trackSecondLoop(const vpImage< unsigned char > &I, vpMatrix &L, vpColVector &_error, vpHomogeneousMatrix &cMo, const unsigned int lvl=0)
virtual void initCircle(const vpPoint &, const vpPoint &, const vpPoint &, const double r, const int idFace=0, const std::string &name="")
std::list< vpMbtDistanceKltPoints * > kltPolygons
Vector of the cylinders used here only to display the full model.
double lambda
The gain of the virtual visual servoing stage.
unsigned int getMaskBorder() const
void setQuality(const double &q)
void postTrackingMbt(vpColVector &w, const unsigned int level=0)
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void displayMovingEdges(const vpImage< unsigned char > &I)
void setMovingEdge(const vpMe &me)
double getHarrisParam() const
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:199
Parse an Xml file to extract configuration parameters of a mbtConfig object.Data parser for the model...
unsigned int nbFeature
The number of moving edges.
void reInitModel(const vpImage< unsigned char > &I, const std::string &cad_name, const vpHomogeneousMatrix &cMo_, const bool verbose=false)
void upScale(const unsigned int _scale)
void parse(const char *filename)
virtual void initFaceFromCorners(vpMbtPolygon &polygon)
void MEstimator(const vpRobustEstimatorType method, const vpColVector &residues, vpColVector &weights)
Compute the weights according a residue vector and a PsiFunction.
Definition: vpRobust.cpp:136
void setHarrisFreeParameter(double harris_k)
bool isAppearing(const unsigned int i)
void getCameraParameters(vpCameraParameters &_cam) const
vpMbHiddenFaces< vpMbtPolygon > faces
Set of faces describing the object.
Definition: vpMbTracker.h:135
bool Reinit
Indicates if the line has to be reinitialized.
void setMeanWeight2(const double wmean)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpERROR_TRACE
Definition: vpDebug.h:395
#define vpTRACE
Definition: vpDebug.h:418
unsigned int scaleLevel
Current scale level used. This attribute must not be modified outside of the downScale() and upScale(...
std::list< int > Lindex_polygon
Index of the faces which contain the line.
vpMatrix L
The interaction matrix.
Performs search in a given direction(normal) for a given distance(pixels) for a given 'site'...
Definition: vpMeSite.h:76
virtual void initFaceFromCorners(vpMbtPolygon &polygon)
std::vector< std::list< vpMbtDistanceCircle * > > circles
Vector of the tracked circles.
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
void setMaxFeatures(const int maxCount)
vpHomogeneousMatrix cMo
The current pose.
Definition: vpMbTracker.h:112
bool postTracking(const vpImage< unsigned char > &I, vpColVector &w)
unsigned int getBlockSize() const
void displayMovingEdges(const vpImage< unsigned char > &I)
double getNearClippingDistance() const
double thresholdKLT
The threshold used in the robust estimation of KLT.
vpColVector error
The error vector.
void getMe(vpMe &_ecm) const
void setMinDistance(double minDistance)
error that can be emited by ViSP classes.
Definition: vpException.h:76
void computeJTR(const vpMatrix &J, const vpColVector &R, vpMatrix &JTR)
vpMbtMeEllipse * meEllipse
Polygon describing the circle bbox.
Manage a cylinder used in the model-based tracker.
Contains predetermined masks for sites and holds moving edges tracking parameters.
Definition: vpMe.h:70
unsigned int getMaxFeatures() const
Manage the line of a polygon used in the model-based tracker.
unsigned int nbFeature
The number of moving edges.
virtual void setClipping(const unsigned int &flags)
std::vector< const vpImage< unsigned char > * > Ipyramid
Pyramid of image associated to the current image. This pyramid is computed in the init() and in the t...
bool useOgre
Use Ogre3d for visibility tests.
Definition: vpMbTracker.h:147
virtual void setCameraParameters(const vpCameraParameters &camera)
void reInitModel(const vpImage< unsigned char > &I, const std::string &cad_name, const vpHomogeneousMatrix &cMo_, const bool verbose=false)
Definition of the vpSubMatrix vpSubMatrix class provides a mask on a vpMatrix all properties of vpMat...
Definition: vpSubMatrix.h:66
bool computeCovariance
Flag used to specify if the covariance matrix has to be computed or not.
Definition: vpMbTracker.h:124
vpMe me
The moving edges parameters.
void updateMovingEdge(const vpImage< unsigned char > &I)
std::vector< std::list< vpMbtDistanceLine * > > lines
Vector of list of all the lines tracked (each line is linked to a list of moving edges). Each element of the vector is for a scale (element 0 = level 0 = no subsampling).
virtual void reinit(const vpImage< unsigned char > &I)
virtual void setFarClippingDistance(const double &dist)
unsigned int nbFeature
The number of moving edges.
void downScale(const unsigned int _scale)
Implementation of a polygon of the model containing points of interest. It is used by the model-based...
bool hasNearClippingDistance() const
Class that defines what is a point.
Definition: vpPoint.h:65
vpMatrix L
The interaction matrix.
vpMeSiteState getState() const
Definition: vpMeSite.h:202
vpCameraParameters cam
The camera parameters.
Definition: vpMbTracker.h:110
void setQuality(double qualityLevel)
virtual void init(const vpImage< unsigned char > &I)
vpHomogeneousMatrix ctTc0
The estimated displacement of the pose between the current instant and the initial position...
double getFarClippingDistance() const
unsigned int nbFeaturel1
The number of moving edges on line 1.
bool hasFarClippingDistance() const
This class aims to compute the homography wrt.two images.
Definition: vpHomography.h:178
vpAROgre * getOgreContext()
void reInitModel(const vpImage< unsigned char > &I, const std::string &cad_name, const vpHomogeneousMatrix &cMo_, const bool verbose=false)
void setPyramidLevels(const unsigned int &pL)
Manage a circle used in the model-based tracker.
vpMatrix oJo
The Degrees of Freedom to estimate.
Definition: vpMbTracker.h:114
std::vector< bool > scales
Vector of scale level to use for the multi-scale tracking.
bool Reinit
Indicates if the circle has to be reinitialized.
unsigned int getWindowSize() const
Error that can be emited by the vpTracker class and its derivates.
double getAngleDisappear() const
Implementation of a polygon of the model used by the model-based tracker.
Definition: vpMbtPolygon.h:67
void initMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &_cMo)
vpMatrix AtA() const
Definition: vpMatrix.cpp:1479
void displayPrimitive(const vpImage< unsigned char > &_I)
void setWindowSize(const unsigned int &w)
vpVelocityTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static double sqr(double x)
Definition: vpMath.h:106
void setAngleDisappear(const double &adisappear)
vpColVector error
The error vector.
virtual void initFaceFromCorners(vpMbtPolygon &polygon)
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)
vpColVector m_error
Error s-s*.
Definition: vpMbTracker.h:132
bool isVisible() const
virtual void track(const vpImage< unsigned char > &I)
Definition of the vpSubColVector vpSubColVector class provides a mask on a vpColVector all properties...
unsigned int maskBorder
Erosion of the mask.
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)
Generic class defining intrinsic camera parameters.
void computeInteractionMatrixError(const vpHomogeneousMatrix &cMo)
void initPyramid(const vpImage< unsigned char > &_I, std::vector< const vpImage< unsigned char > * > &_pyramid)
void cleanPyramid(std::vector< const vpImage< unsigned char > * > &_pyramid)
vpMbtMeLine * meline1
The moving edge containers (first line of the cylinder)
bool compute_interaction
If true, compute the interaction matrix at each iteration of the minimization. Otherwise, compute it only on the first iteration.
void setHarrisParam(const double &hp)
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void setMinDistance(const double &mD)
void loadConfigFile(const char *configFile)
void setCameraParameters(const vpCameraParameters &_cam)
double angleAppears
Angle used to detect a face appearance.
Definition: vpMbTracker.h:137
vpKltOpencv tracker
Points tracker.
virtual bool isVisible(const vpHomogeneousMatrix &cMo, const double alpha, const bool &modulo=false, const vpCameraParameters &cam=vpCameraParameters(), const vpImage< unsigned char > &I=vpImage< unsigned char >())
void stackMatrices(const vpMatrix &A)
Definition: vpMatrix.cpp:3272
void setPyramidLevels(const int pyrMaxLevel)
void setState(const vpMeSiteState &flag)
Definition: vpMeSite.h:189
void reinitMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &_cMo)
void displayMovingEdges(const vpImage< unsigned char > &I)
double get_px() const
static double rad(double deg)
Definition: vpMath.h:100
void setMeanWeight1(const double wmean)
virtual void setCameraParameters(const vpCameraParameters &cam)
virtual void setMaxIter(const unsigned int max)
void diag(const vpColVector &A)
Definition: vpMatrix.cpp:2841
void preTracking(const vpImage< unsigned char > &I, unsigned int &nbInfos, unsigned int &nbFaceUsed)
void computeCovarianceMatrix(const vpHomogeneousMatrix &cMoPrev, const vpColVector &deltaS, const vpMatrix &Ls, const vpMatrix &W)
virtual void initFaceFromLines(vpMbtPolygon &polygon)
unsigned int maxIter
The maximum iteration of the virtual visual servoing stage.
void setMaxFeatures(const unsigned int &mF)
void computeHomography(const vpHomogeneousMatrix &_cTc0, vpHomography &cHc0)
virtual void initCylinder(const vpPoint &p1, const vpPoint &p2, const double radius, const int idFace=0, const std::string &name="")
bool closeToImageBorder(const vpImage< unsigned char > &I, const unsigned int threshold)
void setWindowSize(const int winSize)
double getQuality() const
static double deg(double rad)
Definition: vpMath.h:93
bool displayFeatures
If true, the features are displayed.
Definition: vpMbTracker.h:128
virtual void initCylinder(const vpPoint &, const vpPoint &, const double r, const int idFace, const std::string &name="")
void setCameraParameters(const vpCameraParameters &cam)
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
std::vector< std::list< vpMbtDistanceCylinder * > > cylinders
Vector of the tracked cylinders.
void setAngleAppear(const double &aappear)
void computeInteractionMatrixError(const vpHomogeneousMatrix &cMo)
virtual void initCircle(const vpPoint &p1, const vpPoint &p2, const vpPoint &p3, const double radius, const int idFace=0, const std::string &name="")
unsigned int initMbtTracking(const unsigned int level=0)
vpHomogeneousMatrix inverse() const
unsigned int trackFirstLoop(const vpImage< unsigned char > &I, vpColVector &factor, const unsigned int lvl=0)
vpMbHiddenFaces< vpMbtPolygon > * hiddenface
Pointer to the list of faces.
void setBlockSize(const int blockSize)
static vpHomogeneousMatrix direct(const vpColVector &v)
void setMeanWeight(const double _wmean)
Contains an M-Estimator and various influence function.
Definition: vpRobust.h:63
double angleDisappears
Angle used to detect a face disappearance.
Definition: vpMbTracker.h:139
virtual void initFaceFromLines(vpMbtPolygon &polygon)
vpMbtPolygon * polygon
Pointer to the polygon that define a face.
bool postTracking(const vpImage< unsigned char > &I, vpColVector &w_mbt, vpColVector &w_klt, const unsigned int lvl=0)
void setMeanWeight(const double w_mean)
vpColVector error
The error vector.
vpMatrix pseudoInverse(double svThreshold=1e-6) const
Compute the pseudo inverse of the matrix using the SVD.
Definition: vpMatrix.cpp:1932
bool Reinit
Indicates if the line has to be reinitialized.
virtual void init(const vpImage< unsigned char > &I)
void setBlockSize(const unsigned int &bs)
unsigned int clippingFlag
Flags specifying which clipping to used.
Definition: vpMbTracker.h:145
void trackMovingEdge(const vpImage< unsigned char > &I)
void displayOgre(const vpHomogeneousMatrix &cMo)
double getAngleAppear() const
unsigned int nbFeaturel2
The number of moving edges on line 2.
virtual void initFaceFromLines(vpMbtPolygon &polygon)
unsigned int getCurrentNumberPoints() const
void setThreshold(const double noise_threshold)
Definition: vpRobust.h:128
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:161
double getMinDistance() const
void resize(unsigned int n_data)
Resize containers for sort methods.
Definition: vpRobust.cpp:88
vpMbtMeLine * meline2
The moving edge containers (second line of the cylinder)
bool getFovClipping() const
void setIteration(const unsigned int iter)
Set iteration.
Definition: vpRobust.h:122
vpMatrix L
The interaction matrix.
void computeVVS(const unsigned int &nbInfos, vpColVector &w)
void computeInteractionMatrixError(const vpHomogeneousMatrix &cMo, const vpImage< unsigned char > &I)
void setMovingEdge(const vpMe &_ecm)
bool isoJoIdentity
Boolean to know if oJo is identity (for fast computation)
Definition: vpMbTracker.h:116
void setMaskBorder(const unsigned int &mb)
void computeInteractionMatrixAndResidu(vpColVector &_R, vpMatrix &_J)
vpMbtMeLine * meline
The moving edge container.
unsigned int getPyramidLevels() const
double thresholdMBT
The threshold used in the robust estimation of MBT.
virtual void setNearClippingDistance(const double &dist)
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:98
vpColVector m_w
Weights used in the robust scheme.
Definition: vpMbTracker.h:130
void computeVVS(const vpImage< unsigned char > &I, const unsigned int &nbInfos, vpColVector &w_mbt, vpColVector &w_klt, const unsigned int lvl=0)