VisionServer  v2.1.1-1-g21dc5465
FRC vision library
aprilpose.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <string>
5
6#include <opencv2/objdetect.hpp>
7
8#include "visionserver2.h"
9#include "vision.h"
10
11
12constexpr static inline cv::aruco::PredefinedDictionaryType
13 FRC_DICT_2023 = cv::aruco::DICT_APRILTAG_16h5,
14 FRC_DICT = cv::aruco::DICT_APRILTAG_36h11;
15constexpr static inline char const
16 * FRC_DICT_NAME_2023 = "tag16h5",
17 * FRC_DICT_NAME = "tag36h11";
18
19// TODO: need to update to new OpenCV objdetect aruco impl
20// template<class derived_t = void>
21// class AprilPose_ : public vs2::VPipeline<AprilPose_<derived_t> > {
22// using This_t = AprilPose_<derived_t>;
23// public:
24// inline AprilPose_(
25// const cv::Ptr<cv::aruco::Board> f,
26// cv::Ptr<cv::aruco::DetectorParameters> p = cv::aruco::DetectorParameters::create()
27// ) : vs2::VPipeline<This_t>("AprilTag Pose Estimator"), params{p}, markers{f->dictionary}, field{f}
28// {
29// this->getTable()->PutNumber("Scaling", this->scale);
30// this->getTable()->PutNumber("Decimate", this->params->aprilTagQuadDecimate);
31// }
32
33// inline virtual void process(cv::Mat& io_frame) override {
34// this->_detect(io_frame);
35// this->_estimate(io_frame);
36// this->_draw(io_frame);
37// #ifdef APRILPOSE_DEBUG
38// this->_profile(io_frame);
39// #endif
40// }
41
42// protected:
43// inline AprilPose_(
44// const char* n, const cv::Ptr<cv::aruco::Board> f,
45// cv::Ptr<cv::aruco::DetectorParameters> p = cv::aruco::DetectorParameters::create()
46// ) : vs2::VPipeline<This_t>(n), params{p}, markers{f->dictionary}, field{f} {
47// this->getTable()->PutNumber("Scaling", this->scale);
48// this->getTable()->PutNumber("Decimate", this->params->aprilTagQuadDecimate);
49// }
50// inline AprilPose_(
51// const std::string& n, const cv::Ptr<cv::aruco::Board> f,
52// cv::Ptr<cv::aruco::DetectorParameters> p = cv::aruco::DetectorParameters::create()
53// ) : vs2::VPipeline<This_t>(n), params{p}, markers{f->dictionary}, field{f} {
54// this->getTable()->PutNumber("Scaling", this->scale);
55// this->getTable()->PutNumber("Decimate", this->params->aprilTagQuadDecimate);
56// }
57// inline AprilPose_(
58// std::string&& n, const cv::Ptr<cv::aruco::Board> f,
59// cv::Ptr<cv::aruco::DetectorParameters> p = cv::aruco::DetectorParameters::create()
60// ) : vs2::VPipeline<This_t>(n), params{p}, markers{f->dictionary}, field{f} {
61// this->getTable()->PutNumber("Scaling", this->scale);
62// this->getTable()->PutNumber("Decimate", this->params->aprilTagQuadDecimate);
63// }
64
65// void _detect(cv::Mat& io_frame);
66// void _estimate(cv::Mat& io_frame);
67// void _draw(cv::Mat& io_frame);
68
69// cv::Ptr<cv::aruco::DetectorParameters> params;
70// cv::Ptr<cv::aruco::Dictionary> markers;
71// cv::Ptr<cv::aruco::Board> field;
72
73// std::vector<std::vector<cv::Point2f> > corners;
74// std::vector<int32_t> ids;
75// std::array<float, 3> tvec{0.f}, rvec{0.f};
76
77// cv::Mat buffer;
78// size_t scale{1};
79
80// #ifdef APRILPOSE_DEBUG
81// void _profile(cv::Mat& io_frame);
82
83// std::array<float, 6> profiling{0.f};
84// #endif
85
86
87// };
88// typedef AprilPose_<> AprilPose;
89
90
91
92
93
94// #ifdef APRILPOSE_DEBUG
95// #include <chrono>
96// using hrc = std::chrono::high_resolution_clock;
97// #endif
98
99// template<class derived>
100// void AprilPose_<derived>::_detect(cv::Mat&io_frame) {
101// #ifdef APRILPOSE_DEBUG
102// hrc::time_point beg, end;
103// beg = hrc::now();
104// #endif
105// this->scale = this->getTable()->GetNumber("Scaling", 1.0);
106// this->params->aprilTagQuadDecimate = this->getTable()->GetNumber("Decimate", this->params->aprilTagQuadDecimate);
107// this->corners.clear();
108// this->ids.clear();
109// cv::Size2i fsz = io_frame.size() / this->scale;
110// if(this->buffer.size() != fsz) {
111// this->buffer = cv::Mat(fsz, CV_8UC3);
112// }
113// #ifdef APRILPOSE_DEBUG
114// end = hrc::now();
115// this->profiling[0] = (end - beg).count() / 1e6;
116// beg = hrc::now();
117// #endif
118// cv::resize(io_frame, this->buffer, fsz);
119// #ifdef APRILPOSE_DEBUG
120// end = HRC::now();
121// this->profiling[1] = (end - beg).count() / 1e6;
122// beg = HRC::now();
123// #endif
124// cv::aruco::detectMarkers(
125// this->buffer, this->markers,
126// this->corners, this->ids,
127// this->params
128// );
129// #ifdef APRILPOSE_DEBUG
130// end = HRC::now();
131// this->profiling[2] = (end - beg).count() / 1e6;
132// beg = HRC::now();
133// #endif
134// if(this->scale != 1.0) {
135// rescale(this->corners, this->scale);
136// }
137// #ifdef APRILPOSE_DEBUG
138// end = HRC::now();
139// this->profiling[3] = (end - beg).count() / 1e6;
140// #endif
141// }
142
143// template<class derived>
144// void AprilPose_<derived>::_estimate(cv::Mat& io_frame) {
145// #ifdef APRILPOSE_DEBUG
146// hrc::time_point beg, end;
147// beg = hrc::now();
148// #endif
149// cv::aruco::estimatePoseBoard(
150// this->corners, this->ids, this->field,
151// this->getSrcMatrix(), this->getSrcDistort(),
152// this->rvec, this->tvec);
153// #ifdef APRILPOSE_DEBUG
154// end = HRC::now();
155// this->profiling[4] = (end - beg).count() / 1e6;
156// #endif
157// }
158
159// template<class derived>
160// void AprilPose_<derived>::_draw(cv::Mat& io_frame) {
161// #ifdef APRILPOSE_DEBUG
162// hrc::time_point beg, end;
163// beg = hrc::now();
164// #endif
165// //cv::drawFrameAxes(io_frame, this->getSrcMatrix(), this->getSrcDistort(), this->rvec, this->tvec, 100.f);
166// cv::aruco::drawDetectedMarkers(io_frame, this->corners, this->ids);
167// #ifdef APRILPOSE_DEBUG
168// end = HRC::now();
169// this->profiling[5] = (end - beg).count() / 1e6;
170// #endif
171// }
172
173// #ifdef APRILPOSE_DEBUG
174// template<class derived>
175// void AprilPose_<derived>::_profile(cv::Mat& io_frame) {
176// cv::putText(
177// io_frame, "P_init(ms): " + std::to_string(this->profiling[0]),
178// {5, 240}, cv::FONT_HERSHEY_DUPLEX, 0.5, {255, 100, 0}
179// );
180// cv::putText(
181// io_frame, "P_resize(ms): " + std::to_string(this->profiling[1]),
182// {5, 260}, cv::FONT_HERSHEY_DUPLEX, 0.5, {255, 100, 0}
183// );
184// cv::putText(
185// io_frame, "P_detect(ms): " + std::to_string(this->profiling[2]),
186// {5, 280}, cv::FONT_HERSHEY_DUPLEX, 0.5, {255, 100, 0}
187// );
188// cv::putText(
189// io_frame, "P_rescale(ms): " + std::to_string(this->profiling[3]),
190// {5, 300}, cv::FONT_HERSHEY_DUPLEX, 0.5, {255, 100, 0}
191// );
192// cv::putText(
193// io_frame, "P_estimate(ms): " + std::to_string(this->profiling[4]),
194// {5, 320}, cv::FONT_HERSHEY_DUPLEX, 0.5, {255, 100, 0}
195// );
196// cv::putText(
197// io_frame, "P_draw(ms): " + std::to_string(this->profiling[5]),
198// {5, 340}, cv::FONT_HERSHEY_DUPLEX, 0.5, {255, 100, 0}
199// );
200// }
201// #endif
constexpr static cv::aruco::PredefinedDictionaryType FRC_DICT
Definition: aprilpose.h:14
constexpr static char const * FRC_DICT_NAME
Definition: aprilpose.h:17
constexpr static char const * FRC_DICT_NAME_2023
Definition: aprilpose.h:16
constexpr static cv::aruco::PredefinedDictionaryType FRC_DICT_2023
Definition: aprilpose.h:13