Visual Servoing Platform  version 3.1.0
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 modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact INRIA about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr/download/ for more information.
18  *
19  * This software was developed at:
20  * INRIA Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  * http://www.irisa.fr/lagadic
25  *
26  * If you have questions regarding the use of this file, please contact
27  * INRIA at visp@inria.fr
28  *
29  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
30  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31  *
32  *
33  * Description:
34  * Model-based klt tracker with multiple cameras.
35  *
36  * Authors:
37  * Souriya Trinh
38  *
39  *****************************************************************************/
40 
46 #include <visp3/core/vpConfig.h>
47 
48 #if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
49 
50 #include <visp3/core/vpTrackingException.h>
51 #include <visp3/core/vpVelocityTwistMatrix.h>
52 #include <visp3/mbt/vpMbKltMultiTracker.h>
53 
58  : m_mapOfCameraTransformationMatrix(), m_mapOfKltTrackers(), m_referenceCameraName("Camera"), m_L_kltMulti(),
59  m_error_kltMulti(), m_w_kltMulti(), m_weightedError_kltMulti()
60 {
61  m_mapOfKltTrackers["Camera"] = new vpMbKltTracker();
62 
63  // Add default camera transformation matrix
65 }
66 
72 vpMbKltMultiTracker::vpMbKltMultiTracker(const unsigned int nbCameras)
75 {
76 
77  if (nbCameras == 0) {
78  throw vpException(vpTrackingException::fatalError, "Cannot construct a vpMbkltMultiTracker with no camera !");
79  } else if (nbCameras == 1) {
80  m_mapOfKltTrackers["Camera"] = new vpMbKltTracker();
81 
82  // Add default camera transformation matrix
84  } else if (nbCameras == 2) {
85  m_mapOfKltTrackers["Camera1"] = new vpMbKltTracker();
86  m_mapOfKltTrackers["Camera2"] = new vpMbKltTracker();
87 
88  // Add default camera transformation matrix
91 
92  // Set by default the reference camera
93  m_referenceCameraName = "Camera1";
94  } else {
95  for (unsigned int i = 1; i <= nbCameras; i++) {
96  std::stringstream ss;
97  ss << "Camera" << i;
98  m_mapOfKltTrackers[ss.str()] = new vpMbKltTracker();
99 
100  // Add default camera transformation matrix
102  }
103 
104  // Set by default the reference camera to the first one
105  m_referenceCameraName = m_mapOfKltTrackers.begin()->first;
106  }
107 }
108 
114 vpMbKltMultiTracker::vpMbKltMultiTracker(const std::vector<std::string> &cameraNames)
117 {
118  if (cameraNames.empty()) {
119  throw vpException(vpTrackingException::fatalError, "Cannot construct a vpMbKltMultiTracker with no camera !");
120  }
121 
122  for (std::vector<std::string>::const_iterator it = cameraNames.begin(); it != cameraNames.end(); ++it) {
123  m_mapOfKltTrackers[*it] = new vpMbKltTracker();
124  }
125 
126  // Set by default the reference camera
127  m_referenceCameraName = cameraNames.front();
128 }
129 
134 {
135  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
136  it != m_mapOfKltTrackers.end(); ++it) {
137  delete it->second;
138  }
139 }
140 
149 void vpMbKltMultiTracker::addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, const double r,
150  const std::string &name)
151 {
152  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
153  it != m_mapOfKltTrackers.end(); ++it) {
154  it->second->addCircle(P1, P2, P3, r, name);
155  }
156 }
157 
159 {
160  vpMatrix L_true;
161  vpMatrix LVJ_true;
162  vpColVector v;
163 
164  vpMatrix LTL;
165  vpColVector LTR;
166  vpHomogeneousMatrix cMoPrev;
167  vpHomogeneousMatrix ctTc0_Prev;
168  vpColVector error_prev;
169  double mu = m_initialMu;
170 
171  double normRes = 0;
172  double normRes_1 = -1;
173  unsigned int iter = 0;
174 
175  computeVVSInit();
176  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
177  it != m_mapOfKltTrackers.end(); ++it) {
178  vpMbKltTracker *klt = it->second;
179  klt->computeVVSInit();
180  }
181 
182  std::map<std::string, vpVelocityTwistMatrix> mapOfVelocityTwist;
183  for (std::map<std::string, vpHomogeneousMatrix>::const_iterator it = m_mapOfCameraTransformationMatrix.begin();
184  it != m_mapOfCameraTransformationMatrix.end(); ++it) {
186  cVo.buildFrom(it->second);
187  mapOfVelocityTwist[it->first] = cVo;
188  }
189 
190  while (((int)((normRes - normRes_1) * 1e8) != 0) && (iter < m_maxIter)) {
191  computeVVSInteractionMatrixAndResidu(mapOfVelocityTwist);
192 
193  bool reStartFromLastIncrement = false;
194  computeVVSCheckLevenbergMarquardt(iter, m_error_kltMulti, error_prev, cMoPrev, mu, reStartFromLastIncrement);
195  if (reStartFromLastIncrement) {
196  ctTc0 = ctTc0_Prev;
197  }
198 
199  if (!reStartFromLastIncrement) {
201 
202  if (computeCovariance) {
203  L_true = m_L_kltMulti;
204  if (!isoJoIdentity) {
206  cVo.buildFrom(cMo);
207  LVJ_true = (m_L_kltMulti * cVo * oJo);
208  }
209  }
210 
211  normRes_1 = normRes;
212  normRes = 0.0;
213 
214  for (unsigned int i = 0; i < m_weightedError_kltMulti.getRows(); i++) {
216  normRes += m_weightedError_kltMulti[i];
217  }
218 
219  if ((iter == 0) || m_computeInteraction) {
220  for (unsigned int i = 0; i < m_L_kltMulti.getRows(); i++) {
221  for (unsigned int j = 0; j < 6; j++) {
222  m_L_kltMulti[i][j] *= m_w_kltMulti[i];
223  }
224  }
225  }
226 
228  error_prev, LTR, mu, v);
229 
230  cMoPrev = cMo;
231  ctTc0_Prev = ctTc0;
233  cMo = ctTc0 * c0Mo;
234  } // endif(!reStartFromLastIncrement)
235 
236  iter++;
237  }
238 
240 }
241 
243 {
244  unsigned int nbFeatures = 2 * m_nbInfos;
245 
246  m_L_kltMulti.resize(nbFeatures, 6, false, false);
247  m_w_kltMulti.resize(nbFeatures, false);
248  m_error_kltMulti.resize(nbFeatures, false);
249  m_weightedError_kltMulti.resize(nbFeatures, false);
250 }
251 
253 {
254  throw vpException(vpException::fatalError, "vpMbKltMultiTracker::"
255  "computeVVSInteractionMatrixAndR"
256  "esidu() should not be called!");
257 }
258 
260  std::map<std::string, vpVelocityTwistMatrix> &mapOfVelocityTwist)
261 {
262  unsigned int startIdx = 0;
263 
264  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
265  it != m_mapOfKltTrackers.end(); ++it) {
266  vpMbKltTracker *klt = it->second;
267 
268  // Use the ctTc0 variable instead of the formula in the monocular case
269  // to ensure that we have the same result than vpMbKltTracker
270  // as some slight differences can occur due to numerical imprecision
271  if (m_mapOfKltTrackers.size() == 1) {
272  klt->ctTc0 = ctTc0;
274  } else {
275  vpHomogeneousMatrix c_curr_tTc_curr0 =
276  m_mapOfCameraTransformationMatrix[it->first] * cMo * it->second->c0Mo.inverse();
277  klt->ctTc0 = c_curr_tTc_curr0;
279  }
280 
281  m_error_kltMulti.insert(startIdx, klt->m_error_klt);
282  m_L_kltMulti.insert(klt->m_L_klt * mapOfVelocityTwist[it->first], startIdx, 0);
283 
284  startIdx += klt->m_error_klt.getRows();
285  }
286 }
287 
289 {
290  unsigned int startIdx = 0;
291  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
292  it != m_mapOfKltTrackers.end(); ++it) {
293  vpMbKltTracker *klt = it->second;
294  klt->computeVVSWeights(klt->m_robust_klt, klt->m_error_klt, klt->m_w_klt);
295 
296  m_w_kltMulti.insert(startIdx, klt->m_w_klt);
297  startIdx += klt->m_w_klt.getRows();
298  }
299 }
300 
313  const vpCameraParameters &cam_, const vpColor &col, const unsigned int thickness,
314  const bool displayFullModel)
315 {
316  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
317  if (it != m_mapOfKltTrackers.end()) {
318  it->second->display(I, cMo_, cam_, col, thickness, displayFullModel);
319  } else {
320  std::cerr << "Cannot find reference camera:" << m_referenceCameraName << " !" << std::endl;
321  }
322 }
323 
336  const vpCameraParameters &cam_, const vpColor &color, const unsigned int thickness,
337  const bool displayFullModel)
338 {
339  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
340  if (it != m_mapOfKltTrackers.end()) {
341  it->second->display(I, cMo_, cam_, color, thickness, displayFullModel);
342  } else {
343  std::cerr << "Cannot find reference camera:" << m_referenceCameraName << " !" << std::endl;
344  }
345 }
346 
362  const vpHomogeneousMatrix &c1Mo, const vpHomogeneousMatrix &c2Mo,
363  const vpCameraParameters &cam1, const vpCameraParameters &cam2, const vpColor &color,
364  const unsigned int thickness, const bool displayFullModel)
365 {
366  if (m_mapOfKltTrackers.size() == 2) {
367  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
368  it->second->display(I1, c1Mo, cam1, color, thickness, displayFullModel);
369  ++it;
370 
371  it->second->display(I2, c2Mo, cam2, color, thickness, displayFullModel);
372  } else {
373  std::cerr << "This display is only for the stereo case ! There are " << m_mapOfKltTrackers.size() << " cameras !"
374  << std::endl;
375  }
376 }
377 
393  const vpHomogeneousMatrix &c2Mo, const vpCameraParameters &cam1,
394  const vpCameraParameters &cam2, const vpColor &color, const unsigned int thickness,
395  const bool displayFullModel)
396 {
397  if (m_mapOfKltTrackers.size() == 2) {
398  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
399  it->second->display(I1, c1Mo, cam1, color, thickness, displayFullModel);
400  ++it;
401 
402  it->second->display(I2, c2Mo, cam2, color, thickness, displayFullModel);
403  } else {
404  std::cerr << "This display is only for the stereo case ! There are " << m_mapOfKltTrackers.size() << " cameras !"
405  << std::endl;
406  }
407 }
408 
420 void vpMbKltMultiTracker::display(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
421  const std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses,
422  const std::map<std::string, vpCameraParameters> &mapOfCameraParameters,
423  const vpColor &col, const unsigned int thickness, const bool displayFullModel)
424 {
425 
426  // Display only for the given images
427  for (std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img = mapOfImages.begin();
428  it_img != mapOfImages.end(); ++it_img) {
429  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(it_img->first);
430  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camPose = mapOfCameraPoses.find(it_img->first);
431  std::map<std::string, vpCameraParameters>::const_iterator it_cam = mapOfCameraParameters.find(it_img->first);
432 
433  if (it_klt != m_mapOfKltTrackers.end() && it_camPose != mapOfCameraPoses.end() &&
434  it_cam != mapOfCameraParameters.end()) {
435  it_klt->second->display(*it_img->second, it_camPose->second, it_cam->second, col, thickness, displayFullModel);
436  } else {
437  std::cerr << "Missing elements ! " << std::endl;
438  }
439  }
440 }
441 
453 void vpMbKltMultiTracker::display(const std::map<std::string, const vpImage<vpRGBa> *> &mapOfImages,
454  const std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses,
455  const std::map<std::string, vpCameraParameters> &mapOfCameraParameters,
456  const vpColor &col, const unsigned int thickness, const bool displayFullModel)
457 {
458 
459  // Display only for the given images
460  for (std::map<std::string, const vpImage<vpRGBa> *>::const_iterator it_img = mapOfImages.begin();
461  it_img != mapOfImages.end(); ++it_img) {
462  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(it_img->first);
463  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camPose = mapOfCameraPoses.find(it_img->first);
464  std::map<std::string, vpCameraParameters>::const_iterator it_cam = mapOfCameraParameters.find(it_img->first);
465 
466  if (it_klt != m_mapOfKltTrackers.end() && it_camPose != mapOfCameraPoses.end() &&
467  it_cam != mapOfCameraParameters.end()) {
468  it_klt->second->display(*it_img->second, it_camPose->second, it_cam->second, col, thickness, displayFullModel);
469  } else {
470  std::cerr << "Missing elements ! " << std::endl;
471  }
472  }
473 }
474 
480 std::vector<std::string> vpMbKltMultiTracker::getCameraNames() const
481 {
482  std::vector<std::string> cameraNames;
483 
484  for (std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.begin();
485  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
486  cameraNames.push_back(it_klt->first);
487  }
488 
489  return cameraNames;
490 }
491 
498 {
499  // Get the reference camera parameters
500  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
501  if (it != m_mapOfKltTrackers.end()) {
502  it->second->getCameraParameters(camera);
503  } else {
504  std::cerr << "The reference camera name: " << m_referenceCameraName << " does not exist !" << std::endl;
505  }
506 }
507 
515 {
516  if (m_mapOfKltTrackers.size() == 2) {
517  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
518  it->second->getCameraParameters(cam1);
519  ++it;
520 
521  it->second->getCameraParameters(cam2);
522  } else {
523  std::cerr << "Problem with the number of cameras ! There are " << m_mapOfKltTrackers.size() << " cameras !"
524  << std::endl;
525  }
526 }
527 
534 void vpMbKltMultiTracker::getCameraParameters(const std::string &cameraName, vpCameraParameters &camera) const
535 {
536  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
537  if (it != m_mapOfKltTrackers.end()) {
538  it->second->getCameraParameters(camera);
539  } else {
540  std::cerr << "The camera: " << cameraName << " does not exist !" << std::endl;
541  }
542 }
543 
549 void vpMbKltMultiTracker::getCameraParameters(std::map<std::string, vpCameraParameters> &mapOfCameraParameters) const
550 {
551  // Clear the input map
552  mapOfCameraParameters.clear();
553 
554  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
555  it != m_mapOfKltTrackers.end(); ++it) {
556  vpCameraParameters cam_;
557  it->second->getCameraParameters(cam_);
558  mapOfCameraParameters[it->first] = cam_;
559  }
560 }
561 
569 unsigned int vpMbKltMultiTracker::getClipping(const std::string &cameraName) const
570 {
571  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
572  if (it != m_mapOfKltTrackers.end()) {
573  return it->second->getClipping();
574  } else {
575  std::cerr << "Cannot find camera: " << cameraName << std::endl;
576  }
577 
578  return vpMbTracker::getClipping();
579 }
580 
587 {
588  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
589  if (it != m_mapOfKltTrackers.end()) {
590  return it->second->getFaces();
591  }
592 
593  std::cerr << "The reference camera: " << m_referenceCameraName << " cannot be found !" << std::endl;
594  return faces;
595 }
596 
603 {
604  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
605  if (it != m_mapOfKltTrackers.end()) {
606  return it->second->getFaces();
607  }
608 
609  std::cerr << "The camera: " << cameraName << " cannot be found !" << std::endl;
610  return faces;
611 }
612 
618 std::map<std::string, vpMbHiddenFaces<vpMbtPolygon> > vpMbKltMultiTracker::getFaces() const
619 {
620  std::map<std::string, vpMbHiddenFaces<vpMbtPolygon> > mapOfFaces;
621  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
622  it != m_mapOfKltTrackers.end(); ++it) {
623  mapOfFaces[it->first] = it->second->faces;
624  }
625 
626  return mapOfFaces;
627 }
628 
634 std::list<vpMbtDistanceCircle *> &vpMbKltMultiTracker::getFeaturesCircle()
635 {
636  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
637 
638  if (it_klt != m_mapOfKltTrackers.end()) {
639  return it_klt->second->getFeaturesCircle();
640  } else {
641  std::cerr << "Cannot find reference camera: " << m_referenceCameraName << " !" << std::endl;
642  }
643 
644  return circles_disp;
645 }
646 
653 std::list<vpMbtDistanceCircle *> &vpMbKltMultiTracker::getFeaturesCircle(const std::string &cameraName)
654 {
655  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
656 
657  if (it != m_mapOfKltTrackers.end()) {
658  return it->second->getFeaturesCircle();
659  } else {
660  std::cerr << "The camera: " << cameraName << " does not exist !";
661  }
662 
663  return circles_disp;
664 }
665 
672 std::list<vpMbtDistanceKltPoints *> &vpMbKltMultiTracker::getFeaturesKlt()
673 {
674  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
675 
676  if (it_klt != m_mapOfKltTrackers.end()) {
677  return it_klt->second->getFeaturesKlt();
678  } else {
679  std::cerr << "Cannot find reference camera: " << m_referenceCameraName << " !" << std::endl;
680  }
681 
682  return kltPolygons;
683 }
684 
692 std::list<vpMbtDistanceKltPoints *> &vpMbKltMultiTracker::getFeaturesKlt(const std::string &cameraName)
693 {
694  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
695 
696  if (it != m_mapOfKltTrackers.end()) {
697  return it->second->getFeaturesKlt();
698  } else {
699  std::cerr << "The camera: " << cameraName << " does not exist !";
700  }
701 
702  return kltPolygons;
703 }
704 
710 std::list<vpMbtDistanceKltCylinder *> &vpMbKltMultiTracker::getFeaturesKltCylinder()
711 {
712  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
713 
714  if (it_klt != m_mapOfKltTrackers.end()) {
715  return it_klt->second->getFeaturesKltCylinder();
716  } else {
717  std::cerr << "Cannot find reference camera: " << m_referenceCameraName << " !" << std::endl;
718  }
719 
720  return kltCylinders;
721 }
722 
729 std::list<vpMbtDistanceKltCylinder *> &vpMbKltMultiTracker::getFeaturesKltCylinder(const std::string &cameraName)
730 {
731  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
732 
733  if (it != m_mapOfKltTrackers.end()) {
734  return it->second->getFeaturesKltCylinder();
735  } else {
736  std::cerr << "The camera: " << cameraName << " does not exist !";
737  }
738 
739  return kltCylinders;
740 }
741 
750 std::map<std::string, std::vector<vpImagePoint> > vpMbKltMultiTracker::getKltImagePoints() const
751 {
752  std::map<std::string, std::vector<vpImagePoint> > mapOfFeatures;
753 
754  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
755  it != m_mapOfKltTrackers.end(); ++it) {
756  mapOfFeatures[it->first] = it->second->getKltImagePoints();
757  }
758 
759  return mapOfFeatures;
760 }
761 
771 std::map<std::string, std::map<int, vpImagePoint> > vpMbKltMultiTracker::getKltImagePointsWithId() const
772 {
773  std::map<std::string, std::map<int, vpImagePoint> > mapOfFeatures;
774 
775  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
776  it != m_mapOfKltTrackers.end(); ++it) {
777  mapOfFeatures[it->first] = it->second->getKltImagePointsWithId();
778  }
779 
780  return mapOfFeatures;
781 }
782 
788 std::map<std::string, vpKltOpencv> vpMbKltMultiTracker::getKltOpencv() const
789 {
790  std::map<std::string, vpKltOpencv> mapOfKltOpenCVTracker;
791 
792  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
793  it != m_mapOfKltTrackers.end(); ++it) {
794  mapOfKltOpenCVTracker[it->first] = it->second->getKltOpencv();
795  }
796 
797  return mapOfKltOpenCVTracker;
798 }
799 
805 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
806 std::map<std::string, std::vector<cv::Point2f> > vpMbKltMultiTracker::getKltPoints() const
807 {
808  std::map<std::string, std::vector<cv::Point2f> > mapOfFeatures;
809 
810  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
811  it != m_mapOfKltTrackers.end(); ++it) {
812  mapOfFeatures[it->first] = it->second->getKltPoints();
813  }
814 
815  return mapOfFeatures;
816 }
817 #else
818 std::map<std::string, CvPoint2D32f *> vpMbKltMultiTracker::getKltPoints()
819 {
820  std::map<std::string, CvPoint2D32f *> mapOfFeatures;
821 
822  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
823  it != m_mapOfKltTrackers.end(); ++it) {
824  mapOfFeatures[it->first] = it->second->getKltPoints();
825  }
826 
827  return mapOfFeatures;
828 }
829 #endif
830 
836 std::map<std::string, int> vpMbKltMultiTracker::getKltNbPoints() const
837 {
838  std::map<std::string, int> mapOfFeatures;
839 
840  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
841  it != m_mapOfKltTrackers.end(); ++it) {
842  mapOfFeatures[it->first] = it->second->getKltNbPoints();
843  }
844 
845  return mapOfFeatures;
846 }
847 
854 {
855  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
856  if (it != m_mapOfKltTrackers.end()) {
857  return it->second->getNbPolygon();
858  }
859 
860  std::cerr << "The reference camera: " << m_referenceCameraName << " cannot be found !" << std::endl;
861  return 0;
862 }
863 
870 std::map<std::string, unsigned int> vpMbKltMultiTracker::getMultiNbPolygon() const
871 {
872  std::map<std::string, unsigned int> mapOfNbPolygons;
873  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
874  it != m_mapOfKltTrackers.end(); ++it) {
875  mapOfNbPolygons[it->first] = it->second->getNbPolygon();
876  }
877 
878  return mapOfNbPolygons;
879 }
880 
888 {
889  if (m_mapOfKltTrackers.size() == 2) {
890  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
891  it->second->getPose(c1Mo);
892  ++it;
893 
894  it->second->getPose(c2Mo);
895  } else {
896  std::cerr << "Require two cameras ! There are " << m_mapOfKltTrackers.size() << " cameras !" << std::endl;
897  }
898 }
899 
908 void vpMbKltMultiTracker::getPose(const std::string &cameraName, vpHomogeneousMatrix &cMo_) const
909 {
910  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
911  if (it != m_mapOfKltTrackers.end()) {
912  it->second->getPose(cMo_);
913  } else {
914  std::cerr << "The camera: " << cameraName << " does not exist !" << std::endl;
915  }
916 }
917 
923 void vpMbKltMultiTracker::getPose(std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses) const
924 {
925  // Clear the map
926  mapOfCameraPoses.clear();
927 
928  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
929  it != m_mapOfKltTrackers.end(); ++it) {
930  vpHomogeneousMatrix cMo_;
931  it->second->getPose(cMo_);
932  mapOfCameraPoses[it->first] = cMo_;
933  }
934 }
935 
937 
938 #ifdef VISP_HAVE_MODULE_GUI
939 
949 void vpMbKltMultiTracker::initClick(const vpImage<unsigned char> &I, const std::vector<vpPoint> &points3D_list,
950  const std::string &displayFile)
951 {
952  if (m_mapOfKltTrackers.empty()) {
953  throw vpException(vpTrackingException::initializationError, "There is no camera !");
954  } else if (m_mapOfKltTrackers.size() > 1) {
955  throw vpException(vpTrackingException::initializationError, "There is more than one camera !");
956  } else {
957  // Get the vpMbKltTracker object for the reference camera name
958  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
959  if (it != m_mapOfKltTrackers.end()) {
960  it->second->initClick(I, points3D_list, displayFile);
961  it->second->getPose(cMo);
962 
963  // Init c0Mo
964  c0Mo = cMo;
965  ctTc0.eye();
966  } else {
967  std::stringstream ss;
968  ss << "Cannot initClick as the reference camera: " << m_referenceCameraName << " does not exist !";
969  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
970  }
971  }
972 }
973 
1002 void vpMbKltMultiTracker::initClick(const vpImage<unsigned char> &I, const std::string &initFile,
1003  const bool displayHelp)
1004 {
1005  if (m_mapOfKltTrackers.empty()) {
1006  throw vpException(vpTrackingException::initializationError, "There is no camera !");
1007  } else if (m_mapOfKltTrackers.size() > 1) {
1008  throw vpException(vpTrackingException::initializationError, "There is more than one camera !");
1009  } else {
1010  // Get the vpMbKltTracker object for the reference camera name
1011  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
1012  if (it != m_mapOfKltTrackers.end()) {
1013  it->second->initClick(I, initFile, displayHelp);
1014  it->second->getPose(cMo);
1015 
1016  // Init c0Mo
1017  c0Mo = cMo;
1018  ctTc0.eye();
1019  } else {
1020  std::stringstream ss;
1021  ss << "Cannot initClick as the reference camera: " << m_referenceCameraName << " does not exist !";
1022  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1023  }
1024  }
1025 }
1026 
1065  const std::string &initFile1, const std::string &initFile2, const bool displayHelp,
1066  const bool firstCameraIsReference)
1067 {
1068  if (m_mapOfKltTrackers.size() == 2) {
1069  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1070  it->second->initClick(I1, initFile1, displayHelp);
1071 
1072  if (firstCameraIsReference) {
1073  // Set the reference cMo
1074  it->second->getPose(cMo);
1075 
1076  // Set the reference camera parameters
1077  it->second->getCameraParameters(this->cam);
1078 
1079  // Init c0Mo and ctTc0
1080  c0Mo = cMo;
1081  ctTc0.eye();
1082  }
1083 
1084  ++it;
1085 
1086  it->second->initClick(I2, initFile2, displayHelp);
1087 
1088  if (!firstCameraIsReference) {
1089  // Set the reference cMo
1090  it->second->getPose(cMo);
1091 
1092  // Set the reference camera parameters
1093  it->second->getCameraParameters(this->cam);
1094 
1095  // Init c0Mo and ctTc0
1096  c0Mo = cMo;
1097  ctTc0.eye();
1098  }
1099  } else {
1100  std::stringstream ss;
1101  ss << "Cannot init click ! Require two cameras but there are " << m_mapOfKltTrackers.size() << " cameras !";
1102  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1103  }
1104 }
1105 
1135 void vpMbKltMultiTracker::initClick(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1136  const std::string &initFile, const bool displayHelp)
1137 {
1138  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1139  if (it_klt != m_mapOfKltTrackers.end()) {
1140  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img =
1141  mapOfImages.find(m_referenceCameraName);
1142 
1143  if (it_img != mapOfImages.end()) {
1144  it_klt->second->initClick(*it_img->second, initFile, displayHelp);
1145 
1146  // Set the reference cMo
1147  it_klt->second->getPose(cMo);
1148 
1149  // Init c0Mo
1150  c0Mo = cMo;
1151  ctTc0.eye();
1152 
1153  // Set the pose for the others cameras
1154  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1155  if (it_klt->first != m_referenceCameraName) {
1156  it_img = mapOfImages.find(it_klt->first);
1157  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
1158  m_mapOfCameraTransformationMatrix.find(it_klt->first);
1159 
1160  if (it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
1161  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1162  it_klt->second->cMo = cCurrentMo;
1163  it_klt->second->init(*it_img->second);
1164  } else {
1165  std::stringstream ss;
1166  ss << "Cannot init click ! Missing image for camera: " << m_referenceCameraName << " !";
1167  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1168  }
1169  }
1170  }
1171  } else {
1172  std::stringstream ss;
1173  ss << "Cannot init click ! Missing image for camera: " << m_referenceCameraName << " !";
1174  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1175  }
1176  } else {
1177  std::stringstream ss;
1178  ss << "Cannot init click ! The reference camera: " << m_referenceCameraName << " does not exist !";
1179  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1180  }
1181 }
1182 
1212 void vpMbKltMultiTracker::initClick(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1213  const std::map<std::string, std::string> &mapOfInitFiles, const bool displayHelp)
1214 {
1215  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1216  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img =
1217  mapOfImages.find(m_referenceCameraName);
1218  std::map<std::string, std::string>::const_iterator it_initFile = mapOfInitFiles.find(m_referenceCameraName);
1219 
1220  if (it_klt != m_mapOfKltTrackers.end() && it_img != mapOfImages.end() && it_initFile != mapOfInitFiles.end()) {
1221  it_klt->second->initClick(*it_img->second, it_initFile->second, displayHelp);
1222  it_klt->second->getPose(cMo);
1223 
1224  c0Mo = this->cMo;
1225  ctTc0.eye();
1226  } else {
1227  throw vpException(vpTrackingException::initializationError, "Cannot initClick for the reference camera !");
1228  }
1229 
1230  // Vector of missing initFile for cameras
1231  std::vector<std::string> vectorOfMissingCameraPoses;
1232 
1233  // Set pose for the specified cameras
1234  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1235  if (it_klt->first != m_referenceCameraName) {
1236  it_img = mapOfImages.find(it_klt->first);
1237  it_initFile = mapOfInitFiles.find(it_klt->first);
1238 
1239  if (it_img != mapOfImages.end() && it_initFile != mapOfInitFiles.end()) {
1240  // InitClick for the current camera
1241  it_klt->second->initClick(*it_img->second, it_initFile->second, displayHelp);
1242  } else {
1243  vectorOfMissingCameraPoses.push_back(it_klt->first);
1244  }
1245  }
1246  }
1247 
1248  // Set pose for cameras that do not have an initFile
1249  for (std::vector<std::string>::const_iterator it1 = vectorOfMissingCameraPoses.begin();
1250  it1 != vectorOfMissingCameraPoses.end(); ++it1) {
1251  it_img = mapOfImages.find(*it1);
1252  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
1254 
1255  if (it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
1256  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1257  m_mapOfKltTrackers[*it1]->cMo = cCurrentMo;
1258  m_mapOfKltTrackers[*it1]->init(*it_img->second);
1259  } else {
1260  std::stringstream ss;
1261  ss << "Missing image or missing camera transformation matrix ! Cannot "
1262  "set the pose for camera: "
1263  << (*it1) << " !";
1264  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1265  }
1266  }
1267 }
1268 #endif //#ifdef VISP_HAVE_MODULE_GUI
1269 
1288 void vpMbKltMultiTracker::initFromPose(const vpImage<unsigned char> &I, const std::string &initFile)
1289 {
1290  // Monocular case only !
1291  if (m_mapOfKltTrackers.size() > 1) {
1292  throw vpException(vpTrackingException::fatalError, "This function can only be used for the monocular case !");
1293  }
1294 
1295  char s[FILENAME_MAX];
1296  std::fstream finit;
1297  vpPoseVector init_pos;
1298 
1299  std::string ext = ".pos";
1300  size_t pos = initFile.rfind(ext);
1301 
1302  if (pos == initFile.size() - ext.size() && pos != 0)
1303  sprintf(s, "%s", initFile.c_str());
1304  else
1305  sprintf(s, "%s.pos", initFile.c_str());
1306 
1307  finit.open(s, std::ios::in);
1308  if (finit.fail()) {
1309  std::cerr << "cannot read " << s << std::endl;
1310  throw vpException(vpException::ioError, "cannot read init file");
1311  }
1312 
1313  for (unsigned int i = 0; i < 6; i += 1) {
1314  finit >> init_pos[i];
1315  }
1316 
1317  // Set the new pose for the reference camera
1318  cMo.buildFrom(init_pos);
1319  c0Mo = cMo;
1320  ctTc0.eye();
1321 
1322  // Init for the reference camera
1323  std::map<std::string, vpMbKltTracker *>::iterator it_ref = m_mapOfKltTrackers.find(m_referenceCameraName);
1324  if (it_ref == m_mapOfKltTrackers.end()) {
1325  throw vpException(vpTrackingException::initializationError, "Cannot find the reference camera !");
1326  }
1327 
1328  it_ref->second->initFromPose(I, cMo);
1329 }
1330 
1338 {
1339  this->cMo = cMo_;
1340  c0Mo = cMo;
1341  ctTc0.eye();
1342 
1343  // Init for the reference camera
1344  std::map<std::string, vpMbKltTracker *>::iterator it_ref = m_mapOfKltTrackers.find(m_referenceCameraName);
1345  if (it_ref == m_mapOfKltTrackers.end()) {
1346  throw vpException(vpTrackingException::initializationError, "Cannot find the reference camera !");
1347  }
1348 
1349  it_ref->second->initFromPose(I, cMo);
1350 }
1351 
1359 {
1360  vpHomogeneousMatrix _cMo(cPo);
1361  initFromPose(I, _cMo);
1362 }
1363 
1375  const vpHomogeneousMatrix &c1Mo, const vpHomogeneousMatrix &c2Mo,
1376  const bool firstCameraIsReference)
1377 {
1378  if (m_mapOfKltTrackers.size() == 2) {
1379  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1380  it->second->initFromPose(I1, c1Mo);
1381 
1382  ++it;
1383 
1384  it->second->initFromPose(I2, c2Mo);
1385 
1386  if (firstCameraIsReference) {
1387  this->cMo = c1Mo;
1388  } else {
1389  this->cMo = c2Mo;
1390  }
1391 
1392  c0Mo = this->cMo;
1393  ctTc0.eye();
1394  } else {
1395  std::stringstream ss;
1396  ss << "This method requires 2 cameras but there are " << m_mapOfKltTrackers.size() << " cameras !";
1398  }
1399 }
1400 
1408 void vpMbKltMultiTracker::initFromPose(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1409  const vpHomogeneousMatrix &cMo_)
1410 {
1411  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1412 
1413  if (it_klt != m_mapOfKltTrackers.end()) {
1414  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img = mapOfImages.find(it_klt->first);
1415 
1416  if (it_img != mapOfImages.end()) {
1417  it_klt->second->initFromPose(*it_img->second, cMo_);
1418 
1419  cMo = cMo_;
1420  c0Mo = this->cMo;
1421  ctTc0.eye();
1422 
1423  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1424  if (it_klt->first != m_referenceCameraName) {
1425  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
1426  m_mapOfCameraTransformationMatrix.find(it_klt->first);
1427  it_img = mapOfImages.find(it_klt->first);
1428 
1429  if (it_camTrans != m_mapOfCameraTransformationMatrix.end() && it_img != mapOfImages.end()) {
1430  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1431  it_klt->second->initFromPose(*it_img->second, cCurrentMo);
1432  } else {
1434  "Cannot find camera transformation matrix or image !");
1435  }
1436  }
1437  }
1438  } else {
1439  throw vpException(vpTrackingException::initializationError, "Cannot find image for reference camera !");
1440  }
1441  } else {
1442  std::stringstream ss;
1443  ss << "Cannot find reference camera: " << m_referenceCameraName << std::endl;
1445  }
1446 }
1447 
1454 void vpMbKltMultiTracker::initFromPose(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1455  const std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses)
1456 {
1457  // Set the reference cMo
1458  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1459  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img =
1460  mapOfImages.find(m_referenceCameraName);
1461  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camPose = mapOfCameraPoses.find(m_referenceCameraName);
1462 
1463  if (it_klt != m_mapOfKltTrackers.end() && it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
1464  it_klt->second->initFromPose(*it_img->second, it_camPose->second);
1465  it_klt->second->getPose(cMo);
1466 
1467  c0Mo = this->cMo;
1468  ctTc0.eye();
1469  } else {
1470  throw vpException(vpTrackingException::initializationError, "Cannot set pose for the reference camera !");
1471  }
1472 
1473  // Vector of missing pose matrices for cameras
1474  std::vector<std::string> vectorOfMissingCameraPoses;
1475 
1476  // Set pose for the specified cameras
1477  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1478  it_img = mapOfImages.find(it_klt->first);
1479  it_camPose = mapOfCameraPoses.find(it_klt->first);
1480 
1481  if (it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
1482  // Set pose
1483  it_klt->second->initFromPose(*it_img->second, it_camPose->second);
1484  } else {
1485  vectorOfMissingCameraPoses.push_back(it_klt->first);
1486  }
1487  }
1488 
1489  for (std::vector<std::string>::const_iterator it1 = vectorOfMissingCameraPoses.begin();
1490  it1 != vectorOfMissingCameraPoses.end(); ++it1) {
1491  it_img = mapOfImages.find(*it1);
1492  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
1494 
1495  if (it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
1496  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1497  m_mapOfKltTrackers[*it1]->initFromPose(*it_img->second, cCurrentMo);
1498  } else {
1499  std::stringstream ss;
1500  ss << "Missing image or missing camera transformation matrix ! Cannot "
1501  "set the pose for camera: "
1502  << (*it1) << " !";
1503  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1504  }
1505  }
1506 }
1507 
1555 void vpMbKltMultiTracker::loadConfigFile(const std::string &configFile)
1556 {
1557  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
1558  if (it != m_mapOfKltTrackers.end()) {
1559  // Load ConfigFile for reference camera
1560  it->second->loadConfigFile(configFile);
1561  it->second->getCameraParameters(cam);
1562 
1563  // Set clipping
1564  this->clippingFlag = it->second->getClipping();
1565  this->angleAppears = it->second->getAngleAppear();
1566  this->angleDisappears = it->second->getAngleDisappear();
1567  } else {
1568  std::stringstream ss;
1569  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
1570  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1571  }
1572 }
1573 
1590 void vpMbKltMultiTracker::loadConfigFile(const std::string &configFile1, const std::string &configFile2,
1591  const bool firstCameraIsReference)
1592 {
1593  if (m_mapOfKltTrackers.size() == 2) {
1594  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1595  it->second->loadConfigFile(configFile1);
1596 
1597  if (firstCameraIsReference) {
1598  it->second->getCameraParameters(cam);
1599 
1600  // Set clipping
1601  this->clippingFlag = it->second->getClipping();
1602  this->angleAppears = it->second->getAngleAppear();
1603  this->angleDisappears = it->second->getAngleDisappear();
1604  }
1605  ++it;
1606 
1607  it->second->loadConfigFile(configFile2);
1608 
1609  if (!firstCameraIsReference) {
1610  it->second->getCameraParameters(cam);
1611 
1612  // Set clipping
1613  this->clippingFlag = it->second->getClipping();
1614  this->angleAppears = it->second->getAngleAppear();
1615  this->angleDisappears = it->second->getAngleDisappear();
1616  }
1617  } else {
1618  std::stringstream ss;
1619  ss << "Cannot loadConfigFile. Require two cameras ! There are " << m_mapOfKltTrackers.size() << " cameras !";
1620  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1621  }
1622 }
1623 
1637 void vpMbKltMultiTracker::loadConfigFile(const std::map<std::string, std::string> &mapOfConfigFiles)
1638 {
1639  for (std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.begin();
1640  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1641  std::map<std::string, std::string>::const_iterator it_config = mapOfConfigFiles.find(it_klt->first);
1642  if (it_config != mapOfConfigFiles.end()) {
1643  it_klt->second->loadConfigFile(it_config->second);
1644  } else {
1645  std::stringstream ss;
1646  ss << "Missing configuration file for camera: " << it_klt->first << " !";
1647  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1648  }
1649  }
1650 
1651  // Set the reference camera parameters
1652  std::map<std::string, vpMbKltTracker *>::iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
1653  if (it != m_mapOfKltTrackers.end()) {
1654  it->second->getCameraParameters(cam);
1655 
1656  // Set clipping
1657  this->clippingFlag = it->second->getClipping();
1658  this->angleAppears = it->second->getAngleAppear();
1659  this->angleDisappears = it->second->getAngleDisappear();
1660  } else {
1661  std::stringstream ss;
1662  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
1663  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1664  }
1665 }
1666 
1692 void vpMbKltMultiTracker::loadModel(const std::string &modelFile, const bool verbose)
1693 {
1694  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1695  it != m_mapOfKltTrackers.end(); ++it) {
1696  it->second->loadModel(modelFile, verbose);
1697  }
1698 
1699  modelInitialised = true;
1700 }
1701 
1702 void vpMbKltMultiTracker::preTracking(std::map<std::string, const vpImage<unsigned char> *> &mapOfImages)
1703 {
1704  m_nbInfos = 0;
1705  m_nbFaceUsed = 0;
1706 
1707  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1708  it != m_mapOfKltTrackers.end(); ++it) {
1709  vpMbKltTracker *klt = it->second;
1710 
1711  try {
1712  klt->preTracking(*mapOfImages[it->first]);
1713  m_nbInfos += klt->m_nbInfos;
1714  m_nbFaceUsed += klt->m_nbFaceUsed;
1715  } catch (...) {
1716  }
1717  }
1718 }
1719 
1720 void vpMbKltMultiTracker::postTracking(std::map<std::string, const vpImage<unsigned char> *> &mapOfImages)
1721 {
1722 
1723  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1724  it != m_mapOfKltTrackers.end(); ++it) {
1725  vpMbKltTracker *klt = it->second;
1726 
1727  // Set the camera pose
1728  it->second->cMo = m_mapOfCameraTransformationMatrix[it->first] * cMo;
1729 
1730  if (klt->m_nbInfos > 0 && klt->postTracking(*mapOfImages[it->first], klt->m_w_klt)) {
1731  klt->reinit(*mapOfImages[it->first]);
1732 
1733  // set ctTc0 to identity
1734  if (it->first == m_referenceCameraName) {
1735  reinit(/*mapOfImages[it->first]*/);
1736  }
1737  }
1738  }
1739 }
1740 
1744 void vpMbKltMultiTracker::reinit(/* const vpImage<unsigned char>& I*/)
1745 {
1746  c0Mo = cMo;
1747  ctTc0.eye();
1748 }
1749 
1759 void vpMbKltMultiTracker::reInitModel(const vpImage<unsigned char> &I, const std::string &cad_name,
1760  const vpHomogeneousMatrix &cMo_, const bool verbose)
1761 {
1762  if (m_mapOfKltTrackers.size() != 1) {
1763  std::stringstream ss;
1764  ss << "This method requires exactly one camera, there are " << m_mapOfKltTrackers.size() << " cameras !";
1765  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
1766  }
1767 
1768  firstInitialisation = true;
1769  modelInitialised = true;
1770 
1771  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1772  if (it_klt != m_mapOfKltTrackers.end()) {
1773  it_klt->second->reInitModel(I, cad_name, cMo_, verbose);
1774 
1775  // Set reference pose
1776  it_klt->second->getPose(cMo);
1777 
1778  c0Mo = cMo;
1779  ctTc0.eye();
1780  } else {
1781  throw vpException(vpTrackingException::fatalError, "Cannot reInitModel the reference camera !");
1782  }
1783 }
1784 
1799  const std::string &cad_name, const vpHomogeneousMatrix &c1Mo,
1800  const vpHomogeneousMatrix &c2Mo, const bool verbose,
1801  const bool firstCameraIsReference)
1802 {
1803  if (m_mapOfKltTrackers.size() == 2) {
1804  std::map<std::string, vpMbKltTracker *>::const_iterator it_edge = m_mapOfKltTrackers.begin();
1805 
1806  it_edge->second->reInitModel(I1, cad_name, c1Mo, verbose);
1807 
1808  if (firstCameraIsReference) {
1809  // Set reference pose
1810  it_edge->second->getPose(cMo);
1811  }
1812 
1813  ++it_edge;
1814 
1815  it_edge->second->reInitModel(I2, cad_name, c2Mo, verbose);
1816 
1817  if (!firstCameraIsReference) {
1818  // Set reference pose
1819  it_edge->second->getPose(cMo);
1820  }
1821 
1822  c0Mo = cMo;
1823  ctTc0.eye();
1824  } else {
1825  throw vpException(vpTrackingException::fatalError, "This method requires exactly two cameras !");
1826  }
1827 }
1828 
1839 void vpMbKltMultiTracker::reInitModel(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1840  const std::string &cad_name,
1841  const std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses,
1842  const bool verbose)
1843 {
1844  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1845  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img =
1846  mapOfImages.find(m_referenceCameraName);
1847  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camPose = mapOfCameraPoses.find(m_referenceCameraName);
1848 
1849  if (it_klt != m_mapOfKltTrackers.end() && it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
1850  it_klt->second->reInitModel(*it_img->second, cad_name, it_camPose->second, verbose);
1851  modelInitialised = true;
1852 
1853  // Set reference pose
1854  it_klt->second->getPose(cMo);
1855 
1856  c0Mo = cMo;
1857  ctTc0.eye();
1858  } else {
1859  throw vpException(vpTrackingException::fatalError, "Cannot reInitModel for reference camera !");
1860  }
1861 
1862  std::vector<std::string> vectorOfMissingCameras;
1863  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1864  if (it_klt->first != m_referenceCameraName) {
1865  it_img = mapOfImages.find(it_klt->first);
1866  it_camPose = mapOfCameraPoses.find(it_klt->first);
1867 
1868  if (it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
1869  it_klt->second->reInitModel(*it_img->second, cad_name, it_camPose->second, verbose);
1870  } else {
1871  vectorOfMissingCameras.push_back(it_klt->first);
1872  }
1873  }
1874  }
1875 
1876  for (std::vector<std::string>::const_iterator it = vectorOfMissingCameras.begin(); it != vectorOfMissingCameras.end();
1877  ++it) {
1878  it_img = mapOfImages.find(*it);
1879  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
1881 
1882  if (it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
1883  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1884  m_mapOfKltTrackers[*it]->reInitModel(*it_img->second, cad_name, cCurrentMo, verbose);
1885  }
1886  }
1887 }
1888 
1894 {
1895  cMo.eye();
1896  ctTc0.eye();
1897 
1898  // Call resetTracker for all the cameras
1899  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1900  it != m_mapOfKltTrackers.end(); ++it) {
1901  it->second->resetTracker();
1902  }
1903 
1904  m_computeInteraction = true;
1905  firstInitialisation = true;
1906  computeCovariance = false;
1907 
1908  angleAppears = vpMath::rad(65);
1910 
1912 
1913  maskBorder = 5;
1914  threshold_outlier = 0.5;
1915  percentGood = 0.7;
1916 
1917  m_lambda = 0.8;
1918  m_maxIter = 200;
1919 
1921 
1922  useScanLine = false;
1923 
1924 #ifdef VISP_HAVE_OGRE
1925  useOgre = false;
1926 #endif
1927 }
1928 
1939 {
1941 
1942  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1943  it != m_mapOfKltTrackers.end(); ++it) {
1944  it->second->setAngleAppear(a);
1945  }
1946 }
1947 
1958 {
1960 
1961  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1962  it != m_mapOfKltTrackers.end(); ++it) {
1963  it->second->setAngleDisappear(a);
1964  }
1965 }
1966 
1973 {
1974  if (m_mapOfKltTrackers.empty()) {
1975  throw vpException(vpTrackingException::fatalError, "There is no camera !");
1976  } else if (m_mapOfKltTrackers.size() > 1) {
1977  throw vpException(vpTrackingException::fatalError, "There is more than one camera !");
1978  } else {
1979  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
1980  if (it != m_mapOfKltTrackers.end()) {
1981  it->second->setCameraParameters(camera);
1982 
1983  // Set reference camera parameters
1984  this->cam = camera;
1985  } else {
1986  std::stringstream ss;
1987  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
1989  }
1990  }
1991 }
1992 
2002  const bool firstCameraIsReference)
2003 {
2004  if (m_mapOfKltTrackers.empty()) {
2005  throw vpException(vpTrackingException::fatalError, "There is no camera !");
2006  } else if (m_mapOfKltTrackers.size() == 2) {
2007  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2008  it->second->setCameraParameters(camera1);
2009 
2010  ++it;
2011  it->second->setCameraParameters(camera2);
2012 
2013  if (firstCameraIsReference) {
2014  this->cam = camera1;
2015  } else {
2016  this->cam = camera2;
2017  }
2018  } else {
2019  std::stringstream ss;
2020  ss << "Require two cameras ! There are " << m_mapOfKltTrackers.size() << " cameras !";
2022  }
2023 }
2024 
2031 void vpMbKltMultiTracker::setCameraParameters(const std::string &cameraName, const vpCameraParameters &camera)
2032 {
2033  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
2034  if (it != m_mapOfKltTrackers.end()) {
2035  it->second->setCameraParameters(camera);
2036 
2037  if (it->first == m_referenceCameraName) {
2038  this->cam = camera;
2039  }
2040  } else {
2041  std::stringstream ss;
2042  ss << "The camera: " << cameraName << " does not exist !";
2044  }
2045 }
2046 
2052 void vpMbKltMultiTracker::setCameraParameters(const std::map<std::string, vpCameraParameters> &mapOfCameraParameters)
2053 {
2054  for (std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.begin();
2055  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2056  std::map<std::string, vpCameraParameters>::const_iterator it_cam = mapOfCameraParameters.find(it_klt->first);
2057  if (it_cam != mapOfCameraParameters.end()) {
2058  it_klt->second->setCameraParameters(it_cam->second);
2059 
2060  if (it_klt->first == m_referenceCameraName) {
2061  this->cam = it_cam->second;
2062  }
2063  } else {
2064  std::stringstream ss;
2065  ss << "Missing camera parameters for camera: " << it_klt->first << " !";
2067  }
2068  }
2069 }
2070 
2079 void vpMbKltMultiTracker::setCameraTransformationMatrix(const std::string &cameraName,
2080  const vpHomogeneousMatrix &cameraTransformationMatrix)
2081 {
2082  std::map<std::string, vpHomogeneousMatrix>::iterator it = m_mapOfCameraTransformationMatrix.find(cameraName);
2083  if (it != m_mapOfCameraTransformationMatrix.end()) {
2084  it->second = cameraTransformationMatrix;
2085  } else {
2086  std::stringstream ss;
2087  ss << "Cannot find camera: " << cameraName << " !";
2089  }
2090 }
2091 
2100  const std::map<std::string, vpHomogeneousMatrix> &mapOfTransformationMatrix)
2101 {
2102  // Check if all cameras have a transformation matrix
2103  for (std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.begin();
2104  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2105  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
2106  mapOfTransformationMatrix.find(it_klt->first);
2107 
2108  if (it_camTrans == mapOfTransformationMatrix.end()) {
2109  throw vpException(vpTrackingException::initializationError, "Missing camera transformation matrix !");
2110  }
2111  }
2112 
2113  m_mapOfCameraTransformationMatrix = mapOfTransformationMatrix;
2114 }
2115 
2123 void vpMbKltMultiTracker::setClipping(const unsigned int &flags)
2124 {
2125  vpMbTracker::setClipping(flags);
2126 
2127  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2128  it != m_mapOfKltTrackers.end(); ++it) {
2129  it->second->setClipping(flags);
2130  }
2131 }
2132 
2141 void vpMbKltMultiTracker::setClipping(const std::string &cameraName, const unsigned int &flags)
2142 {
2143  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
2144  if (it != m_mapOfKltTrackers.end()) {
2145  it->second->setClipping(flags);
2146  } else {
2147  std::cerr << "Camera: " << cameraName << " does not exist !" << std::endl;
2148  }
2149 }
2150 
2157 {
2159 
2160  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2161  it != m_mapOfKltTrackers.end(); ++it) {
2162  it->second->setCovarianceComputation(flag);
2163  }
2164 }
2165 
2172 {
2173  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2174  it != m_mapOfKltTrackers.end(); ++it) {
2175  it->second->setDisplayFeatures(displayF);
2176  }
2177 
2178  displayFeatures = displayF;
2179 }
2180 
2187 {
2189 
2190  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2191  it != m_mapOfKltTrackers.end(); ++it) {
2192  it->second->setFarClippingDistance(dist);
2193  }
2194 }
2195 
2202 void vpMbKltMultiTracker::setFarClippingDistance(const std::string &cameraName, const double &dist)
2203 {
2204  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
2205  if (it != m_mapOfKltTrackers.end()) {
2206  it->second->setFarClippingDistance(dist);
2207  } else {
2208  std::cerr << "Camera: " << cameraName << " does not exist !" << std::endl;
2209  }
2210 }
2211 
2212 #ifdef VISP_HAVE_OGRE
2213 
2223 {
2224  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2225  it != m_mapOfKltTrackers.end(); ++it) {
2226  it->second->setGoodNbRayCastingAttemptsRatio(ratio);
2227  }
2228 }
2229 
2240 {
2241  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2242  it != m_mapOfKltTrackers.end(); ++it) {
2243  it->second->setNbRayCastingAttemptsForVisibility(attempts);
2244  }
2245 }
2246 #endif
2247 
2254 {
2255  for (std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.begin();
2256  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2257  it_klt->second->setKltOpencv(t);
2258  }
2259 }
2260 
2266 void vpMbKltMultiTracker::setKltOpencv(const std::map<std::string, vpKltOpencv> &mapOfOpenCVTrackers)
2267 {
2268  for (std::map<std::string, vpKltOpencv>::const_iterator it_kltOpenCV = mapOfOpenCVTrackers.begin();
2269  it_kltOpenCV != mapOfOpenCVTrackers.end(); ++it_kltOpenCV) {
2270  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(it_kltOpenCV->first);
2271  if (it_klt != m_mapOfKltTrackers.end()) {
2272  it_klt->second->setKltOpencv(it_kltOpenCV->second);
2273  } else {
2274  std::cerr << "The camera: " << it_kltOpenCV->first << " does not exist !" << std::endl;
2275  }
2276  }
2277 }
2278 
2290 void vpMbKltMultiTracker::setLod(const bool useLod, const std::string &name)
2291 {
2292  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2293  it != m_mapOfKltTrackers.end(); ++it) {
2294  it->second->setLod(useLod, name);
2295  }
2296 }
2297 
2310 void vpMbKltMultiTracker::setLod(const bool useLod, const std::string &cameraName, const std::string &name)
2311 {
2312  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(cameraName);
2313 
2314  if (it_klt != m_mapOfKltTrackers.end()) {
2315  it_klt->second->setLod(useLod, name);
2316  } else {
2317  std::cerr << "The camera: " << cameraName << " cannot be found !" << std::endl;
2318  }
2319 }
2320 
2326 void vpMbKltMultiTracker::setKltMaskBorder(const unsigned int &e)
2327 {
2328  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2329  it != m_mapOfKltTrackers.end(); ++it) {
2330  it->second->setKltMaskBorder(e);
2331  }
2332 
2333  maskBorder = e;
2334 }
2335 
2339 void vpMbKltMultiTracker::setMinLineLengthThresh(const double /*minLineLengthThresh*/, const std::string & /*name*/)
2340 {
2341  std::cerr << "Useless for KLT tracker !" << std::endl;
2342 }
2343 
2352 void vpMbKltMultiTracker::setMinPolygonAreaThresh(const double minPolygonAreaThresh, const std::string &name)
2353 {
2354  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2355  it != m_mapOfKltTrackers.end(); ++it) {
2356  it->second->setMinPolygonAreaThresh(minPolygonAreaThresh, name);
2357  }
2358 }
2359 
2370 void vpMbKltMultiTracker::setMinPolygonAreaThresh(const double minPolygonAreaThresh, const std::string &cameraName,
2371  const std::string &name)
2372 {
2373  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(cameraName);
2374 
2375  if (it_klt != m_mapOfKltTrackers.end()) {
2376  it_klt->second->setMinPolygonAreaThresh(minPolygonAreaThresh, name);
2377  } else {
2378  std::cerr << "The camera: " << cameraName << " cannot be found !" << std::endl;
2379  }
2380 }
2381 
2388 {
2390 
2391  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2392  it != m_mapOfKltTrackers.end(); ++it) {
2393  it->second->setNearClippingDistance(dist);
2394  }
2395 }
2396 
2407 void vpMbKltMultiTracker::setOgreShowConfigDialog(const bool showConfigDialog)
2408 {
2409  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2410  it != m_mapOfKltTrackers.end(); ++it) {
2411  it->second->setOgreShowConfigDialog(showConfigDialog);
2412  }
2413 }
2414 
2424 {
2425  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2426  it != m_mapOfKltTrackers.end(); ++it) {
2427  it->second->setOgreVisibilityTest(v);
2428  }
2429 
2430 #ifdef VISP_HAVE_OGRE
2431  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2432  it != m_mapOfKltTrackers.end(); ++it) {
2433  it->second->faces.getOgreContext()->setWindowName("Multi MBT Klt (" + it->first + ")");
2434  }
2435 #endif
2436 
2437  useOgre = v;
2438 }
2439 
2446 void vpMbKltMultiTracker::setNearClippingDistance(const std::string &cameraName, const double &dist)
2447 {
2448  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
2449  if (it != m_mapOfKltTrackers.end()) {
2450  it->second->setNearClippingDistance(dist);
2451  } else {
2452  std::cerr << "Camera: " << cameraName << " does not exist !" << std::endl;
2453  }
2454 }
2455 
2462 {
2463  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2464  it != m_mapOfKltTrackers.end(); ++it) {
2465  it->second->setOptimizationMethod(opt);
2466  }
2467 
2468  m_optimizationMethod = opt;
2469 }
2470 
2479 {
2480  if (m_mapOfKltTrackers.size() == 1) {
2481  std::map<std::string, vpMbKltTracker *>::iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
2482  if (it != m_mapOfKltTrackers.end()) {
2483  it->second->setPose(I, cMo_);
2484  this->cMo = cMo_;
2485 
2486  c0Mo = this->cMo;
2487  ctTc0.eye();
2488  } else {
2489  std::stringstream ss;
2490  ss << "Cannot find the reference camera: " << m_referenceCameraName << " !";
2492  }
2493  } else {
2494  std::stringstream ss;
2495  ss << "You are trying to set the pose with only one image and cMo "
2496  "but there are multiple cameras !";
2498  }
2499 }
2500 
2513  const vpHomogeneousMatrix &c1Mo, const vpHomogeneousMatrix &c2Mo,
2514  const bool firstCameraIsReference)
2515 {
2516  if (m_mapOfKltTrackers.size() == 2) {
2517  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2518  it->second->setPose(I1, c1Mo);
2519 
2520  ++it;
2521 
2522  it->second->setPose(I2, c2Mo);
2523 
2524  if (firstCameraIsReference) {
2525  this->cMo = c1Mo;
2526  } else {
2527  this->cMo = c2Mo;
2528  }
2529 
2530  c0Mo = this->cMo;
2531  ctTc0.eye();
2532  } else {
2533  std::stringstream ss;
2534  ss << "This method requires 2 cameras but there are " << m_mapOfKltTrackers.size() << " cameras !";
2536  }
2537 }
2538 
2547 void vpMbKltMultiTracker::setPose(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
2548  const vpHomogeneousMatrix &cMo_)
2549 {
2550  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
2551  if (it_klt != m_mapOfKltTrackers.end()) {
2552  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img =
2553  mapOfImages.find(m_referenceCameraName);
2554 
2555  if (it_img != mapOfImages.end()) {
2556  // Set pose on reference camera
2557  it_klt->second->setPose(*it_img->second, cMo_);
2558 
2559  // Set the reference cMo
2560  cMo = cMo_;
2561 
2562  c0Mo = this->cMo;
2563  ctTc0.eye();
2564 
2565  // Set the pose for the others cameras
2566  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2567  if (it_klt->first != m_referenceCameraName) {
2568  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
2569  m_mapOfCameraTransformationMatrix.find(it_klt->first);
2570  it_img = mapOfImages.find(it_klt->first);
2571 
2572  if (it_camTrans != m_mapOfCameraTransformationMatrix.end() && it_img != mapOfImages.end()) {
2573  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
2574  it_klt->second->setPose(*it_img->second, cCurrentMo);
2575  } else {
2576  throw vpException(vpTrackingException::fatalError, "Cannot find camera transformation matrix or image !");
2577  }
2578  }
2579  }
2580  } else {
2581  std::stringstream ss;
2582  ss << "Missing image for reference camera: " << m_referenceCameraName << " !";
2583  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2584  }
2585  } else {
2586  std::stringstream ss;
2587  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
2588  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2589  }
2590 }
2591 
2602 void vpMbKltMultiTracker::setPose(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
2603  const std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses)
2604 {
2605  // Set the reference cMo
2606  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
2607  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img =
2608  mapOfImages.find(m_referenceCameraName);
2609  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camPose = mapOfCameraPoses.find(m_referenceCameraName);
2610 
2611  if (it_klt != m_mapOfKltTrackers.end() && it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
2612  it_klt->second->setPose(*it_img->second, it_camPose->second);
2613  it_klt->second->getPose(cMo);
2614 
2615  c0Mo = this->cMo;
2616  ctTc0.eye();
2617  } else {
2618  throw vpException(vpTrackingException::fatalError, "Cannot set pose for the reference camera !");
2619  }
2620 
2621  // Vector of missing pose matrices for cameras
2622  std::vector<std::string> vectorOfMissingCameraPoses;
2623 
2624  // Set pose for the specified cameras
2625  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2626  if (it_klt->first != m_referenceCameraName) {
2627  it_img = mapOfImages.find(it_klt->first);
2628  it_camPose = mapOfCameraPoses.find(it_klt->first);
2629 
2630  if (it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
2631  // Set pose
2632  it_klt->second->setPose(*it_img->second, it_camPose->second);
2633  } else {
2634  vectorOfMissingCameraPoses.push_back(it_klt->first);
2635  }
2636  }
2637  }
2638 
2639  for (std::vector<std::string>::const_iterator it1 = vectorOfMissingCameraPoses.begin();
2640  it1 != vectorOfMissingCameraPoses.end(); ++it1) {
2641  it_img = mapOfImages.find(*it1);
2642  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
2644 
2645  if (it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
2646  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
2647  m_mapOfKltTrackers[*it1]->setPose(*it_img->second, cCurrentMo);
2648  } else {
2649  std::stringstream ss;
2650  ss << "Missing image or missing camera transformation matrix ! Cannot "
2651  "set the pose for camera: "
2652  << (*it1) << " !";
2653  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2654  }
2655  }
2656 }
2657 
2663 void vpMbKltMultiTracker::setReferenceCameraName(const std::string &referenceCameraName)
2664 {
2665  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(referenceCameraName);
2666  if (it != m_mapOfKltTrackers.end()) {
2667  m_referenceCameraName = referenceCameraName;
2668  } else {
2669  std::stringstream ss;
2670  ss << "The reference camera: " << referenceCameraName << " does not exist !";
2671  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2672  }
2673 }
2674 
2681 {
2682  // Set general setScanLineVisibilityTest
2684 
2685  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2686  it != m_mapOfKltTrackers.end(); ++it) {
2687  it->second->setScanLineVisibilityTest(v);
2688  }
2689 }
2690 
2697 {
2698  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2699  it != m_mapOfKltTrackers.end(); ++it) {
2700  it->second->setKltThresholdAcceptation(th);
2701  }
2702 
2703  threshold_outlier = th;
2704 }
2705 
2713 void vpMbKltMultiTracker::setUseKltTracking(const std::string &name, const bool &useKltTracking)
2714 {
2715  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2716  it != m_mapOfKltTrackers.end(); ++it) {
2717  it->second->setUseKltTracking(name, useKltTracking);
2718  }
2719 }
2720 
2729 {
2730  // Track only with reference camera
2731  // Get the reference camera parameters
2732  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
2733 
2734  if (it != m_mapOfKltTrackers.end()) {
2735  it->second->track(I);
2736  it->second->getPose(cMo);
2737  } else {
2738  std::stringstream ss;
2739  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
2740  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2741  }
2742 }
2743 
2753 {
2754  if (m_mapOfKltTrackers.size() == 2) {
2755  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2756  std::map<std::string, const vpImage<unsigned char> *> mapOfImages;
2757  mapOfImages[it->first] = &I1;
2758  ++it;
2759 
2760  mapOfImages[it->first] = &I2;
2761  track(mapOfImages);
2762  } else {
2763  std::stringstream ss;
2764  ss << "Require two cameras ! There are " << m_mapOfKltTrackers.size() << " cameras !";
2765  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2766  }
2767 }
2768 
2776 void vpMbKltMultiTracker::track(std::map<std::string, const vpImage<unsigned char> *> &mapOfImages)
2777 {
2778  // Check if there is an image for each camera
2779  for (std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.begin();
2780  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2781  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img = mapOfImages.find(it_klt->first);
2782 
2783  if (it_img == mapOfImages.end()) {
2784  throw vpException(vpTrackingException::fatalError, "Missing images !");
2785  }
2786  }
2787 
2788  preTracking(mapOfImages);
2789 
2790  if (m_nbInfos < 4 || m_nbFaceUsed == 0) {
2791  throw vpTrackingException(vpTrackingException::notEnoughPointError, "Error: not enough features");
2792  }
2793 
2794  computeVVS();
2795 
2796  postTracking(mapOfImages);
2797 }
2798 
2800 
2806 std::map<std::string, int> vpMbKltMultiTracker::getNbKltPoints() const { return getKltNbPoints(); }
2807 
2814 void vpMbKltMultiTracker::setMaskBorder(const unsigned int &e) { setKltMaskBorder(e); }
2815 
2822 
2823 #elif !defined(VISP_BUILD_SHARED_LIBS)
2824 // Work arround to avoid warning:
2825 // libvisp_mbt.a(dummy_vpMbKltMultiTracker.cpp.o) has no symbols
2826 void dummy_vpMbKltMultiTracker(){};
2827 #endif // VISP_HAVE_OPENCV
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:223
bool m_computeInteraction
Definition: vpMbTracker.h:187
void setGoodNbRayCastingAttemptsRatio(const double &ratio)
virtual void setCovarianceComputation(const bool &flag)
Definition: vpMbTracker.h:452
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
virtual std::vector< std::string > getCameraNames() const
virtual std::map< std::string, int > getKltNbPoints() const
vpMatrix m_L_kltMulti
Interaction matrix.
virtual std::list< vpMbtDistanceKltCylinder * > & getFeaturesKltCylinder()
virtual void setFarClippingDistance(const double &dist)
virtual void initFromPose(const vpImage< unsigned char > &I, const std::string &initFile)
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 void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo)
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:433
vpMbHiddenFaces< vpMbtPolygon > faces
Set of faces describing the object.
Definition: vpMbTracker.h:145
virtual void setAngleDisappear(const double &a)
virtual std::map< std::string, std::map< int, vpImagePoint > > getKltImagePointsWithId() const
virtual void loadConfigFile(const std::string &configFile)
Implementation of an homogeneous matrix and operations on such kind of matrices.
virtual std::list< vpMbtDistanceKltPoints * > & getFeaturesKlt()
virtual std::map< std::string, std::vector< vpImagePoint > > getKltImagePoints() const
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
Definition: vpArray2D.h:171
Class to define colors available for display functionnalities.
Definition: vpColor.h:120
std::list< vpMbtDistanceKltCylinder * > kltCylinders
vpHomogeneousMatrix cMo
The current pose.
Definition: vpMbTracker.h:115
virtual void setKltThresholdAcceptation(const double th)
bool postTracking(const vpImage< unsigned char > &I, vpColVector &w)
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
Definition: vpMbTracker.h:125
error that can be emited by ViSP classes.
Definition: vpException.h:71
virtual void setThresholdAcceptation(const double th)
virtual void computeVVSInteractionMatrixAndResidu()
unsigned int getRows() const
Definition: vpArray2D.h:156
vpHomogeneousMatrix inverse() const
bool useOgre
Use Ogre3d for visibility tests.
Definition: vpMbTracker.h:157
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:130
virtual void reinit(const vpImage< unsigned char > &I)
virtual void computeVVSInteractionMatrixAndResidu()
Class that defines what is a point.
Definition: vpPoint.h:58
vpCameraParameters cam
The camera parameters.
Definition: vpMbTracker.h:113
std::map< std::string, vpHomogeneousMatrix > m_mapOfCameraTransformationMatrix
vpHomogeneousMatrix ctTc0
virtual void setReferenceCameraName(const std::string &referenceCameraName)
virtual unsigned int getNbPolygon() const
virtual std::map< std::string, int > getNbKltPoints() const
virtual vpHomogeneousMatrix getPose() const
Definition: vpMbTracker.h:390
virtual void setKltMaskBorder(const unsigned int &e)
virtual std::map< std::string, vpKltOpencv > getKltOpencv() const
vpRobust m_robust_klt
Robust.
void insert(const vpMatrix &A, const unsigned int r, const unsigned int c)
Definition: vpMatrix.cpp:4512
vpMatrix oJo
The Degrees of Freedom to estimate.
Definition: vpMbTracker.h:117
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 void setOgreShowConfigDialog(const bool showConfigDialog)
virtual std::list< vpMbtDistanceCircle * > & getFeaturesCircle()
bool useScanLine
Use Scanline for visibility tests.
Definition: vpMbTracker.h:160
vpVelocityTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
virtual void setMinLineLengthThresh(const double minLineLengthThresh, const std::string &name="")
vpColVector m_weightedError_kltMulti
Weighted error.
unsigned int maskBorder
Erosion of the mask.
Generic class defining intrinsic camera parameters.
unsigned int m_nbFaceUsed
virtual void preTracking(std::map< std::string, const vpImage< unsigned char > *> &mapOfImages)
double m_initialMu
Initial Mu for Levenberg Marquardt optimization loop.
Definition: vpMbTracker.h:195
double m_lambda
Gain of the virtual visual servoing stage.
Definition: vpMbTracker.h:189
virtual void computeVVSWeights(vpRobust &robust, const vpColVector &error, vpColVector &w)
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:422
virtual void setAngleAppear(const double &a)
Model based tracker using only KLT.
vpMbtOptimizationMethod m_optimizationMethod
Optimization method used.
Definition: vpMbTracker.h:142
std::string m_referenceCameraName
Name of the reference camera.
double angleAppears
Angle used to detect a face appearance.
Definition: vpMbTracker.h:147
unsigned int m_maxIter
Maximum number of iterations of the virtual visual servoing stage.
Definition: vpMbTracker.h:191
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
virtual void postTracking(std::map< std::string, const vpImage< unsigned char > *> &mapOfImages)
static double rad(double deg)
Definition: vpMath.h:102
double threshold_outlier
vpColVector m_w_kltMulti
Robust weights.
virtual void setKltOpencv(const vpKltOpencv &t)
void insert(unsigned int i, const vpColVector &v)
virtual void setNearClippingDistance(const double &dist)
virtual void setCameraParameters(const vpCameraParameters &camera)
virtual void setOgreVisibilityTest(const bool &v)
vpColVector m_w_klt
Robust weights.
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:140
void preTracking(const vpImage< unsigned char > &I)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
std::map< std::string, vpMbKltTracker * > m_mapOfKltTrackers
Map of Model-based klt trackers.
virtual std::map< std::string, unsigned int > getMultiNbPolygon() const
virtual void loadModel(const std::string &modelFile, const bool verbose=false)
virtual void computeVVSPoseEstimation(const bool isoJoIdentity_, const unsigned int iter, vpMatrix &L, vpMatrix &LTL, vpColVector &R, const vpColVector &error, vpColVector &error_prev, vpColVector &LTR, double &mu, vpColVector &v, const vpColVector *const w=NULL, vpColVector *const m_w_prev=NULL)
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:92
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:78
static vpHomogeneousMatrix direct(const vpColVector &v)
virtual void reInitModel(const vpImage< unsigned char > &I, const std::string &cad_name, const vpHomogeneousMatrix &cMo_, const bool verbose=false)
virtual void computeCovarianceMatrixVVS(const bool isoJoIdentity_, const vpColVector &w_true, const vpHomogeneousMatrix &cMoPrev, const vpMatrix &L_true, const vpMatrix &LVJ_true, const vpColVector &error)
double angleDisappears
Angle used to detect a face disappearance.
Definition: vpMbTracker.h:149
virtual void computeVVSInit()
virtual void setScanLineVisibilityTest(const bool &v)
Definition: vpMbTracker.h:533
virtual void getCameraParameters(vpCameraParameters &camera) const
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="")
unsigned int m_nbInfos
virtual void computeVVSWeights()
unsigned int clippingFlag
Flags specifying which clipping to used.
Definition: vpMbTracker.h:155
virtual std::map< std::string, std::vector< cv::Point2f > > getKltPoints() const
vpColVector m_error_klt
(s - s*)
vpColVector m_error_kltMulti
(s - s*)
virtual void setFarClippingDistance(const double &dist)
virtual void setMaskBorder(const unsigned int &e)
virtual void computeVVSCheckLevenbergMarquardt(const unsigned int iter, vpColVector &error, const vpColVector &m_error_prev, const vpHomogeneousMatrix &cMoPrev, double &mu, bool &reStartFromLastIncrement, vpColVector *const w=NULL, const vpColVector *const m_w_prev=NULL)
std::list< vpMbtDistanceKltPoints * > kltPolygons
bool isoJoIdentity
Boolean to know if oJo is identity (for fast computation)
Definition: vpMbTracker.h:119
vpHomogeneousMatrix c0Mo
Initial pose.
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:241
virtual void setNearClippingDistance(const double &dist)
vpMatrix m_L_klt
Interaction matrix.