Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpMbKltMultiTracker.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2016 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://www.irisa.fr/lagadic/visp/visp.html 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  * http://www.irisa.fr/lagadic
24  *
25  * If you have questions regarding the use of this file, please contact
26  * INRIA at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  *
32  * Description:
33  * Model-based klt tracker with multiple cameras.
34  *
35  * Authors:
36  * Souriya Trinh
37  *
38  *****************************************************************************/
39 
45 #include <visp3/core/vpConfig.h>
46 
47 #if (defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100))
48 
49 #include <visp3/core/vpTrackingException.h>
50 #include <visp3/core/vpVelocityTwistMatrix.h>
51 #include <visp3/mbt/vpMbKltMultiTracker.h>
52 
53 
57 vpMbKltMultiTracker::vpMbKltMultiTracker() : m_mapOfCameraTransformationMatrix(), m_mapOfKltTrackers(),
58  m_referenceCameraName("Camera") {
59  m_mapOfKltTrackers["Camera"] = new vpMbKltTracker();
60 
61  //Add default camera transformation matrix
63 }
64 
70 vpMbKltMultiTracker::vpMbKltMultiTracker(const unsigned int nbCameras) : m_mapOfCameraTransformationMatrix(),
71  m_mapOfKltTrackers(), m_referenceCameraName("Camera") {
72 
73  if(nbCameras == 0) {
74  throw vpException(vpTrackingException::fatalError, "Cannot construct a vpMbkltMultiTracker with no camera !");
75  } else if(nbCameras == 1) {
76  m_mapOfKltTrackers["Camera"] = new vpMbKltTracker();
77 
78  //Add default camera transformation matrix
80  } else if(nbCameras == 2) {
81  m_mapOfKltTrackers["Camera1"] = new vpMbKltTracker();
82  m_mapOfKltTrackers["Camera2"] = new vpMbKltTracker();
83 
84  //Add default camera transformation matrix
87 
88  //Set by default the reference camera
89  m_referenceCameraName = "Camera1";
90  } else {
91  for(unsigned int i = 1; i <= nbCameras; i++) {
92  std::stringstream ss;
93  ss << "Camera" << i;
94  m_mapOfKltTrackers[ss.str()] = new vpMbKltTracker();
95 
96  //Add default camera transformation matrix
98  }
99 
100  //Set by default the reference camera to the first one
101  m_referenceCameraName = m_mapOfKltTrackers.begin()->first;
102  }
103 }
104 
110 vpMbKltMultiTracker::vpMbKltMultiTracker(const std::vector<std::string> &cameraNames) : m_mapOfCameraTransformationMatrix(),
111  m_mapOfKltTrackers(), m_referenceCameraName("Camera") {
112  if(cameraNames.empty()) {
113  throw vpException(vpTrackingException::fatalError, "Cannot construct a vpMbKltMultiTracker with no camera !");
114  }
115 
116  for(std::vector<std::string>::const_iterator it = cameraNames.begin(); it != cameraNames.end(); ++it) {
117  m_mapOfKltTrackers[*it] = new vpMbKltTracker();
118  }
119 
120  //Set by default the reference camera
121  m_referenceCameraName = cameraNames.front();
122 }
123 
128  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
129  it != m_mapOfKltTrackers.end(); ++it) {
130  delete it->second;
131  }
132 }
133 
143 void
144 vpMbKltMultiTracker::addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, const double r, const std::string &name)
145 {
146  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
147  it != m_mapOfKltTrackers.end(); ++it) {
148  it->second->addCircle(P1, P2, P3, r, name);
149  }
150 }
151 
152 void vpMbKltMultiTracker::computeVVS(std::map<std::string, unsigned int> &mapOfNbInfos, vpColVector &w) {
153  vpMatrix L; // interaction matrix
154  vpColVector R; // residu
155  vpMatrix L_true; // interaction matrix
156  vpMatrix LVJ_true;
157  //vpColVector R_true; // residu
158  vpColVector w_true;
159  vpColVector v; // "speed" for VVS
160 
161  unsigned int nbInfos = 0;
162  for(std::map<std::string, unsigned int>::const_iterator it = mapOfNbInfos.begin(); it != mapOfNbInfos.end(); ++it) {
163  nbInfos += it->second;
164  }
165 
166  std::map<std::string, vpRobust> mapOfRobusts;
167 
168  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
169  it != m_mapOfKltTrackers.end(); ++it) {
170  mapOfRobusts[it->first] = vpRobust(2*mapOfNbInfos[it->first]);
171  }
172 
173  vpMatrix LTL;
174  vpColVector LTR;
175  vpHomogeneousMatrix cMoPrev;
176  vpHomogeneousMatrix ctTc0_Prev;
177  vpColVector error_prev(2*nbInfos);
178  double mu = 0.01;
179 
180  double normRes = 0;
181  double normRes_1 = -1;
182  unsigned int iter = 0;
183 
184  std::map<std::string, vpVelocityTwistMatrix> mapOfVelocityTwist;
185  for(std::map<std::string, vpHomogeneousMatrix>::const_iterator it = m_mapOfCameraTransformationMatrix.begin();
186  it != m_mapOfCameraTransformationMatrix.end(); ++it) {
188  cVo.buildFrom(it->second);
189  mapOfVelocityTwist[it->first] = cVo;
190  }
191 
192  while( ((int)((normRes - normRes_1)*1e8) != 0 ) && (iter<maxIter) ) {
193  L.resize(0,0);
194  R.resize(0);
195 
196  for(std::map<std::string, vpMbKltTracker*>::const_iterator it1 = m_mapOfKltTrackers.begin();
197  it1 != m_mapOfKltTrackers.end(); ++it1) {
198  unsigned int shift = 0;
199  vpColVector R_current; // residu
200  vpMatrix L_current; // interaction matrix
201  vpHomography H_current;
202 
203  R_current.resize(2 * mapOfNbInfos[it1->first]);
204  L_current.resize(2 * mapOfNbInfos[it1->first], 6, 0);
205 
206  //Use the ctTc0 variable instead of the formula in the monocular case
207  //to ensure that we have the same result than vpMbKltTracker
208  //as some slight differences can occur due to numerical imprecision
209  if(m_mapOfKltTrackers.size() == 1) {
210  computeVVSInteractionMatrixAndResidu(shift, R_current, L_current, H_current,
211  it1->second->kltPolygons, it1->second->kltCylinders, ctTc0);
212  } else {
213  vpHomogeneousMatrix c_curr_tTc_curr0 = m_mapOfCameraTransformationMatrix[it1->first] *
214  cMo * it1->second->c0Mo.inverse();
215  computeVVSInteractionMatrixAndResidu(shift, R_current, L_current, H_current,
216  it1->second->kltPolygons, it1->second->kltCylinders, c_curr_tTc_curr0);
217  }
218 
219  //VelocityTwistMatrix
220  L_current = L_current*mapOfVelocityTwist[it1->first];
221 
222  //Stack residu and interaction matrix
223  R.stack(R_current);
224  L.stack(L_current);
225  }
226 
227  bool reStartFromLastIncrement = false;
228  computeVVSCheckLevenbergMarquardtKlt(iter, nbInfos, cMoPrev, error_prev, ctTc0_Prev, mu, reStartFromLastIncrement);
229 
230  if(!reStartFromLastIncrement) {
231  vpMbKltMultiTracker::computeVVSWeights(iter, nbInfos, mapOfNbInfos, R, w_true, w, mapOfRobusts, 2.0);
232 
233  computeVVSPoseEstimation(iter, L, w, L_true, LVJ_true, normRes, normRes_1, w_true, R, LTL, LTR,
234  error_prev, v, mu, cMoPrev, ctTc0_Prev);
235  } // endif(!reStartFromLastIncrement)
236 
237  iter++;
238  }
239 
240  computeVVSCovariance(w_true, cMoPrev, L_true, LVJ_true);
241 }
242 
243 void vpMbKltMultiTracker::computeVVSWeights(const unsigned int iter, const unsigned int nbInfos,
244  std::map<std::string, unsigned int> &mapOfNbInfos, vpColVector &R, vpColVector &w_true, vpColVector &w,
245  std::map<std::string, vpRobust> &mapOfRobusts, double threshold) {
246  /* robust */
247  if(iter == 0) {
248  w_true.resize(2*nbInfos);
249  w.resize(2*nbInfos);
250  w = 1;
251  w_true = 1;
252 
253  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
254  it != m_mapOfKltTrackers.end(); ++it) {
255  vpCameraParameters camTmp;
256  it->second->getCameraParameters(camTmp);
257  mapOfRobusts[it->first].setThreshold(threshold / camTmp.get_px());
258  }
259  }
260 
261  unsigned int shift = 0;
262  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
263  it != m_mapOfKltTrackers.end(); ++it) {
264  if(mapOfNbInfos[it->first] > 0) {
265  vpSubColVector sub_w(w, shift, 2*mapOfNbInfos[it->first]);
266  vpSubColVector sub_r(R, shift, 2*mapOfNbInfos[it->first]);
267  shift += 2*mapOfNbInfos[it->first];
268 
269  mapOfRobusts[it->first].setIteration(iter);
270  mapOfRobusts[it->first].MEstimator(vpRobust::TUKEY, sub_r, sub_w);
271  }
272  }
273 }
274 
286  const vpCameraParameters &cam_, const vpColor& col , const unsigned int thickness, const bool displayFullModel) {
287  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
288  if(it != m_mapOfKltTrackers.end()) {
289  it->second->display(I, cMo_, cam_, col, thickness, displayFullModel);
290  } else {
291  std::cerr << "Cannot find reference camera:" << m_referenceCameraName << " !" << std::endl;
292  }
293 }
294 
306  const vpColor& color , const unsigned int thickness, const bool displayFullModel) {
307  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
308  if(it != m_mapOfKltTrackers.end()) {
309  it->second->display(I, cMo_, cam_, color, thickness, displayFullModel);
310  } else {
311  std::cerr << "Cannot find reference camera:" << m_referenceCameraName << " !" << std::endl;
312  }
313 }
314 
329  const vpHomogeneousMatrix &c1Mo, const vpHomogeneousMatrix &c2Mo, const vpCameraParameters &cam1,
330  const vpCameraParameters &cam2, const vpColor& color, const unsigned int thickness, const bool displayFullModel) {
331  if(m_mapOfKltTrackers.size() == 2) {
332  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
333  it->second->display(I1, c1Mo, cam1, color, thickness, displayFullModel);
334  ++it;
335 
336  it->second->display(I2, c2Mo, cam2, color, thickness, displayFullModel);
337  } else {
338  std::cerr << "This display is only for the stereo case ! There are "
339  << m_mapOfKltTrackers.size() << " cameras !" << std::endl;
340  }
341 }
342 
357  const vpHomogeneousMatrix &c1Mo, const vpHomogeneousMatrix &c2Mo, const vpCameraParameters &cam1,
358  const vpCameraParameters &cam2, const vpColor& color, const unsigned int thickness, const bool displayFullModel) {
359  if(m_mapOfKltTrackers.size() == 2) {
360  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
361  it->second->display(I1, c1Mo, cam1, color, thickness, displayFullModel);
362  ++it;
363 
364  it->second->display(I2, c2Mo, cam2, color, thickness, displayFullModel);
365  } else {
366  std::cerr << "This display is only for the stereo case ! There are "
367  << m_mapOfKltTrackers.size() << " cameras !" << std::endl;
368  }
369 }
370 
381 void vpMbKltMultiTracker::display(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
382  const std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses,
383  const std::map<std::string, vpCameraParameters> &mapOfCameraParameters,
384  const vpColor& col, const unsigned int thickness, const bool displayFullModel) {
385 
386  //Display only for the given images
387  for(std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img = mapOfImages.begin();
388  it_img != mapOfImages.end(); ++it_img) {
389  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(it_img->first);
390  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camPose = mapOfCameraPoses.find(it_img->first);
391  std::map<std::string, vpCameraParameters>::const_iterator it_cam = mapOfCameraParameters.find(it_img->first);
392 
393  if(it_klt != m_mapOfKltTrackers.end() && it_camPose != mapOfCameraPoses.end() && it_cam != mapOfCameraParameters.end()) {
394  it_klt->second->display(*it_img->second, it_camPose->second, it_cam->second, col, thickness, displayFullModel);
395  } else {
396  std::cerr << "Missing elements ! " << std::endl;
397  }
398  }
399 }
400 
411 void vpMbKltMultiTracker::display(const std::map<std::string, const vpImage<vpRGBa> *> &mapOfImages,
412  const std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses,
413  const std::map<std::string, vpCameraParameters> &mapOfCameraParameters,
414  const vpColor& col, const unsigned int thickness, const bool displayFullModel) {
415 
416  //Display only for the given images
417  for(std::map<std::string, const vpImage<vpRGBa> *>::const_iterator it_img = mapOfImages.begin();
418  it_img != mapOfImages.end(); ++it_img) {
419  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(it_img->first);
420  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camPose = mapOfCameraPoses.find(it_img->first);
421  std::map<std::string, vpCameraParameters>::const_iterator it_cam = mapOfCameraParameters.find(it_img->first);
422 
423  if(it_klt != m_mapOfKltTrackers.end() && it_camPose != mapOfCameraPoses.end() && it_cam != mapOfCameraParameters.end()) {
424  it_klt->second->display(*it_img->second, it_camPose->second, it_cam->second, col, thickness, displayFullModel);
425  } else {
426  std::cerr << "Missing elements ! " << std::endl;
427  }
428  }
429 }
430 
436 std::vector<std::string> vpMbKltMultiTracker::getCameraNames() const {
437  std::vector<std::string> cameraNames;
438 
439  for(std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.begin();
440  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
441  cameraNames.push_back(it_klt->first);
442  }
443 
444  return cameraNames;
445 }
446 
453  //Get the reference camera parameters
454  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
455  if(it != m_mapOfKltTrackers.end()) {
456  it->second->getCameraParameters(camera);
457  } else {
458  std::cerr << "The reference camera name: " << m_referenceCameraName << " does not exist !" << std::endl;
459  }
460 }
461 
469  if(m_mapOfKltTrackers.size() == 2) {
470  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
471  it->second->getCameraParameters(cam1);
472  ++it;
473 
474  it->second->getCameraParameters(cam2);
475  } else {
476  std::cerr << "Problem with the number of cameras ! There are "
477  << m_mapOfKltTrackers.size() << " cameras !" << std::endl;
478  }
479 }
480 
487 void vpMbKltMultiTracker::getCameraParameters(const std::string &cameraName, vpCameraParameters &camera) const {
488  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
489  if(it != m_mapOfKltTrackers.end()) {
490  it->second->getCameraParameters(camera);
491  } else {
492  std::cerr << "The camera: " << cameraName << " does not exist !" << std::endl;
493  }
494 }
495 
501 void vpMbKltMultiTracker::getCameraParameters(std::map<std::string, vpCameraParameters> &mapOfCameraParameters) const {
502  //Clear the input map
503  mapOfCameraParameters.clear();
504 
505  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
506  it != m_mapOfKltTrackers.end(); ++it) {
507  vpCameraParameters cam_;
508  it->second->getCameraParameters(cam_);
509  mapOfCameraParameters[it->first] = cam_;
510  }
511 }
512 
519 unsigned int vpMbKltMultiTracker::getClipping(const std::string &cameraName) const {
520  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
521  if(it != m_mapOfKltTrackers.end()) {
522  return it->second->getClipping();
523  } else {
524  std::cerr << "Cannot find camera: " << cameraName << std::endl;
525  }
526 
527  return vpMbTracker::getClipping();
528 }
529 
536  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
537  if(it != m_mapOfKltTrackers.end()) {
538  return it->second->getFaces();
539  }
540 
541  std::cerr << "The reference camera: " << m_referenceCameraName << " cannot be found !" << std::endl;
542  return faces;
543 }
544 
551  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
552  if(it != m_mapOfKltTrackers.end()) {
553  return it->second->getFaces();
554  }
555 
556  std::cerr << "The camera: " << cameraName << " cannot be found !" << std::endl;
557  return faces;
558 }
559 
565 std::map<std::string, vpMbHiddenFaces<vpMbtPolygon> > vpMbKltMultiTracker::getFaces() const {
566  std::map<std::string, vpMbHiddenFaces<vpMbtPolygon> > mapOfFaces;
567  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
568  it != m_mapOfKltTrackers.end(); ++it) {
569  mapOfFaces[it->first] = it->second->faces;
570  }
571 
572  return mapOfFaces;
573 }
574 
580 std::list<vpMbtDistanceCircle*>& vpMbKltMultiTracker::getFeaturesCircle() {
581  std::map<std::string, vpMbKltTracker*>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
582 
583  if(it_klt != m_mapOfKltTrackers.end()) {
584  return it_klt->second->getFeaturesCircle();
585  } else {
586  std::cerr << "Cannot find reference camera: " << m_referenceCameraName << " !" << std::endl;
587  }
588 
589  return circles_disp;
590 }
591 
598 std::list<vpMbtDistanceCircle*>& vpMbKltMultiTracker::getFeaturesCircle(const std::string &cameraName) {
599  std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
600 
601  if(it != m_mapOfKltTrackers.end()) {
602  return it->second->getFeaturesCircle();
603  } else {
604  std::cerr << "The camera: " << cameraName << " does not exist !";
605  }
606 
607  return circles_disp;
608 }
609 
616 std::list<vpMbtDistanceKltPoints*>& vpMbKltMultiTracker::getFeaturesKlt() {
617  std::map<std::string, vpMbKltTracker*>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
618 
619  if(it_klt != m_mapOfKltTrackers.end()) {
620  return it_klt->second->getFeaturesKlt();
621  } else {
622  std::cerr << "Cannot find reference camera: " << m_referenceCameraName << " !" << std::endl;
623  }
624 
625  return kltPolygons;
626 }
627 
635 std::list<vpMbtDistanceKltPoints*>& vpMbKltMultiTracker::getFeaturesKlt(const std::string &cameraName) {
636  std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
637 
638  if(it != m_mapOfKltTrackers.end()) {
639  return it->second->getFeaturesKlt();
640  } else {
641  std::cerr << "The camera: " << cameraName << " does not exist !";
642  }
643 
644  return kltPolygons;
645 }
646 
652 std::list<vpMbtDistanceKltCylinder*>& vpMbKltMultiTracker::getFeaturesKltCylinder() {
653  std::map<std::string, vpMbKltTracker*>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
654 
655  if(it_klt != m_mapOfKltTrackers.end()) {
656  return it_klt->second->getFeaturesKltCylinder();
657  } else {
658  std::cerr << "Cannot find reference camera: " << m_referenceCameraName << " !" << std::endl;
659  }
660 
661  return kltCylinders;
662 }
663 
670 std::list<vpMbtDistanceKltCylinder*>& vpMbKltMultiTracker::getFeaturesKltCylinder(const std::string &cameraName) {
671  std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
672 
673  if(it != m_mapOfKltTrackers.end()) {
674  return it->second->getFeaturesKltCylinder();
675  } else {
676  std::cerr << "The camera: " << cameraName << " does not exist !";
677  }
678 
679  return kltCylinders;
680 }
681 
690 std::map<std::string, std::vector<vpImagePoint> > vpMbKltMultiTracker::getKltImagePoints() const {
691  std::map<std::string, std::vector<vpImagePoint> > mapOfFeatures;
692 
693  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
694  it != m_mapOfKltTrackers.end(); ++it) {
695  mapOfFeatures[it->first] = it->second->getKltImagePoints();
696  }
697 
698  return mapOfFeatures;
699 }
700 
709 std::map<std::string, std::map<int, vpImagePoint> > vpMbKltMultiTracker::getKltImagePointsWithId() const {
710  std::map<std::string, std::map<int, vpImagePoint> > mapOfFeatures;
711 
712  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
713  it != m_mapOfKltTrackers.end(); ++it) {
714  mapOfFeatures[it->first] = it->second->getKltImagePointsWithId();
715  }
716 
717  return mapOfFeatures;
718 }
719 
725 std::map<std::string, vpKltOpencv> vpMbKltMultiTracker::getKltOpencv() const {
726  std::map<std::string, vpKltOpencv> mapOfKltOpenCVTracker;
727 
728  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
729  it != m_mapOfKltTrackers.end(); ++it) {
730  mapOfKltOpenCVTracker[it->first] = it->second->getKltOpencv();
731  }
732 
733  return mapOfKltOpenCVTracker;
734 }
735 
741 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
742 std::map<std::string, std::vector<cv::Point2f> > vpMbKltMultiTracker::getKltPoints() const {
743  std::map<std::string, std::vector<cv::Point2f> > mapOfFeatures;
744 
745  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
746  it != m_mapOfKltTrackers.end(); ++it) {
747  mapOfFeatures[it->first] = it->second->getKltPoints();
748  }
749 
750  return mapOfFeatures;
751 }
752 #else
753 std::map<std::string, CvPoint2D32f*> vpMbKltMultiTracker::getKltPoints() {
754  std::map<std::string, CvPoint2D32f*> mapOfFeatures;
755 
756  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
757  it != m_mapOfKltTrackers.end(); ++it) {
758  mapOfFeatures[it->first] = it->second->getKltPoints();
759  }
760 
761  return mapOfFeatures;
762 }
763 #endif
764 
770 std::map<std::string, int> vpMbKltMultiTracker::getNbKltPoints() const {
771  std::map<std::string, int> mapOfFeatures;
772 
773  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
774  it != m_mapOfKltTrackers.end(); ++it) {
775  mapOfFeatures[it->first] = it->second->getNbKltPoints();
776  }
777 
778  return mapOfFeatures;
779 }
780 
786 unsigned int vpMbKltMultiTracker::getNbPolygon() const {
787  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
788  if(it != m_mapOfKltTrackers.end()) {
789  return it->second->getNbPolygon();
790  }
791 
792  std::cerr << "The reference camera: " << m_referenceCameraName << " cannot be found !" << std::endl;
793  return 0;
794 }
795 
801 std::map<std::string, unsigned int> vpMbKltMultiTracker::getMultiNbPolygon() const {
802  std::map<std::string, unsigned int> mapOfNbPolygons;
803  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
804  it != m_mapOfKltTrackers.end(); ++it) {
805  mapOfNbPolygons[it->first] = it->second->getNbPolygon();
806  }
807 
808  return mapOfNbPolygons;
809 }
810 
818  if(m_mapOfKltTrackers.size() == 2) {
819  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
820  it->second->getPose(c1Mo);
821  ++it;
822 
823  it->second->getPose(c2Mo);
824  } else {
825  std::cerr << "Require two cameras ! There are "
826  << m_mapOfKltTrackers.size() << " cameras !" << std::endl;
827  }
828 }
829 
838 void vpMbKltMultiTracker::getPose(const std::string &cameraName, vpHomogeneousMatrix &cMo_) const {
839  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
840  if(it != m_mapOfKltTrackers.end()) {
841  it->second->getPose(cMo_);
842  } else {
843  std::cerr << "The camera: " << cameraName << " does not exist !" << std::endl;
844  }
845 }
846 
852 void vpMbKltMultiTracker::getPose(std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses) const {
853  //Clear the map
854  mapOfCameraPoses.clear();
855 
856  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
857  it != m_mapOfKltTrackers.end(); ++it) {
858  vpHomogeneousMatrix cMo_;
859  it->second->getPose(cMo_);
860  mapOfCameraPoses[it->first] = cMo_;
861  }
862 }
863 
865 }
866 
867 #ifdef VISP_HAVE_MODULE_GUI
868 
877 void vpMbKltMultiTracker::initClick(const vpImage<unsigned char>& I, const std::vector<vpPoint> &points3D_list,
878  const std::string &displayFile) {
879  if(m_mapOfKltTrackers.empty()) {
880  throw vpException(vpTrackingException::initializationError, "There is no camera !");
881  } else if(m_mapOfKltTrackers.size() > 1) {
882  throw vpException(vpTrackingException::initializationError, "There is more than one camera !");
883  } else {
884  //Get the vpMbKltTracker object for the reference camera name
885  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
886  if(it != m_mapOfKltTrackers.end()) {
887  it->second->initClick(I, points3D_list, displayFile);
888  it->second->getPose(cMo);
889 
890  //Init c0Mo
891  c0Mo = cMo;
892  ctTc0.eye();
893  } else {
894  std::stringstream ss;
895  ss << "Cannot initClick as the reference camera: " << m_referenceCameraName << " does not exist !";
896  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
897  }
898  }
899 }
900 
921 void vpMbKltMultiTracker::initClick(const vpImage<unsigned char>& I, const std::string& initFile, const bool displayHelp) {
922  if(m_mapOfKltTrackers.empty()) {
923  throw vpException(vpTrackingException::initializationError, "There is no camera !");
924  } else if(m_mapOfKltTrackers.size() > 1) {
925  throw vpException(vpTrackingException::initializationError, "There is more than one camera !");
926  } else {
927  //Get the vpMbKltTracker object for the reference camera name
928  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
929  if(it != m_mapOfKltTrackers.end()) {
930  it->second->initClick(I, initFile, displayHelp);
931  it->second->getPose(cMo);
932 
933  //Init c0Mo
934  c0Mo = cMo;
935  ctTc0.eye();
936  } else {
937  std::stringstream ss;
938  ss << "Cannot initClick as the reference camera: " << m_referenceCameraName << " does not exist !";
939  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
940  }
941  }
942 }
943 
968  const std::string& initFile1, const std::string& initFile2, const bool displayHelp, const bool firstCameraIsReference) {
969  if(m_mapOfKltTrackers.size() == 2) {
970  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
971  it->second->initClick(I1, initFile1, displayHelp);
972 
973  if(firstCameraIsReference) {
974  //Set the reference cMo
975  it->second->getPose(cMo);
976 
977  //Set the reference camera parameters
978  it->second->getCameraParameters(this->cam);
979 
980  //Init c0Mo and ctTc0
981  c0Mo = cMo;
982  ctTc0.eye();
983  }
984 
985  ++it;
986 
987  it->second->initClick(I2, initFile2, displayHelp);
988 
989  if(!firstCameraIsReference) {
990  //Set the reference cMo
991  it->second->getPose(cMo);
992 
993  //Set the reference camera parameters
994  it->second->getCameraParameters(this->cam);
995 
996  //Init c0Mo and ctTc0
997  c0Mo = cMo;
998  ctTc0.eye();
999  }
1000  } else {
1001  std::stringstream ss;
1002  ss << "Cannot init click ! Require two cameras but there are " << m_mapOfKltTrackers.size() << " cameras !";
1003  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1004  }
1005 }
1006 
1028 void vpMbKltMultiTracker::initClick(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1029  const std::string &initFile, const bool displayHelp) {
1030  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1031  if(it_klt != m_mapOfKltTrackers.end()) {
1032  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img = mapOfImages.find(m_referenceCameraName);
1033 
1034  if(it_img != mapOfImages.end()) {
1035  it_klt->second->initClick(*it_img->second, initFile, displayHelp);
1036 
1037  //Set the reference cMo
1038  it_klt->second->getPose(cMo);
1039 
1040  //Init c0Mo
1041  c0Mo = cMo;
1042  ctTc0.eye();
1043 
1044  //Set the pose for the others cameras
1045  for(it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1046  if(it_klt->first != m_referenceCameraName) {
1047  it_img = mapOfImages.find(it_klt->first);
1048  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
1049  m_mapOfCameraTransformationMatrix.find(it_klt->first);
1050 
1051  if(it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
1052  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1053  it_klt->second->cMo = cCurrentMo;
1054  it_klt->second->init(*it_img->second);
1055  } else {
1056  std::stringstream ss;
1057  ss << "Cannot init click ! Missing image for camera: " << m_referenceCameraName << " !";
1058  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1059  }
1060  }
1061  }
1062  } else {
1063  std::stringstream ss;
1064  ss << "Cannot init click ! Missing image for camera: " << m_referenceCameraName << " !";
1065  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1066  }
1067  } else {
1068  std::stringstream ss;
1069  ss << "Cannot init click ! The reference camera: " << m_referenceCameraName << " does not exist !";
1070  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1071  }
1072 }
1073 
1096 void vpMbKltMultiTracker::initClick(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1097  const std::map<std::string, std::string> &mapOfInitFiles, const bool displayHelp) {
1098  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1099  std::map<std::string, const vpImage<unsigned char>* >::const_iterator it_img = mapOfImages.find(m_referenceCameraName);
1100  std::map<std::string, std::string>::const_iterator it_initFile = mapOfInitFiles.find(m_referenceCameraName);
1101 
1102  if(it_klt != m_mapOfKltTrackers.end() && it_img != mapOfImages.end() && it_initFile != mapOfInitFiles.end()) {
1103  it_klt->second->initClick(*it_img->second, it_initFile->second, displayHelp);
1104  it_klt->second->getPose(cMo);
1105 
1106  c0Mo = this->cMo;
1107  ctTc0.eye();
1108  } else {
1109  throw vpException(vpTrackingException::initializationError, "Cannot initClick for the reference camera !");
1110  }
1111 
1112  //Vector of missing initFile for cameras
1113  std::vector<std::string> vectorOfMissingCameraPoses;
1114 
1115  //Set pose for the specified cameras
1116  for(it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1117  if(it_klt->first != m_referenceCameraName) {
1118  it_img = mapOfImages.find(it_klt->first);
1119  it_initFile = mapOfInitFiles.find(it_klt->first);
1120 
1121  if(it_img != mapOfImages.end() && it_initFile != mapOfInitFiles.end()) {
1122  //InitClick for the current camera
1123  it_klt->second->initClick(*it_img->second, it_initFile->second, displayHelp);
1124  } else {
1125  vectorOfMissingCameraPoses.push_back(it_klt->first);
1126  }
1127  }
1128  }
1129 
1130  //Set pose for cameras that do not have an initFile
1131  for(std::vector<std::string>::const_iterator it1 = vectorOfMissingCameraPoses.begin();
1132  it1 != vectorOfMissingCameraPoses.end(); ++it1) {
1133  it_img = mapOfImages.find(*it1);
1134  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans = m_mapOfCameraTransformationMatrix.find(*it1);
1135 
1136  if(it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
1137  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1138  m_mapOfKltTrackers[*it1]->cMo = cCurrentMo;
1139  m_mapOfKltTrackers[*it1]->init(*it_img->second);
1140  } else {
1141  std::stringstream ss;
1142  ss << "Missing image or missing camera transformation matrix ! Cannot set the pose for camera: " << (*it1) << " !";
1143  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1144  }
1145  }
1146 }
1147 #endif //#ifdef VISP_HAVE_MODULE_GUI
1148 
1167 void vpMbKltMultiTracker::initFromPose(const vpImage<unsigned char>& I, const std::string &initFile) {
1168  //Monocular case only !
1169  if(m_mapOfKltTrackers.size() > 1) {
1170  throw vpException(vpTrackingException::fatalError, "This function can only be used for the monocular case !");
1171  }
1172 
1173  char s[FILENAME_MAX];
1174  std::fstream finit ;
1175  vpPoseVector init_pos;
1176 
1177  std::string ext = ".pos";
1178  size_t pos = initFile.rfind(ext);
1179 
1180  if( pos == initFile.size()-ext.size() && pos != 0)
1181  sprintf(s,"%s", initFile.c_str());
1182  else
1183  sprintf(s,"%s.pos", initFile.c_str());
1184 
1185  finit.open(s,std::ios::in) ;
1186  if (finit.fail()){
1187  std::cerr << "cannot read " << s << std::endl;
1188  throw vpException(vpException::ioError, "cannot read init file");
1189  }
1190 
1191  for (unsigned int i = 0; i < 6; i += 1){
1192  finit >> init_pos[i];
1193  }
1194 
1195  //Set the new pose for the reference camera
1196  cMo.buildFrom(init_pos);
1197  c0Mo = cMo;
1198  ctTc0.eye();
1199 
1200  //Init for the reference camera
1201  std::map<std::string, vpMbKltTracker *>::iterator it_ref = m_mapOfKltTrackers.find(m_referenceCameraName);
1202  if(it_ref == m_mapOfKltTrackers.end()) {
1203  throw vpException(vpTrackingException::initializationError, "Cannot find the reference camera !");
1204  }
1205 
1206  it_ref->second->initFromPose(I, cMo);
1207 }
1208 
1216  this->cMo = cMo_;
1217  c0Mo = cMo;
1218  ctTc0.eye();
1219 
1220  //Init for the reference camera
1221  std::map<std::string, vpMbKltTracker *>::iterator it_ref = m_mapOfKltTrackers.find(m_referenceCameraName);
1222  if(it_ref == m_mapOfKltTrackers.end()) {
1223  throw vpException(vpTrackingException::initializationError, "Cannot find the reference camera !");
1224  }
1225 
1226  it_ref->second->initFromPose(I, cMo);
1227 }
1228 
1236  vpHomogeneousMatrix _cMo(cPo);
1237  initFromPose(I, _cMo);
1238 }
1239 
1250  const vpHomogeneousMatrix &c1Mo, const vpHomogeneousMatrix &c2Mo, const bool firstCameraIsReference) {
1251  if(m_mapOfKltTrackers.size() == 2) {
1252  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1253  it->second->initFromPose(I1, c1Mo);
1254 
1255  ++it;
1256 
1257  it->second->initFromPose(I2, c2Mo);
1258 
1259  if(firstCameraIsReference) {
1260  this->cMo = c1Mo;
1261  } else {
1262  this->cMo = c2Mo;
1263  }
1264 
1265  c0Mo = this->cMo;
1266  ctTc0.eye();
1267  } else {
1268  std::stringstream ss;
1269  ss << "This method requires 2 cameras but there are " << m_mapOfKltTrackers.size() << " cameras !";
1271  }
1272 }
1273 
1280 void vpMbKltMultiTracker::initFromPose(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1281  const vpHomogeneousMatrix &cMo_) {
1282  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1283 
1284  if(it_klt != m_mapOfKltTrackers.end()) {
1285  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img = mapOfImages.find(it_klt->first);
1286 
1287  if(it_img != mapOfImages.end()) {
1288  it_klt->second->initFromPose(*it_img->second, cMo_);
1289 
1290  cMo = cMo_;
1291  c0Mo = this->cMo;
1292  ctTc0.eye();
1293 
1294  for(it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1295  if(it_klt->first != m_referenceCameraName) {
1296  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
1297  m_mapOfCameraTransformationMatrix.find(it_klt->first);
1298  it_img = mapOfImages.find(it_klt->first);
1299 
1300  if(it_camTrans != m_mapOfCameraTransformationMatrix.end() && it_img != mapOfImages.end()) {
1301  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1302  it_klt->second->initFromPose(*it_img->second, cCurrentMo);
1303  } else {
1304  throw vpException(vpTrackingException::initializationError, "Cannot find camera transformation matrix or image !");
1305  }
1306  }
1307  }
1308  } else {
1309  throw vpException(vpTrackingException::initializationError, "Cannot find image for reference camera !");
1310  }
1311  } else {
1312  std::stringstream ss;
1313  ss << "Cannot find reference camera: " << m_referenceCameraName << std::endl;
1315  }
1316 }
1317 
1324 void vpMbKltMultiTracker::initFromPose(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1325  const std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses) {
1326  //Set the reference cMo
1327  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1328  std::map<std::string, const vpImage<unsigned char>* >::const_iterator it_img = mapOfImages.find(m_referenceCameraName);
1329  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camPose = mapOfCameraPoses.find(m_referenceCameraName);
1330 
1331  if(it_klt != m_mapOfKltTrackers.end() && it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
1332  it_klt->second->initFromPose(*it_img->second, it_camPose->second);
1333  it_klt->second->getPose(cMo);
1334 
1335  c0Mo = this->cMo;
1336  ctTc0.eye();
1337  } else {
1338  throw vpException(vpTrackingException::initializationError, "Cannot set pose for the reference camera !");
1339  }
1340 
1341  //Vector of missing pose matrices for cameras
1342  std::vector<std::string> vectorOfMissingCameraPoses;
1343 
1344  //Set pose for the specified cameras
1345  for(it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1346  it_img = mapOfImages.find(it_klt->first);
1347  it_camPose = mapOfCameraPoses.find(it_klt->first);
1348 
1349  if(it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
1350  //Set pose
1351  it_klt->second->initFromPose(*it_img->second, it_camPose->second);
1352  } else {
1353  vectorOfMissingCameraPoses.push_back(it_klt->first);
1354  }
1355  }
1356 
1357  for(std::vector<std::string>::const_iterator it1 = vectorOfMissingCameraPoses.begin();
1358  it1 != vectorOfMissingCameraPoses.end(); ++it1) {
1359  it_img = mapOfImages.find(*it1);
1360  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans = m_mapOfCameraTransformationMatrix.find(*it1);
1361 
1362  if(it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
1363  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1364  m_mapOfKltTrackers[*it1]->initFromPose(*it_img->second, cCurrentMo);
1365  } else {
1366  std::stringstream ss;
1367  ss << "Missing image or missing camera transformation matrix ! Cannot set the pose for camera: " << (*it1) << " !";
1368  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1369  }
1370  }
1371 }
1372 
1419 void vpMbKltMultiTracker::loadConfigFile(const std::string &configFile) {
1420  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
1421  if(it != m_mapOfKltTrackers.end()) {
1422  //Load ConfigFile for reference camera
1423  it->second->loadConfigFile(configFile);
1424  it->second->getCameraParameters(cam);
1425 
1426  //Set clipping
1427  this->clippingFlag = it->second->getClipping();
1428  this->angleAppears = it->second->getAngleAppear();
1429  this->angleDisappears = it->second->getAngleDisappear();
1430  } else {
1431  std::stringstream ss;
1432  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
1433  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1434  }
1435 }
1436 
1451 void vpMbKltMultiTracker::loadConfigFile(const std::string& configFile1, const std::string& configFile2,
1452  const bool firstCameraIsReference) {
1453  if(m_mapOfKltTrackers.size() == 2) {
1454  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1455  it->second->loadConfigFile(configFile1);
1456 
1457  if(firstCameraIsReference) {
1458  it->second->getCameraParameters(cam);
1459 
1460  //Set clipping
1461  this->clippingFlag = it->second->getClipping();
1462  this->angleAppears = it->second->getAngleAppear();
1463  this->angleDisappears = it->second->getAngleDisappear();
1464  }
1465  ++it;
1466 
1467  it->second->loadConfigFile(configFile2);
1468 
1469  if(!firstCameraIsReference) {
1470  it->second->getCameraParameters(cam);
1471 
1472  //Set clipping
1473  this->clippingFlag = it->second->getClipping();
1474  this->angleAppears = it->second->getAngleAppear();
1475  this->angleDisappears = it->second->getAngleDisappear();
1476  }
1477  } else {
1478  std::stringstream ss;
1479  ss << "Cannot loadConfigFile. Require two cameras ! There are " << m_mapOfKltTrackers.size() << " cameras !";
1480  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1481  }
1482 }
1483 
1496 void vpMbKltMultiTracker::loadConfigFile(const std::map<std::string, std::string> &mapOfConfigFiles) {
1497  for(std::map<std::string, vpMbKltTracker*>::const_iterator it_klt = m_mapOfKltTrackers.begin();
1498  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1499  std::map<std::string, std::string>::const_iterator it_config = mapOfConfigFiles.find(it_klt->first);
1500  if(it_config != mapOfConfigFiles.end()) {
1501  it_klt->second->loadConfigFile(it_config->second);
1502  } else {
1503  std::stringstream ss;
1504  ss << "Missing configuration file for camera: " << it_klt->first << " !";
1505  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1506  }
1507  }
1508 
1509  //Set the reference camera parameters
1510  std::map<std::string, vpMbKltTracker *>::iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
1511  if(it != m_mapOfKltTrackers.end()) {
1512  it->second->getCameraParameters(cam);
1513 
1514  //Set clipping
1515  this->clippingFlag = it->second->getClipping();
1516  this->angleAppears = it->second->getAngleAppear();
1517  this->angleDisappears = it->second->getAngleDisappear();
1518  } else {
1519  std::stringstream ss;
1520  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
1521  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1522  }
1523 }
1524 
1550 void vpMbKltMultiTracker::loadModel(const std::string &modelFile, const bool verbose) {
1551  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1552  it != m_mapOfKltTrackers.end(); ++it) {
1553  it->second->loadModel(modelFile, verbose);
1554  }
1555 
1556  modelInitialised = true;
1557 }
1558 
1559 void vpMbKltMultiTracker::preTracking(std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1560  std::map<std::string, unsigned int> &mapOfNbInfos,
1561  std::map<std::string, unsigned int> &mapOfNbFaceUsed) {
1562  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
1563  it != m_mapOfKltTrackers.end(); ++it) {
1564  mapOfNbInfos[it->first] = 0;
1565  mapOfNbFaceUsed[it->first] = 0;
1566  }
1567 
1568  for (std::map<std::string, vpMbKltTracker*>::const_iterator it =
1569  m_mapOfKltTrackers.begin(); it != m_mapOfKltTrackers.end(); ++it) {
1570  try {
1571  it->second->preTracking(*mapOfImages[it->first], mapOfNbInfos[it->first], mapOfNbFaceUsed[it->first]);
1572  } catch (/*vpException &e*/...) {
1573 // throw e;
1574  }
1575  }
1576 }
1577 
1578 void vpMbKltMultiTracker::postTracking(std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1579  std::map<std::string, unsigned int> &mapOfNbInfos, vpColVector &w_klt) {
1580  unsigned int shift = 0;
1581  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1582  it != m_mapOfKltTrackers.end(); ++it) {
1583  //Set the camera pose
1584  it->second->cMo = m_mapOfCameraTransformationMatrix[it->first]*cMo;
1585 
1586  if(mapOfNbInfos[it->first] > 0) {
1587  vpSubColVector sub_w(w_klt, shift, 2*mapOfNbInfos[it->first]);
1588  shift += 2*mapOfNbInfos[it->first];
1589  if(it->second->postTracking(*mapOfImages[it->first], sub_w)) {
1590  it->second->reinit(*mapOfImages[it->first]);
1591 
1592  //set ctTc0 to identity
1593  if(it->first == m_referenceCameraName) {
1594  reinit(/*mapOfImages[it->first]*/);
1595  }
1596  }
1597  }
1598  }
1599 }
1600 
1604 void vpMbKltMultiTracker::reinit(/* const vpImage<unsigned char>& I*/) {
1605  c0Mo = cMo;
1606  ctTc0.eye();
1607 }
1608 
1618 void vpMbKltMultiTracker::reInitModel(const vpImage<unsigned char>& I, const std::string &cad_name,
1619  const vpHomogeneousMatrix& cMo_, const bool verbose) {
1620  if(m_mapOfKltTrackers.size() != 1) {
1621  std::stringstream ss;
1622  ss << "This method requires exactly one camera, there are " << m_mapOfKltTrackers.size() << " cameras !";
1623  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
1624  }
1625 
1626  firstInitialisation = true;
1627  modelInitialised = true;
1628 
1629  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1630  if(it_klt != m_mapOfKltTrackers.end()) {
1631  it_klt->second->reInitModel(I, cad_name, cMo_, verbose);
1632 
1633  //Set reference pose
1634  it_klt->second->getPose(cMo);
1635 
1636  c0Mo = cMo;
1637  ctTc0.eye();
1638  } else {
1639  throw vpException(vpTrackingException::fatalError, "Cannot reInitModel the reference camera !");
1640  }
1641 }
1642 
1656  const std::string &cad_name, const vpHomogeneousMatrix &c1Mo, const vpHomogeneousMatrix &c2Mo,
1657  const bool verbose, const bool firstCameraIsReference) {
1658  if(m_mapOfKltTrackers.size() == 2) {
1659  std::map<std::string, vpMbKltTracker *>::const_iterator it_edge = m_mapOfKltTrackers.begin();
1660 
1661  it_edge->second->reInitModel(I1, cad_name, c1Mo, verbose);
1662 
1663  if(firstCameraIsReference) {
1664  //Set reference pose
1665  it_edge->second->getPose(cMo);
1666  }
1667 
1668  ++it_edge;
1669 
1670  it_edge->second->reInitModel(I2, cad_name, c2Mo, verbose);
1671 
1672  if(!firstCameraIsReference) {
1673  //Set reference pose
1674  it_edge->second->getPose(cMo);
1675  }
1676 
1677  c0Mo = cMo;
1678  ctTc0.eye();
1679  } else {
1680  throw vpException(vpTrackingException::fatalError, "This method requires exactly two cameras !");
1681  }
1682 }
1683 
1693 void vpMbKltMultiTracker::reInitModel(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1694  const std::string &cad_name, const std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses,
1695  const bool verbose) {
1696  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1697  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img = mapOfImages.find(m_referenceCameraName);
1698  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camPose = mapOfCameraPoses.find(m_referenceCameraName);
1699 
1700  if(it_klt != m_mapOfKltTrackers.end() && it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
1701  it_klt->second->reInitModel(*it_img->second, cad_name, it_camPose->second, verbose);
1702  modelInitialised = true;
1703 
1704  //Set reference pose
1705  it_klt->second->getPose(cMo);
1706 
1707  c0Mo = cMo;
1708  ctTc0.eye();
1709  } else {
1710  throw vpException(vpTrackingException::fatalError, "Cannot reInitModel for reference camera !");
1711  }
1712 
1713  std::vector<std::string> vectorOfMissingCameras;
1714  for(it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1715  if(it_klt->first != m_referenceCameraName) {
1716  it_img = mapOfImages.find(it_klt->first);
1717  it_camPose = mapOfCameraPoses.find(it_klt->first);
1718 
1719  if(it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
1720  it_klt->second->reInitModel(*it_img->second, cad_name, it_camPose->second, verbose);
1721  } else {
1722  vectorOfMissingCameras.push_back(it_klt->first);
1723  }
1724  }
1725  }
1726 
1727  for(std::vector<std::string>::const_iterator it = vectorOfMissingCameras.begin();
1728  it != vectorOfMissingCameras.end(); ++it) {
1729  it_img = mapOfImages.find(*it);
1730  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans = m_mapOfCameraTransformationMatrix.find(*it);
1731 
1732  if(it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
1733  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1734  m_mapOfKltTrackers[*it]->reInitModel(*it_img->second, cad_name, cCurrentMo, verbose);
1735  }
1736  }
1737 }
1738 
1744  cMo.eye();
1745  ctTc0.eye();
1746 
1747  //Call resetTracker for all the cameras
1748  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1749  it != m_mapOfKltTrackers.end(); ++it) {
1750  it->second->resetTracker();
1751  }
1752 
1753  compute_interaction = true;
1754  firstInitialisation = true;
1755  computeCovariance = false;
1756 
1757  angleAppears = vpMath::rad(65);
1759 
1761 
1762  maskBorder = 5;
1763  threshold_outlier = 0.5;
1764  percentGood = 0.7;
1765 
1766  lambda = 0.8;
1767  maxIter = 200;
1768 
1770 
1771  useScanLine = false;
1772 
1773 #ifdef VISP_HAVE_OGRE
1774  useOgre = false;
1775 #endif
1776 }
1777 
1789 
1790  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1791  it != m_mapOfKltTrackers.end(); ++it) {
1792  it->second->setAngleAppear(a);
1793  }
1794 }
1795 
1807 
1808  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1809  it != m_mapOfKltTrackers.end(); ++it) {
1810  it->second->setAngleDisappear(a);
1811  }
1812 }
1813 
1820  if(m_mapOfKltTrackers.empty()) {
1821  throw vpException(vpTrackingException::fatalError, "There is no camera !");
1822  } else if(m_mapOfKltTrackers.size() > 1) {
1823  throw vpException(vpTrackingException::fatalError, "There is more than one camera !");
1824  } else {
1825  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
1826  if(it != m_mapOfKltTrackers.end()) {
1827  it->second->setCameraParameters(camera);
1828 
1829  //Set reference camera parameters
1830  this->cam = camera;
1831  } else {
1832  std::stringstream ss;
1833  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
1835  }
1836  }
1837 }
1838 
1847  const bool firstCameraIsReference) {
1848  if(m_mapOfKltTrackers.empty()) {
1849  throw vpException(vpTrackingException::fatalError, "There is no camera !");
1850  } else if(m_mapOfKltTrackers.size() == 2) {
1851  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1852  it->second->setCameraParameters(camera1);
1853 
1854  ++it;
1855  it->second->setCameraParameters(camera2);
1856 
1857  if(firstCameraIsReference) {
1858  this->cam = camera1;
1859  } else {
1860  this->cam = camera2;
1861  }
1862  } else {
1863  std::stringstream ss;
1864  ss << "Require two cameras ! There are " << m_mapOfKltTrackers.size() << " cameras !";
1866  }
1867 }
1868 
1875 void vpMbKltMultiTracker::setCameraParameters(const std::string &cameraName, const vpCameraParameters& camera) {
1876  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
1877  if(it != m_mapOfKltTrackers.end()) {
1878  it->second->setCameraParameters(camera);
1879 
1880  if(it->first == m_referenceCameraName) {
1881  this->cam = camera;
1882  }
1883  } else {
1884  std::stringstream ss;
1885  ss << "The camera: " << cameraName << " does not exist !";
1887  }
1888 }
1889 
1895 void vpMbKltMultiTracker::setCameraParameters(const std::map<std::string, vpCameraParameters> &mapOfCameraParameters) {
1896  for(std::map<std::string, vpMbKltTracker*>::const_iterator it_klt = m_mapOfKltTrackers.begin();
1897  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1898  std::map<std::string, vpCameraParameters>::const_iterator it_cam = mapOfCameraParameters.find(it_klt->first);
1899  if(it_cam != mapOfCameraParameters.end()) {
1900  it_klt->second->setCameraParameters(it_cam->second);
1901 
1902  if(it_klt->first == m_referenceCameraName) {
1903  this->cam = it_cam->second;
1904  }
1905  } else {
1906  std::stringstream ss;
1907  ss << "Missing camera parameters for camera: " << it_klt->first << " !";
1909  }
1910  }
1911 }
1912 
1919 void vpMbKltMultiTracker::setCameraTransformationMatrix(const std::string &cameraName,
1920  const vpHomogeneousMatrix &cameraTransformationMatrix) {
1921  std::map<std::string, vpHomogeneousMatrix>::iterator it = m_mapOfCameraTransformationMatrix.find(cameraName);
1922  if(it != m_mapOfCameraTransformationMatrix.end()) {
1923  it->second = cameraTransformationMatrix;
1924  } else {
1925  std::stringstream ss;
1926  ss << "Cannot find camera: " << cameraName << " !";
1928  }
1929 }
1930 
1938  const std::map<std::string, vpHomogeneousMatrix> &mapOfTransformationMatrix) {
1939  //Check if all cameras have a transformation matrix
1940  for(std::map<std::string, vpMbKltTracker*>::const_iterator it_klt = m_mapOfKltTrackers.begin();
1941  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1942  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans = mapOfTransformationMatrix.find(it_klt->first);
1943 
1944  if(it_camTrans == mapOfTransformationMatrix.end()) {
1945  throw vpException(vpTrackingException::initializationError, "Missing camera transformation matrix !");
1946  }
1947  }
1948 
1949  m_mapOfCameraTransformationMatrix = mapOfTransformationMatrix;
1950 }
1951 
1959 void vpMbKltMultiTracker::setClipping(const unsigned int &flags) {
1960  vpMbTracker::setClipping(flags);
1961 
1962  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1963  it != m_mapOfKltTrackers.end(); ++it) {
1964  it->second->setClipping(flags);
1965  }
1966 }
1967 
1976 void vpMbKltMultiTracker::setClipping(const std::string &cameraName, const unsigned int &flags) {
1977  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
1978  if(it != m_mapOfKltTrackers.end()) {
1979  it->second->setClipping(flags);
1980  } else {
1981  std::cerr << "Camera: " << cameraName << " does not exist !" << std::endl;
1982  }
1983 }
1984 
1992 
1993  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1994  it != m_mapOfKltTrackers.end(); ++it) {
1995  it->second->setCovarianceComputation(flag);
1996  }
1997 }
1998 
2004 void vpMbKltMultiTracker::setDisplayFeatures(const bool displayF) {
2005  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2006  it != m_mapOfKltTrackers.end(); ++it) {
2007  it->second->setDisplayFeatures(displayF);
2008  }
2009 
2010  displayFeatures = displayF;
2011 }
2012 
2020 
2021  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2022  it != m_mapOfKltTrackers.end(); ++it) {
2023  it->second->setFarClippingDistance(dist);
2024  }
2025 }
2026 
2033 void vpMbKltMultiTracker::setFarClippingDistance(const std::string &cameraName, const double &dist) {
2034  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
2035  if(it != m_mapOfKltTrackers.end()) {
2036  it->second->setFarClippingDistance(dist);
2037  } else {
2038  std::cerr << "Camera: " << cameraName << " does not exist !" << std::endl;
2039  }
2040 }
2041 
2042 #ifdef VISP_HAVE_OGRE
2043 
2051  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2052  it != m_mapOfKltTrackers.end(); ++it) {
2053  it->second->setGoodNbRayCastingAttemptsRatio(ratio);
2054  }
2055  }
2056 
2066  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2067  it != m_mapOfKltTrackers.end(); ++it) {
2068  it->second->setNbRayCastingAttemptsForVisibility(attempts);
2069  }
2070  }
2071 #endif
2072 
2079  for(std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.begin();
2080  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2081  it_klt->second->setKltOpencv(t);
2082  }
2083 }
2084 
2090 void vpMbKltMultiTracker::setKltOpencv(const std::map<std::string, vpKltOpencv> &mapOfOpenCVTrackers) {
2091  for(std::map<std::string, vpKltOpencv>::const_iterator it_kltOpenCV = mapOfOpenCVTrackers.begin();
2092  it_kltOpenCV != mapOfOpenCVTrackers.end(); ++it_kltOpenCV) {
2093  std::map<std::string, vpMbKltTracker*>::const_iterator it_klt = m_mapOfKltTrackers.find(it_kltOpenCV->first);
2094  if(it_klt != m_mapOfKltTrackers.end()) {
2095  it_klt->second->setKltOpencv(it_kltOpenCV->second);
2096  } else {
2097  std::cerr << "The camera: " << it_kltOpenCV->first << " does not exist !" << std::endl;
2098  }
2099  }
2100 }
2101 
2111 void vpMbKltMultiTracker::setLod(const bool useLod, const std::string &name) {
2112  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2113  it != m_mapOfKltTrackers.end(); ++it) {
2114  it->second->setLod(useLod, name);
2115  }
2116 }
2117 
2128 void vpMbKltMultiTracker::setLod(const bool useLod, const std::string &cameraName, const std::string &name) {
2129  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(cameraName);
2130 
2131  if(it_klt != m_mapOfKltTrackers.end()) {
2132  it_klt->second->setLod(useLod, name);
2133  } else {
2134  std::cerr << "The camera: " << cameraName << " cannot be found !" << std::endl;
2135  }
2136 }
2137 
2143 void vpMbKltMultiTracker::setMaskBorder(const unsigned int &e) {
2144  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
2145  it != m_mapOfKltTrackers.end(); ++it) {
2146  it->second->setMaskBorder(e);
2147  }
2148 
2149  maskBorder = e;
2150 }
2151 
2155 void vpMbKltMultiTracker::setMinLineLengthThresh(const double /*minLineLengthThresh*/, const std::string &/*name*/) {
2156  std::cerr << "Useless for KLT tracker !" << std::endl;
2157 }
2158 
2167 void vpMbKltMultiTracker::setMinPolygonAreaThresh(const double minPolygonAreaThresh, const std::string &name) {
2168  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2169  it != m_mapOfKltTrackers.end(); ++it) {
2170  it->second->setMinPolygonAreaThresh(minPolygonAreaThresh, name);
2171  }
2172 }
2173 
2183 void vpMbKltMultiTracker::setMinPolygonAreaThresh(const double minPolygonAreaThresh, const std::string &cameraName,
2184  const std::string &name) {
2185  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(cameraName);
2186 
2187  if(it_klt != m_mapOfKltTrackers.end()) {
2188  it_klt->second->setMinPolygonAreaThresh(minPolygonAreaThresh, name);
2189  } else {
2190  std::cerr << "The camera: " << cameraName << " cannot be found !" << std::endl;
2191  }
2192 }
2193 
2201 
2202  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2203  it != m_mapOfKltTrackers.end(); ++it) {
2204  it->second->setNearClippingDistance(dist);
2205  }
2206 }
2207 
2218 void vpMbKltMultiTracker::setOgreShowConfigDialog(const bool showConfigDialog) {
2219  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2220  it != m_mapOfKltTrackers.end(); ++it) {
2221  it->second->setOgreShowConfigDialog(showConfigDialog);
2222  }
2223 }
2224 
2233  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2234  it != m_mapOfKltTrackers.end(); ++it) {
2235  it->second->setOgreVisibilityTest(v);
2236  }
2237 
2238 #ifdef VISP_HAVE_OGRE
2239  for(std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2240  it != m_mapOfKltTrackers.end(); ++it) {
2241  it->second->faces.getOgreContext()->setWindowName("Multi MBT Klt (" + it->first + ")");
2242  }
2243 #endif
2244 
2245  useOgre = v;
2246 }
2247 
2254 void vpMbKltMultiTracker::setNearClippingDistance(const std::string &cameraName, const double &dist) {
2255  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
2256  if(it != m_mapOfKltTrackers.end()) {
2257  it->second->setNearClippingDistance(dist);
2258  } else {
2259  std::cerr << "Camera: " << cameraName << " does not exist !" << std::endl;
2260  }
2261 }
2262 
2269  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
2270  it != m_mapOfKltTrackers.end(); ++it) {
2271  it->second->setOptimizationMethod(opt);
2272  }
2273 
2274  m_optimizationMethod = opt;
2275 }
2276 
2285  if(m_mapOfKltTrackers.size() == 1) {
2286  std::map<std::string, vpMbKltTracker *>::iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
2287  if(it != m_mapOfKltTrackers.end()) {
2288  it->second->setPose(I, cMo_);
2289  this->cMo = cMo_;
2290 
2291  c0Mo = this->cMo;
2292  ctTc0.eye();
2293  } else {
2294  std::stringstream ss;
2295  ss << "Cannot find the reference camera: " << m_referenceCameraName << " !";
2297  }
2298  } else {
2299  std::stringstream ss;
2300  ss << "You are trying to set the pose with only one image and cMo "
2301  "but there are multiple cameras !";
2303  }
2304 }
2305 
2317  const vpHomogeneousMatrix &c1Mo, const vpHomogeneousMatrix c2Mo, const bool firstCameraIsReference) {
2318  if(m_mapOfKltTrackers.size() == 2) {
2319  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2320  it->second->setPose(I1, c1Mo);
2321 
2322  ++it;
2323 
2324  it->second->setPose(I2, c2Mo);
2325 
2326  if(firstCameraIsReference) {
2327  this->cMo = c1Mo;
2328  } else {
2329  this->cMo = c2Mo;
2330  }
2331 
2332  c0Mo = this->cMo;
2333  ctTc0.eye();
2334  } else {
2335  std::stringstream ss;
2336  ss << "This method requires 2 cameras but there are " << m_mapOfKltTrackers.size() << " cameras !";
2338  }
2339 }
2340 
2349 void vpMbKltMultiTracker::setPose(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
2350  const vpHomogeneousMatrix &cMo_) {
2351  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
2352  if(it_klt != m_mapOfKltTrackers.end()) {
2353  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img = mapOfImages.find(m_referenceCameraName);
2354 
2355  if(it_img != mapOfImages.end()) {
2356  //Set pose on reference camera
2357  it_klt->second->setPose(*it_img->second, cMo_);
2358 
2359  //Set the reference cMo
2360  cMo = cMo_;
2361 
2362  c0Mo = this->cMo;
2363  ctTc0.eye();
2364 
2365  //Set the pose for the others cameras
2366  for(it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2367  if(it_klt->first != m_referenceCameraName) {
2368  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
2369  m_mapOfCameraTransformationMatrix.find(it_klt->first);
2370  it_img = mapOfImages.find(it_klt->first);
2371 
2372  if(it_camTrans != m_mapOfCameraTransformationMatrix.end() && it_img != mapOfImages.end()) {
2373  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
2374  it_klt->second->setPose(*it_img->second, cCurrentMo);
2375  } else {
2376  throw vpException(vpTrackingException::fatalError, "Cannot find camera transformation matrix or image !");
2377  }
2378  }
2379  }
2380  } else {
2381  std::stringstream ss;
2382  ss << "Missing image for reference camera: " << m_referenceCameraName << " !";
2383  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2384  }
2385  } else {
2386  std::stringstream ss;
2387  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
2388  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2389  }
2390 }
2391 
2401 void vpMbKltMultiTracker::setPose(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
2402  const std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses) {
2403  //Set the reference cMo
2404  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
2405  std::map<std::string, const vpImage<unsigned char>* >::const_iterator it_img = mapOfImages.find(m_referenceCameraName);
2406  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camPose = mapOfCameraPoses.find(m_referenceCameraName);
2407 
2408  if(it_klt != m_mapOfKltTrackers.end() && it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
2409  it_klt->second->setPose(*it_img->second, it_camPose->second);
2410  it_klt->second->getPose(cMo);
2411 
2412  c0Mo = this->cMo;
2413  ctTc0.eye();
2414  } else {
2415  throw vpException(vpTrackingException::fatalError, "Cannot set pose for the reference camera !");
2416  }
2417 
2418  //Vector of missing pose matrices for cameras
2419  std::vector<std::string> vectorOfMissingCameraPoses;
2420 
2421  //Set pose for the specified cameras
2422  for(it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2423  if(it_klt->first != m_referenceCameraName) {
2424  it_img = mapOfImages.find(it_klt->first);
2425  it_camPose = mapOfCameraPoses.find(it_klt->first);
2426 
2427  if(it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
2428  //Set pose
2429  it_klt->second->setPose(*it_img->second, it_camPose->second);
2430  } else {
2431  vectorOfMissingCameraPoses.push_back(it_klt->first);
2432  }
2433  }
2434  }
2435 
2436  for(std::vector<std::string>::const_iterator it1 = vectorOfMissingCameraPoses.begin();
2437  it1 != vectorOfMissingCameraPoses.end(); ++it1) {
2438  it_img = mapOfImages.find(*it1);
2439  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans = m_mapOfCameraTransformationMatrix.find(*it1);
2440 
2441  if(it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
2442  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
2443  m_mapOfKltTrackers[*it1]->setPose(*it_img->second, cCurrentMo);
2444  } else {
2445  std::stringstream ss;
2446  ss << "Missing image or missing camera transformation matrix ! Cannot set the pose for camera: " << (*it1) << " !";
2447  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2448  }
2449  }
2450 }
2451 
2457 void vpMbKltMultiTracker::setReferenceCameraName(const std::string &referenceCameraName) {
2458  std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.find(referenceCameraName);
2459  if(it != m_mapOfKltTrackers.end()) {
2460  m_referenceCameraName = referenceCameraName;
2461  } else {
2462  std::stringstream ss;
2463  ss << "The reference camera: " << referenceCameraName << " does not exist !";
2464  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2465  }
2466 }
2467 
2474  //Set general setScanLineVisibilityTest
2476 
2477  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
2478  it != m_mapOfKltTrackers.end(); ++it) {
2479  it->second->setScanLineVisibilityTest(v);
2480  }
2481 }
2482 
2489  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
2490  it != m_mapOfKltTrackers.end(); ++it) {
2491  it->second->setThresholdAcceptation(th);
2492  }
2493 
2494  threshold_outlier = th;
2495 }
2496 
2503 void vpMbKltMultiTracker::setUseKltTracking(const std::string &name, const bool &useKltTracking) {
2504  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
2505  it != m_mapOfKltTrackers.end(); ++it) {
2506  it->second->setUseKltTracking(name, useKltTracking);
2507  }
2508 }
2509 
2518  //Track only with reference camera
2519  //Get the reference camera parameters
2520  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
2521 
2522  if(it != m_mapOfKltTrackers.end()) {
2523  it->second->track(I);
2524  it->second->getPose(cMo);
2525  } else {
2526  std::stringstream ss;
2527  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
2528  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2529  }
2530 }
2531 
2541  if(m_mapOfKltTrackers.size() == 2) {
2542  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2543  std::map<std::string, const vpImage<unsigned char> *> mapOfImages;
2544  mapOfImages[it->first] = &I1;
2545  ++it;
2546 
2547  mapOfImages[it->first] = &I2;
2548  track(mapOfImages);
2549  } else {
2550  std::stringstream ss;
2551  ss << "Require two cameras ! There are " << m_mapOfKltTrackers.size() << " cameras !";
2552  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2553  }
2554 }
2555 
2563 void vpMbKltMultiTracker::track(std::map<std::string, const vpImage<unsigned char> *> &mapOfImages) {
2564  //Check if there is an image for each camera
2565  for(std::map<std::string, vpMbKltTracker*>::const_iterator it_klt = m_mapOfKltTrackers.begin();
2566  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2567  std::map<std::string, const vpImage<unsigned char>* >::const_iterator it_img = mapOfImages.find(it_klt->first);
2568 
2569  if(it_img == mapOfImages.end()) {
2570  throw vpException(vpTrackingException::fatalError, "Missing images !");
2571  }
2572  }
2573 
2574  std::map<std::string, unsigned int> mapOfNbInfos;
2575  std::map<std::string, unsigned int> mapOfNbFaceUsed;
2576 
2577  preTracking(mapOfImages, mapOfNbInfos, mapOfNbFaceUsed);
2578 
2579  bool atLeastOneTrackerOk = false;
2580  for(std::map<std::string, vpMbKltTracker*>::const_iterator it = m_mapOfKltTrackers.begin();
2581  it != m_mapOfKltTrackers.end(); ++it) {
2582  if(mapOfNbInfos[it->first] >= 4 && mapOfNbFaceUsed[it->first] != 0) {
2583  atLeastOneTrackerOk = true;
2584  }
2585  }
2586 
2587  if(!atLeastOneTrackerOk) {
2588  vpERROR_TRACE("\n\t\t Error-> not enough data") ;
2589  throw vpTrackingException(vpTrackingException::notEnoughPointError, "\n\t\t Error-> not enough data");
2590  }
2591 
2592  computeVVS(mapOfNbInfos, m_w);
2593 
2594  postTracking(mapOfImages, mapOfNbInfos, m_w);
2595 }
2596 
2597 #elif !defined(VISP_BUILD_SHARED_LIBS)
2598 // Work arround to avoid warning: libvisp_mbt.a(dummy_vpMbKltMultiTracker.cpp.o) has no symbols
2599 void dummy_vpMbKltMultiTracker() {};
2600 #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.
std::list< vpMbtDistanceKltPoints * > kltPolygons
virtual unsigned int getNbPolygon() const
void setGoodNbRayCastingAttemptsRatio(const double &ratio)
virtual void setCovarianceComputation(const bool &flag)
Definition: vpMbTracker.h:398
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
virtual std::vector< std::string > getCameraNames() const
virtual void getCameraParameters(vpCameraParameters &camera) const
virtual std::list< vpMbtDistanceKltCylinder * > & getFeaturesKltCylinder()
virtual void setFarClippingDistance(const double &dist)
virtual void initFromPose(const vpImage< unsigned char > &I, const std::string &initFile)
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 track(const vpImage< unsigned char > &I)
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 setCovarianceComputation(const bool &flag)
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:208
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo)
void stack(const double &d)
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:382
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpArray2D.h:167
vpMbHiddenFaces< vpMbtPolygon > faces
Set of faces describing the object.
Definition: vpMbTracker.h:144
virtual std::map< std::string, int > getNbKltPoints() const
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)
virtual void setAngleDisappear(const double &a)
virtual void loadConfigFile(const std::string &configFile)
Implementation of an homogeneous matrix and operations on such kind of matrices.
virtual void computeVVS(std::map< std::string, unsigned int > &mapOfNbInfos, vpColVector &w)
virtual std::list< vpMbtDistanceKltPoints * > & getFeaturesKlt()
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)
#define vpERROR_TRACE
Definition: vpDebug.h:391
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
void stack(const vpMatrix &A)
Definition: vpMatrix.cpp:2981
virtual void setDisplayFeatures(const bool displayF)
virtual void setCameraTransformationMatrix(const std::string &cameraName, const vpHomogeneousMatrix &cameraTransformationMatrix)
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)
bool modelInitialised
Flag used to ensure that the CAD model is loaded before the initialisation.
Definition: vpMbTracker.h:123
error that can be emited by ViSP classes.
Definition: vpException.h:73
virtual void setThresholdAcceptation(const double th)
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 useOgre
Use Ogre3d for visibility tests.
Definition: vpMbTracker.h:156
virtual void setLod(const bool useLod, const std::string &name="")
virtual void init(const vpImage< unsigned char > &I)
virtual void setMinPolygonAreaThresh(const double minPolygonAreaThresh, const std::string &name="")
bool computeCovariance
Flag used to specify if the covariance matrix has to be computed or not.
Definition: vpMbTracker.h:127
bool firstInitialisation
Flag to specify whether the init method is called the first or not (specific calls to realize in this...
Class that defines what is a point.
Definition: vpPoint.h:59
vpCameraParameters cam
The camera parameters.
Definition: vpMbTracker.h:113
std::map< std::string, vpHomogeneousMatrix > m_mapOfCameraTransformationMatrix
Map of camera transformation matrix between the current camera frame to the reference camera frame (c...
vpHomogeneousMatrix ctTc0
The estimated displacement of the pose between the current instant and the initial position...
virtual void setReferenceCameraName(const std::string &referenceCameraName)
virtual std::map< std::string, vpKltOpencv > getKltOpencv() const
virtual std::map< std::string, std::vector< vpImagePoint > > getKltImagePoints() const
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:179
void computeVVSInteractionMatrixAndResidu(unsigned int shift, vpColVector &R, vpMatrix &L, vpHomography &H, std::list< vpMbtDistanceKltPoints * > &kltPolygons_, std::list< vpMbtDistanceKltCylinder * > &kltCylinders_, const vpHomogeneousMatrix &ctTc0_)
Error that can be emited by the vpTracker class and its derivates.
vp_deprecated void init()
virtual vpMbHiddenFaces< vpMbtPolygon > & getFaces()
virtual void setClipping(const unsigned int &flags)
virtual vpHomogeneousMatrix getPose() const
Definition: vpMbTracker.h:342
virtual void setOgreShowConfigDialog(const bool showConfigDialog)
virtual std::list< vpMbtDistanceCircle * > & getFeaturesCircle()
bool useScanLine
Use Scanline for visibility tests.
Definition: vpMbTracker.h:159
vpVelocityTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
virtual std::map< std::string, std::vector< cv::Point2f > > getKltPoints() const
virtual void setMinLineLengthThresh(const double minLineLengthThresh, const std::string &name="")
unsigned int maskBorder
Erosion of the mask.
Generic class defining intrinsic camera parameters.
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:371
virtual void setAngleAppear(const double &a)
Implementation of a velocity twist matrix and operations on such kind of matrices.
vpMbtOptimizationMethod m_optimizationMethod
Optimization method used.
Definition: vpMbTracker.h:141
std::string m_referenceCameraName
Name of the reference camera.
double angleAppears
Angle used to detect a face appearance.
Definition: vpMbTracker.h:146
std::list< vpMbtDistanceKltCylinder * > kltCylinders
virtual void postTracking(std::map< std::string, const vpImage< unsigned char > * > &mapOfImages, std::map< std::string, unsigned int > &mapOfNbInfos, vpColVector &w_klt)
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
double get_px() const
static double rad(double deg)
Definition: vpMath.h:104
double threshold_outlier
Threshold below which the weight associated to a point to consider this one as an outlier...
std::map< std::string, vpMbKltTracker * > m_mapOfKltTrackers
Map of Model-based klt trackers.
virtual void setKltOpencv(const vpKltOpencv &t)
virtual std::map< std::string, std::map< int, vpImagePoint > > getKltImagePointsWithId() const
virtual void setNearClippingDistance(const double &dist)
double lambda
The gain of the virtual visual servoing stage.
virtual void setCameraParameters(const vpCameraParameters &camera)
virtual void setOgreVisibilityTest(const bool &v)
void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts)
std::list< vpMbtDistanceCircle * > circles_disp
Vector of the circles used here only to display the full model.
bool displayFeatures
If true, the features are displayed.
Definition: vpMbTracker.h:135
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
virtual void loadModel(const std::string &modelFile, const bool verbose=false)
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:76
vpHomogeneousMatrix inverse() const
virtual void reInitModel(const vpImage< unsigned char > &I, const std::string &cad_name, const vpHomogeneousMatrix &cMo_, const bool verbose=false)
Contains an M-Estimator and various influence function.
Definition: vpRobust.h:60
double angleDisappears
Angle used to detect a face disappearance.
Definition: vpMbTracker.h:148
virtual void setScanLineVisibilityTest(const bool &v)
Definition: vpMbTracker.h:439
virtual void setClipping(const unsigned int &flags)
virtual void initClick(const vpImage< unsigned char > &I, const std::vector< vpPoint > &points3D_list, const std::string &displayFile="")
void computeVVSCovariance(const vpColVector &w_true, const vpHomogeneousMatrix &cMoPrev, const vpMatrix &L_true, const vpMatrix &LVJ_true)
unsigned int clippingFlag
Flags specifying which clipping to used.
Definition: vpMbTracker.h:154
virtual void setFarClippingDistance(const double &dist)
virtual void setMaskBorder(const unsigned int &e)
vpHomogeneousMatrix c0Mo
Initial pose.
virtual std::map< std::string, unsigned int > getMultiNbPolygon() const
virtual void setOptimizationMethod(const vpMbtOptimizationMethod &opt)
virtual void setScanLineVisibilityTest(const bool &v)
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:225
vpColVector m_w
Weights used in the robust scheme.
Definition: vpMbTracker.h:137
virtual void setNearClippingDistance(const double &dist)