WPILibC++ 2025.3.2
Loading...
Searching...
No Matches
apriltag.h
Go to the documentation of this file.
1/* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2All rights reserved.
3This software was developed in the APRIL Robotics Lab under the
4direction of Edwin Olson, ebolson@umich.edu. This software may be
5available under alternative licensing terms; contact the address above.
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
81. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23The views and conclusions contained in the software and documentation are those
24of the authors and should not be interpreted as representing official policies,
25either expressed or implied, of the Regents of The University of Michigan.
26*/
27
28#pragma once
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#include <stdlib.h>
35
36#include "common/matd.h"
37#include "common/image_u8.h"
38#include "common/zarray.h"
39#include "common/workerpool.h"
40#include "common/timeprofile.h"
42
43#define APRILTAG_TASKS_PER_THREAD_TARGET 10
44
45struct quad
46{
47 float p[4][2]; // corners
48
50
51 // H: tag coordinates ([-1,1] at the black corners) to pixels
52 // Hinv: pixels to tag
54};
55
56// Represents a tag family. Every tag belongs to a tag family. Tag
57// families are generated by the Java tool
58// april.tag.TagFamilyGenerator and can be converted to C using
59// april.tag.TagToC.
62{
63 // How many codes are there in this tag family?
64 uint32_t ncodes;
65
66 // The codes in the family.
67 uint64_t *codes;
68
72
73 // The bit locations.
74 uint32_t nbits;
75 uint32_t *bit_x;
76 uint32_t *bit_y;
77
78 // minimum hamming distance between any two codes. (e.g. 36h11 => 11)
79 uint32_t h;
80
81 // a human-readable name, e.g., "tag36h11"
82 char *name;
83
84 // some detector implementations may preprocess codes in order to
85 // accelerate decoding. They put their data here. (Do not use the
86 // same apriltag_family instance in more than one implementation)
87 void *impl;
88};
89
90
92{
93 // reject quads containing too few pixels
95
96 // how many corner candidates to consider when segmenting a group
97 // of pixels into a quad.
99
100 // Reject quads where pairs of edges have angles that are close to
101 // straight or close to 180 degrees. Zero means that no quads are
102 // rejected. (In radians).
105
106 // When fitting lines to the contours, what is the maximum mean
107 // squared error allowed? This is useful in rejecting contours
108 // that are far from being quad shaped; rejecting these quads "early"
109 // saves expensive decoding processing.
111
112 // When we build our model of black & white pixels, we add an
113 // extra check that the white model must be (overall) brighter
114 // than the black model. How much brighter? (in pixel values,
115 // [0,255]). .
117
118 // should the thresholded image be deglitched? Only useful for
119 // very noisy images
121};
122
123// Represents a detector object. Upon creating a detector, all fields
124// are set to reasonable values, but can be overridden by accessing
125// these fields.
128{
129 ///////////////////////////////////////////////////////////////
130 // User-configurable parameters.
131
132 // How many threads should be used?
134
135 // detection of quads can be done on a lower-resolution image,
136 // improving speed at a cost of pose accuracy and a slight
137 // decrease in detection rate. Decoding the binary payload is
138 // still done at full resolution. .
140
141 // What Gaussian blur should be applied to the segmented image
142 // (used for quad detection?) Parameter is the standard deviation
143 // in pixels. Very noisy images benefit from non-zero values
144 // (e.g. 0.8).
146
147 // When true, the edges of the each quad are adjusted to "snap
148 // to" strong gradients nearby. This is useful when decimation is
149 // employed, as it can increase the quality of the initial quad
150 // estimate substantially. Generally recommended to be on (true).
151 //
152 // Very computationally inexpensive. Option is ignored if
153 // quad_decimate = 1.
155
156 // How much sharpening should be done to decoded images? This
157 // can help decode small tags but may or may not help in odd
158 // lighting conditions or low light conditions.
159 //
160 // The default value is 0.25.
162
163 // When true, write a variety of debugging images to the
164 // current working directory at various stages through the
165 // detection process. (Somewhat slow).
166 bool debug;
167
169
170 ///////////////////////////////////////////////////////////////
171 // Statistics relating to last processed frame
173
174 uint32_t nedges;
175 uint32_t nsegments;
176 uint32_t nquads;
177
178 ///////////////////////////////////////////////////////////////
179 // Internal variables below
180
181 // Not freed on apriltag_destroy; a tag family can be shared
182 // between multiple users. The user should ultimately destroy the
183 // tag family passed into the constructor.
185
186 // Used to manage multi-threading.
188
189 // Used for thread safety.
190 pthread_mutex_t mutex;
191};
192
193// Represents the detection of a tag. These are returned to the user
194// and must be individually destroyed by the user.
197{
198 // a pointer for convenience. not freed by apriltag_detection_destroy.
200
201 // The decoded ID of the tag
202 int id;
203
204 // How many error bits were corrected? Note: accepting large numbers of
205 // corrected errors leads to greatly increased false positive rates.
206 // NOTE: As of this implementation, the detector cannot detect tags with
207 // a hamming distance greater than 2.
209
210 // A measure of the quality of the binary decoding process: the
211 // average difference between the intensity of a data bit versus
212 // the decision threshold. Higher numbers roughly indicate better
213 // decodes. This is a reasonable measure of detection accuracy
214 // only for very small tags-- not effective for larger tags (where
215 // we could have sampled anywhere within a bit cell and still
216 // gotten a good detection.)
218
219 // The 3x3 homography matrix describing the projection from an
220 // "ideal" tag (with corners at (-1,1), (1,1), (1,-1), and (-1,
221 // -1)) to pixels in the image. This matrix will be freed by
222 // apriltag_detection_destroy.
224
225 // The center of the detection in image pixel coordinates.
226 double c[2];
227
228 // The corners of the tag in image pixel coordinates. These always
229 // wrap counter-clock wise around the tag.
230 double p[4][2];
231};
232
233// don't forget to add a family!
235
236// add a family to the apriltag detector. caller still "owns" the family.
237// a single instance should only be provided to one apriltag detector instance.
239
240// Tunable, but really, 2 is a good choice. Values of >=3
241// consume prohibitively large amounts of memory, and otherwise
242// you want the largest value possible.
247
248// does not deallocate the family.
250
251// unregister all families, but does not deallocate the underlying tag family objects.
253
254// Destroy the april tag detector (but not the underlying
255// apriltag_family_t used to initialize it.)
257
258// Detect tags from an image and return an array of
259// apriltag_detection_t*. You can use apriltag_detections_destroy to
260// free the array and the detections it contains, or call
261// _detection_destroy and zarray_destroy yourself.
263
264// Call this method on each of the tags returned by apriltag_detector_detect
266
267// destroys the array AND the detections within it.
269
270// Renders the apriltag.
271// Caller is responsible for calling image_u8_destroy on the image
273
274#ifdef __cplusplus
275}
276#endif
Definition apriltag.h:197
double p[4][2]
Definition apriltag.h:230
int id
Definition apriltag.h:202
float decision_margin
Definition apriltag.h:217
int hamming
Definition apriltag.h:208
double c[2]
Definition apriltag.h:226
matd_t * H
Definition apriltag.h:223
apriltag_family_t * family
Definition apriltag.h:199
Definition apriltag.h:128
uint32_t nedges
Definition apriltag.h:174
workerpool_t * wp
Definition apriltag.h:187
float quad_decimate
Definition apriltag.h:139
zarray_t * tag_families
Definition apriltag.h:184
struct apriltag_quad_thresh_params qtp
Definition apriltag.h:168
uint32_t nsegments
Definition apriltag.h:175
bool refine_edges
Definition apriltag.h:154
uint32_t nquads
Definition apriltag.h:176
bool debug
Definition apriltag.h:166
int nthreads
Definition apriltag.h:133
timeprofile_t * tp
Definition apriltag.h:172
float quad_sigma
Definition apriltag.h:145
double decode_sharpening
Definition apriltag.h:161
pthread_mutex_t mutex
Definition apriltag.h:190
Definition apriltag.h:62
int total_width
Definition apriltag.h:70
uint32_t ncodes
Definition apriltag.h:64
uint32_t * bit_x
Definition apriltag.h:75
uint32_t h
Definition apriltag.h:79
char * name
Definition apriltag.h:82
uint32_t * bit_y
Definition apriltag.h:76
uint32_t nbits
Definition apriltag.h:74
void * impl
Definition apriltag.h:87
int width_at_border
Definition apriltag.h:69
bool reversed_border
Definition apriltag.h:71
uint64_t * codes
Definition apriltag.h:67
Definition apriltag.h:92
float critical_rad
Definition apriltag.h:103
int max_nmaxima
Definition apriltag.h:98
int min_white_black_diff
Definition apriltag.h:116
float cos_critical_rad
Definition apriltag.h:104
int deglitch
Definition apriltag.h:120
float max_line_fit_mse
Definition apriltag.h:110
int min_cluster_pixels
Definition apriltag.h:94
Definition image_types.h:38
Defines a matrix structure for holding double-precision values with data in row-major order (i....
Definition matd.h:46
Definition apriltag.h:46
matd_t * Hinv
Definition apriltag.h:53
bool reversed_border
Definition apriltag.h:49
float p[4][2]
Definition apriltag.h:47
matd_t * H
Definition apriltag.h:53
Definition timeprofile.h:49
Definition zarray.h:44
void apriltag_detector_add_family_bits(apriltag_detector_t *td, apriltag_family_t *fam, int bits_corrected)
zarray_t * apriltag_detector_detect(apriltag_detector_t *td, image_u8_t *im_orig)
void apriltag_detector_destroy(apriltag_detector_t *td)
void apriltag_detection_destroy(apriltag_detection_t *det)
void apriltag_detector_clear_families(apriltag_detector_t *td)
apriltag_detector_t * apriltag_detector_create(void)
void apriltag_detections_destroy(zarray_t *detections)
image_u8_t * apriltag_to_image(apriltag_family_t *fam, uint32_t idx)
static void apriltag_detector_add_family(apriltag_detector_t *td, apriltag_family_t *fam)
Definition apriltag.h:243
void apriltag_detector_remove_family(apriltag_detector_t *td, apriltag_family_t *fam)
struct workerpool workerpool_t
Definition workerpool.h:32