void ImageTagManagerImpl::FindTagsWorker() { apriltag_family_t* tf(tag36h11_create()); apriltag_detector_t* td(apriltag_detector_create()); apriltag_detector_add_family(td, tf); td->quad_decimate = 1.0; td->quad_sigma = 0.0; td->nthreads = 1; td->debug = 0; td->refine_decode = 0; td->refine_pose = 0; while (!m_stopping) { if (m_event.Wait()) { if (m_locator) { m_locator->FindTags(td); } } } apriltag_detector_destroy(td); tag36h11_destroy(tf); }
ImageTagManagerImpl::~ImageTagManagerImpl() { tag36h11_destroy(m_tf); if (m_workerThread) { if (m_locator) { m_locator->Cancel(); } m_stopping = 1; m_event.Set(); m_workerThread->WaitForThreadExit(); } }
int main(int argc, char *argv[]) { 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"); 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] <input files>\n", argv[0]); getopt_do_usage(getopt); exit(0); } const zarray_t *inputs = getopt_get_extra_args(getopt); apriltag_family_t *tf = NULL; 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); } tf->black_border = getopt_get_int(getopt, "border"); apriltag_detector_t *td = apriltag_detector_create(); apriltag_detector_add_family(td, tf); 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_decode = getopt_get_bool(getopt, "refine-decode"); td->refine_pose = getopt_get_bool(getopt, "refine-pose"); int quiet = getopt_get_bool(getopt, "quiet"); int maxiters = getopt_get_int(getopt, "iters"); const int hamm_hist_max = 10; for (int iter = 0; iter < maxiters; iter++) { if (maxiters > 1) printf("iter %d / %d\n", iter + 1, maxiters); for (int input = 0; input < zarray_size(inputs); input++) { int hamm_hist[hamm_hist_max]; memset(hamm_hist, 0, sizeof(hamm_hist)); char *path; zarray_get(inputs, input, &path); if (!quiet) printf("loading %s\n", path); image_u8_t *im = image_u8_create_from_pnm(path); if (im == NULL) { printf("couldn't find %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]++; apriltag_detection_destroy(det); } zarray_destroy(detections); if (!quiet) { timeprofile_display(td->tp); printf("nedges: %d, nsegments: %d, nquads: %d\n", td->nedges, td->nsegments, td->nquads); } if (!quiet) printf("Hamming histogram: "); for (int i = 0; i < hamm_hist_max; i++) printf("%5d", hamm_hist[i]); if (quiet) { printf("%12.3f", timeprofile_total_utime(td->tp) / 1.0E3); } printf("\n"); image_u8_destroy(im); } } // don't deallocate contents of inputs; those are the argv apriltag_detector_destroy(td); tag36h11_destroy(tf); return 0; }
int main(){ bool showGradient = true; bool found = false; VideoCapture cap(0); // open the default camera Size size(854,480); // size of desired frame origionally 1280x720, 1024x576, 854x480 if(!cap.isOpened()) // check if camera opened return -1; Mat frame; Mat src; /* From apriltag_demo.c */ int maxiters = 5; const int hamm_hist_max = 10; int quiet = 0; apriltag_family_t *tf = tag36h11_create(); // Apriltag family 36h11, can change tf->black_border = 1; // Set tag family border size apriltag_detector_t *td = apriltag_detector_create(); // Apriltag detector apriltag_detector_add_family(td, tf); // Add apriltag family td->quad_decimate = 1.0; // Decimate input image by factor td->quad_sigma = 0.0; // No blur (I think) td->nthreads = 4; // 4 treads provided td->debug = 0; // No debuging output td->refine_decode = 0; // Don't refine decode td->refine_pose = 0; // Don't refine pose // Output variables char imgSize[20]; char renderTime[20]; char detectString[50]; char convertTime[50]; char displayString[120]; char outputString[120]; char locationString[120]; double time_taken = 0.0; long double totalFPS = 0.0; double count = 0.0; /* End of apriltag_demo.c */ while(1){ clock_t t; t = clock(); cap >> src; // Get a new frame from camera if(found){ resize(src,frame,size); } // Resize to smaller image if tag found else{ frame = src; } // Keep standard image if no tag if(showGradient){ cvtColor(src, frame, CV_BGR2GRAY); cvtColor(frame, frame, CV_GRAY2RGB); src = gradientEdges(frame); // Show gradient for fun }else{ cvtColor(src, src, CV_BGR2GRAY); } pnm_t *pnm = mat2pnm(&frame); // Convert Mat fram to pnm image_u8_t *im = pnm_to_image_u8(pnm); // Convert pnm to gray image_u8 if (im == NULL) { // Error - no image created from pnm std::cout << "Error, not a proper pnm" << std::endl; return -1; } /*** Start from origional Apriltags from apriltag_demo.c ***/ int hamm_hist[hamm_hist_max]; memset(hamm_hist, 0, sizeof(hamm_hist)); 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); sprintf(locationString, "Tag Center: (%f,%f)", det->c[0], det->c[1]); sprintf(detectString, "detection %2d: id (%2dx%2d)-%-4d, hamming %d, goodness %5.3f, margin %5.3f\n", i+1, det->family->d*det->family->d, det->family->h, det->id, det->hamming, det->goodness, det->decision_margin); hamm_hist[det->hamming]++; // draws a vertical rectangle around tag, not ideal, but easy to implement // det->p[corner][positon], counter clockwise Point pt1 = Point(det->p[0][0], det->p[0][1]); Point pt2 = Point(det->p[2][0], det->p[2][1]); cv::rectangle(src, pt1, pt2, cvScalar(102,255,0)); apriltag_detection_destroy(det); } if(zarray_size(detections) < 1){ found = false; sprintf(detectString, "No tag detected"); sprintf(locationString, "No tag detected"); }else{ found = false; } zarray_destroy(detections); image_u8_destroy(im); t = clock() - t; double time_taken = ((double)t)/(CLOCKS_PER_SEC/1000); //printf("ms to render: %5.3f\n", time_taken); if (!quiet) { //timeprofile_display(td->tp); totalFPS += (1000.0/time_taken); count += 1.0; if(count > 30000.0){ totalFPS = 0.0; count = 0.0; } sprintf(displayString, "fps: %2.2Lf, nquads: %d",totalFPS/count, td->nquads); //std::cout << displayString; } //for (int i = 0; i < hamm_hist_max; i++) //printf("%5d", hamm_hist[i]); sprintf(renderTime, "Render: %5.3fms", time_taken); sprintf(imgSize, "%dx%d", frame.cols, frame.rows); sprintf(outputString, "%s %s %s", renderTime, convertTime, imgSize); printf("%s %s\r", detectString, outputString); if (quiet) { printf("%12.3f", timeprofile_total_utime(td->tp) / 1.0E3); } printf("\n"); /*** End of origional Apriltags from apriltag_demo.c ***/ // displays fps, edges, segments, quads putText(src, displayString, cvPoint(30,30), FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(150,150,250), 1, CV_AA); // displays render time, convert time, and image size putText(src, outputString, cvPoint(30,50), FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(150,150,250), 1, CV_AA); // Displays any detections (if any) putText(src, detectString, cvPoint(30,70), FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(150,150,250), 1, CV_AA); // Displays tag location (if any) putText(src, locationString, cvPoint(30,90), FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(150,150,250), 1, CV_AA); imshow("Display Apriltags", src); if(waitKey(30) >= 0) break; } /* deallocate apriltag constructs */ apriltag_detector_destroy(td); tag36h11_destroy(tf); return 0; }
int main(int argc, char *argv[]) { april_tag_family_t *tf = tag36h11_create(); april_tag_detector_t *td = april_tag_detector_create(tf); td->small_tag_refinement = 0; int maxiters = 1; zarray_t *inputs = zarray_create(sizeof(char*)); int waitsec = 0; for (int i = 1; i < argc; i++) { if (!strcmp(argv[i], "-d")) td->debug = 1; else if (!strcmp(argv[i], "-t")) td->nthreads = atoi(argv[++i]); else if (!strcmp(argv[i], "-f")) td->seg_decimate = (i+1 < argc && isdigit(argv[i+1][0])) ? atoi(argv[++i]) : 2; else if (!strcmp(argv[i], "-i")) maxiters = atoi(argv[++i]); else if (!strcmp(argv[i], "-r")) td->small_tag_refinement = 1; else if (!strcmp(argv[i], "-w")) waitsec = atoi(argv[++i]); else if (!strcmp(argv[i], "-b")) td->seg_sigma = atof(argv[++i]); /* else if (!strcmp(argv[i], "--family")) { char *fam = argv[++i]; if (!strcmp(fam, "36h11")) td->tag_family = tag36h11_create(); else if (!strcmp(fam, "36h10")) td->tag_family = tag36h10_create(); } */ else zarray_add(inputs, &argv[i]); } for (int iter = 0; iter < maxiters; iter++) { if (maxiters > 1) printf("iter %d / %d\n", iter + 1, maxiters); for (int input = 0; input < zarray_size(inputs); input++) { char *path; zarray_get(inputs, input, &path); printf("loading %s\n", path); image_u8_t *im = image_u8_create_from_pnm(path); if (im == NULL) { printf("couldn't find %s\n", path); continue; } zarray_t *detections = april_tag_detector_detect(td, im); for (int i = 0; i < zarray_size(detections); i++) { april_tag_detection_t *det; zarray_get(detections, i, &det); printf("detection %3d: id %4d, hamming %d, goodness %f\n", i, det->id, det->hamming, det->goodness); april_tag_detection_destroy(det); } zarray_destroy(detections); timeprofile_display(td->tp); printf("nedges: %d, nsegments: %d, nquads: %d\n", td->nedges, td->nsegments, td->nquads); image_u8_destroy(im); if (zarray_size(inputs) > 1 || iter > 0) sleep(waitsec); } } april_tag_detector_destroy(td); tag36h11_destroy(tf); return 0; }
double* getTag(char* path) { apriltag_family_t *tf = NULL; tf = tag36h11_create(); tf->black_border = 1; apriltag_detector_t *td = apriltag_detector_create(); apriltag_detector_add_family(td, tf); td->quad_decimate = 1.0; td->quad_sigma = 0.0; td->nthreads = 4; td->debug = 0; td->refine_decode = 0; td->refine_pose = 0; int quiet = 0; int maxiters = 1; const int hamm_hist_max = 10; int hamm_hist[hamm_hist_max]; memset(hamm_hist, 0, sizeof(hamm_hist)); image_u8_t *im = image_u8_create_from_pnm(path); if (im == NULL) { printf("couldn't find %s\n", path); return NULL; } zarray_t *detections = apriltag_detector_detect(td, im); nrows = zarray_size(detections); ncols = 9; if (nrows == 0) return NULL; double* output_matrix = new double[nrows*ncols]; for (int i = 0; i < zarray_size(detections); i++) { apriltag_detection_t *det; zarray_get(detections, i, &det); output_matrix[ncols*i+0] = det->id; for(int j=0; j<4; j++) { output_matrix[ncols*i+ 2*j +1] = det->p[j][0]; output_matrix[ncols*i+ 2*j +2] = det->p[j][1]; } hamm_hist[det->hamming]++; apriltag_detection_destroy(det); } zarray_destroy(detections); image_u8_destroy(im); // don't deallocate contents of inputs; those are the argv apriltag_detector_destroy(td); tag36h11_destroy(tf); return output_matrix; }