test_boarddetection.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. By downloading, copying, installing or using the software you agree to this
  3. license. If you do not agree to this license, do not download, install,
  4. copy or use the software.
  5. License Agreement
  6. For Open Source Computer Vision Library
  7. (3-clause BSD License)
  8. Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  9. Third party copyrights are property of their respective owners.
  10. Redistribution and use in source and binary forms, with or without modification,
  11. are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of the contributors
  18. may be used to endorse or promote products derived from this software
  19. without specific prior written permission.
  20. This software is provided by the copyright holders and contributors "as is" and
  21. any express or implied warranties, including, but not limited to, the implied
  22. warranties of merchantability and fitness for a particular purpose are
  23. disclaimed. In no event shall copyright holders or contributors be liable for
  24. any direct, indirect, incidental, special, exemplary, or consequential damages
  25. (including, but not limited to, procurement of substitute goods or services;
  26. loss of use, data, or profits; or business interruption) however caused
  27. and on any theory of liability, whether in contract, strict liability,
  28. or tort (including negligence or otherwise) arising in any way out of
  29. the use of this software, even if advised of the possibility of such damage.
  30. */
  31. #include "test_precomp.hpp"
  32. namespace opencv_test { namespace {
  33. static double deg2rad(double deg) { return deg * CV_PI / 180.; }
  34. /**
  35. * @brief Get rvec and tvec from yaw, pitch and distance
  36. */
  37. static void getSyntheticRT(double yaw, double pitch, double distance, Mat &rvec, Mat &tvec) {
  38. rvec = Mat(3, 1, CV_64FC1);
  39. tvec = Mat(3, 1, CV_64FC1);
  40. // Rvec
  41. // first put the Z axis aiming to -X (like the camera axis system)
  42. Mat rotZ(3, 1, CV_64FC1);
  43. rotZ.ptr< double >(0)[0] = 0;
  44. rotZ.ptr< double >(0)[1] = 0;
  45. rotZ.ptr< double >(0)[2] = -0.5 * CV_PI;
  46. Mat rotX(3, 1, CV_64FC1);
  47. rotX.ptr< double >(0)[0] = 0.5 * CV_PI;
  48. rotX.ptr< double >(0)[1] = 0;
  49. rotX.ptr< double >(0)[2] = 0;
  50. Mat camRvec, camTvec;
  51. composeRT(rotZ, Mat(3, 1, CV_64FC1, Scalar::all(0)), rotX, Mat(3, 1, CV_64FC1, Scalar::all(0)),
  52. camRvec, camTvec);
  53. // now pitch and yaw angles
  54. Mat rotPitch(3, 1, CV_64FC1);
  55. rotPitch.ptr< double >(0)[0] = 0;
  56. rotPitch.ptr< double >(0)[1] = pitch;
  57. rotPitch.ptr< double >(0)[2] = 0;
  58. Mat rotYaw(3, 1, CV_64FC1);
  59. rotYaw.ptr< double >(0)[0] = yaw;
  60. rotYaw.ptr< double >(0)[1] = 0;
  61. rotYaw.ptr< double >(0)[2] = 0;
  62. composeRT(rotPitch, Mat(3, 1, CV_64FC1, Scalar::all(0)), rotYaw,
  63. Mat(3, 1, CV_64FC1, Scalar::all(0)), rvec, tvec);
  64. // compose both rotations
  65. composeRT(camRvec, Mat(3, 1, CV_64FC1, Scalar::all(0)), rvec,
  66. Mat(3, 1, CV_64FC1, Scalar::all(0)), rvec, tvec);
  67. // Tvec, just move in z (camera) direction the specific distance
  68. tvec.ptr< double >(0)[0] = 0.;
  69. tvec.ptr< double >(0)[1] = 0.;
  70. tvec.ptr< double >(0)[2] = distance;
  71. }
  72. /**
  73. * @brief Project a synthetic marker
  74. */
  75. static void projectMarker(Mat &img, Ptr<aruco::Dictionary> &dictionary, int id,
  76. vector< Point3f > markerObjPoints, Mat cameraMatrix, Mat rvec, Mat tvec,
  77. int markerBorder) {
  78. // canonical image
  79. Mat markerImg;
  80. const int markerSizePixels = 100;
  81. aruco::drawMarker(dictionary, id, markerSizePixels, markerImg, markerBorder);
  82. // projected corners
  83. Mat distCoeffs(5, 1, CV_64FC1, Scalar::all(0));
  84. vector< Point2f > corners;
  85. projectPoints(markerObjPoints, rvec, tvec, cameraMatrix, distCoeffs, corners);
  86. // get perspective transform
  87. vector< Point2f > originalCorners;
  88. originalCorners.push_back(Point2f(0, 0));
  89. originalCorners.push_back(Point2f((float)markerSizePixels, 0));
  90. originalCorners.push_back(Point2f((float)markerSizePixels, (float)markerSizePixels));
  91. originalCorners.push_back(Point2f(0, (float)markerSizePixels));
  92. Mat transformation = getPerspectiveTransform(originalCorners, corners);
  93. // apply transformation
  94. Mat aux;
  95. const char borderValue = 127;
  96. warpPerspective(markerImg, aux, transformation, img.size(), INTER_NEAREST, BORDER_CONSTANT,
  97. Scalar::all(borderValue));
  98. // copy only not-border pixels
  99. for(int y = 0; y < aux.rows; y++) {
  100. for(int x = 0; x < aux.cols; x++) {
  101. if(aux.at< unsigned char >(y, x) == borderValue) continue;
  102. img.at< unsigned char >(y, x) = aux.at< unsigned char >(y, x);
  103. }
  104. }
  105. }
  106. /**
  107. * @brief Get a synthetic image of GridBoard in perspective
  108. */
  109. static Mat projectBoard(Ptr<aruco::GridBoard> &board, Mat cameraMatrix, double yaw, double pitch,
  110. double distance, Size imageSize, int markerBorder) {
  111. Mat rvec, tvec;
  112. getSyntheticRT(yaw, pitch, distance, rvec, tvec);
  113. Mat img = Mat(imageSize, CV_8UC1, Scalar::all(255));
  114. for(unsigned int m = 0; m < board->ids.size(); m++) {
  115. projectMarker(img, board->dictionary, board->ids[m], board->objPoints[m], cameraMatrix, rvec,
  116. tvec, markerBorder);
  117. }
  118. return img;
  119. }
  120. enum class ArucoAlgParams
  121. {
  122. USE_DEFAULT = 0,
  123. USE_ARUCO3 = 1
  124. };
  125. /**
  126. * @brief Check pose estimation of aruco board
  127. */
  128. class CV_ArucoBoardPose : public cvtest::BaseTest {
  129. public:
  130. CV_ArucoBoardPose(ArucoAlgParams arucoAlgParams)
  131. {
  132. params = aruco::DetectorParameters::create();
  133. params->minDistanceToBorder = 3;
  134. if (arucoAlgParams == ArucoAlgParams::USE_ARUCO3) {
  135. params->useAruco3Detection = true;
  136. params->cornerRefinementMethod = aruco::CORNER_REFINE_SUBPIX;
  137. params->minSideLengthCanonicalImg = 16;
  138. }
  139. }
  140. protected:
  141. Ptr<aruco::DetectorParameters> params;
  142. void run(int);
  143. };
  144. void CV_ArucoBoardPose::run(int) {
  145. int iter = 0;
  146. Mat cameraMatrix = Mat::eye(3, 3, CV_64FC1);
  147. Size imgSize(500, 500);
  148. Ptr<aruco::Dictionary> dictionary = aruco::getPredefinedDictionary(aruco::DICT_6X6_250);
  149. Ptr<aruco::GridBoard> gridboard = aruco::GridBoard::create(3, 3, 0.02f, 0.005f, dictionary);
  150. Ptr<aruco::Board> board = gridboard.staticCast<aruco::Board>();
  151. cameraMatrix.at< double >(0, 0) = cameraMatrix.at< double >(1, 1) = 650;
  152. cameraMatrix.at< double >(0, 2) = imgSize.width / 2;
  153. cameraMatrix.at< double >(1, 2) = imgSize.height / 2;
  154. Mat distCoeffs(5, 1, CV_64FC1, Scalar::all(0));
  155. // for different perspectives
  156. for(double distance = 0.2; distance <= 0.4; distance += 0.2) {
  157. for(int yaw = 0; yaw < 360; yaw += 100) {
  158. for(int pitch = 30; pitch <= 90; pitch += 50) {
  159. for(unsigned int i = 0; i < gridboard->ids.size(); i++)
  160. gridboard->ids[i] = (iter + int(i)) % 250;
  161. int markerBorder = iter % 2 + 1;
  162. iter++;
  163. // create synthetic image
  164. Mat img = projectBoard(gridboard, cameraMatrix, deg2rad(pitch), deg2rad(yaw), distance,
  165. imgSize, markerBorder);
  166. vector< vector< Point2f > > corners;
  167. vector< int > ids;
  168. params->markerBorderBits = markerBorder;
  169. aruco::detectMarkers(img, dictionary, corners, ids, params);
  170. if(ids.size() == 0) {
  171. ts->printf(cvtest::TS::LOG, "Marker detection failed in Board test");
  172. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  173. return;
  174. }
  175. // estimate pose
  176. Mat rvec, tvec;
  177. aruco::estimatePoseBoard(corners, ids, board, cameraMatrix, distCoeffs, rvec, tvec);
  178. // check result
  179. for(unsigned int i = 0; i < ids.size(); i++) {
  180. int foundIdx = -1;
  181. for(unsigned int j = 0; j < gridboard->ids.size(); j++) {
  182. if(gridboard->ids[j] == ids[i]) {
  183. foundIdx = int(j);
  184. break;
  185. }
  186. }
  187. if(foundIdx == -1) {
  188. ts->printf(cvtest::TS::LOG, "Marker detected with wrong ID in Board test");
  189. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  190. return;
  191. }
  192. vector< Point2f > projectedCorners;
  193. projectPoints(gridboard->objPoints[foundIdx], rvec, tvec, cameraMatrix, distCoeffs,
  194. projectedCorners);
  195. for(int c = 0; c < 4; c++) {
  196. double repError = cv::norm(projectedCorners[c] - corners[i][c]); // TODO cvtest
  197. if(repError > 5.) {
  198. ts->printf(cvtest::TS::LOG, "Corner reprojection error too high");
  199. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  200. return;
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. /**
  209. * @brief Check refine strategy
  210. */
  211. class CV_ArucoRefine : public cvtest::BaseTest {
  212. public:
  213. CV_ArucoRefine(ArucoAlgParams arucoAlgParams)
  214. {
  215. params = aruco::DetectorParameters::create();
  216. params->minDistanceToBorder = 3;
  217. params->cornerRefinementMethod = aruco::CORNER_REFINE_SUBPIX;
  218. if (arucoAlgParams == ArucoAlgParams::USE_ARUCO3)
  219. params->useAruco3Detection = true;
  220. }
  221. protected:
  222. Ptr<aruco::DetectorParameters> params;
  223. void run(int);
  224. };
  225. void CV_ArucoRefine::run(int) {
  226. int iter = 0;
  227. Mat cameraMatrix = Mat::eye(3, 3, CV_64FC1);
  228. Size imgSize(500, 500);
  229. Ptr<aruco::Dictionary> dictionary = aruco::getPredefinedDictionary(aruco::DICT_6X6_250);
  230. Ptr<aruco::GridBoard> gridboard = aruco::GridBoard::create(3, 3, 0.02f, 0.005f, dictionary);
  231. Ptr<aruco::Board> board = gridboard.staticCast<aruco::Board>();
  232. cameraMatrix.at< double >(0, 0) = cameraMatrix.at< double >(1, 1) = 650;
  233. cameraMatrix.at< double >(0, 2) = imgSize.width / 2;
  234. cameraMatrix.at< double >(1, 2) = imgSize.height / 2;
  235. Mat distCoeffs(5, 1, CV_64FC1, Scalar::all(0));
  236. // for different perspectives
  237. for(double distance = 0.2; distance <= 0.4; distance += 0.2) {
  238. for(int yaw = 0; yaw < 360; yaw += 100) {
  239. for(int pitch = 30; pitch <= 90; pitch += 50) {
  240. for(unsigned int i = 0; i < gridboard->ids.size(); i++)
  241. gridboard->ids[i] = (iter + int(i)) % 250;
  242. int markerBorder = iter % 2 + 1;
  243. iter++;
  244. // create synthetic image
  245. Mat img = projectBoard(gridboard, cameraMatrix, deg2rad(pitch), deg2rad(yaw), distance,
  246. imgSize, markerBorder);
  247. // detect markers
  248. vector< vector< Point2f > > corners, rejected;
  249. vector< int > ids;
  250. params->markerBorderBits = markerBorder;
  251. aruco::detectMarkers(img, dictionary, corners, ids, params, rejected);
  252. // remove a marker from detection
  253. int markersBeforeDelete = (int)ids.size();
  254. if(markersBeforeDelete < 2) continue;
  255. rejected.push_back(corners[0]);
  256. corners.erase(corners.begin(), corners.begin() + 1);
  257. ids.erase(ids.begin(), ids.begin() + 1);
  258. // try to refind the erased marker
  259. aruco::refineDetectedMarkers(img, board, corners, ids, rejected, cameraMatrix,
  260. distCoeffs, 10, 3., true, noArray(), params);
  261. // check result
  262. if((int)ids.size() < markersBeforeDelete) {
  263. ts->printf(cvtest::TS::LOG, "Error in refine detected markers");
  264. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  265. return;
  266. }
  267. }
  268. }
  269. }
  270. }
  271. TEST(CV_ArucoBoardPose, accuracy) {
  272. CV_ArucoBoardPose test(ArucoAlgParams::USE_DEFAULT);
  273. test.safe_run();
  274. }
  275. typedef CV_ArucoBoardPose CV_Aruco3BoardPose;
  276. TEST(CV_Aruco3BoardPose, accuracy) {
  277. CV_Aruco3BoardPose test(ArucoAlgParams::USE_ARUCO3);
  278. test.safe_run();
  279. }
  280. typedef CV_ArucoRefine CV_Aruco3Refine;
  281. TEST(CV_ArucoRefine, accuracy) {
  282. CV_ArucoRefine test(ArucoAlgParams::USE_DEFAULT);
  283. test.safe_run();
  284. }
  285. TEST(CV_Aruco3Refine, accuracy) {
  286. CV_Aruco3Refine test(ArucoAlgParams::USE_ARUCO3);
  287. test.safe_run();
  288. }
  289. TEST(CV_ArucoBoardPose, CheckNegativeZ)
  290. {
  291. double matrixData[9] = { -3.9062571886921410e+02, 0., 4.2350000000000000e+02,
  292. 0., 3.9062571886921410e+02, 2.3950000000000000e+02,
  293. 0., 0., 1 };
  294. cv::Mat cameraMatrix = cv::Mat(3, 3, CV_64F, matrixData);
  295. cv::Ptr<cv::aruco::Board> boardPtr(new cv::aruco::Board);
  296. cv::aruco::Board& board = *boardPtr;
  297. board.ids.push_back(0);
  298. board.ids.push_back(1);
  299. vector<cv::Point3f> pts3d;
  300. pts3d.push_back(cv::Point3f(0.326198f, -0.030621f, 0.303620f));
  301. pts3d.push_back(cv::Point3f(0.325340f, -0.100594f, 0.301862f));
  302. pts3d.push_back(cv::Point3f(0.255859f, -0.099530f, 0.293416f));
  303. pts3d.push_back(cv::Point3f(0.256717f, -0.029557f, 0.295174f));
  304. board.objPoints.push_back(pts3d);
  305. pts3d.clear();
  306. pts3d.push_back(cv::Point3f(-0.033144f, -0.034819f, 0.245216f));
  307. pts3d.push_back(cv::Point3f(-0.035507f, -0.104705f, 0.241987f));
  308. pts3d.push_back(cv::Point3f(-0.105289f, -0.102120f, 0.237120f));
  309. pts3d.push_back(cv::Point3f(-0.102926f, -0.032235f, 0.240349f));
  310. board.objPoints.push_back(pts3d);
  311. vector<vector<Point2f> > corners;
  312. vector<Point2f> pts2d;
  313. pts2d.push_back(cv::Point2f(37.7f, 203.3f));
  314. pts2d.push_back(cv::Point2f(38.5f, 120.5f));
  315. pts2d.push_back(cv::Point2f(105.5f, 115.8f));
  316. pts2d.push_back(cv::Point2f(104.2f, 202.7f));
  317. corners.push_back(pts2d);
  318. pts2d.clear();
  319. pts2d.push_back(cv::Point2f(476.0f, 184.2f));
  320. pts2d.push_back(cv::Point2f(479.6f, 73.8f));
  321. pts2d.push_back(cv::Point2f(590.9f, 77.0f));
  322. pts2d.push_back(cv::Point2f(587.5f, 188.1f));
  323. corners.push_back(pts2d);
  324. Vec3d rvec, tvec;
  325. int nUsed = cv::aruco::estimatePoseBoard(corners, board.ids, boardPtr, cameraMatrix, Mat(), rvec, tvec);
  326. ASSERT_EQ(nUsed, 2);
  327. cv::Matx33d rotm; cv::Point3d out;
  328. cv::Rodrigues(rvec, rotm);
  329. out = cv::Point3d(tvec) + rotm*Point3d(board.objPoints[0][0]);
  330. ASSERT_GT(out.z, 0);
  331. corners.clear(); pts2d.clear();
  332. pts2d.push_back(cv::Point2f(38.4f, 204.5f));
  333. pts2d.push_back(cv::Point2f(40.0f, 124.7f));
  334. pts2d.push_back(cv::Point2f(102.0f, 119.1f));
  335. pts2d.push_back(cv::Point2f(99.9f, 203.6f));
  336. corners.push_back(pts2d);
  337. pts2d.clear();
  338. pts2d.push_back(cv::Point2f(476.0f, 184.3f));
  339. pts2d.push_back(cv::Point2f(479.2f, 75.1f));
  340. pts2d.push_back(cv::Point2f(588.7f, 79.2f));
  341. pts2d.push_back(cv::Point2f(586.3f, 188.5f));
  342. corners.push_back(pts2d);
  343. nUsed = cv::aruco::estimatePoseBoard(corners, board.ids, boardPtr, cameraMatrix, Mat(), rvec, tvec, true);
  344. ASSERT_EQ(nUsed, 2);
  345. cv::Rodrigues(rvec, rotm);
  346. out = cv::Point3d(tvec) + rotm*Point3d(board.objPoints[0][0]);
  347. ASSERT_GT(out.z, 0);
  348. }
  349. }} // namespace