test_aruco_tutorial.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. #include "opencv2/objdetect/aruco_detector.hpp"
  6. namespace opencv_test { namespace {
  7. TEST(CV_ArucoTutorial, can_find_singlemarkersoriginal)
  8. {
  9. string img_path = cvtest::findDataFile("aruco/singlemarkersoriginal.jpg");
  10. Mat image = imread(img_path);
  11. aruco::ArucoDetector detector(aruco::getPredefinedDictionary(aruco::DICT_6X6_250));
  12. vector<int> ids;
  13. vector<vector<Point2f> > corners, rejected;
  14. const size_t N = 6ull;
  15. // corners of ArUco markers with indices goldCornersIds
  16. const int goldCorners[N][8] = { {359,310, 404,310, 410,350, 362,350}, {427,255, 469,256, 477,289, 434,288},
  17. {233,273, 190,273, 196,241, 237,241}, {298,185, 334,186, 335,212, 297,211},
  18. {425,163, 430,186, 394,186, 390,162}, {195,155, 230,155, 227,178, 190,178} };
  19. const int goldCornersIds[N] = { 40, 98, 62, 23, 124, 203};
  20. map<int, const int*> mapGoldCorners;
  21. for (size_t i = 0; i < N; i++)
  22. mapGoldCorners[goldCornersIds[i]] = goldCorners[i];
  23. detector.detectMarkers(image, corners, ids, rejected);
  24. ASSERT_EQ(N, ids.size());
  25. for (size_t i = 0; i < N; i++)
  26. {
  27. int arucoId = ids[i];
  28. ASSERT_EQ(4ull, corners[i].size());
  29. ASSERT_TRUE(mapGoldCorners.find(arucoId) != mapGoldCorners.end());
  30. for (int j = 0; j < 4; j++)
  31. {
  32. EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j * 2]), corners[i][j].x, 1.f);
  33. EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j * 2 + 1]), corners[i][j].y, 1.f);
  34. }
  35. }
  36. }
  37. TEST(CV_ArucoTutorial, can_find_gboriginal)
  38. {
  39. string imgPath = cvtest::findDataFile("aruco/gboriginal.jpg");
  40. Mat image = imread(imgPath);
  41. string dictPath = cvtest::findDataFile("aruco/tutorial_dict.yml");
  42. aruco::Dictionary dictionary;
  43. FileStorage fs(dictPath, FileStorage::READ);
  44. dictionary.aruco::Dictionary::readDictionary(fs.root()); // set marker from tutorial_dict.yml
  45. aruco::DetectorParameters detectorParams;
  46. aruco::ArucoDetector detector(dictionary, detectorParams);
  47. vector<int> ids;
  48. vector<vector<Point2f> > corners, rejected;
  49. const size_t N = 35ull;
  50. // corners of ArUco markers with indices 0, 1, ..., 34
  51. const int goldCorners[N][8] = { {252,74, 286,81, 274,102, 238,95}, {295,82, 330,89, 319,111, 282,104},
  52. {338,91, 375,99, 365,121, 327,113}, {383,100, 421,107, 412,130, 374,123},
  53. {429,109, 468,116, 461,139, 421,132}, {235,100, 270,108, 257,130, 220,122},
  54. {279,109, 316,117, 304,140, 266,133}, {324,119, 362,126, 352,150, 313,143},
  55. {371,128, 410,136, 400,161, 360,152}, {418,139, 459,145, 451,170, 410,163},
  56. {216,128, 253,136, 239,161, 200,152}, {262,138, 300,146, 287,172, 248,164},
  57. {309,148, 349,156, 337,183, 296,174}, {358,158, 398,167, 388,194, 346,185},
  58. {407,169, 449,176, 440,205, 397,196}, {196,158, 235,168, 218,195, 179,185},
  59. {243,170, 283,178, 269,206, 228,197}, {293,180, 334,190, 321,218, 279,209},
  60. {343,192, 385,200, 374,230, 330,220}, {395,203, 438,211, 429,241, 384,233},
  61. {174,192, 215,201, 197,231, 156,221}, {223,204, 265,213, 249,244, 207,234},
  62. {275,215, 317,225, 303,257, 259,246}, {327,227, 371,238, 359,270, 313,259},
  63. {381,240, 426,249, 416,282, 369,273}, {151,228, 193,238, 173,271, 130,260},
  64. {202,241, 245,251, 228,285, 183,274}, {255,254, 300,264, 284,299, 238,288},
  65. {310,267, 355,278, 342,314, 295,302}, {366,281, 413,290, 402,327, 353,317},
  66. {125,267, 168,278, 147,314, 102,303}, {178,281, 223,293, 204,330, 157,317},
  67. {233,296, 280,307, 263,346, 214,333}, {291,310, 338,322, 323,363, 274,349},
  68. {349,325, 399,336, 386,378, 335,366} };
  69. map<int, const int*> mapGoldCorners;
  70. for (int i = 0; i < static_cast<int>(N); i++)
  71. mapGoldCorners[i] = goldCorners[i];
  72. detector.detectMarkers(image, corners, ids, rejected);
  73. ASSERT_EQ(N, ids.size());
  74. for (size_t i = 0; i < N; i++)
  75. {
  76. int arucoId = ids[i];
  77. ASSERT_EQ(4ull, corners[i].size());
  78. ASSERT_TRUE(mapGoldCorners.find(arucoId) != mapGoldCorners.end());
  79. for (int j = 0; j < 4; j++)
  80. {
  81. EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j*2]), corners[i][j].x, 1.f);
  82. EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j*2+1]), corners[i][j].y, 1.f);
  83. }
  84. }
  85. }
  86. TEST(CV_ArucoTutorial, can_find_choriginal)
  87. {
  88. string imgPath = cvtest::findDataFile("aruco/choriginal.jpg");
  89. Mat image = imread(imgPath);
  90. aruco::ArucoDetector detector(aruco::getPredefinedDictionary(aruco::DICT_6X6_250));
  91. vector< int > ids;
  92. vector< vector< Point2f > > corners, rejected;
  93. const size_t N = 17ull;
  94. // corners of aruco markers with indices goldCornersIds
  95. const int goldCorners[N][8] = { {268,77, 290,80, 286,97, 263,94}, {360,90, 382,93, 379,111, 357,108},
  96. {211,106, 233,109, 228,127, 205,123}, {306,120, 328,124, 325,142, 302,138},
  97. {402,135, 425,139, 423,157, 400,154}, {247,152, 271,155, 267,174, 242,171},
  98. {347,167, 371,171, 369,191, 344,187}, {185,185, 209,189, 203,210, 178,206},
  99. {288,201, 313,206, 309,227, 284,223}, {393,218, 418,222, 416,245, 391,241},
  100. {223,240, 250,244, 244,268, 217,263}, {333,258, 359,262, 356,286, 329,282},
  101. {152,281, 179,285, 171,312, 143,307}, {267,300, 294,305, 289,331, 261,327},
  102. {383,319, 410,324, 408,351, 380,347}, {194,347, 223,352, 216,382, 186,377},
  103. {315,368, 345,373, 341,403, 310,398} };
  104. map<int, const int*> mapGoldCorners;
  105. for (int i = 0; i < static_cast<int>(N); i++)
  106. mapGoldCorners[i] = goldCorners[i];
  107. detector.detectMarkers(image, corners, ids, rejected);
  108. ASSERT_EQ(N, ids.size());
  109. for (size_t i = 0; i < N; i++)
  110. {
  111. int arucoId = ids[i];
  112. ASSERT_EQ(4ull, corners[i].size());
  113. ASSERT_TRUE(mapGoldCorners.find(arucoId) != mapGoldCorners.end());
  114. for (int j = 0; j < 4; j++)
  115. {
  116. EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j * 2]), corners[i][j].x, 1.f);
  117. EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j * 2 + 1]), corners[i][j].y, 1.f);
  118. }
  119. }
  120. }
  121. TEST(CV_ArucoTutorial, can_find_chocclusion)
  122. {
  123. string imgPath = cvtest::findDataFile("aruco/chocclusion_original.jpg");
  124. Mat image = imread(imgPath);
  125. aruco::ArucoDetector detector(aruco::getPredefinedDictionary(aruco::DICT_6X6_250));
  126. vector< int > ids;
  127. vector< vector< Point2f > > corners, rejected;
  128. const size_t N = 13ull;
  129. // corners of aruco markers with indices goldCornersIds
  130. const int goldCorners[N][8] = { {301,57, 322,62, 317,79, 295,73}, {391,80, 413,85, 408,103, 386,97},
  131. {242,79, 264,85, 256,102, 234,96}, {334,103, 357,109, 352,126, 329,121},
  132. {428,129, 451,134, 448,152, 425,146}, {274,128, 296,134, 290,153, 266,147},
  133. {371,154, 394,160, 390,180, 366,174}, {208,155, 232,161, 223,181, 199,175},
  134. {309,182, 333,188, 327,209, 302,203}, {411,210, 436,216, 432,238, 407,231},
  135. {241,212, 267,219, 258,242, 232,235}, {167,244, 194,252, 183,277, 156,269},
  136. {202,314, 230,322, 220,349, 191,341} };
  137. map<int, const int*> mapGoldCorners;
  138. const int goldCornersIds[N] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15};
  139. for (int i = 0; i < static_cast<int>(N); i++)
  140. mapGoldCorners[goldCornersIds[i]] = goldCorners[i];
  141. detector.detectMarkers(image, corners, ids, rejected);
  142. ASSERT_EQ(N, ids.size());
  143. for (size_t i = 0; i < N; i++)
  144. {
  145. int arucoId = ids[i];
  146. ASSERT_EQ(4ull, corners[i].size());
  147. ASSERT_TRUE(mapGoldCorners.find(arucoId) != mapGoldCorners.end());
  148. for (int j = 0; j < 4; j++)
  149. {
  150. EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j * 2]), corners[i][j].x, 1.f);
  151. EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j * 2 + 1]), corners[i][j].y, 1.f);
  152. }
  153. }
  154. }
  155. TEST(CV_ArucoTutorial, can_find_diamondmarkers)
  156. {
  157. string imgPath = cvtest::findDataFile("aruco/diamondmarkers.jpg");
  158. Mat image = imread(imgPath);
  159. string dictPath = cvtest::findDataFile("aruco/tutorial_dict.yml");
  160. aruco::Dictionary dictionary;
  161. FileStorage fs(dictPath, FileStorage::READ);
  162. dictionary.aruco::Dictionary::readDictionary(fs.root()); // set marker from tutorial_dict.yml
  163. string detectorPath = cvtest::findDataFile("aruco/detector_params.yml");
  164. fs = FileStorage(detectorPath, FileStorage::READ);
  165. aruco::DetectorParameters detectorParams;
  166. detectorParams.readDetectorParameters(fs.root());
  167. detectorParams.cornerRefinementMethod = aruco::CORNER_REFINE_APRILTAG;
  168. aruco::CharucoBoard charucoBoard(Size(3, 3), 0.4f, 0.25f, dictionary);
  169. aruco::CharucoDetector detector(charucoBoard, aruco::CharucoParameters(), detectorParams);
  170. vector<int> ids;
  171. vector<vector<Point2f> > corners, diamondCorners;
  172. vector<Vec4i> diamondIds;
  173. const size_t N = 12ull;
  174. // corner indices of ArUco markers
  175. const int goldCornersIds[N] = { 4, 12, 11, 3, 12, 10, 12, 10, 10, 11, 2, 11 };
  176. map<int, int> counterGoldCornersIds;
  177. for (int i = 0; i < static_cast<int>(N); i++)
  178. counterGoldCornersIds[goldCornersIds[i]]++;
  179. const size_t diamondsN = 3;
  180. // corners of diamonds with Vec4i indices
  181. const float goldDiamondCorners[diamondsN][8] = {{195.6f,150.9f, 213.5f,201.2f, 136.4f,215.3f, 122.4f,163.5f},
  182. {501.1f,171.3f, 501.9f,208.5f, 446.2f,199.8f, 447.8f,163.3f},
  183. {343.4f,361.2f, 359.7f,328.7f, 400.8f,344.6f, 385.7f,378.4f}};
  184. auto comp = [](const Vec4i& a, const Vec4i& b) {
  185. for (int i = 0; i < 3; i++)
  186. if (a[i] != b[i]) return a[i] < b[i];
  187. return a[3] < b[3];
  188. };
  189. map<Vec4i, const float*, decltype(comp)> goldDiamonds(comp);
  190. goldDiamonds[Vec4i(10, 4, 11, 12)] = goldDiamondCorners[0];
  191. goldDiamonds[Vec4i(10, 3, 11, 12)] = goldDiamondCorners[1];
  192. goldDiamonds[Vec4i(10, 2, 11, 12)] = goldDiamondCorners[2];
  193. detector.detectDiamonds(image, diamondCorners, diamondIds, corners, ids);
  194. map<int, int> counterRes;
  195. ASSERT_EQ(N, ids.size());
  196. for (size_t i = 0; i < N; i++)
  197. {
  198. int arucoId = ids[i];
  199. counterRes[arucoId]++;
  200. }
  201. ASSERT_EQ(counterGoldCornersIds, counterRes); // check the number of ArUco markers
  202. ASSERT_EQ(goldDiamonds.size(), diamondIds.size()); // check the number of diamonds
  203. for (size_t i = 0; i < goldDiamonds.size(); i++)
  204. {
  205. Vec4i diamondId = diamondIds[i];
  206. ASSERT_TRUE(goldDiamonds.find(diamondId) != goldDiamonds.end());
  207. for (int j = 0; j < 4; j++)
  208. {
  209. EXPECT_NEAR(goldDiamonds[diamondId][j * 2], diamondCorners[i][j].x, 0.5f);
  210. EXPECT_NEAR(goldDiamonds[diamondId][j * 2 + 1], diamondCorners[i][j].y, 0.5f);
  211. }
  212. }
  213. }
  214. }} // namespace