Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
testAprilTag.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  * Test AprilTag detection.
33  *
34  *****************************************************************************/
41 #include <visp3/core/vpConfig.h>
42 
43 #if defined(VISP_HAVE_CATCH2) && defined(VISP_HAVE_APRILTAG)
44 #define CATCH_CONFIG_RUNNER
45 #include <catch.hpp>
46 
47 #include <iostream>
48 #include <visp3/core/vpDisplay.h>
49 #include <visp3/core/vpIoTools.h>
50 #include <visp3/core/vpPoint.h>
51 #include <visp3/core/vpPixelMeterConversion.h>
52 #include <visp3/detection/vpDetectorAprilTag.h>
53 #include <visp3/io/vpImageIo.h>
54 #include <visp3/vision/vpPose.h>
55 namespace
56 {
57 struct TagGroundTruth {
58  std::string m_message;
59  std::vector<vpImagePoint> m_corners;
60 
61  TagGroundTruth(const std::string &msg, const std::vector<vpImagePoint> &c) : m_message(msg), m_corners(c) {}
62 
63  bool operator==(const TagGroundTruth &b) const
64  {
65  if (m_message != b.m_message || m_corners.size() != b.m_corners.size()) {
66  return false;
67  }
68 
69  for (size_t i = 0; i < m_corners.size(); i++) {
70  // Allow 0.5 pixel of difference
71  if (!vpMath::equal(m_corners[i].get_u(), b.m_corners[i].get_u(), 0.5) ||
72  !vpMath::equal(m_corners[i].get_v(), b.m_corners[i].get_v(), 0.5)) {
73  return false;
74  }
75  }
76 
77  return true;
78  }
79 
80  bool operator!=(const TagGroundTruth &b) const { return !(*this == b); }
81 
82  double rmse(const std::vector<vpImagePoint> &c)
83  {
84  double error = 0;
85 
86  if (m_corners.size() == c.size()) {
87  for (size_t i = 0; i < m_corners.size(); i++) {
88  const vpImagePoint& a = m_corners[i];
89  const vpImagePoint& b = c[i];
90  error += (a.get_i()-b.get_i())*(a.get_i()-b.get_i()) +
91  (a.get_j()-b.get_j())*(a.get_j()-b.get_j());
92  }
93  } else {
94  return -1;
95  }
96 
97  return sqrt(error / (2*m_corners.size()));
98  }
99 };
100 
101 std::ostream &operator<<(std::ostream &os, TagGroundTruth &t)
102 {
103  os << t.m_message << std::endl;
104  for (size_t i = 0; i < t.m_corners.size(); i++) {
105  os << t.m_corners[i] << std::endl;
106  }
107 
108  return os;
109 }
110 
111 struct FailedTestCase {
114  int m_tagId;
115 
116  FailedTestCase(const vpDetectorAprilTag::vpAprilTagFamily &family,
118  int tagId)
119  : m_family(family), m_method(method), m_tagId(tagId) {}
120 
121  bool operator==(const FailedTestCase &b) const
122  {
123  return m_family == b.m_family && m_method == b.m_method && m_tagId == b.m_tagId;
124  }
125 
126  bool operator!=(const FailedTestCase &b) const { return !(*this == b); }
127 };
128 }
129 
130 TEST_CASE("Apriltag pose estimation test", "[apriltag_pose_estimation_test]") {
131  std::map<vpDetectorAprilTag::vpAprilTagFamily, std::string> apriltagMap = {
132  {vpDetectorAprilTag::TAG_16h5, "tag16_05"},
133  {vpDetectorAprilTag::TAG_25h9, "tag25_09"},
134  {vpDetectorAprilTag::TAG_36h11, "tag36_11"},
136  #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
137  , {vpDetectorAprilTag::TAG_CIRCLE49h12, "tag49_12"},
141  #endif
142  };
143 
144  std::map<vpDetectorAprilTag::vpAprilTagFamily, double> tagSizeScales = {
149  #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
154  #endif
155  };
156 
157  std::vector<vpDetectorAprilTag::vpPoseEstimationMethod> poseMethods = {vpDetectorAprilTag::HOMOGRAPHY,
163  };
164  std::map<vpDetectorAprilTag::vpPoseEstimationMethod, std::string> methodNames = {
165  {vpDetectorAprilTag::HOMOGRAPHY, "HOMOGRAPHY"},
166  {vpDetectorAprilTag::HOMOGRAPHY_VIRTUAL_VS, "HOMOGRAPHY_VIRTUAL_VS"},
167  {vpDetectorAprilTag::HOMOGRAPHY_ORTHOGONAL_ITERATION, "HOMOGRAPHY_ORTHOGONAL_ITERATION"},
168  {vpDetectorAprilTag::DEMENTHON_VIRTUAL_VS, "DEMENTHON_VIRTUAL_VS"},
169  {vpDetectorAprilTag::LAGRANGE_VIRTUAL_VS, "LAGRANGE_VIRTUAL_VS"},
170  {vpDetectorAprilTag::BEST_RESIDUAL_VIRTUAL_VS, "BEST_RESIDUAL_VIRTUAL_VS"}
171  };
172 
173  const size_t nbTags = 5;
174  const double tagSize_ = 0.25;
175  std::map<int, double> tagsSize = {
176  {0, tagSize_},
177  {1, tagSize_},
178  {2, tagSize_},
179  {3, tagSize_/2},
180  {4, tagSize_/2},
181  };
182 
183  std::map<int, double> errorTranslationThresh = {
184  {0, 0.025},
185  {1, 0.09},
186  {2, 0.05},
187  {3, 0.13},
188  {4, 0.09},
189  };
190  std::map<int, double> errorRotationThresh = {
191  {0, 0.04},
192  {1, 0.075},
193  {2, 0.07},
194  {3, 0.18},
195  {4, 0.13},
196  };
197  std::vector<FailedTestCase> ignoreTests = {
201  };
202 
203  vpCameraParameters cam;
204  cam.initPersProjWithoutDistortion(700.0, 700.0, 320.0, 240.0);
205 
206  std::map<int, vpHomogeneousMatrix> groundTruthPoses;
207  for (size_t i = 0; i < nbTags; i++) {
209  std::string("AprilTag/benchmark/640x480/cMo_") +
210  std::to_string(i) + std::string(".txt"));
211  std::ifstream file(filename);
212  groundTruthPoses[static_cast<int>(i)].load(file);
213  }
214 
215  for (const auto& kv : apriltagMap) {
216  auto family = kv.first;
217  std::cout << "\nApriltag family: " << family << std::endl;
219  std::string("AprilTag/benchmark/640x480/") +
220  kv.second + std::string("_640x480.png"));
221  const double tagSize = tagSize_ * tagSizeScales[family];
222  REQUIRE(vpIoTools::checkFilename(filename));
223 
225  vpImageIo::read(I, filename);
226  REQUIRE(I.getSize() == 640*480);
227 
228  vpDetectorAprilTag apriltag_detector(family);
229 
230  for (auto method : poseMethods) {
231  std::cout << "\tPose estimation method: " << method << std::endl;
232  apriltag_detector.setAprilTagPoseEstimationMethod(method);
233 
234  //Same tags size
235  {
236  std::vector<vpHomogeneousMatrix> cMo_vec;
237  apriltag_detector.detect(I, tagSize, cam, cMo_vec);
238  CHECK(cMo_vec.size() == nbTags);
239 
240  std::vector<std::vector<vpImagePoint>> tagsCorners = apriltag_detector.getPolygon();
241  CHECK(tagsCorners.size() == nbTags);
242 
243  std::vector<std::string> messages = apriltag_detector.getMessage();
244  CHECK(messages.size() == nbTags);
245 
246  std::vector<int> tagsId = apriltag_detector.getTagsId();
247  CHECK(tagsId.size() == nbTags);
248  std::map<int, int> idsCount;
249  for (size_t i = 0; i < tagsId.size(); i++) {
250  idsCount[tagsId[i]]++;
251  CHECK((tagsId[i] >= 0 && tagsId[i] < 5));
252  }
253  CHECK(idsCount.size() == nbTags);
254 
255  for (size_t i = 0; i < cMo_vec.size(); i++) {
256  const vpHomogeneousMatrix& cMo = cMo_vec[i];
257  const vpPoseVector pose(cMo);
258  int id = tagsId[i];
259  if (id >= 3) {
260  continue;
261  }
262  std::cout << "\t\tSame size, Tag: " << i << std::endl;
263  const vpHomogeneousMatrix& cMo_truth = groundTruthPoses[id];
264  const vpPoseVector pose_truth(cMo_truth);
265 
266  vpColVector error_translation = pose.getTranslationVector() - pose_truth.getTranslationVector();
267  vpColVector error_thetau = vpColVector(pose.getThetaUVector()) - vpColVector(pose_truth.getThetaUVector());
268  double error_trans = sqrt(error_translation.sumSquare()/3);
269  double error_orientation = sqrt(error_thetau.sumSquare() / 3);
270  std::cout << "\t\t\tTranslation error: " << error_trans << " / Rotation error: " << error_orientation << std::endl;
271  CHECK((error_trans < errorTranslationThresh[id] &&
272  error_orientation < errorRotationThresh[id]));
273  }
274  }
275 
276  //Custom tags size
277  {
278  apriltag_detector.detect(I);
279 
280  std::vector<std::vector<vpImagePoint>> tagsCorners = apriltag_detector.getPolygon();
281  CHECK(tagsCorners.size() == nbTags);
282 
283  std::vector<std::string> messages = apriltag_detector.getMessage();
284  CHECK(messages.size() == nbTags);
285 
286  std::vector<int> tagsId = apriltag_detector.getTagsId();
287  CHECK(tagsId.size() == nbTags);
288  std::map<int, int> idsCount;
289  for (size_t i = 0; i < tagsId.size(); i++) {
290  idsCount[tagsId[i]]++;
291  CHECK((tagsId[i] >= 0 && tagsId[i] < 5));
292  }
293  CHECK(idsCount.size() == nbTags);
294 
295  for (size_t idx = 0; idx < tagsId.size(); idx++) {
296  std::cout << "\t\tCustom size, Tag: " << idx << std::endl;
297  const int id = tagsId[idx];
299  apriltag_detector.getPose(idx, tagsSize[id] * tagSizeScales[family], cam, cMo);
300 
301  const vpPoseVector pose(cMo);
302  const vpHomogeneousMatrix& cMo_truth = groundTruthPoses[id];
303  const vpPoseVector pose_truth(cMo_truth);
304 
305  vpColVector error_translation = pose.getTranslationVector() - pose_truth.getTranslationVector();
306  vpColVector error_thetau = vpColVector(pose.getThetaUVector()) - vpColVector(pose_truth.getThetaUVector());
307  double error_trans = sqrt(error_translation.sumSquare()/3);
308  double error_orientation = sqrt(error_thetau.sumSquare() / 3);
309  std::cout << "\t\t\tTranslation error: " << error_trans << " / Rotation error: " << error_orientation << std::endl;
310  if (std::find(ignoreTests.begin(), ignoreTests.end(), FailedTestCase(family, method, static_cast<int>(idx))) == ignoreTests.end()) {
311  CHECK((error_trans < errorTranslationThresh[id] &&
312  error_orientation < errorRotationThresh[id]));
313  }
314  }
315  }
316 
317  //Custom tags size + aligned Z-axis
318  {
319  apriltag_detector.detect(I);
320 
321  std::vector<std::vector<vpImagePoint>> tagsCorners = apriltag_detector.getPolygon();
322  CHECK(tagsCorners.size() == nbTags);
323 
324  std::vector<std::string> messages = apriltag_detector.getMessage();
325  CHECK(messages.size() == nbTags);
326 
327  std::vector<int> tagsId = apriltag_detector.getTagsId();
328  CHECK(tagsId.size() == nbTags);
329  std::map<int, int> idsCount;
330  for (size_t i = 0; i < tagsId.size(); i++) {
331  idsCount[tagsId[i]]++;
332  CHECK((tagsId[i] >= 0 && tagsId[i] < 5));
333  }
334  CHECK(idsCount.size() == nbTags);
335 
336  for (size_t idx = 0; idx < tagsId.size(); idx++) {
337  std::cout << "\t\tCustom size + aligned Z-axis, Tag: " << idx << std::endl;
338  const int id = tagsId[idx];
340  apriltag_detector.setZAlignedWithCameraAxis(true);
341  apriltag_detector.getPose(idx, tagsSize[id] * tagSizeScales[family], cam, cMo);
342  apriltag_detector.setZAlignedWithCameraAxis(false);
343 
344  const vpPoseVector pose(cMo);
345  const vpHomogeneousMatrix oMo2(vpTranslationVector(), vpThetaUVector(M_PI, 0, 0));
346  const vpHomogeneousMatrix cMo_truth = groundTruthPoses[id] * oMo2;
347  const vpPoseVector pose_truth(cMo_truth);
348 
349  vpColVector error_translation = pose.getTranslationVector() - pose_truth.getTranslationVector();
350  vpColVector error_thetau = vpColVector(pose.getThetaUVector()) - vpColVector(pose_truth.getThetaUVector());
351  double error_trans = sqrt(error_translation.sumSquare()/3);
352  double error_orientation = sqrt(error_thetau.sumSquare() / 3);
353  std::cout << "\t\t\tTranslation error: " << error_trans << " / Rotation error: " << error_orientation << std::endl;
354  if (std::find(ignoreTests.begin(), ignoreTests.end(), FailedTestCase(family, method, static_cast<int>(idx))) == ignoreTests.end()) {
355  CHECK((error_trans < errorTranslationThresh[id] &&
356  error_orientation < errorRotationThresh[id]));
357  }
358  }
359  }
360  }
361  }
362 }
363 
364 TEST_CASE("Apriltag corners accuracy test", "[apriltag_corners_accuracy_test]") {
365  std::map<vpDetectorAprilTag::vpAprilTagFamily, std::string> apriltagMap = {
366  {vpDetectorAprilTag::TAG_16h5, "tag16_05"},
367  {vpDetectorAprilTag::TAG_25h9, "tag25_09"},
368  {vpDetectorAprilTag::TAG_36h11, "tag36_11"},
370  #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
371  , {vpDetectorAprilTag::TAG_CIRCLE49h12, "tag49_12"},
375  #endif
376  };
377 
378  const size_t nbTags = 5;
379  std::map<vpDetectorAprilTag::vpAprilTagFamily, std::map<int, std::vector<vpImagePoint>>> groundTruthCorners;
380  for (const auto& kv : apriltagMap) {
382  std::string("AprilTag/benchmark/640x480/corners_") +
383  kv.second + std::string(".txt"));
384  std::ifstream file(filename);
385  REQUIRE(file.is_open());
386 
387  int id = 0;
388  double p0x = 0, p0y = 0;
389  double p1x = 0, p1y = 0;
390  double p2x = 0, p2y = 0;
391  double p3x = 0, p3y = 0;
392  while (file >> id >> p0x >> p0y >> p1x >> p1y >>
393  p2x >> p2y >> p3x >> p3y) {
394  groundTruthCorners[kv.first][id].push_back(vpImagePoint(p0y, p0x));
395  groundTruthCorners[kv.first][id].push_back(vpImagePoint(p1y, p1x));
396  groundTruthCorners[kv.first][id].push_back(vpImagePoint(p2y, p2x));
397  groundTruthCorners[kv.first][id].push_back(vpImagePoint(p3y, p3x));
398  REQUIRE(groundTruthCorners[kv.first][id].size() == 4);
399  }
400  }
401 
402  for (const auto& kv : apriltagMap) {
403  auto family = kv.first;
404  std::cout << "\nApriltag family: " << family << std::endl;
406  std::string("AprilTag/benchmark/640x480/") +
407  kv.second + std::string("_640x480.png"));
408  REQUIRE(vpIoTools::checkFilename(filename));
409 
411  vpImageIo::read(I, filename);
412  REQUIRE(I.getSize() == 640*480);
413 
414  vpDetectorAprilTag apriltag_detector(family);
415  apriltag_detector.detect(I);
416  std::vector<int> tagsId = apriltag_detector.getTagsId();
417  std::vector<std::vector<vpImagePoint>> tagsCorners = apriltag_detector.getTagsCorners();
418 
419  REQUIRE(tagsCorners.size() == nbTags);
420  REQUIRE(tagsId.size() == nbTags);
421  for (size_t i = 0; i < tagsCorners.size(); i++) {
422  const int tagId = tagsId[i];
423  REQUIRE(tagsCorners[i].size() == 4);
424 
425  TagGroundTruth corners_ref("", groundTruthCorners[family][tagId]);
426  TagGroundTruth corners_cur("", tagsCorners[i]);
427  CHECK((corners_ref == corners_cur));
428 
429  std::cout << "\tid: " << tagId << " - RMSE: " << corners_ref.rmse(corners_cur.m_corners) << std::endl;
430  }
431  }
432 }
433 
434 TEST_CASE("Apriltag regression test", "[apriltag_regression_test]") {
435  const std::string filename = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "AprilTag/AprilTag.pgm");
436  REQUIRE(vpIoTools::checkFilename(filename));
437 
439  vpImageIo::read(I, filename);
440  REQUIRE(I.getSize() == 640*480);
441 
444  const double tagSize = 0.053;
445  const float quad_decimate = 1.0;
446  vpDetectorBase *detector = new vpDetectorAprilTag(tagFamily);
447  dynamic_cast<vpDetectorAprilTag *>(detector)->setAprilTagQuadDecimate(quad_decimate);
448  dynamic_cast<vpDetectorAprilTag *>(detector)->setAprilTagPoseEstimationMethod(poseEstimationMethod);
449 
450  vpCameraParameters cam;
451  cam.initPersProjWithoutDistortion(615.1674805, 615.1675415, 312.1889954, 243.4373779);
452 
453  std::vector<vpHomogeneousMatrix> cMo_vec;
454  dynamic_cast<vpDetectorAprilTag *>(detector)->detect(I, tagSize, cam, cMo_vec);
455 
456  // Ground truth
457  std::map<std::string, TagGroundTruth> mapOfTagsGroundTruth;
458  {
459  std::string filename_ground_truth = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(),
460  "AprilTag/ground_truth_detection.txt");
461  std::ifstream file_ground_truth(filename_ground_truth.c_str());
462  REQUIRE(file_ground_truth.is_open());
463  std::string message = "";
464  double v1 = 0.0, v2 = 0.0, v3 = 0.0, v4 = 0.0;
465  double u1 = 0.0, u2 = 0.0, u3 = 0.0, u4 = 0.0;
466  while (file_ground_truth >> message >> v1 >> u1 >> v2 >> u2 >> v3 >> u3 >> v4 >> u4) {
467  std::vector<vpImagePoint> tagCorners(4);
468  tagCorners[0].set_ij(v1, u1);
469  tagCorners[1].set_ij(v2, u2);
470  tagCorners[2].set_ij(v3, u3);
471  tagCorners[3].set_ij(v4, u4);
472  mapOfTagsGroundTruth.insert(std::make_pair(message, TagGroundTruth(message, tagCorners)));
473  }
474  }
475 
476  std::map<std::string, vpPoseVector> mapOfPosesGroundTruth;
477  {
478  std::string filename_ground_truth = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(),
479  "AprilTag/ground_truth_pose.txt");
480  std::ifstream file_ground_truth(filename_ground_truth.c_str());
481  REQUIRE(file_ground_truth.is_open());
482  std::string message = "";
483  double tx = 0.0, ty = 0.0, tz = 0.0;
484  double tux = 0.0, tuy = 0.0, tuz = 0.0;
485  while (file_ground_truth >> message >> tx >> ty >> tz >> tux >> tuy >> tuz) {
486  mapOfPosesGroundTruth.insert(std::make_pair(message, vpPoseVector(tx, ty, tz, tux, tuy, tuz)));
487  }
488  }
489 
490  for (size_t i = 0; i < detector->getNbObjects(); i++) {
491  std::vector<vpImagePoint> p = detector->getPolygon(i);
492 
493  std::string message = detector->getMessage(i);
494  std::replace(message.begin(), message.end(), ' ', '_');
495  std::map<std::string, TagGroundTruth>::iterator it = mapOfTagsGroundTruth.find(message);
496  TagGroundTruth current(message, p);
497  if (it == mapOfTagsGroundTruth.end()) {
498  std::cerr << "Problem with tag decoding (tag_family or id): " << message << std::endl;
499  } else if (it->second != current) {
500  std::cerr << "Problem, current detection:\n" << current << "\nReference:\n" << it->second << std::endl;
501  }
502  REQUIRE(it != mapOfTagsGroundTruth.end());
503  CHECK(it->second == current);
504  }
505 
506  for (size_t i = 0; i < cMo_vec.size(); i++) {
507  vpPoseVector pose_vec(cMo_vec[i]);
508 
509  std::string message = detector->getMessage(i);
510  std::replace(message.begin(), message.end(), ' ', '_');
511  std::map<std::string, vpPoseVector>::iterator it = mapOfPosesGroundTruth.find(message);
512  if (it == mapOfPosesGroundTruth.end()) {
513  std::cerr << "Problem with tag decoding (tag_family or id): " << message << std::endl;
514  }
515  REQUIRE(it != mapOfPosesGroundTruth.end());
516  std::cout << "Tag: " << message << std::endl;
517  std::cout << "\tEstimated pose: " << pose_vec.t() << std::endl;
518  std::cout << "\tReference pose: " << it->second.t() << std::endl;
519  for (unsigned int cpt = 0; cpt < 3; cpt++) {
520  if (!vpMath::equal(it->second[cpt], pose_vec[cpt], 0.005) ||
521  !vpMath::equal(it->second[cpt+3], pose_vec[cpt+3], 0.005)) {
522  std::cerr << "Problem, current pose: " << pose_vec.t() << "\nReference pose: " << it->second.t()
523  << std::endl;
524  }
525  CHECK((vpMath::equal(it->second[cpt], pose_vec[cpt], 0.005) &&
526  vpMath::equal(it->second[cpt+3], pose_vec[cpt+3], 0.005)));
527  }
528  }
529 
530  delete detector;
531 }
532 
533 TEST_CASE("Apriltag copy constructor test", "[apriltag_copy_constructor_test]") {
534  const std::string filename = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(),
535  "AprilTag/benchmark/640x480/tag21_07_640x480.png");
536  REQUIRE(vpIoTools::checkFilename(filename));
537 
539  vpImageIo::read(I, filename);
540  REQUIRE(I.getSize() == 640*480);
541 
544  const double tagSize = 0.25 * 5/9;
545  const float quad_decimate = 1.0;
546  vpDetectorAprilTag *detector = new vpDetectorAprilTag(tagFamily, poseEstimationMethod);
547  detector->setAprilTagQuadDecimate(quad_decimate);
548 
549  vpCameraParameters cam;
550  cam.initPersProjWithoutDistortion(700, 700, 320, 240);
551 
552  std::vector<vpHomogeneousMatrix> cMo_vec;
553  detector->detect(I, tagSize, cam, cMo_vec);
554  std::vector<std::vector<vpImagePoint> > tagsCorners = detector->getTagsCorners();
555  std::vector<int> tagsId = detector->getTagsId();
556 
557  //Copy
558  vpDetectorAprilTag detector_copy(*detector);
559  //Delete old detector
560  delete detector;
561 
562  std::vector<std::vector<vpImagePoint> > tagsCorners_copy = detector_copy.getTagsCorners();
563  std::vector<int> tagsId_copy = detector_copy.getTagsId();
564  REQUIRE(tagsCorners_copy.size() == tagsCorners.size());
565  REQUIRE(tagsId_copy.size() == tagsId.size());
566  REQUIRE(tagsCorners_copy.size() == tagsId_copy.size());
567 
568  for (size_t i = 0; i < tagsCorners.size(); i++) {
569  const std::vector<vpImagePoint>& corners_ref = tagsCorners[i];
570  const std::vector<vpImagePoint>& corners_copy = tagsCorners_copy[i];
571  REQUIRE(corners_ref.size() == corners_copy.size());
572 
573  for (size_t j = 0; j < corners_ref.size(); j++) {
574  const vpImagePoint& corner_ref = corners_ref[j];
575  const vpImagePoint& corner_copy = corners_copy[j];
576  CHECK(corner_ref == corner_copy);
577  }
578 
579  int id_ref = tagsId[i];
580  int id_copy = tagsId_copy[i];
581  CHECK(id_ref == id_copy);
582  }
583 
584  std::vector<vpHomogeneousMatrix> cMo_vec_copy;
585  detector_copy.detect(I, tagSize, cam, cMo_vec_copy);
586  REQUIRE(cMo_vec.size() == cMo_vec_copy.size());
587  for (size_t idx = 0; idx < cMo_vec_copy.size(); idx++) {
588  const vpHomogeneousMatrix& cMo = cMo_vec[idx];
589  const vpHomogeneousMatrix& cMo_copy = cMo_vec_copy[idx];
590  for (int i = 0; i < 3; i++) {
591  for (int j = 0; j < 4; j++) {
592  CHECK(vpMath::equal(cMo[i][j], cMo_copy[i][j],
593  std::numeric_limits<double>::epsilon()));
594  }
595  }
596  }
597 }
598 
599 TEST_CASE("Apriltag assignment operator test", "[apriltag_assignment_operator_test]") {
600  const std::string filename = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(),
601  "AprilTag/benchmark/640x480/tag21_07_640x480.png");
602  REQUIRE(vpIoTools::checkFilename(filename));
603 
605  vpImageIo::read(I, filename);
606  REQUIRE(I.getSize() == 640*480);
607 
610  const double tagSize = 0.25 * 5/9;
611  const float quad_decimate = 1.0;
612  vpDetectorAprilTag *detector = new vpDetectorAprilTag(tagFamily, poseEstimationMethod);
613  detector->setAprilTagQuadDecimate(quad_decimate);
614 
615  vpCameraParameters cam;
616  cam.initPersProjWithoutDistortion(700, 700, 320, 240);
617 
618  std::vector<vpHomogeneousMatrix> cMo_vec;
619  detector->detect(I, tagSize, cam, cMo_vec);
620  std::vector<std::vector<vpImagePoint> > tagsCorners = detector->getTagsCorners();
621  std::vector<int> tagsId = detector->getTagsId();
622 
623  //Copy
624  vpDetectorAprilTag detector_copy = *detector;
625  //Delete old detector
626  delete detector;
627 
628  std::vector<std::vector<vpImagePoint> > tagsCorners_copy = detector_copy.getTagsCorners();
629  std::vector<int> tagsId_copy = detector_copy.getTagsId();
630  REQUIRE(tagsCorners_copy.size() == tagsCorners.size());
631  REQUIRE(tagsId_copy.size() == tagsId.size());
632  REQUIRE(tagsCorners_copy.size() == tagsId_copy.size());
633 
634  for (size_t i = 0; i < tagsCorners.size(); i++) {
635  const std::vector<vpImagePoint>& corners_ref = tagsCorners[i];
636  const std::vector<vpImagePoint>& corners_copy = tagsCorners_copy[i];
637  REQUIRE(corners_ref.size() == corners_copy.size());
638 
639  for (size_t j = 0; j < corners_ref.size(); j++) {
640  const vpImagePoint& corner_ref = corners_ref[j];
641  const vpImagePoint& corner_copy = corners_copy[j];
642  CHECK(corner_ref == corner_copy);
643  }
644 
645  int id_ref = tagsId[i];
646  int id_copy = tagsId_copy[i];
647  CHECK(id_ref == id_copy);
648  }
649 
650  std::vector<vpHomogeneousMatrix> cMo_vec_copy;
651  detector_copy.detect(I, tagSize, cam, cMo_vec_copy);
652  REQUIRE(cMo_vec.size() == cMo_vec_copy.size());
653  for (size_t idx = 0; idx < cMo_vec_copy.size(); idx++) {
654  const vpHomogeneousMatrix& cMo = cMo_vec[idx];
655  const vpHomogeneousMatrix& cMo_copy = cMo_vec_copy[idx];
656  for (int i = 0; i < 3; i++) {
657  for (int j = 0; j < 4; j++) {
658  CHECK(vpMath::equal(cMo[i][j], cMo_copy[i][j],
659  std::numeric_limits<double>::epsilon()));
660  }
661  }
662  }
663 }
664 
665 TEST_CASE("Apriltag getTagsPoints3D test", "[apriltag_get_tags_points3D_test]") {
666  const std::string filename = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(),
667  "AprilTag/benchmark/640x480/tag21_07_640x480.png");
668  REQUIRE(vpIoTools::checkFilename(filename));
669 
671  vpImageIo::read(I, filename);
672  REQUIRE(I.getSize() == 640*480);
673 
676  const double familyScale = 5.0/9;
677  const double tagSize = 0.25;
678  std::map<int, double> tagsSize = {
679  {-1, tagSize * familyScale},
680  {3, tagSize / 2 * familyScale},
681  {4, tagSize / 2 * familyScale}
682  };
683 
684  vpDetectorAprilTag detector(tagFamily, poseEstimationMethod);
685 
686  vpCameraParameters cam;
687  cam.initPersProjWithoutDistortion(700, 700, 320, 240);
688 
689  std::vector<vpHomogeneousMatrix> cMo_vec;
690  REQUIRE(detector.detect(I));
691 
692  //Compute pose with getPose
693  std::vector<int> tagsId = detector.getTagsId();
694  for (size_t i = 0; i < tagsId.size(); i++) {
695  int id = tagsId[i];
696  double size = tagsSize[-1];
697  if (tagsSize.find(id) != tagsSize.end()) {
698  size = tagsSize[id];
699  }
700 
702  detector.getPose(i, size, cam, cMo);
703  cMo_vec.push_back(cMo);
704  }
705 
706  //Compute pose manually
707  std::vector<std::vector<vpPoint>> tagsPoints = detector.getTagsPoints3D(tagsId, tagsSize);
708  std::vector<std::vector<vpImagePoint>> tagsCorners = detector.getTagsCorners();
709  REQUIRE(tagsPoints.size() == tagsCorners.size());
710 
711  for (size_t i = 0; i < tagsPoints.size(); i++) {
712  REQUIRE(tagsPoints[i].size() == tagsCorners[i].size());
713 
714  for (size_t j = 0; j < tagsPoints[i].size(); j++) {
715  vpPoint& pt = tagsPoints[i][j];
716  const vpImagePoint& imPt = tagsCorners[i][j];
717  double x = 0, y = 0;
718  vpPixelMeterConversion::convertPoint(cam, imPt, x, y);
719  pt.set_x(x);
720  pt.set_y(y);
721  }
722 
723  vpPose pose(tagsPoints[i]);
724  vpHomogeneousMatrix cMo_manual;
725  pose.computePose(vpPose::DEMENTHON_VIRTUAL_VS, cMo_manual);
726 
727  const vpHomogeneousMatrix& cMo = cMo_vec[i];
728  for (unsigned int row = 0; row < cMo.getRows(); row++) {
729  for (unsigned int col = 0; col < cMo.getCols(); col++) {
730  CHECK(vpMath::equal(cMo[row][col], cMo_manual[row][col], std::numeric_limits<double>::epsilon()));
731  }
732  }
733  }
734 }
735 
736 int main(int argc, const char *argv[])
737 {
738  Catch::Session session; // There must be exactly one instance
739 
740  // Let Catch (using Clara) parse the command line
741  session.applyCommandLine(argc, argv);
742 
743  int numFailed = session.run();
744 
745  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
746  // This clamping has already been applied, so just return it here
747  // You can also do any post run clean-up here
748  return numFailed;
749 }
750 #else
751 int main()
752 {
753  return 0;
754 }
755 #endif
AprilTag 16h5 pattern.
AprilTag Standard52h13 pattern.
double get_i() const
Definition: vpImagePoint.h:204
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1292
Implementation of an homogeneous matrix and operations on such kind of matrices.
AprilTag 36h11 pattern (recommended)
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:296
AprilTag Circle21h7 pattern.
std::vector< std::vector< vpPoint > > getTagsPoints3D(const std::vector< int > &tagsId, const std::map< int, double > &tagsSize) const
unsigned int getRows() const
Definition: vpArray2D.h:289
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
size_t getNbObjects() const
bool getPose(size_t tagIndex, double tagSize, const vpCameraParameters &cam, vpHomogeneousMatrix &cMo, vpHomogeneousMatrix *cMo2=NULL, double *projError=NULL, double *projError2=NULL)
Class that defines what is a point.
Definition: vpPoint.h:58
void set_x(double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.cpp:472
AprilTag Circle49h12 pattern.
void set_y(double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.cpp:474
unsigned int getCols() const
Definition: vpArray2D.h:279
void setAprilTagQuadDecimate(float quadDecimate)
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1537
double get_j() const
Definition: vpImagePoint.h:215
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:80
Generic class defining intrinsic camera parameters.
std::vector< std::vector< vpImagePoint > > getTagsCorners() const
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
std::vector< int > getTagsId() const
AprilTag Standard41h12 pattern.
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:243
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
unsigned int getSize() const
Definition: vpImage.h:225
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:151
double sumSquare() const
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:730
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
AprilTag Custom48h12 pattern.
std::vector< std::string > & getMessage()
Class that consider the case of a translation vector.
Implementation of a rotation vector as axis-angle minimal representation.
AprilTag 25h9 pattern.
std::vector< std::vector< vpImagePoint > > & getPolygon()
bool detect(const vpImage< unsigned char > &I)