WPILibC++ 2024.3.2
AprilTagDetection.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <span>
10#include <string_view>
11
12#include <Eigen/Core>
13#include <wpi/SymbolExports.h>
14
15namespace frc {
16
17/**
18 * A detection of an AprilTag tag.
19 */
21 public:
25
26 /** A point. Used for center and corner points. */
27 struct Point {
28 double x;
29 double y;
30 };
31
32 /**
33 * Gets the decoded tag's family name.
34 *
35 * @return Decoded family name
36 */
38
39 /**
40 * Gets the decoded ID of the tag.
41 *
42 * @return Decoded ID
43 */
44 int GetId() const { return id; }
45
46 /**
47 * Gets how many error bits were corrected. Note: accepting large numbers of
48 * corrected errors leads to greatly increased false positive rates.
49 * NOTE: As of this implementation, the detector cannot detect tags with
50 * a hamming distance greater than 2.
51 *
52 * @return Hamming distance (number of corrected error bits)
53 */
54 int GetHamming() const { return hamming; }
55
56 /**
57 * Gets a measure of the quality of the binary decoding process: the
58 * average difference between the intensity of a data bit versus
59 * the decision threshold. Higher numbers roughly indicate better
60 * decodes. This is a reasonable measure of detection accuracy
61 * only for very small tags-- not effective for larger tags (where
62 * we could have sampled anywhere within a bit cell and still
63 * gotten a good detection.)
64 *
65 * @return Decision margin
66 */
67 float GetDecisionMargin() const { return decision_margin; }
68
69 /**
70 * Gets the 3x3 homography matrix describing the projection from an
71 * "ideal" tag (with corners at (-1,1), (1,1), (1,-1), and (-1,
72 * -1)) to pixels in the image.
73 *
74 * @return Homography matrix data
75 */
76 std::span<const double, 9> GetHomography() const;
77
78 /**
79 * Gets the 3x3 homography matrix describing the projection from an
80 * "ideal" tag (with corners at (-1,1), (1,1), (1,-1), and (-1,
81 * -1)) to pixels in the image.
82 *
83 * @return Homography matrix
84 */
85 Eigen::Matrix3d GetHomographyMatrix() const;
86
87 /**
88 * Gets the center of the detection in image pixel coordinates.
89 *
90 * @return Center point
91 */
92 const Point& GetCenter() const { return *reinterpret_cast<const Point*>(c); }
93
94 /**
95 * Gets a corner of the tag in image pixel coordinates. These always
96 * wrap counter-clock wise around the tag. Index 0 is the bottom left corner.
97 *
98 * @param ndx Corner index (range is 0-3, inclusive)
99 * @return Corner point
100 */
101 const Point& GetCorner(int ndx) const {
102 return *reinterpret_cast<const Point*>(p[ndx]);
103 }
104
105 /**
106 * Gets the corners of the tag in image pixel coordinates. These always
107 * wrap counter-clock wise around the tag. The first set of corner coordinates
108 * are the coordinates for the bottom left corner.
109 *
110 * @param cornersBuf Corner point array (X and Y for each corner in order)
111 * @return Corner point array (copy of cornersBuf span)
112 */
113 std::span<double, 8> GetCorners(std::span<double, 8> cornersBuf) const {
114 for (int i = 0; i < 4; i++) {
115 cornersBuf[i * 2] = p[i][0];
116 cornersBuf[i * 2 + 1] = p[i][1];
117 }
118 return cornersBuf;
119 }
120
121 private:
122 // This class *must* be standard-layout-compatible with apriltag_detection
123 // as we use reinterpret_cast from that structure. This means the below
124 // members must exactly match the contents of the apriltag_detection struct.
125
126 // The tag family.
127 void* family;
128
129 // The decoded ID of the tag.
130 int id;
131
132 // How many error bits were corrected? Note: accepting large numbers of
133 // corrected errors leads to greatly increased false positive rates.
134 // NOTE: As of this implementation, the detector cannot detect tags with
135 // a hamming distance greater than 2.
136 int hamming;
137
138 // A measure of the quality of the binary decoding process: the
139 // average difference between the intensity of a data bit versus
140 // the decision threshold. Higher numbers roughly indicate better
141 // decodes. This is a reasonable measure of detection accuracy
142 // only for very small tags-- not effective for larger tags (where
143 // we could have sampled anywhere within a bit cell and still
144 // gotten a good detection.)
145 float decision_margin;
146
147 // The 3x3 homography matrix describing the projection from an
148 // "ideal" tag (with corners at (-1,1), (1,1), (1,-1), and (-1,
149 // -1)) to pixels in the image.
150 void* H;
151
152 // The center of the detection in image pixel coordinates.
153 double c[2];
154
155 // The corners of the tag in image pixel coordinates. These always
156 // wrap counter-clock wise around the tag.
157 double p[4][2];
158};
159
160} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
A detection of an AprilTag tag.
Definition: AprilTagDetection.h:20
std::span< const double, 9 > GetHomography() const
Gets the 3x3 homography matrix describing the projection from an "ideal" tag (with corners at (-1,...
const Point & GetCenter() const
Gets the center of the detection in image pixel coordinates.
Definition: AprilTagDetection.h:92
AprilTagDetection & operator=(const AprilTagDetection &)=delete
const Point & GetCorner(int ndx) const
Gets a corner of the tag in image pixel coordinates.
Definition: AprilTagDetection.h:101
int GetHamming() const
Gets how many error bits were corrected.
Definition: AprilTagDetection.h:54
Eigen::Matrix3d GetHomographyMatrix() const
Gets the 3x3 homography matrix describing the projection from an "ideal" tag (with corners at (-1,...
std::string_view GetFamily() const
Gets the decoded tag's family name.
std::span< double, 8 > GetCorners(std::span< double, 8 > cornersBuf) const
Gets the corners of the tag in image pixel coordinates.
Definition: AprilTagDetection.h:113
int GetId() const
Gets the decoded ID of the tag.
Definition: AprilTagDetection.h:44
AprilTagDetection(const AprilTagDetection &)=delete
float GetDecisionMargin() const
Gets a measure of the quality of the binary decoding process: the average difference between the inte...
Definition: AprilTagDetection.h:67
basic_string_view< char > string_view
Definition: core.h:501
Definition: AprilTagPoseEstimator.h:15
static constexpr const velocity::meters_per_second_t c(299792458.0)
Speed of light in vacuum.
A point.
Definition: AprilTagDetection.h:27
double y
Definition: AprilTagDetection.h:29
double x
Definition: AprilTagDetection.h:28