From 63204aca972aa0171d25bebb1fe95d8ffbbc2bbe Mon Sep 17 00:00:00 2001 From: AleksandrPanov Date: Thu, 3 Mar 2022 06:00:59 +0300 Subject: [PATCH] add axes tests --- modules/aruco/test/test_aruco_utils.hpp | 22 +++++++++++++++++ modules/aruco/test/test_boarddetection.cpp | 26 +++++++++++++------- modules/aruco/test/test_charucodetection.cpp | 18 +++++++++----- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/modules/aruco/test/test_aruco_utils.hpp b/modules/aruco/test/test_aruco_utils.hpp index 7c7fd046203..853c2712c0c 100644 --- a/modules/aruco/test/test_aruco_utils.hpp +++ b/modules/aruco/test/test_aruco_utils.hpp @@ -5,6 +5,28 @@ namespace opencv_test { namespace { +static inline vector getAxis(InputArray _cameraMatrix, InputArray _distCoeffs, + InputArray _rvec, InputArray _tvec, float length, + const float offset = 0.f) +{ + vector axis; + axis.push_back(Point3f(offset, offset, 0.f)); + axis.push_back(Point3f(length+offset, offset, 0.f)); + axis.push_back(Point3f(offset, length+offset, 0.f)); + axis.push_back(Point3f(offset, offset, -length)); + vector axis_to_img; + projectPoints(axis, _rvec, _tvec, _cameraMatrix, _distCoeffs, axis_to_img); + return axis_to_img; +} + +static inline vector getMarkerById(int id, const vector >& corners, const vector& ids) +{ + for (size_t i = 0ull; i < ids.size(); i++) + if (ids[i] == id) + return corners[i]; + return vector(); +} + static inline double deg2rad(double deg) { return deg * CV_PI / 180.; } /** diff --git a/modules/aruco/test/test_boarddetection.cpp b/modules/aruco/test/test_boarddetection.cpp index debe3dcd867..00cfb054da4 100644 --- a/modules/aruco/test/test_boarddetection.cpp +++ b/modules/aruco/test/test_boarddetection.cpp @@ -70,9 +70,9 @@ void CV_ArucoBoardPose::run(int) { Mat distCoeffs(5, 1, CV_64FC1, Scalar::all(0)); // for different perspectives - for(double distance = 0.2; distance <= 0.4; distance += 0.2) { - for(int yaw = -60; yaw <= 60; yaw += 30) { - for(int pitch = -60; pitch <= 60; pitch += 30) { + for(double distance = 0.2; distance <= 0.4; distance += 0.15) { + for(int yaw = -55; yaw <= 50; yaw += 25) { + for(int pitch = -55; pitch <= 50; pitch += 25) { for(unsigned int i = 0; i < gridboard->ids.size(); i++) gridboard->ids[i] = (iter + int(i)) % 250; int markerBorder = iter % 2 + 1; @@ -87,17 +87,25 @@ void CV_ArucoBoardPose::run(int) { params->markerBorderBits = markerBorder; aruco::detectMarkers(img, dictionary, corners, ids, params); - if(ids.size() == 0) { - ts->printf(cvtest::TS::LOG, "Marker detection failed in Board test"); - ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH); - return; - } + ASSERT_EQ(ids.size(), gridboard->ids.size()); // estimate pose Mat rvec, tvec; aruco::estimatePoseBoard(corners, ids, board, cameraMatrix, distCoeffs, rvec, tvec); - // check result + // check axes + vector axes = getAxis(cameraMatrix, distCoeffs, rvec, tvec, gridboard->rightBottomBorder.x); + vector topLeft = getMarkerById(gridboard->ids[0], corners, ids); + ASSERT_NEAR(topLeft[0].x, axes[0].x, 2.f); + ASSERT_NEAR(topLeft[0].y, axes[0].y, 2.f); + vector topRight = getMarkerById(gridboard->ids[2], corners, ids); + ASSERT_NEAR(topRight[1].x, axes[1].x, 2.f); + ASSERT_NEAR(topRight[1].y, axes[1].y, 2.f); + vector bottomLeft = getMarkerById(gridboard->ids[6], corners, ids); + ASSERT_NEAR(bottomLeft[3].x, axes[2].x, 2.f); + ASSERT_NEAR(bottomLeft[3].y, axes[2].y, 2.f); + + // check estimate result for(unsigned int i = 0; i < ids.size(); i++) { int foundIdx = -1; for(unsigned int j = 0; j < gridboard->ids.size(); j++) { diff --git a/modules/aruco/test/test_charucodetection.cpp b/modules/aruco/test/test_charucodetection.cpp index 96eb9e5d782..9140e33ce35 100644 --- a/modules/aruco/test/test_charucodetection.cpp +++ b/modules/aruco/test/test_charucodetection.cpp @@ -266,11 +266,7 @@ void CV_CharucoPoseEstimation::run(int) { params->markerBorderBits = markerBorder; aruco::detectMarkers(img, dictionary, corners, ids, params); - if(ids.size() == 0) { - ts->printf(cvtest::TS::LOG, "Marker detection failed"); - ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH); - return; - } + ASSERT_EQ(ids.size(), board->ids.size()); // interpolate charuco corners vector< Point2f > charucoCorners; @@ -291,7 +287,17 @@ void CV_CharucoPoseEstimation::run(int) { distCoeffs, rvec, tvec); - // check result + // check axes + const float offset = (board->getSquareLength() - board->getMarkerLength()) / 2.f; + vector axes = getAxis(cameraMatrix, distCoeffs, rvec, tvec, board->getSquareLength(), offset); + vector topLeft = getMarkerById(board->ids[0], corners, ids); + ASSERT_NEAR(topLeft[0].x, axes[1].x, 3.f); + ASSERT_NEAR(topLeft[0].y, axes[1].y, 3.f); + vector bottomLeft = getMarkerById(board->ids[2], corners, ids); + ASSERT_NEAR(bottomLeft[0].x, axes[2].x, 3.f); + ASSERT_NEAR(bottomLeft[0].y, axes[2].y, 3.f); + + // check estimate result vector< Point2f > projectedCharucoCorners; projectPoints(board->chessboardCorners, rvec, tvec, cameraMatrix, distCoeffs,