/* 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 #include #include #include #include #include #include "apriltag.h" #include "tag36h11.h" #include "tag36h10.h" #include "tag36artoolkit.h" #include "tag25h9.h" #include "tag25h7.h" #include "common/getopt.h" #include "common/image_u8.h" #include "common/image_u8x4.h" #include "common/pjpeg.h" #include "common/zarray.h" #define JS #ifdef JS #include #endif // Invoke: // // tagtest [options] input.pnm int main(int argc, char *argv[]) { #ifdef JS EM_ASM( FS.mkdir('/working'); FS.mount(NODEFS, { root: '.' }, '/working'); ); #endif #ifndef JS getopt_t *getopt = getopt_create(); getopt_add_bool(getopt, 'h', "help", 0, "Show this help"); getopt_add_bool(getopt, 'd', "debug", 0, "Enable debugging output (slow)"); getopt_add_bool(getopt, 'q', "quiet", 0, "Reduce output"); getopt_add_string(getopt, 'f', "family", "tag36h11", "Tag family to use"); getopt_add_int(getopt, '\0', "border", "1", "Set tag family border size"); getopt_add_int(getopt, 'i', "iters", "1", "Repeat processing on input set this many times"); getopt_add_int(getopt, 't', "threads", "4", "Use this many CPU threads"); getopt_add_double(getopt, 'x', "decimate", "1.0", "Decimate input image by this factor"); getopt_add_double(getopt, 'b', "blur", "0.0", "Apply low-pass blur to input; negative sharpens"); getopt_add_bool(getopt, '0', "refine-edges", 1, "Spend more time trying to align edges of tags"); getopt_add_bool(getopt, '1', "refine-decode", 0, "Spend more time trying to decode tags"); getopt_add_bool(getopt, '2', "refine-pose", 0, "Spend more time trying to precisely localize tags"); if (!getopt_parse(getopt, argc, argv, 1) || getopt_get_bool(getopt, "help")) { printf("Usage: %s [options] \n", argv[0]); getopt_do_usage(getopt); exit(0); } #endif #ifdef JS printf("Arg: %s\n", argv[1]); zarray_t *inputs = zarray_create(sizeof(char*)); zarray_add(inputs, argv[1]); #else const zarray_t *inputs = getopt_get_extra_args(getopt); #endif apriltag_family_t *tf = NULL; #ifdef JS tf = tag36h11_create(); #else const char *famname = getopt_get_string(getopt, "family"); if (!strcmp(famname, "tag36h11")) tf = tag36h11_create(); else if (!strcmp(famname, "tag36h10")) tf = tag36h10_create(); else if (!strcmp(famname, "tag36artoolkit")) tf = tag36artoolkit_create(); else if (!strcmp(famname, "tag25h9")) tf = tag25h9_create(); else if (!strcmp(famname, "tag25h7")) tf = tag25h7_create(); else { printf("Unrecognized tag family name. Use e.g. \"tag36h11\".\n"); exit(-1); } #endif #ifdef JS tf->black_border = 1; #else tf->black_border = getopt_get_int(getopt, "border"); #endif apriltag_detector_t *td = apriltag_detector_create(); apriltag_detector_add_family(td, tf); #ifdef JS td->quad_decimate = 1.0; td->quad_sigma = 0.0; td->nthreads = 1; td->debug = true; td->refine_edges = true; td->refine_decode = false; td->refine_pose = false; #else td->quad_decimate = getopt_get_double(getopt, "decimate"); td->quad_sigma = getopt_get_double(getopt, "blur"); td->nthreads = getopt_get_int(getopt, "threads"); td->debug = getopt_get_bool(getopt, "debug"); td->refine_edges = getopt_get_bool(getopt, "refine-edges"); td->refine_decode = getopt_get_bool(getopt, "refine-decode"); td->refine_pose = getopt_get_bool(getopt, "refine-pose"); #endif #ifdef JS int quiet = false; int maxiters = 1; #else int quiet = getopt_get_bool(getopt, "quiet"); int maxiters = getopt_get_int(getopt, "iters"); #endif const int hamm_hist_max = 10; 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) printf("iter %d / %d\n", iter + 1, maxiters); #ifdef JS for (int input = 1; input < argc; input++) { #else for (int input = 0; input < zarray_size(inputs); input++) { #endif int hamm_hist[hamm_hist_max]; memset(hamm_hist, 0, sizeof(hamm_hist)); char *path; #ifdef JS path = argv[input]; #else zarray_get(inputs, input, &path); #endif if (!quiet) printf("loading %s\n", path); else printf("%20s ", path); image_u8_t *im = NULL; if (str_ends_with(path, "pnm") || str_ends_with(path, "PNM") || str_ends_with(path, "pgm") || str_ends_with(path, "PGM")) im = image_u8_create_from_pnm(path); else if (str_ends_with(path, "jpg") || str_ends_with(path, "JPG")) { int err = 0; pjpeg_t *pjpeg = pjpeg_create_from_file(path, 0, &err); if (pjpeg == NULL) { printf("pjpeg error %d\n", err); continue; } if (1) { im = pjpeg_to_u8_baseline(pjpeg); } else { printf("illumination invariant\n"); image_u8x3_t *imc = pjpeg_to_u8x3_baseline(pjpeg); im = image_u8_create(imc->width, imc->height); for (int y = 0; y < imc->height; y++) { for (int x = 0; x < imc->width; x++) { double r = imc->buf[y*imc->stride + 3*x + 0] / 255.0; double g = imc->buf[y*imc->stride + 3*x + 1] / 255.0; double b = imc->buf[y*imc->stride + 3*x + 2] / 255.0; double alpha = 0.42; double v = 0.5 + log(g) - alpha*log(b) - (1-alpha)*log(r); int iv = v * 255; if (iv < 0) iv = 0; if (iv > 255) iv = 255; im->buf[y*im->stride + x] = iv; } } image_u8x3_destroy(imc); if (td->debug) image_u8_write_pnm(im, "debug_invariant.pnm"); } pjpeg_destroy(pjpeg); } if (im == NULL) { printf("couldn't load %s\n", path); continue; } zarray_t *detections = apriltag_detector_detect(td, im); for (int i = 0; i < zarray_size(detections); i++) { apriltag_detection_t *det; zarray_get(detections, i, &det); if (!quiet) printf("detection %3d: id (%2dx%2d)-%-4d, hamming %d, goodness %8.3f, margin %8.3f\n", i, det->family->d*det->family->d, det->family->h, det->id, det->hamming, det->goodness, det->decision_margin); hamm_hist[det->hamming]++; total_hamm_hist[det->hamming]++; } apriltag_detections_destroy(detections); if (!quiet) { timeprofile_display(td->tp); } total_quads += td->nquads; if (!quiet) printf("hamm "); for (int i = 0; i < hamm_hist_max; i++) printf("%5d ", hamm_hist[i]); double t = timeprofile_total_utime(td->tp) / 1.0E3; total_time += t; printf("%12.3f ", t); printf("%5d", td->nquads); printf("\n"); image_u8_destroy(im); } printf("Summary\n"); printf("hamm "); for (int i = 0; i < hamm_hist_max; i++) printf("%5d ", total_hamm_hist[i]); printf("%12.3f ", total_time); printf("%5d", total_quads); printf("\n"); } // don't deallocate contents of inputs; those are the argv apriltag_detector_destroy(td); tag36h11_destroy(tf); return 0; }