21 #include <opencv2/calib3d.hpp>
22 #include <opencv2/imgproc/imgproc.hpp>
24 #include <Eigen/Geometry>
39 cv::aruco::DetectorParameters dparams;
41 switch (aruco::dictionary::get())
43 case aruco::Dict::ATAG_16h5:
44 case aruco::Dict::ATAG_25h9:
45 case aruco::Dict::ATAG_36h10:
46 case aruco::Dict::ATAG_36h11:
47 dparams.cornerRefinementMethod = cv::aruco::CORNER_REFINE_APRILTAG;
51 dparams.cornerRefinementMethod = cv::aruco::CORNER_REFINE_SUBPIX;
55 std::string
const dpf = aruco::detparams::get();
56 if (dpf.empty() ==
false)
58 cv::FileStorage fs(dpf, cv::FileStorage::READ);
61 if (dparams.readDetectorParameters(fs.root()) ==
false)
62 LERROR(
"Error reading ArUco detector parameters from file [" << dpf <<
"] -- IGNORED");
64 else LERROR(
"Failed to read ArUco detector parameters from file [" << dpf <<
"] -- IGNORED");
68 cv::aruco::Dictionary dico;
69 switch (aruco::dictionary::get())
71 case aruco::Dict::Original: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_ARUCO_ORIGINAL);
break;
72 case aruco::Dict::D4X4_50: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);
break;
73 case aruco::Dict::D4X4_100: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_100);
break;
74 case aruco::Dict::D4X4_250: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_250);
break;
75 case aruco::Dict::D4X4_1000: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_1000);
break;
76 case aruco::Dict::D5X5_50: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_5X5_50);
break;
77 case aruco::Dict::D5X5_100: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_5X5_100);
break;
78 case aruco::Dict::D5X5_250: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_5X5_250);
break;
79 case aruco::Dict::D5X5_1000: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_5X5_1000);
break;
80 case aruco::Dict::D6X6_50: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_50);
break;
81 case aruco::Dict::D6X6_100: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_100);
break;
82 case aruco::Dict::D6X6_250: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);
break;
83 case aruco::Dict::D6X6_1000: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_1000);
break;
84 case aruco::Dict::D7X7_50: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_7X7_50);
break;
85 case aruco::Dict::D7X7_100: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_7X7_100);
break;
86 case aruco::Dict::D7X7_250: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_7X7_250);
break;
87 case aruco::Dict::D7X7_1000: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_7X7_1000);
break;
88 case aruco::Dict::ATAG_16h5: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_APRILTAG_16h5);
break;
89 case aruco::Dict::ATAG_25h9: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_APRILTAG_25h9);
break;
90 case aruco::Dict::ATAG_36h10: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_APRILTAG_36h10);
break;
91 case aruco::Dict::ATAG_36h11: dico = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_APRILTAG_36h11);
break;
95 itsDetector = cv::Ptr<cv::aruco::ArucoDetector>(
new cv::aruco::ArucoDetector(dico, dparams));
114 std::string
const cpf = std::string(JEVOIS_SHARE_PATH) +
"/camera/" + aruco::camparams::get() +
117 cv::FileStorage fs(cpf, cv::FileStorage::READ);
122 LINFO(
"Loaded camera calibration from " << cpf);
126 LERROR(
"Failed to read camera parameters from file [" << cpf <<
"] -- IGNORED");
143 unsigned int w,
unsigned int h, std::vector<cv::Vec3d>
const & rvecs,
144 std::vector<cv::Vec3d>
const & tvecs)
146 if (rvecs.empty() ==
false)
148 float const siz = markerlen::get();
151 for (
size_t i = 0; i < corners.size(); ++i)
153 cv::Vec3d
const & rv = rvecs[i];
154 cv::Vec3d
const & tv = tvecs[i];
157 float theta = std::sqrt(rv[0] * rv[0] + rv[1] * rv[1] + rv[2] * rv[2]);
158 Eigen::Vector3f axis(rv[0], rv[1], rv[2]);
159 Eigen::Quaternion<float>
q(Eigen::AngleAxis<float>(theta, axis));
163 q.w(),
q.x(),
q.y(),
q.z(),
170 for (
size_t i = 0; i < corners.size(); ++i)
172 std::vector<cv::Point2f>
const & currentMarker = corners[i];
180 std::vector<std::vector<cv::Point2f> > corners, std::vector<cv::Vec3d>
const & rvecs,
181 std::vector<cv::Vec3d>
const & tvecs)
184 int nMarkers =
int(corners.size());
185 for (
int i = 0; i < nMarkers; ++i)
187 std::vector<cv::Point2f>
const & currentMarker = corners[i];
190 for (
int j = 0; j < 4; ++j)
192 cv::Point2f
const & p0 = currentMarker[j];
193 cv::Point2f
const & p1 = currentMarker[ (j+1) % 4 ];
195 int(p1.x + 0.5F),
int(p1.y + 0.5F), 1, jevois::yuyv::LightGreen);
200 3, jevois::yuyv::LightGreen);
203 if (ids.empty() ==
false)
205 cv::Point2f cent(0.0
F, 0.0
F);
for (
int p = 0; p < 4; ++p) cent += currentMarker[p] * 0.25
F;
207 int(cent.x + 0.5F),
int(cent.y + 0.5F) - 5, jevois::yuyv::LightGreen);
212 if (dopose::get() && ids.empty() ==
false)
214 float const length = markerlen::get() * 0.4F;
216 for (
size_t i = 0; i < ids.size(); ++i)
219 std::vector<cv::Point3f> axisPoints;
220 axisPoints.push_back(cv::Point3f(0.0
F, 0.0
F, 0.0
F));
221 axisPoints.push_back(cv::Point3f(length, 0.0
F, 0.0
F));
222 axisPoints.push_back(cv::Point3f(0.0
F, length, 0.0
F));
223 axisPoints.push_back(cv::Point3f(0.0
F, 0.0
F, length));
225 std::vector<cv::Point2f> imagePoints;
230 int(imagePoints[1].x + 0.5
F),
int(imagePoints[1].y + 0.5
F),
231 2, jevois::yuyv::MedPurple);
233 int(imagePoints[2].x + 0.5
F),
int(imagePoints[2].y + 0.5
F),
234 2, jevois::yuyv::MedGreen);
236 int(imagePoints[3].x + 0.5
F),
int(imagePoints[3].y + 0.5
F),
237 2, jevois::yuyv::MedGrey);
242 float const len = markerlen::get() * 0.5F;
244 std::vector<cv::Point3f> cubePoints;
245 cubePoints.push_back(cv::Point3f(-len, -len, 0.0
F));
246 cubePoints.push_back(cv::Point3f(len, -len, 0.0
F));
247 cubePoints.push_back(cv::Point3f(len, len, 0.0
F));
248 cubePoints.push_back(cv::Point3f(-len, len, 0.0
F));
249 cubePoints.push_back(cv::Point3f(-len, -len, len * 2.0
F));
250 cubePoints.push_back(cv::Point3f(len, -len, len * 2.0
F));
251 cubePoints.push_back(cv::Point3f(len, len, len * 2.0
F));
252 cubePoints.push_back(cv::Point3f(-len, len, len * 2.0
F));
254 std::vector<cv::Point2f> cuf;
258 std::vector<cv::Point> cu;
259 for (
auto const & p : cuf) cu.push_back(cv::Point(
int(p.x + 0.5F),
int(p.y + 0.5F)));
279 if (txtx >=0 && txty >= 0)
281 txtx, txty, jevois::yuyv::White);
287 std::vector<std::vector<cv::Point2f> > corners, std::vector<cv::Vec3d>
const & rvecs,
288 std::vector<cv::Vec3d>
const & tvecs)
290 ImU32
const col = ImColor(128, 255, 128, 255);
293 int nMarkers =
int(corners.size());
294 for (
int i = 0; i < nMarkers; ++i)
296 std::vector<cv::Point2f>
const & currentMarker = corners[i];
299 helper.
drawPoly(currentMarker, col,
true);
302 helper.
drawCircle(currentMarker[0].x, currentMarker[0].y, 3.0
F, col,
true);
305 if (ids.empty() ==
false)
307 cv::Point2f cent(0.0
F, 0.0
F);
for (
int p = 0; p < 4; ++p) cent += currentMarker[p] * 0.25
F;
313 if (dopose::get() && ids.empty() ==
false)
315 float const length = markerlen::get() * 0.4F;
317 for (
size_t i = 0; i < ids.size(); ++i)
320 std::vector<cv::Point3f> axisPoints;
321 axisPoints.push_back(cv::Point3f(0.0
F, 0.0
F, 0.0
F));
322 axisPoints.push_back(cv::Point3f(length, 0.0
F, 0.0
F));
323 axisPoints.push_back(cv::Point3f(0.0
F, length, 0.0
F));
324 axisPoints.push_back(cv::Point3f(0.0
F, 0.0
F, length));
326 std::vector<cv::Point2f> imagePoints;
330 helper.
drawLine(imagePoints[0].x, imagePoints[0].y, imagePoints[1].x, imagePoints[1].y, 0xff0000ff);
331 helper.
drawLine(imagePoints[0].x, imagePoints[0].y, imagePoints[2].x, imagePoints[2].y, 0xff00ff00);
332 helper.
drawLine(imagePoints[0].x, imagePoints[0].y, imagePoints[3].x, imagePoints[3].y, 0xffff0000);
337 float const len = markerlen::get() * 0.5F;
339 std::vector<cv::Point3f> cubePoints;
340 cubePoints.push_back(cv::Point3f(-len, -len, 0.0
F));
341 cubePoints.push_back(cv::Point3f(len, -len, 0.0
F));
342 cubePoints.push_back(cv::Point3f(len, len, 0.0
F));
343 cubePoints.push_back(cv::Point3f(-len, len, 0.0
F));
344 cubePoints.push_back(cv::Point3f(-len, -len, len * 2.0
F));
345 cubePoints.push_back(cv::Point3f(len, -len, len * 2.0
F));
346 cubePoints.push_back(cv::Point3f(len, len, len * 2.0
F));
347 cubePoints.push_back(cv::Point3f(-len, len, len * 2.0
F));
349 std::vector<cv::Point2f> cuf;
353 [&](
int a,
int b,
int c,
int d)
355 std::vector<cv::Point2f> p { cuf[a], cuf[b], cuf[c], cuf[d] };
360 drawface(0, 1, 2, 3);
361 drawface(0, 1, 5, 4);
362 drawface(1, 2, 6, 5);
363 drawface(2, 3, 7, 6);
364 drawface(3, 0, 4, 7);
365 drawface(4, 5, 6, 7);