test_detection.c
Go to the documentation of this file.
1 #include <stdarg.h>
2 #include <apriltag.h>
3 #include <tag36h11.h>
4 #include <common/pjpeg.h>
5 #include <math.h>
6 
7 #include "getline.h"
8 
9 
10 char*
11 format(const char* fmt, ...)
12 {
13  va_list args1;
14  va_list args2;
15 
16  va_start(args1, fmt);
17  va_copy(args2, args1);
18 
19  const int N = vsnprintf(NULL, 0, fmt, args1);
20  va_end(args1);
21 
22  if (N < 0) {
23  va_end(args2);
24  return NULL;
25  }
26 
27  char* res_fmt = calloc(N+1, sizeof(char));
28  const int n = vsnprintf(res_fmt, N+1, fmt, args2);
29  va_end(args2);
30 
31  if (n != N) {
32  free(res_fmt);
33  return NULL;
34  }
35 
36  // caller has to free returned string
37  return res_fmt;
38 }
39 
40 int
42 {
43  if (a->id != b->id) {
44  return copysign(1, a->id - b->id);
45  }
46 
47  for (int e = 0; e<4; e++) {
48  for (int c = 0; c<2; c++) {
49  const double d = a->p[e][c] - b->p[e][c];
50  if (fabs(d) > 1e-1) {
51  return copysign(1, d);
52  }
53  }
54  }
55 
56  return 0;
57 }
58 
59 int
60 detection_array_element_compare_function(const void *_a, const void *_b)
61 {
62  const apriltag_detection_t * const a = *(apriltag_detection_t**) _a;
63  const apriltag_detection_t * const b = *(apriltag_detection_t**) _b;
64 
65  return detection_compare_function(a, b);
66 }
67 
68 int
69 main(int argc, char *argv[])
70 {
71  if (argc!=2) {
72  return EXIT_FAILURE;
73  }
74 
75  // load image
76  char* const path_img = format("%s.jpg", argv[1]);
77  pjpeg_t *pjpeg = pjpeg_create_from_file(path_img, 0, NULL);
79  free(path_img);
80 
81  // load true detection
82  char* const path_det_true = format("%s.txt", argv[1]);
83  FILE *fp = fopen(path_det_true, "r");
84  if (fp == NULL) {
85  return EXIT_FAILURE;
86  }
87  free(path_det_true);
88 
90  td->quad_decimate = 1;
91  td->refine_edges = false;
94 
95  const char fmt_det[] = "%i, (%.4lf %.4lf), (%.4lf %.4lf), (%.4lf %.4lf), (%.4lf %.4lf)";
96  const char fmt_ref_parse[] = "%i, (%lf %lf), (%lf %lf), (%lf %lf), (%lf %lf)";
97 
98  bool ok = true;
99 
100  zarray_t *detections = apriltag_detector_detect(td, im);
101 
102  // sort detections by detected corners for deterministic sorting order
104 
105  int i = 0;
106  for (; i < zarray_size(detections); i++) {
108  zarray_get(detections, i, &det);
109 
110  char* const det_fmt = format(fmt_det,
111  det->id,
112  det->p[0][0], det->p[0][1], det->p[1][0], det->p[1][1],
113  det->p[2][0], det->p[2][1], det->p[3][0], det->p[3][1]);
114 
115  char* line = NULL;
116  size_t len = 0;
117  const ssize_t nread = apriltag_test_getline(&line, &len, fp);
118  if (nread == -1) {
119  free(line);
120  return EXIT_FAILURE;
121  }
122 
123  printf("Got: %s\n", det_fmt);
124  printf("Expected: %s\n", line);
125 
126  // parse reference detection
128  const int nparsed = sscanf(
129  line, fmt_ref_parse,
130  &ref.id,
131  &ref.p[0][0], &ref.p[0][1], &ref.p[1][0], &ref.p[1][1],
132  &ref.p[2][0], &ref.p[2][1], &ref.p[3][0], &ref.p[3][1]);
133 
134  (void) nparsed;
135  assert(nparsed == 9);
136 
137  // compare detections
138  const bool equ = detection_compare_function(det, &ref) == 0;
139 
140  if (!equ || det->id != ref.id) {
141  fprintf(stderr, "Mismatch.\nGot:\n %s\nExpected:\n %s\n", det_fmt, line);
142  ok = false;
143  }
144 
145  free(det_fmt);
146  free(line);
147  }
148 
149  // check that we compared the expected amount of detections
150  // if there are no "true" detections left, we should be at the end of the file
151  // if there are no "detected" detections left, we should be at the end of the array
152  if ((fgetc(fp) != -1) && (i != zarray_size(detections))) {
153  return EXIT_FAILURE;
154  }
155 
156  fclose(fp);
157 
158  apriltag_detections_destroy(detections);
159  image_u8_destroy(im);
161 
163  tag36h11_destroy(tf);
164 
165  if (!ok) {
166  return EXIT_FAILURE;
167  }
168 
169  return EXIT_SUCCESS;
170 }
image_u8_destroy
void image_u8_destroy(image_u8_t *im)
Definition: image_u8.c:82
tag36h11_destroy
void tag36h11_destroy(apriltag_family_t *tf)
Definition: tag36h11.c:708
apriltag_detector_detect
zarray_t * apriltag_detector_detect(apriltag_detector_t *td, image_u8_t *im_orig)
Definition: apriltag.c:1029
format
char * format(const char *fmt,...)
Definition: test_detection.c:11
zarray
Definition: zarray.h:43
pjpeg_to_u8_baseline
image_u8_t * pjpeg_to_u8_baseline(pjpeg_t *pj)
Definition: pjpeg.c:703
zarray_size
static int zarray_size(const zarray_t *za)
Definition: zarray.h:130
apriltag.h
main
int main(int argc, char *argv[])
Definition: test_detection.c:69
apriltag_detector_create
apriltag_detector_t * apriltag_detector_create()
Definition: apriltag.c:354
image_u8
Definition: image_types.h:37
pjpeg.h
apriltag_detections_destroy
void apriltag_detections_destroy(zarray_t *detections)
Definition: apriltag.c:1436
apriltag_detector
Definition: apriltag.h:127
getline.h
apriltag_family
Definition: apriltag.h:61
apriltag_detector::quad_decimate
float quad_decimate
Definition: apriltag.h:139
apriltag_detection::p
double p[4][2]
Definition: apriltag.h:230
tag36h11.h
apriltag_detection::id
int id
Definition: apriltag.h:202
apriltag_detector_destroy
void apriltag_detector_destroy(apriltag_detector_t *td)
Definition: apriltag.c:388
detection_compare_function
int detection_compare_function(const apriltag_detection_t *a, const apriltag_detection_t *b)
Definition: test_detection.c:41
apriltag_detector_add_family
static void apriltag_detector_add_family(apriltag_detector_t *td, apriltag_family_t *fam)
Definition: apriltag.h:243
pjpeg_destroy
void pjpeg_destroy(pjpeg_t *pj)
Definition: pjpeg.c:689
zarray_get
static void zarray_get(const zarray_t *za, int idx, void *p)
Definition: zarray.h:195
zarray_sort
static void zarray_sort(zarray_t *za, int(*compar)(const void *, const void *))
Definition: zarray.h:406
apriltag_detection
Definition: apriltag.h:196
apriltag_detector::refine_edges
bool refine_edges
Definition: apriltag.h:154
apriltag_test_getline
ssize_t apriltag_test_getline(char **lineptr, size_t *n, FILE *stream)
Definition: getline.c:9
detection_array_element_compare_function
int detection_array_element_compare_function(const void *_a, const void *_b)
Definition: test_detection.c:60
pjpeg
Definition: pjpeg.h:64
tag36h11_create
apriltag_family_t * tag36h11_create()
Definition: tag36h11.c:620
ssize_t
intptr_t ssize_t
Definition: getline.h:7
pjpeg_create_from_file
pjpeg_t * pjpeg_create_from_file(const char *path, uint32_t flags, int *error)
Definition: pjpeg.c:825


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