Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpMbKltMultiTracker.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://visp.inria.fr 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  *
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  * Description:
32  * Model-based klt tracker with multiple cameras.
33  *
34  * Authors:
35  * Souriya Trinh
36  *
37  *****************************************************************************/
38 
44 #include <visp3/core/vpConfig.h>
45 
46 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
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 
1005 void vpMbKltMultiTracker::initClick(const vpImage<unsigned char> &I, const std::string &initFile,
1006  const bool displayHelp, const vpHomogeneousMatrix &T)
1007 {
1008  if (m_mapOfKltTrackers.empty()) {
1009  throw vpException(vpTrackingException::initializationError, "There is no camera !");
1010  } else if (m_mapOfKltTrackers.size() > 1) {
1011  throw vpException(vpTrackingException::initializationError, "There is more than one camera !");
1012  } else {
1013  // Get the vpMbKltTracker object for the reference camera name
1014  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
1015  if (it != m_mapOfKltTrackers.end()) {
1016  it->second->initClick(I, initFile, displayHelp, T);
1017  it->second->getPose(cMo);
1018 
1019  // Init c0Mo
1020  c0Mo = cMo;
1021  ctTc0.eye();
1022  } else {
1023  std::stringstream ss;
1024  ss << "Cannot initClick as the reference camera: " << m_referenceCameraName << " does not exist !";
1025  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1026  }
1027  }
1028 }
1029 
1068  const std::string &initFile1, const std::string &initFile2, const bool displayHelp,
1069  const bool firstCameraIsReference)
1070 {
1071  if (m_mapOfKltTrackers.size() == 2) {
1072  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1073  it->second->initClick(I1, initFile1, displayHelp);
1074 
1075  if (firstCameraIsReference) {
1076  // Set the reference cMo
1077  it->second->getPose(cMo);
1078 
1079  // Set the reference camera parameters
1080  it->second->getCameraParameters(this->cam);
1081 
1082  // Init c0Mo and ctTc0
1083  c0Mo = cMo;
1084  ctTc0.eye();
1085  }
1086 
1087  ++it;
1088 
1089  it->second->initClick(I2, initFile2, displayHelp);
1090 
1091  if (!firstCameraIsReference) {
1092  // Set the reference cMo
1093  it->second->getPose(cMo);
1094 
1095  // Set the reference camera parameters
1096  it->second->getCameraParameters(this->cam);
1097 
1098  // Init c0Mo and ctTc0
1099  c0Mo = cMo;
1100  ctTc0.eye();
1101  }
1102  } else {
1103  std::stringstream ss;
1104  ss << "Cannot init click ! Require two cameras but there are " << m_mapOfKltTrackers.size() << " cameras !";
1105  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1106  }
1107 }
1108 
1138 void vpMbKltMultiTracker::initClick(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1139  const std::string &initFile, const bool displayHelp)
1140 {
1141  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1142  if (it_klt != m_mapOfKltTrackers.end()) {
1143  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img =
1144  mapOfImages.find(m_referenceCameraName);
1145 
1146  if (it_img != mapOfImages.end()) {
1147  it_klt->second->initClick(*it_img->second, initFile, displayHelp);
1148 
1149  // Set the reference cMo
1150  it_klt->second->getPose(cMo);
1151 
1152  // Init c0Mo
1153  c0Mo = cMo;
1154  ctTc0.eye();
1155 
1156  // Set the pose for the others cameras
1157  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1158  if (it_klt->first != m_referenceCameraName) {
1159  it_img = mapOfImages.find(it_klt->first);
1160  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
1161  m_mapOfCameraTransformationMatrix.find(it_klt->first);
1162 
1163  if (it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
1164  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1165  it_klt->second->cMo = cCurrentMo;
1166  it_klt->second->init(*it_img->second);
1167  } else {
1168  std::stringstream ss;
1169  ss << "Cannot init click ! Missing image for camera: " << m_referenceCameraName << " !";
1170  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1171  }
1172  }
1173  }
1174  } else {
1175  std::stringstream ss;
1176  ss << "Cannot init click ! Missing image for camera: " << m_referenceCameraName << " !";
1177  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1178  }
1179  } else {
1180  std::stringstream ss;
1181  ss << "Cannot init click ! The reference camera: " << m_referenceCameraName << " does not exist !";
1182  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1183  }
1184 }
1185 
1215 void vpMbKltMultiTracker::initClick(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1216  const std::map<std::string, std::string> &mapOfInitFiles, const bool displayHelp)
1217 {
1218  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1219  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img =
1220  mapOfImages.find(m_referenceCameraName);
1221  std::map<std::string, std::string>::const_iterator it_initFile = mapOfInitFiles.find(m_referenceCameraName);
1222 
1223  if (it_klt != m_mapOfKltTrackers.end() && it_img != mapOfImages.end() && it_initFile != mapOfInitFiles.end()) {
1224  it_klt->second->initClick(*it_img->second, it_initFile->second, displayHelp);
1225  it_klt->second->getPose(cMo);
1226 
1227  c0Mo = this->cMo;
1228  ctTc0.eye();
1229  } else {
1230  throw vpException(vpTrackingException::initializationError, "Cannot initClick for the reference camera !");
1231  }
1232 
1233  // Vector of missing initFile for cameras
1234  std::vector<std::string> vectorOfMissingCameraPoses;
1235 
1236  // Set pose for the specified cameras
1237  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1238  if (it_klt->first != m_referenceCameraName) {
1239  it_img = mapOfImages.find(it_klt->first);
1240  it_initFile = mapOfInitFiles.find(it_klt->first);
1241 
1242  if (it_img != mapOfImages.end() && it_initFile != mapOfInitFiles.end()) {
1243  // InitClick for the current camera
1244  it_klt->second->initClick(*it_img->second, it_initFile->second, displayHelp);
1245  } else {
1246  vectorOfMissingCameraPoses.push_back(it_klt->first);
1247  }
1248  }
1249  }
1250 
1251  // Set pose for cameras that do not have an initFile
1252  for (std::vector<std::string>::const_iterator it1 = vectorOfMissingCameraPoses.begin();
1253  it1 != vectorOfMissingCameraPoses.end(); ++it1) {
1254  it_img = mapOfImages.find(*it1);
1255  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
1257 
1258  if (it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
1259  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1260  m_mapOfKltTrackers[*it1]->cMo = cCurrentMo;
1261  m_mapOfKltTrackers[*it1]->init(*it_img->second);
1262  } else {
1263  std::stringstream ss;
1264  ss << "Missing image or missing camera transformation matrix ! Cannot "
1265  "set the pose for camera: "
1266  << (*it1) << " !";
1267  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1268  }
1269  }
1270 }
1271 #endif //#ifdef VISP_HAVE_MODULE_GUI
1272 
1291 void vpMbKltMultiTracker::initFromPose(const vpImage<unsigned char> &I, const std::string &initFile)
1292 {
1293  // Monocular case only !
1294  if (m_mapOfKltTrackers.size() > 1) {
1295  throw vpException(vpTrackingException::fatalError, "This function can only be used for the monocular case !");
1296  }
1297 
1298  char s[FILENAME_MAX];
1299  std::fstream finit;
1300  vpPoseVector init_pos;
1301 
1302  std::string ext = ".pos";
1303  size_t pos = initFile.rfind(ext);
1304 
1305  if (pos == initFile.size() - ext.size() && pos != 0)
1306  sprintf(s, "%s", initFile.c_str());
1307  else
1308  sprintf(s, "%s.pos", initFile.c_str());
1309 
1310  finit.open(s, std::ios::in);
1311  if (finit.fail()) {
1312  std::cerr << "cannot read " << s << std::endl;
1313  throw vpException(vpException::ioError, "cannot read init file");
1314  }
1315 
1316  for (unsigned int i = 0; i < 6; i += 1) {
1317  finit >> init_pos[i];
1318  }
1319 
1320  // Set the new pose for the reference camera
1321  cMo.buildFrom(init_pos);
1322  c0Mo = cMo;
1323  ctTc0.eye();
1324 
1325  // Init for the reference camera
1326  std::map<std::string, vpMbKltTracker *>::iterator it_ref = m_mapOfKltTrackers.find(m_referenceCameraName);
1327  if (it_ref == m_mapOfKltTrackers.end()) {
1328  throw vpException(vpTrackingException::initializationError, "Cannot find the reference camera !");
1329  }
1330 
1331  it_ref->second->initFromPose(I, cMo);
1332 }
1333 
1341 {
1342  this->cMo = cMo_;
1343  c0Mo = cMo;
1344  ctTc0.eye();
1345 
1346  // Init for the reference camera
1347  std::map<std::string, vpMbKltTracker *>::iterator it_ref = m_mapOfKltTrackers.find(m_referenceCameraName);
1348  if (it_ref == m_mapOfKltTrackers.end()) {
1349  throw vpException(vpTrackingException::initializationError, "Cannot find the reference camera !");
1350  }
1351 
1352  it_ref->second->initFromPose(I, cMo);
1353 }
1354 
1362 {
1363  vpHomogeneousMatrix _cMo(cPo);
1364  initFromPose(I, _cMo);
1365 }
1366 
1378  const vpHomogeneousMatrix &c1Mo, const vpHomogeneousMatrix &c2Mo,
1379  const bool firstCameraIsReference)
1380 {
1381  if (m_mapOfKltTrackers.size() == 2) {
1382  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1383  it->second->initFromPose(I1, c1Mo);
1384 
1385  ++it;
1386 
1387  it->second->initFromPose(I2, c2Mo);
1388 
1389  if (firstCameraIsReference) {
1390  this->cMo = c1Mo;
1391  } else {
1392  this->cMo = c2Mo;
1393  }
1394 
1395  c0Mo = this->cMo;
1396  ctTc0.eye();
1397  } else {
1398  std::stringstream ss;
1399  ss << "This method requires 2 cameras but there are " << m_mapOfKltTrackers.size() << " cameras !";
1401  }
1402 }
1403 
1411 void vpMbKltMultiTracker::initFromPose(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1412  const vpHomogeneousMatrix &cMo_)
1413 {
1414  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1415 
1416  if (it_klt != m_mapOfKltTrackers.end()) {
1417  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img = mapOfImages.find(it_klt->first);
1418 
1419  if (it_img != mapOfImages.end()) {
1420  it_klt->second->initFromPose(*it_img->second, cMo_);
1421 
1422  cMo = cMo_;
1423  c0Mo = this->cMo;
1424  ctTc0.eye();
1425 
1426  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1427  if (it_klt->first != m_referenceCameraName) {
1428  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
1429  m_mapOfCameraTransformationMatrix.find(it_klt->first);
1430  it_img = mapOfImages.find(it_klt->first);
1431 
1432  if (it_camTrans != m_mapOfCameraTransformationMatrix.end() && it_img != mapOfImages.end()) {
1433  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1434  it_klt->second->initFromPose(*it_img->second, cCurrentMo);
1435  } else {
1437  "Cannot find camera transformation matrix or image !");
1438  }
1439  }
1440  }
1441  } else {
1442  throw vpException(vpTrackingException::initializationError, "Cannot find image for reference camera !");
1443  }
1444  } else {
1445  std::stringstream ss;
1446  ss << "Cannot find reference camera: " << m_referenceCameraName << std::endl;
1448  }
1449 }
1450 
1457 void vpMbKltMultiTracker::initFromPose(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1458  const std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses)
1459 {
1460  // Set the reference cMo
1461  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1462  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img =
1463  mapOfImages.find(m_referenceCameraName);
1464  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camPose = mapOfCameraPoses.find(m_referenceCameraName);
1465 
1466  if (it_klt != m_mapOfKltTrackers.end() && it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
1467  it_klt->second->initFromPose(*it_img->second, it_camPose->second);
1468  it_klt->second->getPose(cMo);
1469 
1470  c0Mo = this->cMo;
1471  ctTc0.eye();
1472  } else {
1473  throw vpException(vpTrackingException::initializationError, "Cannot set pose for the reference camera !");
1474  }
1475 
1476  // Vector of missing pose matrices for cameras
1477  std::vector<std::string> vectorOfMissingCameraPoses;
1478 
1479  // Set pose for the specified cameras
1480  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1481  it_img = mapOfImages.find(it_klt->first);
1482  it_camPose = mapOfCameraPoses.find(it_klt->first);
1483 
1484  if (it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
1485  // Set pose
1486  it_klt->second->initFromPose(*it_img->second, it_camPose->second);
1487  } else {
1488  vectorOfMissingCameraPoses.push_back(it_klt->first);
1489  }
1490  }
1491 
1492  for (std::vector<std::string>::const_iterator it1 = vectorOfMissingCameraPoses.begin();
1493  it1 != vectorOfMissingCameraPoses.end(); ++it1) {
1494  it_img = mapOfImages.find(*it1);
1495  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
1497 
1498  if (it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
1499  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1500  m_mapOfKltTrackers[*it1]->initFromPose(*it_img->second, cCurrentMo);
1501  } else {
1502  std::stringstream ss;
1503  ss << "Missing image or missing camera transformation matrix ! Cannot "
1504  "set the pose for camera: "
1505  << (*it1) << " !";
1506  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1507  }
1508  }
1509 }
1510 
1558 void vpMbKltMultiTracker::loadConfigFile(const std::string &configFile)
1559 {
1560  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
1561  if (it != m_mapOfKltTrackers.end()) {
1562  // Load ConfigFile for reference camera
1563  it->second->loadConfigFile(configFile);
1564  it->second->getCameraParameters(cam);
1565 
1566  // Set clipping
1567  this->clippingFlag = it->second->getClipping();
1568  this->angleAppears = it->second->getAngleAppear();
1569  this->angleDisappears = it->second->getAngleDisappear();
1570  } else {
1571  std::stringstream ss;
1572  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
1573  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1574  }
1575 }
1576 
1593 void vpMbKltMultiTracker::loadConfigFile(const std::string &configFile1, const std::string &configFile2,
1594  const bool firstCameraIsReference)
1595 {
1596  if (m_mapOfKltTrackers.size() == 2) {
1597  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1598  it->second->loadConfigFile(configFile1);
1599 
1600  if (firstCameraIsReference) {
1601  it->second->getCameraParameters(cam);
1602 
1603  // Set clipping
1604  this->clippingFlag = it->second->getClipping();
1605  this->angleAppears = it->second->getAngleAppear();
1606  this->angleDisappears = it->second->getAngleDisappear();
1607  }
1608  ++it;
1609 
1610  it->second->loadConfigFile(configFile2);
1611 
1612  if (!firstCameraIsReference) {
1613  it->second->getCameraParameters(cam);
1614 
1615  // Set clipping
1616  this->clippingFlag = it->second->getClipping();
1617  this->angleAppears = it->second->getAngleAppear();
1618  this->angleDisappears = it->second->getAngleDisappear();
1619  }
1620  } else {
1621  std::stringstream ss;
1622  ss << "Cannot loadConfigFile. Require two cameras ! There are " << m_mapOfKltTrackers.size() << " cameras !";
1623  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1624  }
1625 }
1626 
1640 void vpMbKltMultiTracker::loadConfigFile(const std::map<std::string, std::string> &mapOfConfigFiles)
1641 {
1642  for (std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.begin();
1643  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1644  std::map<std::string, std::string>::const_iterator it_config = mapOfConfigFiles.find(it_klt->first);
1645  if (it_config != mapOfConfigFiles.end()) {
1646  it_klt->second->loadConfigFile(it_config->second);
1647  } else {
1648  std::stringstream ss;
1649  ss << "Missing configuration file for camera: " << it_klt->first << " !";
1650  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1651  }
1652  }
1653 
1654  // Set the reference camera parameters
1655  std::map<std::string, vpMbKltTracker *>::iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
1656  if (it != m_mapOfKltTrackers.end()) {
1657  it->second->getCameraParameters(cam);
1658 
1659  // Set clipping
1660  this->clippingFlag = it->second->getClipping();
1661  this->angleAppears = it->second->getAngleAppear();
1662  this->angleDisappears = it->second->getAngleDisappear();
1663  } else {
1664  std::stringstream ss;
1665  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
1666  throw vpException(vpTrackingException::initializationError, ss.str().c_str());
1667  }
1668 }
1669 
1697 void vpMbKltMultiTracker::loadModel(const std::string &modelFile, const bool verbose,
1698  const vpHomogeneousMatrix &T)
1699 {
1700  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1701  it != m_mapOfKltTrackers.end(); ++it) {
1702  it->second->loadModel(modelFile, verbose, T);
1703  }
1704 
1705  modelInitialised = true;
1706 }
1707 
1708 void vpMbKltMultiTracker::preTracking(std::map<std::string, const vpImage<unsigned char> *> &mapOfImages)
1709 {
1710  m_nbInfos = 0;
1711  m_nbFaceUsed = 0;
1712 
1713  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1714  it != m_mapOfKltTrackers.end(); ++it) {
1715  vpMbKltTracker *klt = it->second;
1716 
1717  try {
1718  klt->preTracking(*mapOfImages[it->first]);
1719  m_nbInfos += klt->m_nbInfos;
1720  m_nbFaceUsed += klt->m_nbFaceUsed;
1721  } catch (...) {
1722  }
1723  }
1724 }
1725 
1726 void vpMbKltMultiTracker::postTracking(std::map<std::string, const vpImage<unsigned char> *> &mapOfImages)
1727 {
1728 
1729  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1730  it != m_mapOfKltTrackers.end(); ++it) {
1731  vpMbKltTracker *klt = it->second;
1732 
1733  // Set the camera pose
1734  it->second->cMo = m_mapOfCameraTransformationMatrix[it->first] * cMo;
1735 
1736  if (klt->m_nbInfos > 0 && klt->postTracking(*mapOfImages[it->first], klt->m_w_klt)) {
1737  klt->reinit(*mapOfImages[it->first]);
1738 
1739  // set ctTc0 to identity
1740  if (it->first == m_referenceCameraName) {
1741  reinit(/*mapOfImages[it->first]*/);
1742  }
1743  }
1744  }
1745 }
1746 
1750 void vpMbKltMultiTracker::reinit(/* const vpImage<unsigned char>& I*/)
1751 {
1752  c0Mo = cMo;
1753  ctTc0.eye();
1754 }
1755 
1768 void vpMbKltMultiTracker::reInitModel(const vpImage<unsigned char> &I, const std::string &cad_name,
1769  const vpHomogeneousMatrix &cMo_, const bool verbose,
1770  const vpHomogeneousMatrix &T)
1771 {
1772  if (m_mapOfKltTrackers.size() != 1) {
1773  std::stringstream ss;
1774  ss << "This method requires exactly one camera, there are " << m_mapOfKltTrackers.size() << " cameras !";
1775  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
1776  }
1777 
1778  firstInitialisation = true;
1779  modelInitialised = true;
1780 
1781  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1782  if (it_klt != m_mapOfKltTrackers.end()) {
1783  it_klt->second->reInitModel(I, cad_name, cMo_, verbose, T);
1784 
1785  // Set reference pose
1786  it_klt->second->getPose(cMo);
1787 
1788  c0Mo = cMo;
1789  ctTc0.eye();
1790  } else {
1791  throw vpException(vpTrackingException::fatalError, "Cannot reInitModel the reference camera !");
1792  }
1793 }
1794 
1809  const std::string &cad_name, const vpHomogeneousMatrix &c1Mo,
1810  const vpHomogeneousMatrix &c2Mo, const bool verbose,
1811  const bool firstCameraIsReference)
1812 {
1813  if (m_mapOfKltTrackers.size() == 2) {
1814  std::map<std::string, vpMbKltTracker *>::const_iterator it_edge = m_mapOfKltTrackers.begin();
1815 
1816  it_edge->second->reInitModel(I1, cad_name, c1Mo, verbose);
1817 
1818  if (firstCameraIsReference) {
1819  // Set reference pose
1820  it_edge->second->getPose(cMo);
1821  }
1822 
1823  ++it_edge;
1824 
1825  it_edge->second->reInitModel(I2, cad_name, c2Mo, verbose);
1826 
1827  if (!firstCameraIsReference) {
1828  // Set reference pose
1829  it_edge->second->getPose(cMo);
1830  }
1831 
1832  c0Mo = cMo;
1833  ctTc0.eye();
1834  } else {
1835  throw vpException(vpTrackingException::fatalError, "This method requires exactly two cameras !");
1836  }
1837 }
1838 
1849 void vpMbKltMultiTracker::reInitModel(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
1850  const std::string &cad_name,
1851  const std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses,
1852  const bool verbose)
1853 {
1854  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
1855  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img =
1856  mapOfImages.find(m_referenceCameraName);
1857  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camPose = mapOfCameraPoses.find(m_referenceCameraName);
1858 
1859  if (it_klt != m_mapOfKltTrackers.end() && it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
1860  it_klt->second->reInitModel(*it_img->second, cad_name, it_camPose->second, verbose);
1861  modelInitialised = true;
1862 
1863  // Set reference pose
1864  it_klt->second->getPose(cMo);
1865 
1866  c0Mo = cMo;
1867  ctTc0.eye();
1868  } else {
1869  throw vpException(vpTrackingException::fatalError, "Cannot reInitModel for reference camera !");
1870  }
1871 
1872  std::vector<std::string> vectorOfMissingCameras;
1873  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
1874  if (it_klt->first != m_referenceCameraName) {
1875  it_img = mapOfImages.find(it_klt->first);
1876  it_camPose = mapOfCameraPoses.find(it_klt->first);
1877 
1878  if (it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
1879  it_klt->second->reInitModel(*it_img->second, cad_name, it_camPose->second, verbose);
1880  } else {
1881  vectorOfMissingCameras.push_back(it_klt->first);
1882  }
1883  }
1884  }
1885 
1886  for (std::vector<std::string>::const_iterator it = vectorOfMissingCameras.begin(); it != vectorOfMissingCameras.end();
1887  ++it) {
1888  it_img = mapOfImages.find(*it);
1889  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
1891 
1892  if (it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
1893  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
1894  m_mapOfKltTrackers[*it]->reInitModel(*it_img->second, cad_name, cCurrentMo, verbose);
1895  }
1896  }
1897 }
1898 
1904 {
1905  cMo.eye();
1906  ctTc0.eye();
1907 
1908  // Call resetTracker for all the cameras
1909  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1910  it != m_mapOfKltTrackers.end(); ++it) {
1911  it->second->resetTracker();
1912  }
1913 
1914  m_computeInteraction = true;
1915  firstInitialisation = true;
1916  computeCovariance = false;
1917 
1918  angleAppears = vpMath::rad(65);
1920 
1922 
1923  maskBorder = 5;
1924  threshold_outlier = 0.5;
1925  percentGood = 0.7;
1926 
1927  m_lambda = 0.8;
1928  m_maxIter = 200;
1929 
1931 
1932  useScanLine = false;
1933 
1934 #ifdef VISP_HAVE_OGRE
1935  useOgre = false;
1936 #endif
1937 }
1938 
1949 {
1951 
1952  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1953  it != m_mapOfKltTrackers.end(); ++it) {
1954  it->second->setAngleAppear(a);
1955  }
1956 }
1957 
1968 {
1970 
1971  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
1972  it != m_mapOfKltTrackers.end(); ++it) {
1973  it->second->setAngleDisappear(a);
1974  }
1975 }
1976 
1983 {
1984  if (m_mapOfKltTrackers.empty()) {
1985  throw vpException(vpTrackingException::fatalError, "There is no camera !");
1986  } else if (m_mapOfKltTrackers.size() > 1) {
1987  throw vpException(vpTrackingException::fatalError, "There is more than one camera !");
1988  } else {
1989  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
1990  if (it != m_mapOfKltTrackers.end()) {
1991  it->second->setCameraParameters(camera);
1992 
1993  // Set reference camera parameters
1994  this->cam = camera;
1995  } else {
1996  std::stringstream ss;
1997  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
1999  }
2000  }
2001 }
2002 
2012  const bool firstCameraIsReference)
2013 {
2014  if (m_mapOfKltTrackers.empty()) {
2015  throw vpException(vpTrackingException::fatalError, "There is no camera !");
2016  } else if (m_mapOfKltTrackers.size() == 2) {
2017  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2018  it->second->setCameraParameters(camera1);
2019 
2020  ++it;
2021  it->second->setCameraParameters(camera2);
2022 
2023  if (firstCameraIsReference) {
2024  this->cam = camera1;
2025  } else {
2026  this->cam = camera2;
2027  }
2028  } else {
2029  std::stringstream ss;
2030  ss << "Require two cameras ! There are " << m_mapOfKltTrackers.size() << " cameras !";
2032  }
2033 }
2034 
2041 void vpMbKltMultiTracker::setCameraParameters(const std::string &cameraName, const vpCameraParameters &camera)
2042 {
2043  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
2044  if (it != m_mapOfKltTrackers.end()) {
2045  it->second->setCameraParameters(camera);
2046 
2047  if (it->first == m_referenceCameraName) {
2048  this->cam = camera;
2049  }
2050  } else {
2051  std::stringstream ss;
2052  ss << "The camera: " << cameraName << " does not exist !";
2054  }
2055 }
2056 
2062 void vpMbKltMultiTracker::setCameraParameters(const std::map<std::string, vpCameraParameters> &mapOfCameraParameters)
2063 {
2064  for (std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.begin();
2065  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2066  std::map<std::string, vpCameraParameters>::const_iterator it_cam = mapOfCameraParameters.find(it_klt->first);
2067  if (it_cam != mapOfCameraParameters.end()) {
2068  it_klt->second->setCameraParameters(it_cam->second);
2069 
2070  if (it_klt->first == m_referenceCameraName) {
2071  this->cam = it_cam->second;
2072  }
2073  } else {
2074  std::stringstream ss;
2075  ss << "Missing camera parameters for camera: " << it_klt->first << " !";
2077  }
2078  }
2079 }
2080 
2089 void vpMbKltMultiTracker::setCameraTransformationMatrix(const std::string &cameraName,
2090  const vpHomogeneousMatrix &cameraTransformationMatrix)
2091 {
2092  std::map<std::string, vpHomogeneousMatrix>::iterator it = m_mapOfCameraTransformationMatrix.find(cameraName);
2093  if (it != m_mapOfCameraTransformationMatrix.end()) {
2094  it->second = cameraTransformationMatrix;
2095  } else {
2096  std::stringstream ss;
2097  ss << "Cannot find camera: " << cameraName << " !";
2099  }
2100 }
2101 
2110  const std::map<std::string, vpHomogeneousMatrix> &mapOfTransformationMatrix)
2111 {
2112  // Check if all cameras have a transformation matrix
2113  for (std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.begin();
2114  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2115  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
2116  mapOfTransformationMatrix.find(it_klt->first);
2117 
2118  if (it_camTrans == mapOfTransformationMatrix.end()) {
2119  throw vpException(vpTrackingException::initializationError, "Missing camera transformation matrix !");
2120  }
2121  }
2122 
2123  m_mapOfCameraTransformationMatrix = mapOfTransformationMatrix;
2124 }
2125 
2133 void vpMbKltMultiTracker::setClipping(const unsigned int &flags)
2134 {
2135  vpMbTracker::setClipping(flags);
2136 
2137  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2138  it != m_mapOfKltTrackers.end(); ++it) {
2139  it->second->setClipping(flags);
2140  }
2141 }
2142 
2151 void vpMbKltMultiTracker::setClipping(const std::string &cameraName, const unsigned int &flags)
2152 {
2153  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
2154  if (it != m_mapOfKltTrackers.end()) {
2155  it->second->setClipping(flags);
2156  } else {
2157  std::cerr << "Camera: " << cameraName << " does not exist !" << std::endl;
2158  }
2159 }
2160 
2167 {
2169 
2170  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2171  it != m_mapOfKltTrackers.end(); ++it) {
2172  it->second->setCovarianceComputation(flag);
2173  }
2174 }
2175 
2182 {
2183  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2184  it != m_mapOfKltTrackers.end(); ++it) {
2185  it->second->setDisplayFeatures(displayF);
2186  }
2187 
2188  displayFeatures = displayF;
2189 }
2190 
2197 {
2199 
2200  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2201  it != m_mapOfKltTrackers.end(); ++it) {
2202  it->second->setFarClippingDistance(dist);
2203  }
2204 }
2205 
2212 void vpMbKltMultiTracker::setFarClippingDistance(const std::string &cameraName, const double &dist)
2213 {
2214  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
2215  if (it != m_mapOfKltTrackers.end()) {
2216  it->second->setFarClippingDistance(dist);
2217  } else {
2218  std::cerr << "Camera: " << cameraName << " does not exist !" << std::endl;
2219  }
2220 }
2221 
2222 #ifdef VISP_HAVE_OGRE
2223 
2233 {
2234  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2235  it != m_mapOfKltTrackers.end(); ++it) {
2236  it->second->setGoodNbRayCastingAttemptsRatio(ratio);
2237  }
2238 }
2239 
2250 {
2251  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2252  it != m_mapOfKltTrackers.end(); ++it) {
2253  it->second->setNbRayCastingAttemptsForVisibility(attempts);
2254  }
2255 }
2256 #endif
2257 
2264 {
2265  for (std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.begin();
2266  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2267  it_klt->second->setKltOpencv(t);
2268  }
2269 }
2270 
2276 void vpMbKltMultiTracker::setKltOpencv(const std::map<std::string, vpKltOpencv> &mapOfOpenCVTrackers)
2277 {
2278  for (std::map<std::string, vpKltOpencv>::const_iterator it_kltOpenCV = mapOfOpenCVTrackers.begin();
2279  it_kltOpenCV != mapOfOpenCVTrackers.end(); ++it_kltOpenCV) {
2280  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(it_kltOpenCV->first);
2281  if (it_klt != m_mapOfKltTrackers.end()) {
2282  it_klt->second->setKltOpencv(it_kltOpenCV->second);
2283  } else {
2284  std::cerr << "The camera: " << it_kltOpenCV->first << " does not exist !" << std::endl;
2285  }
2286  }
2287 }
2288 
2300 void vpMbKltMultiTracker::setLod(const bool useLod, const std::string &name)
2301 {
2302  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2303  it != m_mapOfKltTrackers.end(); ++it) {
2304  it->second->setLod(useLod, name);
2305  }
2306 }
2307 
2320 void vpMbKltMultiTracker::setLod(const bool useLod, const std::string &cameraName, const std::string &name)
2321 {
2322  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(cameraName);
2323 
2324  if (it_klt != m_mapOfKltTrackers.end()) {
2325  it_klt->second->setLod(useLod, name);
2326  } else {
2327  std::cerr << "The camera: " << cameraName << " cannot be found !" << std::endl;
2328  }
2329 }
2330 
2336 void vpMbKltMultiTracker::setKltMaskBorder(const unsigned int &e)
2337 {
2338  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2339  it != m_mapOfKltTrackers.end(); ++it) {
2340  it->second->setKltMaskBorder(e);
2341  }
2342 
2343  maskBorder = e;
2344 }
2345 
2349 void vpMbKltMultiTracker::setMinLineLengthThresh(const double /*minLineLengthThresh*/, const std::string & /*name*/)
2350 {
2351  std::cerr << "Useless for KLT tracker !" << std::endl;
2352 }
2353 
2362 void vpMbKltMultiTracker::setMinPolygonAreaThresh(const double minPolygonAreaThresh, const std::string &name)
2363 {
2364  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2365  it != m_mapOfKltTrackers.end(); ++it) {
2366  it->second->setMinPolygonAreaThresh(minPolygonAreaThresh, name);
2367  }
2368 }
2369 
2380 void vpMbKltMultiTracker::setMinPolygonAreaThresh(const double minPolygonAreaThresh, const std::string &cameraName,
2381  const std::string &name)
2382 {
2383  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(cameraName);
2384 
2385  if (it_klt != m_mapOfKltTrackers.end()) {
2386  it_klt->second->setMinPolygonAreaThresh(minPolygonAreaThresh, name);
2387  } else {
2388  std::cerr << "The camera: " << cameraName << " cannot be found !" << std::endl;
2389  }
2390 }
2391 
2398 {
2400 
2401  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2402  it != m_mapOfKltTrackers.end(); ++it) {
2403  it->second->setNearClippingDistance(dist);
2404  }
2405 }
2406 
2417 void vpMbKltMultiTracker::setOgreShowConfigDialog(const bool showConfigDialog)
2418 {
2419  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2420  it != m_mapOfKltTrackers.end(); ++it) {
2421  it->second->setOgreShowConfigDialog(showConfigDialog);
2422  }
2423 }
2424 
2434 {
2435  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2436  it != m_mapOfKltTrackers.end(); ++it) {
2437  it->second->setOgreVisibilityTest(v);
2438  }
2439 
2440 #ifdef VISP_HAVE_OGRE
2441  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2442  it != m_mapOfKltTrackers.end(); ++it) {
2443  it->second->faces.getOgreContext()->setWindowName("Multi MBT Klt (" + it->first + ")");
2444  }
2445 #endif
2446 
2447  useOgre = v;
2448 }
2449 
2456 void vpMbKltMultiTracker::setNearClippingDistance(const std::string &cameraName, const double &dist)
2457 {
2458  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(cameraName);
2459  if (it != m_mapOfKltTrackers.end()) {
2460  it->second->setNearClippingDistance(dist);
2461  } else {
2462  std::cerr << "Camera: " << cameraName << " does not exist !" << std::endl;
2463  }
2464 }
2465 
2472 {
2473  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2474  it != m_mapOfKltTrackers.end(); ++it) {
2475  it->second->setOptimizationMethod(opt);
2476  }
2477 
2478  m_optimizationMethod = opt;
2479 }
2480 
2489 {
2490  if (m_mapOfKltTrackers.size() == 1) {
2491  std::map<std::string, vpMbKltTracker *>::iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
2492  if (it != m_mapOfKltTrackers.end()) {
2493  it->second->setPose(I, cMo_);
2494  this->cMo = cMo_;
2495 
2496  c0Mo = this->cMo;
2497  ctTc0.eye();
2498  } else {
2499  std::stringstream ss;
2500  ss << "Cannot find the reference camera: " << m_referenceCameraName << " !";
2502  }
2503  } else {
2504  std::stringstream ss;
2505  ss << "You are trying to set the pose with only one image and cMo "
2506  "but there are multiple cameras !";
2508  }
2509 }
2510 
2523  const vpHomogeneousMatrix &c1Mo, const vpHomogeneousMatrix &c2Mo,
2524  const bool firstCameraIsReference)
2525 {
2526  if (m_mapOfKltTrackers.size() == 2) {
2527  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2528  it->second->setPose(I1, c1Mo);
2529 
2530  ++it;
2531 
2532  it->second->setPose(I2, c2Mo);
2533 
2534  if (firstCameraIsReference) {
2535  this->cMo = c1Mo;
2536  } else {
2537  this->cMo = c2Mo;
2538  }
2539 
2540  c0Mo = this->cMo;
2541  ctTc0.eye();
2542  } else {
2543  std::stringstream ss;
2544  ss << "This method requires 2 cameras but there are " << m_mapOfKltTrackers.size() << " cameras !";
2546  }
2547 }
2548 
2557 void vpMbKltMultiTracker::setPose(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
2558  const vpHomogeneousMatrix &cMo_)
2559 {
2560  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
2561  if (it_klt != m_mapOfKltTrackers.end()) {
2562  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img =
2563  mapOfImages.find(m_referenceCameraName);
2564 
2565  if (it_img != mapOfImages.end()) {
2566  // Set pose on reference camera
2567  it_klt->second->setPose(*it_img->second, cMo_);
2568 
2569  // Set the reference cMo
2570  cMo = cMo_;
2571 
2572  c0Mo = this->cMo;
2573  ctTc0.eye();
2574 
2575  // Set the pose for the others cameras
2576  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2577  if (it_klt->first != m_referenceCameraName) {
2578  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
2579  m_mapOfCameraTransformationMatrix.find(it_klt->first);
2580  it_img = mapOfImages.find(it_klt->first);
2581 
2582  if (it_camTrans != m_mapOfCameraTransformationMatrix.end() && it_img != mapOfImages.end()) {
2583  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
2584  it_klt->second->setPose(*it_img->second, cCurrentMo);
2585  } else {
2586  throw vpException(vpTrackingException::fatalError, "Cannot find camera transformation matrix or image !");
2587  }
2588  }
2589  }
2590  } else {
2591  std::stringstream ss;
2592  ss << "Missing image for reference camera: " << m_referenceCameraName << " !";
2593  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2594  }
2595  } else {
2596  std::stringstream ss;
2597  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
2598  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2599  }
2600 }
2601 
2612 void vpMbKltMultiTracker::setPose(const std::map<std::string, const vpImage<unsigned char> *> &mapOfImages,
2613  const std::map<std::string, vpHomogeneousMatrix> &mapOfCameraPoses)
2614 {
2615  // Set the reference cMo
2616  std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.find(m_referenceCameraName);
2617  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img =
2618  mapOfImages.find(m_referenceCameraName);
2619  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camPose = mapOfCameraPoses.find(m_referenceCameraName);
2620 
2621  if (it_klt != m_mapOfKltTrackers.end() && it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
2622  it_klt->second->setPose(*it_img->second, it_camPose->second);
2623  it_klt->second->getPose(cMo);
2624 
2625  c0Mo = this->cMo;
2626  ctTc0.eye();
2627  } else {
2628  throw vpException(vpTrackingException::fatalError, "Cannot set pose for the reference camera !");
2629  }
2630 
2631  // Vector of missing pose matrices for cameras
2632  std::vector<std::string> vectorOfMissingCameraPoses;
2633 
2634  // Set pose for the specified cameras
2635  for (it_klt = m_mapOfKltTrackers.begin(); it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2636  if (it_klt->first != m_referenceCameraName) {
2637  it_img = mapOfImages.find(it_klt->first);
2638  it_camPose = mapOfCameraPoses.find(it_klt->first);
2639 
2640  if (it_img != mapOfImages.end() && it_camPose != mapOfCameraPoses.end()) {
2641  // Set pose
2642  it_klt->second->setPose(*it_img->second, it_camPose->second);
2643  } else {
2644  vectorOfMissingCameraPoses.push_back(it_klt->first);
2645  }
2646  }
2647  }
2648 
2649  for (std::vector<std::string>::const_iterator it1 = vectorOfMissingCameraPoses.begin();
2650  it1 != vectorOfMissingCameraPoses.end(); ++it1) {
2651  it_img = mapOfImages.find(*it1);
2652  std::map<std::string, vpHomogeneousMatrix>::const_iterator it_camTrans =
2654 
2655  if (it_img != mapOfImages.end() && it_camTrans != m_mapOfCameraTransformationMatrix.end()) {
2656  vpHomogeneousMatrix cCurrentMo = it_camTrans->second * cMo;
2657  m_mapOfKltTrackers[*it1]->setPose(*it_img->second, cCurrentMo);
2658  } else {
2659  std::stringstream ss;
2660  ss << "Missing image or missing camera transformation matrix ! Cannot "
2661  "set the pose for camera: "
2662  << (*it1) << " !";
2663  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2664  }
2665  }
2666 }
2667 
2673 void vpMbKltMultiTracker::setReferenceCameraName(const std::string &referenceCameraName)
2674 {
2675  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(referenceCameraName);
2676  if (it != m_mapOfKltTrackers.end()) {
2677  m_referenceCameraName = referenceCameraName;
2678  } else {
2679  std::stringstream ss;
2680  ss << "The reference camera: " << referenceCameraName << " does not exist !";
2681  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2682  }
2683 }
2684 
2691 {
2692  // Set general setScanLineVisibilityTest
2694 
2695  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2696  it != m_mapOfKltTrackers.end(); ++it) {
2697  it->second->setScanLineVisibilityTest(v);
2698  }
2699 }
2700 
2707 {
2708  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2709  it != m_mapOfKltTrackers.end(); ++it) {
2710  it->second->setKltThresholdAcceptation(th);
2711  }
2712 
2713  threshold_outlier = th;
2714 }
2715 
2723 void vpMbKltMultiTracker::setUseKltTracking(const std::string &name, const bool &useKltTracking)
2724 {
2725  for (std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2726  it != m_mapOfKltTrackers.end(); ++it) {
2727  it->second->setUseKltTracking(name, useKltTracking);
2728  }
2729 }
2730 
2739 {
2740  // Track only with reference camera
2741  // Get the reference camera parameters
2742  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.find(m_referenceCameraName);
2743 
2744  if (it != m_mapOfKltTrackers.end()) {
2745  it->second->track(I);
2746  it->second->getPose(cMo);
2747  } else {
2748  std::stringstream ss;
2749  ss << "The reference camera: " << m_referenceCameraName << " does not exist !";
2750  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2751  }
2752 }
2753 
2763 {
2764  if (m_mapOfKltTrackers.size() == 2) {
2765  std::map<std::string, vpMbKltTracker *>::const_iterator it = m_mapOfKltTrackers.begin();
2766  std::map<std::string, const vpImage<unsigned char> *> mapOfImages;
2767  mapOfImages[it->first] = &I1;
2768  ++it;
2769 
2770  mapOfImages[it->first] = &I2;
2771  track(mapOfImages);
2772  } else {
2773  std::stringstream ss;
2774  ss << "Require two cameras ! There are " << m_mapOfKltTrackers.size() << " cameras !";
2775  throw vpException(vpTrackingException::fatalError, ss.str().c_str());
2776  }
2777 }
2778 
2786 void vpMbKltMultiTracker::track(std::map<std::string, const vpImage<unsigned char> *> &mapOfImages)
2787 {
2788  // Check if there is an image for each camera
2789  for (std::map<std::string, vpMbKltTracker *>::const_iterator it_klt = m_mapOfKltTrackers.begin();
2790  it_klt != m_mapOfKltTrackers.end(); ++it_klt) {
2791  std::map<std::string, const vpImage<unsigned char> *>::const_iterator it_img = mapOfImages.find(it_klt->first);
2792 
2793  if (it_img == mapOfImages.end()) {
2794  throw vpException(vpTrackingException::fatalError, "Missing images !");
2795  }
2796  }
2797 
2798  preTracking(mapOfImages);
2799 
2800  if (m_nbInfos < 4 || m_nbFaceUsed == 0) {
2801  throw vpTrackingException(vpTrackingException::notEnoughPointError, "Error: not enough features");
2802  }
2803 
2804  computeVVS();
2805 
2806  postTracking(mapOfImages);
2807 }
2808 
2810 
2816 std::map<std::string, int> vpMbKltMultiTracker::getNbKltPoints() const { return getKltNbPoints(); }
2817 
2824 void vpMbKltMultiTracker::setMaskBorder(const unsigned int &e) { setKltMaskBorder(e); }
2825 
2832 
2833 #elif !defined(VISP_BUILD_SHARED_LIBS)
2834 // Work arround to avoid warning:
2835 // libvisp_mbt.a(dummy_vpMbKltMultiTracker.cpp.o) has no symbols
2836 void dummy_vpMbKltMultiTracker(){}
2837 #endif // VISP_HAVE_OPENCV
2838 #elif !defined(VISP_BUILD_SHARED_LIBS)
2839 // Work arround to avoid warning: libvisp_mbt.a(dummy_vpMbKltMultiTracker.cpp.o) has no symbols
2840 void dummy_vpMbKltMultiTracker(){}
2841 #endif //#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
bool m_computeInteraction
Definition: vpMbTracker.h:191
virtual unsigned int getNbPolygon() const
void setGoodNbRayCastingAttemptsRatio(const double &ratio)
virtual void setCovarianceComputation(const bool &flag)
Definition: vpMbTracker.h:485
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
virtual std::vector< std::string > getCameraNames() const
virtual void getCameraParameters(vpCameraParameters &camera) const
vpMatrix m_L_kltMulti
Interaction matrix.
virtual std::list< vpMbtDistanceKltCylinder * > & getFeaturesKltCylinder()
virtual void setFarClippingDistance(const double &dist)
virtual std::map< std::string, int > getKltNbPoints() const
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 unsigned int getClipping() const
Definition: vpMbTracker.h:256
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo)
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:466
vpMbHiddenFaces< vpMbtPolygon > faces
Set of faces describing the object.
Definition: vpMbTracker.h:149
virtual std::map< std::string, int > getNbKltPoints() const
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 std::list< vpMbtDistanceKltPoints * > & getFeaturesKlt()
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:119
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:129
error that can be emited by ViSP classes.
Definition: vpException.h:71
virtual void setThresholdAcceptation(const double th)
virtual void computeVVSInteractionMatrixAndResidu()
bool useOgre
Use Ogre3d for visibility tests.
Definition: vpMbTracker.h:161
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:134
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:117
std::map< std::string, vpHomogeneousMatrix > m_mapOfCameraTransformationMatrix
vpHomogeneousMatrix ctTc0
virtual void setReferenceCameraName(const std::string &referenceCameraName)
virtual std::map< std::string, vpKltOpencv > getKltOpencv() const
virtual void loadModel(const std::string &modelFile, const bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual std::map< std::string, std::vector< vpImagePoint > > getKltImagePoints() const
virtual void setKltMaskBorder(const unsigned int &e)
vpRobust m_robust_klt
Robust.
void insert(const vpMatrix &A, const unsigned int r, const unsigned int c)
Definition: vpMatrix.cpp:4570
vpMatrix oJo
The Degrees of Freedom to estimate.
Definition: vpMbTracker.h:121
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:423
virtual void setOgreShowConfigDialog(const bool showConfigDialog)
virtual std::list< vpMbtDistanceCircle * > & getFeaturesCircle()
bool useScanLine
Use Scanline for visibility tests.
Definition: vpMbTracker.h:164
vpVelocityTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
virtual void reInitModel(const vpImage< unsigned char > &I, const std::string &cad_name, const vpHomogeneousMatrix &cMo_, const bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual std::map< std::string, std::vector< cv::Point2f > > getKltPoints() const
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
double m_initialMu
Initial Mu for Levenberg Marquardt optimization loop.
Definition: vpMbTracker.h:199
double m_lambda
Gain of the virtual visual servoing stage.
Definition: vpMbTracker.h:193
virtual void computeVVSWeights(vpRobust &robust, const vpColVector &error, vpColVector &w)
virtual void preTracking(std::map< std::string, const vpImage< unsigned char > * > &mapOfImages)
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:455
virtual void setAngleAppear(const double &a)
Model based tracker using only KLT.
vpMbtOptimizationMethod m_optimizationMethod
Optimization method used.
Definition: vpMbTracker.h:146
unsigned int getRows() const
Definition: vpArray2D.h:156
std::string m_referenceCameraName
Name of the reference camera.
double angleAppears
Angle used to detect a face appearance.
Definition: vpMbTracker.h:151
unsigned int m_maxIter
Maximum number of iterations of the virtual visual servoing stage.
Definition: vpMbTracker.h:195
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static double rad(double deg)
Definition: vpMath.h:102
double threshold_outlier
vpColVector m_w_kltMulti
Robust weights.
virtual void setKltOpencv(const vpKltOpencv &t)
virtual std::map< std::string, std::map< int, vpImagePoint > > getKltImagePointsWithId() const
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:144
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 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
vpHomogeneousMatrix inverse() const
static vpHomogeneousMatrix direct(const vpColVector &v)
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:153
virtual void computeVVSInit()
virtual void postTracking(std::map< std::string, const vpImage< unsigned char > * > &mapOfImages)
virtual void setScanLineVisibilityTest(const bool &v)
Definition: vpMbTracker.h:587
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:159
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:123
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:244
virtual void setNearClippingDistance(const double &dist)
vpMatrix m_L_klt
Interaction matrix.