/* This illustrates how to use SIFT3D within a function, and free all memory * afterwards. */ int demo(void) { Image im, draw; Mat_rm keys; SIFT3D sift3d; Keypoint_store kp; SIFT3D_Descriptor_store desc; // Initialize the intermediates init_Keypoint_store(&kp); init_SIFT3D_Descriptor_store(&desc); init_im(&im); init_im(&draw); if (init_Mat_rm(&keys, 0, 0, DOUBLE, SIFT3D_FALSE)) return 1; if (init_SIFT3D(&sift3d)) { cleanup_Mat_rm(&keys); return 1; } // Read the image if (im_read(im_path, &im)) goto demo_quit; // Detect keypoints if (SIFT3D_detect_keypoints(&sift3d, &im, &kp)) goto demo_quit; // Write the keypoints to a file if (write_Keypoint_store(keys_path, &kp)) goto demo_quit; printf("Keypoints written to %s. \n", keys_path); // Extract descriptors if (SIFT3D_extract_descriptors(&sift3d, &kp, &desc)) goto demo_quit; // Write the descriptors to a file if (write_SIFT3D_Descriptor_store(desc_path, &desc)) goto demo_quit; printf("Descriptors written to %s. \n", desc_path); // Convert the keypoints to a matrix if (Keypoint_store_to_Mat_rm(&kp, &keys)) goto demo_quit; // Draw the keypoints if (draw_points(&keys, im.dims, 1, &draw)) goto demo_quit; // Write the drawn keypoints to a file if (im_write(draw_path, &draw)) goto demo_quit; printf("Keypoints drawn in %s. \n", draw_path); // Clean up im_free(&im); im_free(&draw); cleanup_Mat_rm(&keys); cleanup_SIFT3D(&sift3d); cleanup_Keypoint_store(&kp); cleanup_SIFT3D_Descriptor_store(&desc); return 0; demo_quit: // Clean up and return an error im_free(&im); im_free(&draw); cleanup_Mat_rm(&keys); cleanup_SIFT3D(&sift3d); cleanup_Keypoint_store(&kp); cleanup_SIFT3D_Descriptor_store(&desc); return 1; }
/* Wrapper for SIFT3D_extract_descriptors. */ int mex_SIFT3D_extract_descriptors(const Keypoint_store *const kp, SIFT3D_Descriptor_store *const desc) { return SIFT3D_extract_descriptors(®.sift3d, kp, desc); }
/* CLI for 3D SIFT */ int main(int argc, char *argv[]) { Image im; SIFT3D sift3d; Keypoint_store kp; SIFT3D_Descriptor_store desc; char *im_path, *keys_path, *desc_path, *draw_path; int c, num_args; const struct option longopts[] = { {"keys", required_argument, NULL, KEYS}, {"desc", required_argument, NULL, DESC}, {"draw", required_argument, NULL, DRAW}, {0, 0, 0, 0} }; // Parse the GNU standard options switch (parse_gnu(argc, argv)) { case SIFT3D_HELP: puts(help_msg); print_opts_SIFT3D(); return 0; case SIFT3D_VERSION: return 0; case SIFT3D_FALSE: break; default: err_msgu("Unexpected return from parse_gnu \n"); return 1; } // Initialize the SIFT data if (init_SIFT3D(&sift3d)) { err_msgu("Failed to initialize SIFT data."); return 1; } // Parse the SIFT3D options and increment the argument list if ((argc = parse_args_SIFT3D(&sift3d, argc, argv, SIFT3D_FALSE)) < 0) return 1; // Parse the kpSift3d options opterr = 1; keys_path = desc_path = draw_path = NULL; while ((c = getopt_long(argc, argv, "", longopts, NULL)) != -1) { switch (c) { case KEYS: keys_path = optarg; break; case DESC: desc_path = optarg; break; case DRAW: draw_path = optarg; break; case '?': default: return 1; } } // Ensure we have at least one output if (keys_path == NULL && desc_path == NULL && draw_path == NULL) { err_msg("No outputs specified."); return 1; } // Parse the required arguments num_args = argc - optind; if (num_args < 1) { err_msg("Not enough arguments."); return 1; } else if (num_args > 1) { err_msg("Too many arguments."); return 1; } im_path = argv[optind]; // Initialize data init_Keypoint_store(&kp); init_SIFT3D_Descriptor_store(&desc); init_im(&im); // Read the image if (im_read(im_path, &im)) { err_msg("Could not read image."); return 1; } // Extract keypoints if (SIFT3D_detect_keypoints(&sift3d, &im, &kp)) { err_msgu("Failed to detect keypoints."); return 1; } // Optionally write the keypoints if (keys_path != NULL && write_Keypoint_store(keys_path, &kp)) { char msg[BUF_SIZE]; snprintf(msg, BUF_SIZE, "Failed to write the keypoints to " "\"%s\"", keys_path); err_msg(msg); return 1; } // Optionally extract descriptors if (desc_path != NULL) { // Extract descriptors if (SIFT3D_extract_descriptors(&sift3d, &kp,&desc)) { err_msgu("Failed to extract descriptors."); return 1; } // Write the descriptors if (write_SIFT3D_Descriptor_store(desc_path, &desc)) { char msg[BUF_SIZE]; snprintf(msg, BUF_SIZE, "Failed to write the " "descriptors to \"%s\"", desc_path); err_msg(msg); return 1; } } // Optionally draw the keypoints if (draw_path != NULL) { Image draw; Mat_rm keys; // Initialize intermediates init_im(&draw); if (init_Mat_rm(&keys, 0, 0, DOUBLE, SIFT3D_FALSE)) err_msgu("Failed to initialize keys matrix"); // Convert to matrices if (Keypoint_store_to_Mat_rm(&kp, &keys)) { err_msgu("Failed to convert the keypoints to " "a matrix."); return 1; } // Draw the points if (draw_points(&keys, SIFT3D_IM_GET_DIMS(&im), 1, &draw)) { err_msgu("Failed to draw the points."); return 1; } // Write the output if (im_write(draw_path, &draw)) { char msg[BUF_SIZE]; snprintf(msg, BUF_SIZE, "Failed to draw the keypoints " "to \"%s\"", draw_path); err_msg(msg); return 1; } // Clean up im_free(&draw); } return 0; }