|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <math.h> |
| 5 | +#include <assert.h> |
| 6 | +#include <stdarg.h> // Added this header file to support variable arguments |
| 7 | + |
| 8 | +#include "apriltag.h" |
| 9 | +#include "apriltag_pose.h" |
| 10 | +#include "common/matd.h" |
| 11 | +#include "common/pjpeg.h" |
| 12 | +#include "test/getline.h" |
| 13 | + |
| 14 | +// Declare tag family creation functions - use tag36h11 instead of tagStandard41h12 |
| 15 | +apriltag_family_t *tag36h11_create(void); |
| 16 | +void tag36h11_destroy(apriltag_family_t *tf); |
| 17 | + |
| 18 | +// format function copied from test_detection.c |
| 19 | +char* format(const char *fmt, ...) { |
| 20 | + va_list args; |
| 21 | + va_start(args, fmt); |
| 22 | + int required = vsnprintf(NULL, 0, fmt, args); |
| 23 | + va_end(args); |
| 24 | + |
| 25 | + if (required < 0) { |
| 26 | + return NULL; |
| 27 | + } |
| 28 | + |
| 29 | + char *buffer = malloc(required + 1); |
| 30 | + if (!buffer) { |
| 31 | + return NULL; |
| 32 | + } |
| 33 | + |
| 34 | + va_start(args, fmt); |
| 35 | + int result = vsnprintf(buffer, required + 1, fmt, args); |
| 36 | + va_end(args); |
| 37 | + |
| 38 | + if (result < 0) { |
| 39 | + free(buffer); |
| 40 | + return NULL; |
| 41 | + } |
| 42 | + |
| 43 | + return buffer; |
| 44 | +} |
| 45 | + |
| 46 | +// Parse expected detection results |
| 47 | +int parse_expected_detection(const char* line, int* id, double corners[4][2]) { |
| 48 | + char* line_copy = strdup(line); |
| 49 | + if (!line_copy) { |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + // Split ID and corners using comma |
| 54 | + char* token = strtok(line_copy, ","); |
| 55 | + if (!token) { |
| 56 | + free(line_copy); |
| 57 | + return 0; |
| 58 | + } |
| 59 | + |
| 60 | + *id = atoi(token); |
| 61 | + |
| 62 | + // Parse four corners |
| 63 | + for (int i = 0; i < 4; i++) { |
| 64 | + token = strtok(NULL, " ()"); |
| 65 | + if (!token) { |
| 66 | + free(line_copy); |
| 67 | + return 0; |
| 68 | + } |
| 69 | + corners[i][0] = atof(token); |
| 70 | + |
| 71 | + token = strtok(NULL, " ()"); |
| 72 | + if (!token) { |
| 73 | + free(line_copy); |
| 74 | + return 0; |
| 75 | + } |
| 76 | + corners[i][1] = atof(token); |
| 77 | + } |
| 78 | + |
| 79 | + free(line_copy); |
| 80 | + return 1; |
| 81 | +} |
| 82 | + |
| 83 | +int main(int argc, char *argv[]) { |
| 84 | + if (argc < 3) { |
| 85 | + printf("Usage: %s <image_path> <expected_data_path>\n", argv[0]); |
| 86 | + printf("Example: %s test/data/33369213973_9d9bb4cc96_c.jpg test/data/33369213973_9d9bb4cc96_c.txt\n", argv[0]); |
| 87 | + return 1; |
| 88 | + } |
| 89 | + |
| 90 | + const char *image_path = argv[1]; |
| 91 | + const char *expected_data_path = argv[2]; |
| 92 | + |
| 93 | + printf("Testing tag pose estimation with image: %s\n", image_path); |
| 94 | + printf("Expected data file: %s\n", expected_data_path); |
| 95 | + |
| 96 | + // Create AprilTag detector - use tag36h11 family |
| 97 | + apriltag_detector_t *td = apriltag_detector_create(); |
| 98 | + apriltag_family_t *tf = tag36h11_create(); |
| 99 | + apriltag_detector_add_family(td, tf); |
| 100 | + |
| 101 | + // Set detector parameters - use same parameters as test_detection.c |
| 102 | + td->quad_decimate = 1.0; // Consistent with test_detection.c |
| 103 | + td->quad_sigma = 0.0; |
| 104 | + td->nthreads = 4; |
| 105 | + td->debug = 0; |
| 106 | + td->refine_edges = 0; // Consistent with test_detection.c |
| 107 | + // Remove non-existent parameters |
| 108 | + // td->refine_decode = 0; |
| 109 | + // td->refine_pose = 0; |
| 110 | + |
| 111 | + // Load image |
| 112 | + int pjpeg_error; |
| 113 | + pjpeg_t *pjpeg = pjpeg_create_from_file(image_path, 0, &pjpeg_error); // Fix function call, add error parameter |
| 114 | + if (!pjpeg || pjpeg_error) { |
| 115 | + printf("Failed to load image: %s, error code: %d\n", image_path, pjpeg_error); |
| 116 | + return 1; |
| 117 | + } |
| 118 | + |
| 119 | + image_u8_t *im = pjpeg_to_u8_baseline(pjpeg); |
| 120 | + pjpeg_destroy(pjpeg); |
| 121 | + |
| 122 | + if (!im) { |
| 123 | + printf("Failed to decode image: %s\n", image_path); |
| 124 | + return 1; |
| 125 | + } |
| 126 | + |
| 127 | + // Perform detection |
| 128 | + zarray_t *detections = apriltag_detector_detect(td, im); |
| 129 | + |
| 130 | + // Read expected data file |
| 131 | + FILE *expected_file = fopen(expected_data_path, "r"); |
| 132 | + if (!expected_file) { |
| 133 | + printf("Failed to open expected data file: %s\n", expected_data_path); |
| 134 | + image_u8_destroy(im); |
| 135 | + apriltag_detections_destroy(detections); |
| 136 | + apriltag_detector_destroy(td); |
| 137 | + tag36h11_destroy(tf); |
| 138 | + return 1; |
| 139 | + } |
| 140 | + |
| 141 | + printf("Found %d detections in image\n", zarray_size(detections)); |
| 142 | + |
| 143 | + // Perform pose estimation test for each detection |
| 144 | + int num_detections = zarray_size(detections); |
| 145 | + int pose_estimation_success = 0; |
| 146 | + |
| 147 | + for (int i = 0; i < num_detections; i++) { |
| 148 | + apriltag_detection_t *det; |
| 149 | + zarray_get(detections, i, &det); |
| 150 | + |
| 151 | + printf("Processing detection %d: tag ID %d\n", i, det->id); |
| 152 | + |
| 153 | + // Set camera parameters and tag size |
| 154 | + apriltag_detection_info_t info; |
| 155 | + info.det = det; |
| 156 | + info.tagsize = 0.16; // Assume tag size is 16cm |
| 157 | + info.fx = 600.0; // Assume focal length |
| 158 | + info.fy = 600.0; |
| 159 | + info.cx = im->width / 2.0; // Assume optical center is at image center |
| 160 | + info.cy = im->height / 2.0; |
| 161 | + |
| 162 | + // Perform pose estimation |
| 163 | + apriltag_pose_t pose; |
| 164 | + double err = estimate_tag_pose(&info, &pose); |
| 165 | + |
| 166 | + printf(" Estimated pose for tag %d:\n", det->id); |
| 167 | + printf(" Translation: [%f, %f, %f]\n", |
| 168 | + MATD_EL(pose.t, 0, 0), |
| 169 | + MATD_EL(pose.t, 1, 0), |
| 170 | + MATD_EL(pose.t, 2, 0)); |
| 171 | + printf(" Rotation matrix:\n"); |
| 172 | + printf(" [%f, %f, %f]\n", |
| 173 | + MATD_EL(pose.R, 0, 0), MATD_EL(pose.R, 0, 1), MATD_EL(pose.R, 0, 2)); |
| 174 | + printf(" [%f, %f, %f]\n", |
| 175 | + MATD_EL(pose.R, 1, 0), MATD_EL(pose.R, 1, 1), MATD_EL(pose.R, 1, 2)); |
| 176 | + printf(" [%f, %f, %f]\n", |
| 177 | + MATD_EL(pose.R, 2, 0), MATD_EL(pose.R, 2, 1), MATD_EL(pose.R, 2, 2)); |
| 178 | + printf(" Reprojection error: %f\n", err); |
| 179 | + |
| 180 | + // Check if pose estimation was successful |
| 181 | + if (pose.R != NULL && pose.t != NULL && err < 1.0) { // Assume error less than 1.0 means success |
| 182 | + pose_estimation_success++; |
| 183 | + } |
| 184 | + |
| 185 | + // Free pose memory |
| 186 | + if (pose.R) matd_destroy(pose.R); |
| 187 | + if (pose.t) matd_destroy(pose.t); |
| 188 | + } |
| 189 | + |
| 190 | + // Output test results |
| 191 | + printf("\nPose estimation test results:\n"); |
| 192 | + printf(" Successful pose estimations: %d/%d\n", pose_estimation_success, num_detections); |
| 193 | + |
| 194 | + if (pose_estimation_success == num_detections && num_detections > 0) { |
| 195 | + printf(" All pose estimations successful!\n"); |
| 196 | + } else if (num_detections > 0) { |
| 197 | + printf(" Some pose estimations failed.\n"); |
| 198 | + } else { |
| 199 | + printf(" No detections found in image.\n"); |
| 200 | + } |
| 201 | + |
| 202 | + // Clean up resources |
| 203 | + image_u8_destroy(im); |
| 204 | + apriltag_detections_destroy(detections); |
| 205 | + apriltag_detector_destroy(td); |
| 206 | + tag36h11_destroy(tf); |
| 207 | + |
| 208 | + return (pose_estimation_success == num_detections && num_detections > 0) ? 0 : 1; |
| 209 | +} |
0 commit comments