apriltag.h
Go to the documentation of this file.
1 /* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2 All rights reserved.
3 This software was developed in the APRIL Robotics Lab under the
4 direction of Edwin Olson, ebolson@umich.edu. This software may be
5 available under alternative licensing terms; contact the address above.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. 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.
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 ON 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
22 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 The views and conclusions contained in the software and documentation are those
24 of the authors and should not be interpreted as representing official policies,
25 either expressed or implied, of the Regents of The University of Michigan.
26 */
27 
28 #pragma once
29 
30 #ifdef __cplusplus
31 extern "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"
41 #include "common/pthreads_cross.h"
42 
43 #define APRILTAG_TASKS_PER_THREAD_TARGET 10
44 
45 struct 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
120  int deglitch;
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 {
130  // User-configurable parameters.
131 
132  // How many threads should be used?
133  int nthreads;
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).
145  float quad_sigma;
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 
171  // Statistics relating to last processed frame
173 
174  uint32_t nedges;
175  uint32_t nsegments;
176  uint32_t nquads;
177 
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.
208  int hamming;
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.
244 {
246 }
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.
268 void apriltag_detections_destroy(zarray_t *detections);
269 
270 // Renders the apriltag.
271 // Caller is responsible for calling image_u8_destroy on the image
272 image_u8_t *apriltag_to_image(apriltag_family_t *fam, uint32_t idx);
273 
274 #ifdef __cplusplus
275 }
276 #endif
apriltag_detector_create
apriltag_detector_t * apriltag_detector_create()
Definition: apriltag.c:354
matd_t
Definition: matd.h:45
apriltag_detector::nthreads
int nthreads
Definition: apriltag.h:133
apriltag_quad_thresh_params
Definition: apriltag.h:91
apriltag_detector::nquads
uint32_t nquads
Definition: apriltag.h:176
apriltag_quad_thresh_params::max_line_fit_mse
float max_line_fit_mse
Definition: apriltag.h:110
apriltag_family::ncodes
uint32_t ncodes
Definition: apriltag.h:64
apriltag_detector::nsegments
uint32_t nsegments
Definition: apriltag.h:175
apriltag_detection::decision_margin
float decision_margin
Definition: apriltag.h:217
apriltag_to_image
image_u8_t * apriltag_to_image(apriltag_family_t *fam, uint32_t idx)
Definition: apriltag.c:1448
quad
Definition: apriltag.h:45
apriltag_quad_thresh_params::deglitch
int deglitch
Definition: apriltag.h:120
apriltag_family::impl
void * impl
Definition: apriltag.h:87
quad::H
matd_t * H
Definition: apriltag.h:53
apriltag_detection::H
matd_t * H
Definition: apriltag.h:223
apriltag_quad_thresh_params::max_nmaxima
int max_nmaxima
Definition: apriltag.h:98
apriltag_family::reversed_border
bool reversed_border
Definition: apriltag.h:71
apriltag_detector_detect
zarray_t * apriltag_detector_detect(apriltag_detector_t *td, image_u8_t *im_orig)
Definition: apriltag.c:1029
zarray
Definition: zarray.h:43
apriltag_detections_destroy
void apriltag_detections_destroy(zarray_t *detections)
Definition: apriltag.c:1436
matd.h
apriltag_detector::tp
timeprofile_t * tp
Definition: apriltag.h:172
apriltag_detector::tag_families
zarray_t * tag_families
Definition: apriltag.h:184
apriltag_detection::hamming
int hamming
Definition: apriltag.h:208
quad::Hinv
matd_t * Hinv
Definition: apriltag.h:53
apriltag_detector::decode_sharpening
double decode_sharpening
Definition: apriltag.h:161
apriltag_family::codes
uint64_t * codes
Definition: apriltag.h:67
quad::p
float p[4][2]
Definition: apriltag.h:47
image_u8
Definition: image_types.h:37
apriltag_quad_thresh_params::min_white_black_diff
int min_white_black_diff
Definition: apriltag.h:116
timeprofile
Definition: timeprofile.h:48
apriltag_detector::debug
bool debug
Definition: apriltag.h:166
apriltag_detector
Definition: apriltag.h:127
apriltag_detection::family
apriltag_family_t * family
Definition: apriltag.h:199
apriltag_family
Definition: apriltag.h:61
timeprofile.h
apriltag_detection_destroy
void apriltag_detection_destroy(apriltag_detection_t *det)
Definition: apriltag.c:1006
apriltag_detector::quad_decimate
float quad_decimate
Definition: apriltag.h:139
apriltag_detection::p
double p[4][2]
Definition: apriltag.h:230
apriltag_detector_add_family_bits
void apriltag_detector_add_family_bits(apriltag_detector_t *td, apriltag_family_t *fam, int bits_corrected)
Definition: apriltag.c:336
apriltag_detector_destroy
void apriltag_detector_destroy(apriltag_detector_t *td)
Definition: apriltag.c:388
apriltag_detection::id
int id
Definition: apriltag.h:202
apriltag_quad_thresh_params::critical_rad
float critical_rad
Definition: apriltag.h:103
apriltag_detector::qtp
struct apriltag_quad_thresh_params qtp
Definition: apriltag.h:168
apriltag_family::h
uint32_t h
Definition: apriltag.h:79
apriltag_family::name
char * name
Definition: apriltag.h:82
apriltag_detector_add_family
static void apriltag_detector_add_family(apriltag_detector_t *td, apriltag_family_t *fam)
Definition: apriltag.h:243
apriltag_quad_thresh_params::min_cluster_pixels
int min_cluster_pixels
Definition: apriltag.h:94
apriltag_family::nbits
uint32_t nbits
Definition: apriltag.h:74
apriltag_detector_remove_family
void apriltag_detector_remove_family(apriltag_detector_t *td, apriltag_family_t *fam)
Definition: apriltag.c:330
apriltag_detector::mutex
pthread_mutex_t mutex
Definition: apriltag.h:190
apriltag_detector::wp
workerpool_t * wp
Definition: apriltag.h:187
workerpool
Definition: workerpool.c:45
pthreads_cross.h
image_u8.h
apriltag_detection
Definition: apriltag.h:196
apriltag_detector::nedges
uint32_t nedges
Definition: apriltag.h:174
apriltag_detector_clear_families
void apriltag_detector_clear_families(apriltag_detector_t *td)
Definition: apriltag.c:344
apriltag_detector::refine_edges
bool refine_edges
Definition: apriltag.h:154
workerpool.h
zarray.h
apriltag_family::bit_x
uint32_t * bit_x
Definition: apriltag.h:75
apriltag_family::bit_y
uint32_t * bit_y
Definition: apriltag.h:76
apriltag_quad_thresh_params::cos_critical_rad
float cos_critical_rad
Definition: apriltag.h:104
apriltag_family::width_at_border
int width_at_border
Definition: apriltag.h:69
quad::reversed_border
bool reversed_border
Definition: apriltag.h:49
apriltag_detection::c
double c[2]
Definition: apriltag.h:226
apriltag_detector::quad_sigma
float quad_sigma
Definition: apriltag.h:145
apriltag_family::total_width
int total_width
Definition: apriltag.h:70


apriltag
Author(s): Edwin Olson , Max Krogius
autogenerated on Sun Apr 20 2025 02:08:19