File: simple_demo.c

package info (click to toggle)
apriltag 3.4.5-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,660 kB
  • sloc: ansic: 174,732; cpp: 153; xml: 21; makefile: 11
file content (299 lines) | stat: -rw-r--r-- 12,509 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* Copyright (C) 2013-2016, The Regents of The University of Michigan.
All rights reserved.

This software was developed in the APRIL Robotics Lab under the
direction of Edwin Olson, ebolson@umich.edu. This software may be
available under alternative licensing terms; contact the address above.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the Regents of The University of Michigan.
*/

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <ctype.h>
#include <unistd.h>
#include <math.h>
#include <getopt.h>

#include <apriltag.h>
#include <tag36h11.h>
#include <tag25h9.h>
#include <tag16h5.h>
#include <tagCircle21h7.h>
#include <tagCircle49h12.h>
#include <tagCustom48h12.h>
#include <tagStandard41h12.h>
#include <tagStandard52h13.h>

#define FAMILIES(_) \
  _(tag36h11) \
  _(tag25h9) \
  _(tag16h5) \
  _(tagCircle21h7) \
  _(tagCircle49h12) \
  _(tagCustom48h12) \
  _(tagStandard41h12) \
  _(tagStandard52h13)

#define STRING_INDENTED_WITHNEWLINE(n) "  "#n"\n"


#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>

