perf_aruco.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 "perf_precomp.hpp"
  5. namespace opencv_test {
  6. using namespace perf;
  7. typedef tuple<bool, int> UseArucoParams;
  8. typedef TestBaseWithParam<UseArucoParams> EstimateAruco;
  9. #define ESTIMATE_PARAMS Combine(Values(false, true), Values(-1))
  10. static double deg2rad(double deg) { return deg * CV_PI / 180.; }
  11. class MarkerPainter
  12. {
  13. private:
  14. int imgMarkerSize = 0;
  15. Mat cameraMatrix;
  16. public:
  17. MarkerPainter(const int size)
  18. {
  19. setImgMarkerSize(size);
  20. }
  21. void setImgMarkerSize(const int size)
  22. {
  23. imgMarkerSize = size;
  24. cameraMatrix = Mat::eye(3, 3, CV_64FC1);
  25. cameraMatrix.at<double>(0, 0) = cameraMatrix.at<double>(1, 1) = imgMarkerSize;
  26. cameraMatrix.at<double>(0, 2) = imgMarkerSize / 2.0;
  27. cameraMatrix.at<double>(1, 2) = imgMarkerSize / 2.0;
  28. }
  29. static std::pair<Mat, Mat> getSyntheticRT(double yaw, double pitch, double distance)
  30. {
  31. auto rvec_tvec = std::make_pair(Mat(3, 1, CV_64FC1), Mat(3, 1, CV_64FC1));
  32. Mat& rvec = rvec_tvec.first;
  33. Mat& tvec = rvec_tvec.second;
  34. // Rvec
  35. // first put the Z axis aiming to -X (like the camera axis system)
  36. Mat rotZ(3, 1, CV_64FC1);
  37. rotZ.ptr<double>(0)[0] = 0;
  38. rotZ.ptr<double>(0)[1] = 0;
  39. rotZ.ptr<double>(0)[2] = -0.5 * CV_PI;
  40. Mat rotX(3, 1, CV_64FC1);
  41. rotX.ptr<double>(0)[0] = 0.5 * CV_PI;
  42. rotX.ptr<double>(0)[1] = 0;
  43. rotX.ptr<double>(0)[2] = 0;
  44. Mat camRvec, camTvec;
  45. composeRT(rotZ, Mat(3, 1, CV_64FC1, Scalar::all(0)), rotX, Mat(3, 1, CV_64FC1, Scalar::all(0)),
  46. camRvec, camTvec);
  47. // now pitch and yaw angles
  48. Mat rotPitch(3, 1, CV_64FC1);
  49. rotPitch.ptr<double>(0)[0] = 0;
  50. rotPitch.ptr<double>(0)[1] = pitch;
  51. rotPitch.ptr<double>(0)[2] = 0;
  52. Mat rotYaw(3, 1, CV_64FC1);
  53. rotYaw.ptr<double>(0)[0] = yaw;
  54. rotYaw.ptr<double>(0)[1] = 0;
  55. rotYaw.ptr<double>(0)[2] = 0;
  56. composeRT(rotPitch, Mat(3, 1, CV_64FC1, Scalar::all(0)), rotYaw,
  57. Mat(3, 1, CV_64FC1, Scalar::all(0)), rvec, tvec);
  58. // compose both rotations
  59. composeRT(camRvec, Mat(3, 1, CV_64FC1, Scalar::all(0)), rvec,
  60. Mat(3, 1, CV_64FC1, Scalar::all(0)), rvec, tvec);
  61. // Tvec, just move in z (camera) direction the specific distance
  62. tvec.ptr<double>(0)[0] = 0.;
  63. tvec.ptr<double>(0)[1] = 0.;
  64. tvec.ptr<double>(0)[2] = distance;
  65. return rvec_tvec;
  66. }
  67. std::pair<Mat, vector<Point2f> > getProjectMarker(int id, double yaw, double pitch,
  68. const Ptr<aruco::DetectorParameters>& parameters,
  69. const Ptr<aruco::Dictionary>& dictionary)
  70. {
  71. auto marker_corners = std::make_pair(Mat(imgMarkerSize, imgMarkerSize, CV_8UC1, Scalar::all(255)), vector<Point2f>());
  72. Mat& img = marker_corners.first;
  73. vector<Point2f>& corners = marker_corners.second;
  74. // canonical image
  75. const int markerSizePixels = static_cast<int>(imgMarkerSize/sqrt(2.f));
  76. aruco::drawMarker(dictionary, id, markerSizePixels, img, parameters->markerBorderBits);
  77. // get rvec and tvec for the perspective
  78. const double distance = 0.1;
  79. auto rvec_tvec = MarkerPainter::getSyntheticRT(yaw, pitch, distance);
  80. Mat& rvec = rvec_tvec.first;
  81. Mat& tvec = rvec_tvec.second;
  82. const float markerLength = 0.05f;
  83. vector<Point3f> markerObjPoints;
  84. markerObjPoints.emplace_back(Point3f(-markerLength / 2.f, +markerLength / 2.f, 0));
  85. markerObjPoints.emplace_back(markerObjPoints[0] + Point3f(markerLength, 0, 0));
  86. markerObjPoints.emplace_back(markerObjPoints[0] + Point3f(markerLength, -markerLength, 0));
  87. markerObjPoints.emplace_back(markerObjPoints[0] + Point3f(0, -markerLength, 0));
  88. // project markers and draw them
  89. Mat distCoeffs(5, 1, CV_64FC1, Scalar::all(0));
  90. projectPoints(markerObjPoints, rvec, tvec, cameraMatrix, distCoeffs, corners);
  91. vector<Point2f> originalCorners;
  92. originalCorners.emplace_back(Point2f(0.f, 0.f));
  93. originalCorners.emplace_back(originalCorners[0]+Point2f((float)markerSizePixels, 0));
  94. originalCorners.emplace_back(originalCorners[0]+Point2f((float)markerSizePixels, (float)markerSizePixels));
  95. originalCorners.emplace_back(originalCorners[0]+Point2f(0, (float)markerSizePixels));
  96. Mat transformation = getPerspectiveTransform(originalCorners, corners);
  97. warpPerspective(img, img, transformation, Size(imgMarkerSize, imgMarkerSize), INTER_NEAREST, BORDER_CONSTANT,
  98. Scalar::all(255));
  99. return marker_corners;
  100. }
  101. std::pair<Mat, map<int, vector<Point2f> > > getProjectMarkersTile(const int numMarkers,
  102. const Ptr<aruco::DetectorParameters>& params,
  103. const Ptr<aruco::Dictionary>& dictionary)
  104. {
  105. Mat tileImage(imgMarkerSize*numMarkers, imgMarkerSize*numMarkers, CV_8UC1, Scalar::all(255));
  106. map<int, vector<Point2f> > idCorners;
  107. int iter = 0, pitch = 0, yaw = 0;
  108. for (int i = 0; i < numMarkers; i++)
  109. {
  110. for (int j = 0; j < numMarkers; j++)
  111. {
  112. int currentId = iter;
  113. auto marker_corners = getProjectMarker(currentId, deg2rad(70+yaw), deg2rad(pitch), params, dictionary);
  114. Point2i startPoint(j*imgMarkerSize, i*imgMarkerSize);
  115. Mat tmp_roi = tileImage(Rect(startPoint.x, startPoint.y, imgMarkerSize, imgMarkerSize));
  116. marker_corners.first.copyTo(tmp_roi);
  117. for (Point2f& point: marker_corners.second)
  118. point += static_cast<Point2f>(startPoint);
  119. idCorners[currentId] = marker_corners.second;
  120. auto test = idCorners[currentId];
  121. yaw = (yaw + 10) % 51; // 70+yaw >= 70 && 70+yaw <= 120
  122. iter++;
  123. }
  124. pitch = (pitch + 60) % 360;
  125. }
  126. return std::make_pair(tileImage, idCorners);
  127. }
  128. };
  129. static inline double getMaxDistance(map<int, vector<Point2f> > &golds, const vector<int>& ids,
  130. const vector<vector<Point2f> >& corners)
  131. {
  132. std::map<int, double> mapDist;
  133. for (const auto& el : golds)
  134. mapDist[el.first] = std::numeric_limits<double>::max();
  135. for (size_t i = 0; i < ids.size(); i++)
  136. {
  137. int id = ids[i];
  138. const auto gold_corners = golds.find(id);
  139. if (gold_corners != golds.end()) {
  140. double distance = 0.;
  141. for (int c = 0; c < 4; c++)
  142. distance = std::max(distance, cv::norm(gold_corners->second[c] - corners[i][c]));
  143. mapDist[id] = distance;
  144. }
  145. }
  146. return std::max_element(std::begin(mapDist), std::end(mapDist),
  147. [](const pair<int, double>& p1, const pair<int, double>& p2){return p1.second < p2.second;})->second;
  148. }
  149. PERF_TEST_P(EstimateAruco, ArucoFirst, ESTIMATE_PARAMS)
  150. {
  151. UseArucoParams testParams = GetParam();
  152. Ptr<aruco::Dictionary> dictionary = aruco::getPredefinedDictionary(aruco::DICT_6X6_250);
  153. Ptr<aruco::DetectorParameters> detectorParams = aruco::DetectorParameters::create();
  154. detectorParams->minDistanceToBorder = 1;
  155. detectorParams->markerBorderBits = 1;
  156. detectorParams->cornerRefinementMethod = cv::aruco::CORNER_REFINE_SUBPIX;
  157. const int markerSize = 100;
  158. const int numMarkersInRow = 9;
  159. //USE_ARUCO3
  160. detectorParams->useAruco3Detection = get<0>(testParams);
  161. if (detectorParams->useAruco3Detection) {
  162. detectorParams->minSideLengthCanonicalImg = 32;
  163. detectorParams->minMarkerLengthRatioOriginalImg = 0.04f / numMarkersInRow;
  164. }
  165. MarkerPainter painter(markerSize);
  166. auto image_map = painter.getProjectMarkersTile(numMarkersInRow, detectorParams, dictionary);
  167. // detect markers
  168. vector<vector<Point2f> > corners;
  169. vector<int> ids;
  170. TEST_CYCLE()
  171. {
  172. aruco::detectMarkers(image_map.first, dictionary, corners, ids, detectorParams);
  173. }
  174. ASSERT_EQ(numMarkersInRow*numMarkersInRow, static_cast<int>(ids.size()));
  175. double maxDistance = getMaxDistance(image_map.second, ids, corners);
  176. ASSERT_LT(maxDistance, 3.);
  177. SANITY_CHECK_NOTHING();
  178. }
  179. PERF_TEST_P(EstimateAruco, ArucoSecond, ESTIMATE_PARAMS)
  180. {
  181. UseArucoParams testParams = GetParam();
  182. Ptr<aruco::Dictionary> dictionary = aruco::getPredefinedDictionary(aruco::DICT_6X6_250);
  183. Ptr<aruco::DetectorParameters> detectorParams = aruco::DetectorParameters::create();
  184. detectorParams->minDistanceToBorder = 1;
  185. detectorParams->markerBorderBits = 1;
  186. detectorParams->cornerRefinementMethod = cv::aruco::CORNER_REFINE_SUBPIX;
  187. //USE_ARUCO3
  188. detectorParams->useAruco3Detection = get<0>(testParams);
  189. if (detectorParams->useAruco3Detection) {
  190. detectorParams->minSideLengthCanonicalImg = 64;
  191. detectorParams->minMarkerLengthRatioOriginalImg = 0.f;
  192. }
  193. const int markerSize = 200;
  194. const int numMarkersInRow = 11;
  195. MarkerPainter painter(markerSize);
  196. auto image_map = painter.getProjectMarkersTile(numMarkersInRow, detectorParams, dictionary);
  197. // detect markers
  198. vector<vector<Point2f> > corners;
  199. vector<int> ids;
  200. TEST_CYCLE()
  201. {
  202. aruco::detectMarkers(image_map.first, dictionary, corners, ids, detectorParams);
  203. }
  204. ASSERT_EQ(numMarkersInRow*numMarkersInRow, static_cast<int>(ids.size()));
  205. double maxDistance = getMaxDistance(image_map.second, ids, corners);
  206. ASSERT_LT(maxDistance, 3.);
  207. SANITY_CHECK_NOTHING();
  208. }
  209. struct Aruco3Params
  210. {
  211. bool useAruco3Detection = false;
  212. float minMarkerLengthRatioOriginalImg = 0.f;
  213. int minSideLengthCanonicalImg = 0;
  214. Aruco3Params(bool useAruco3, float minMarkerLen, int minSideLen): useAruco3Detection(useAruco3),
  215. minMarkerLengthRatioOriginalImg(minMarkerLen),
  216. minSideLengthCanonicalImg(minSideLen) {}
  217. friend std::ostream& operator<<(std::ostream& os, const Aruco3Params& d)
  218. {
  219. os << d.useAruco3Detection << " " << d.minMarkerLengthRatioOriginalImg << " " << d.minSideLengthCanonicalImg;
  220. return os;
  221. }
  222. };
  223. typedef tuple<Aruco3Params, pair<int, int>> ArucoTestParams;
  224. typedef TestBaseWithParam<ArucoTestParams> EstimateLargeAruco;
  225. #define ESTIMATE_FHD_PARAMS Combine(Values(Aruco3Params(false, 0.f, 0), Aruco3Params(true, 0.f, 32), \
  226. Aruco3Params(true, 0.015f, 32), Aruco3Params(true, 0.f, 16), Aruco3Params(true, 0.0069f, 16)), \
  227. Values(std::make_pair(1440, 1), std::make_pair(480, 3), std::make_pair(144, 10)))
  228. PERF_TEST_P(EstimateLargeAruco, ArucoFHD, ESTIMATE_FHD_PARAMS)
  229. {
  230. ArucoTestParams testParams = GetParam();
  231. Ptr<aruco::Dictionary> dictionary = aruco::getPredefinedDictionary(aruco::DICT_6X6_250);
  232. Ptr<aruco::DetectorParameters> detectorParams = aruco::DetectorParameters::create();
  233. detectorParams->minDistanceToBorder = 1;
  234. detectorParams->markerBorderBits = 1;
  235. detectorParams->cornerRefinementMethod = cv::aruco::CORNER_REFINE_SUBPIX;
  236. //USE_ARUCO3
  237. detectorParams->useAruco3Detection = get<0>(testParams).useAruco3Detection;
  238. if (detectorParams->useAruco3Detection) {
  239. detectorParams->minSideLengthCanonicalImg = get<0>(testParams).minSideLengthCanonicalImg;
  240. detectorParams->minMarkerLengthRatioOriginalImg = get<0>(testParams).minMarkerLengthRatioOriginalImg;
  241. }
  242. const int markerSize = get<1>(testParams).first; // 1440 or 480 or 144
  243. const int numMarkersInRow = get<1>(testParams).second; // 1 or 3 or 144
  244. MarkerPainter painter(markerSize); // num pixels is 1440x1440 as in FHD 1920x1080
  245. auto image_map = painter.getProjectMarkersTile(numMarkersInRow, detectorParams, dictionary);
  246. // detect markers
  247. vector<vector<Point2f> > corners;
  248. vector<int> ids;
  249. TEST_CYCLE()
  250. {
  251. aruco::detectMarkers(image_map.first, dictionary, corners, ids, detectorParams);
  252. }
  253. ASSERT_EQ(numMarkersInRow*numMarkersInRow, static_cast<int>(ids.size()));
  254. double maxDistance = getMaxDistance(image_map.second, ids, corners);
  255. ASSERT_LT(maxDistance, 3.);
  256. SANITY_CHECK_NOTHING();
  257. }
  258. }