int main(int argc, char *argv[])
{
    const char* usage =
        "usage: %1$s [options] <input files>\n"
        "\n"
        "SYNOPSIS\n"
        "\n"
        "  $ wget https://april.eecs.umich.edu/media/apriltag/apriltagrobots_overlay.jpg\n"
        "\n"
        "\n"
        "  $ %1$s --vnlog apriltagrobots_overlay.jpg | vnl-align                                                                                                               \n"
        "\n"
        "  # path                     Ndetections hamming   margin   id     xc         yc         xlb        ylb        xrb        yrb        xrt        yrt        xlt        ylt   \n"
        "  apriltagrobots_overlay.jpg 7           -       -          -  -          -          -          -          -          -          -          -          -          -         \n"
        "  apriltagrobots_overlay.jpg -           0       154.156281  3 582.432959 172.987344 572.873413 162.835861 571.657776 182.958588 591.934753 183.077499 592.011963 164.123032\n"
        "  apriltagrobots_overlay.jpg -           0       141.343460  5 703.333306 271.518333 687.420959 253.673584 684.764160 287.597870 719.562561 289.718475 722.153748 255.221191\n"
        "  apriltagrobots_overlay.jpg -           0       149.004272  7 288.166244 226.956937 274.247131 211.727753 273.825287 240.716934 301.680084 241.742706 303.137512 212.592163\n"
        "  apriltagrobots_overlay.jpg -           0       147.246689  8 463.597662 227.929726 450.073639 213.023773 448.587341 241.512299 477.446289 243.193451 478.584595 214.368317\n"
        "  apriltagrobots_overlay.jpg -           0       155.495026 10  93.838268 241.648437  77.001884 226.194351  78.336220 258.154816 110.266693 256.728058 108.630562 225.897797\n"
        "  apriltagrobots_overlay.jpg -           1       100.956978 10 122.027947 237.601807 116.803673 256.071167 127.177071 250.224243 127.195175 219.334122 116.819603 224.834198\n"
        "  apriltagrobots_overlay.jpg -           0       158.080490 14 356.498429 260.225200 338.697937 243.629883 339.127502 278.233917 374.164917 276.695587 373.355896 242.748795\n"
        "\n"
        "\n"
        "  $ %1$s --vnlog apriltagrobots_overlay.jpg \\\n"
        "    | vnl-filter -p xc,id,yc \\\n"
        "    | feedgnuplot \\\n"
        "      --autolegend \\\n"
        "      --image apriltagrobots_overlay.jpg \\\n"
        "      --square \\\n"
        "      --domain \\\n"
        "      --dataid \\\n"
        "      --with 'points pt 7 ps 2'\n"
        "\n"
        "  [ plot pops up showing the input image with the detections overlaid ]\n"
        "\n"
        "ARGUMENTS\n"
        "Option                  Default value   Description\n"
        "  -h | --help          [ true ]       Show this help\n"
        "  -d | --debug         [ false ]      Enable debugging output (slow)\n"
        "  -q | --quiet         [ false ]      Reduce output\n"
        "  -f | --family        [ tag36h11 ]   Tag family to use\n"
        "  -i | --iters         [ 1 ]          Repeat processing on input set this many times\n"
        "  -t | --threads       [ 1 ]          Use this many CPU threads\n"
        "  -x | --decimate      [ 1.0 ]        Decimate input image by this factor\n"
        "  -m | --maxhamming    [ 1 ]          Max number of corrected bits. Larger values guzzle RAM\n"
        "  -b | --blur          [ 0.0 ]        Apply low-pass blur to input; negative sharpens\n"
        "  -0 | --refine-edges  [ true ]       Spend more time trying to align edges of tags\n"
        "  -v | --vnlog         [ false ]      Write output as a vnlog\n"
        "\n"
        "Supported tag families:\n"
        FAMILIES(STRING_INDENTED_WITHNEWLINE);
    
    struct option opts[] =
        {
         { "debug",         no_argument,       NULL, 'd' },
         { "quiet",         no_argument,       NULL, 'q' },
         { "family",        required_argument, NULL, 'f' },
         { "iters",         required_argument, NULL, 'i' },
         { "threads",       required_argument, NULL, 't' },
         { "decimate",      required_argument, NULL, 'x' },
         { "maxhamming",    required_argument, NULL, 'm' },
         { "blur",          required_argument, NULL, 'B' },
         { "refine-edges",  no_argument,       NULL, '0' },
         { "vnlog",         no_argument,       NULL, 'v' },
         { "help",          no_argument,       NULL, 'h' },
         {}
        };

    bool        debug         = false;
    bool        quiet         = false;
    const char* family        = "tag36h11";
    int         maxiters      = 1;
    int         threads       = 1;
    double      decimate      = 1.0;
    int         maxhamming    = 1;
    double      blur          = 0.0;
    bool        refine_edges  = true;
    bool        vnlog         = false;

    int opt;
    do
    {
        opt = getopt_long(argc, argv, "dqf:i:t:x:m:0vh", opts, NULL);
        switch(opt)
        {
        case -1: break;
        case 'd': debug         = true;         break;
        case 'q': quiet         = true;         break;
        case 'f': family        = optarg;       break;
        case 'i': maxiters      = atoi(optarg); break;
        case 't': threads       = atoi(optarg); break;
        case 'x': decimate      = atof(optarg); break;
        case 'm': maxhamming    = atoi(optarg); break;
        case 'B': blur          = atof(optarg); break;
        case '0': refine_edges  = true;         break;
        case 'v': vnlog         = true;         break;

        case 'h':
            printf(usage, argv[0]);
            return 0;
        case '?':
            fprintf(stderr, "Cmdline-parsing error!\n");
            fprintf(stderr, usage, argv[0]);
            return 1;
        }
    } while( opt != -1 );


    apriltag_family_t *tf = NULL;

#define CHECK_AND_CREATE(n) else if (!strcmp(family, #n)) tf = n ## _create();

    if(0); FAMILIES(CHECK_AND_CREATE)
    else {
        fprintf(stderr, "Unrecognized tag family name. Supported families:\n" FAMILIES(STRING_INDENTED_WITHNEWLINE));
        exit(-1);
    }

    apriltag_detector_t *td = apriltag_detector_create();
    apriltag_detector_add_family_bits(td, tf, maxhamming);
    td->quad_decimate       = decimate;
    td->quad_sigma          = blur;
    td->nthreads            = threads;
    td->debug               = debug;
    td->refine_edges        = refine_edges;

    const int hamm_hist_max = 10;

    if(vnlog)
        printf("# path Ndetections hamming margin id xc yc xlb ylb xrb yrb xrt yrt xlt ylt\n");

    for (int iter = 0; iter < maxiters; iter++) {

        int total_quads = 0;
        int total_hamm_hist[hamm_hist_max];
        memset(total_hamm_hist, 0, sizeof(total_hamm_hist));
        double total_time = 0;

        if (maxiters > 1)
            fprintf(stderr, "iter %d / %d\n", iter + 1, maxiters);

        for (; optind < argc; optind++) {

            int hamm_hist[hamm_hist_max];
            memset(hamm_hist, 0, sizeof(hamm_hist));

            char* path = argv[optind];
            if (!quiet && !vnlog)
                fprintf(stderr, "loading %s\n", path);

            int width,height,bytes_per_pixel;
            unsigned char* image_buf = stbi_load(path, &width, &height, &bytes_per_pixel, 1);
            if(image_buf == NULL)
            {
                fprintf(stderr,"couldn't load %s\n", path);
                continue;
            }
            image_u8_t im = {.width  = width,
                             .height = height,
                             .stride = width,
                             .buf    = image_buf};


            zarray_t *detections = apriltag_detector_detect(td, &im);
            if(vnlog)
                printf("%s %d - - - - - - - - - - - - -\n",
                       path,
                       zarray_size(detections));

            for (int i = 0; i < zarray_size(detections); i++) {
                apriltag_detection_t *det;
                zarray_get(detections, i, &det);

                if(vnlog)
                    printf("%s - %d %f %d %f %f %f %f %f %f %f %f %f %f\n",
                           path,
                           det->hamming,
                           det->decision_margin,
                           det->id,
                           det->c[0], det->c[1],
                           det->p[0][0], det->p[0][1],
                           det->p[1][0], det->p[1][1],
                           det->p[2][0], det->p[2][1],
                           det->p[3][0], det->p[3][1]);
                else if (!quiet)
                    printf("detection %3d: id (%2dx%2d)-%-4d, hamming %d, margin %8.3f\n",
                           i, det->family->nbits, det->family->h, det->id, det->hamming, det->decision_margin);

                hamm_hist[det->hamming]++;
                total_hamm_hist[det->hamming]++;
            }

            apriltag_detections_destroy(detections);

            if (!quiet && !vnlog)
            {
                timeprofile_display(td->tp);
                total_quads += td->nquads;
                fprintf(stderr,"hamm ");

                for (int i = 0; i < hamm_hist_max; i++)
                    fprintf(stderr,"%5d ", hamm_hist[i]);

                double t =  timeprofile_total_utime(td->tp) / 1.0E3;
                total_time += t;
                fprintf(stderr,"%12.3f ", t);
                fprintf(stderr,"%5d", td->nquads);
                fprintf(stderr,"\n");
            }

            stbi_image_free(image_buf);
        }

        if(!quiet && !vnlog)
        {
            fprintf(stderr,"Summary\n");
            fprintf(stderr,"hamm ");

            for (int i = 0; i < hamm_hist_max; i++)
                fprintf(stderr,"%5d ", total_hamm_hist[i]);
            fprintf(stderr,"%12.3f ", total_time);
            fprintf(stderr,"%5d", total_quads);
            fprintf(stderr,"\n");
        }
    }

    apriltag_detector_destroy(td);

#define CHECK_AND_DESTROY(n) else if (!strcmp(family, #n)) n ## _destroy(tf);
    if(0); FAMILIES(CHECK_AND_DESTROY)

    return 0;
}