<commit_before>/* siftreg.c
 * ----------------------------------------------------------------
 * Rice MRI Team
 * ----------------------------------------------------------------
 * Complete registration pipeline with synthetically generated 
 * source images.
 * ----------------------------------------------------------------
 * Created: <NAME> 2/17/2014
 * Last updated: <NAME> 11/18/2014
 */

#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <unistd.h>
#include "types.h"
#include "sift.h"
#include "macros.h"
#include "imutil.h"
#include "math.h"

/* Parameters */
#define NN_THRESH_DEFAULT 0.8
#define MIN_INLIERS 0.001
#define ERR_THRESH 5.0
#define NUM_ITER 500

/* Debugging file paths */
#define TFORM_IN_PATH DEBUG_ROOT "affine_input.m"
#define TFORM_REG_PATH DEBUG_ROOT "affine_inferred.m"
#define IM_OUT_PATH DEBUG_ROOT "registered.nii"
#define GRID_PATH DEBUG_ROOT "grid.nii"
#define KP_SRC_IM_PATH DEBUG_ROOT "kp_src.nii"
#define KP_REF_IM_PATH DEBUG_ROOT "kp_ref.nii"
#define BG_PATH DEBUG_ROOT "background.nii"
#define OVERLAY_PATH DEBUG_ROOT "overlay.nii"

int main(int argc, char *argv[]) {

	Image src, ref, srcp, refp, srcp_reg;
	SIFT3D_Detector detector;
	SIFT3D_Extractor extractor;
	Keypoint_store kp_src, kp_ref;
	SIFT3D_Descriptor_store desc_src, desc_ref;
	Mat_rm match_src, match_ref, A;
	Ransac ran;
	Affine aff_syn, aff_reg;
	int *matches;
	char *ref_path, *src_path;
	double nn_thresh;
	struct timespec reg_start, reg_end, diff;
	long elapsed_ms;
	int i, c, syn_mode, nx, ny, nz;

	// Default parameters
	nn_thresh = NN_THRESH_DEFAULT;
	syn_mode = 0;

	// Initialize the SIFT detector
	if (init_SIFT3D_Detector(&detector))
		err_exit("init sift detector\n");

	// Initialize the SIFT descriptor extractor 
	if (init_SIFT3D_Extractor(&extractor))
		err_exit("init sift extractor\n");

        // Parse the SIFT3D options and increment the argument list
        parse_args_SIFT3D_Detector(&detector, argc, argv, &optind, 0);

	// Parse the options	
	while ((c = getopt(argc, argv, "+sn:")) != -1)
		switch (c) {
		case 'n':
			nn_thresh = atof(optarg);
			break;
		case 's':
			syn_mode = 1;
			break;
	        case '?':
			if (optopt == 'n')
			  fprintf (stderr, "Option -%c requires an argument.\n", optopt);
			else if (isprint(optopt))
			  fprintf (stderr, "Unknown option `-%c'.\n", optopt);
			else
			  fprintf (stderr,
				   "Unknown option character `\\x%x'.\n",
				   optopt);
			return 1;
                        break;
	      	default:
			return 1;
	}

	// Parse the required arguments
	switch (argc - optind)  {
	case 1:
		puts("Registering synthetic data \n"); 
		ref_path = argv[optind];
		src_path = NULL;
		syn_mode = 1;
		break;
	case 2:
		puts("Registering real data \n"); 
		ref_path = argv[optind];
		src_path = argv[optind + 1];
		break;
      	default:
		puts("Usage: siftreg [source image] [reference image] \n" 
			 "Only use im1 for synthetic \n");
		return 1;
	}
	printf("nn_thresh: %f \n syn_mode: %d \n", nn_thresh, syn_mode);

	// Initialize data
	matches = NULL;
	init_Keypoint_store(&kp_src); 
	init_Keypoint_store(&kp_ref);
	init_SIFT3D_Descriptor_store(&desc_src); 
	init_SIFT3D_Descriptor_store(&desc_ref);
	init_im(&src);
	init_im(&srcp);
	init_im(&ref);
	init_im(&refp);
	init_im(&srcp_reg);
	init_Affine(&aff_syn, 3);
	init_Affine(&aff_reg, 3);
	init_Ransac(&ran);
	if (init_Mat_rm(&match_src, 0, 0, DOUBLE, FALSE) ||
		init_Mat_rm(&match_ref, 0, 0, DOUBLE, FALSE) ||
		init_Mat_rm(&A, 3, 4, DOUBLE, TRUE))
		err_exit("init data");

        // Set the RANSAC parameters
        set_min_inliers_Ransac(&ran, MIN_INLIERS);
        set_err_thresh_Ransac(&ran, ERR_THRESH);
        set_num_iter_Ransac(&ran, NUM_ITER);

	// Load the reference image
	if (read_nii(ref_path, &ref))
		err_exit("load ref image");

	if (syn_mode) {

		// Temporary source image
		Image src_temp;	
		init_im(&src_temp);
		if (src_path == NULL) {
			// Copy from the reference
			if (im_copy_data(&ref, &src_temp))
				err_exit("copy reference image\n");
		} else {
			// Load the specified source
			if (read_nii(src_path, &src_temp))
				err_exit("load source image\n");
		}

		// Apply an affine transformation to the reference image
		double ang_deg = 10.0;
		double ang_rad = ang_deg * UTIL_PI / 180.0;
		MAT_RM_GET(&A, 0, 0, double) = cos(ang_rad);
		MAT_RM_GET(&A, 0, 1, double) = -sin(ang_rad);
		MAT_RM_GET(&A, 1, 0, double) = sin(ang_rad);
		MAT_RM_GET(&A, 1, 1, double) = cos(ang_rad);
		MAT_RM_GET(&A, 2, 2, double) = 1.0;
		if (Affine_set_mat(&A, &aff_syn))
			err_exit("set affine matrix\n");<commit_msg>recenter in the test transform<commit_after>/* siftreg.c
 * ----------------------------------------------------------------
 * Rice MRI Team
 * ----------------------------------------------------------------
 * Complete registration pipeline with synthetically generated 
 * source images.
 * ----------------------------------------------------------------
 * Created: <NAME> 2/17/2014
 * Last updated: <NAME> 11/18/2014
 */

#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <unistd.h>
#include "types.h"
#include "sift.h"
#include "macros.h"
#include "imutil.h"
#include "math.h"

/* Parameters */
#define NN_THRESH_DEFAULT 0.8
#define MIN_INLIERS 0.001
#define ERR_THRESH 5.0
#define NUM_ITER 500

/* Debugging file paths */
#define TFORM_IN_PATH DEBUG_ROOT "affine_input.m"
#define TFORM_REG_PATH DEBUG_ROOT "affine_inferred.m"
#define IM_OUT_PATH DEBUG_ROOT "registered.nii"
#define GRID_PATH DEBUG_ROOT "grid.nii"
#define KP_SRC_IM_PATH DEBUG_ROOT "kp_src.nii"
#define KP_REF_IM_PATH DEBUG_ROOT "kp_ref.nii"
#define BG_PATH DEBUG_ROOT "background.nii"
#define OVERLAY_PATH DEBUG_ROOT "overlay.nii"

int main(int argc, char *argv[]) {

	Image src, ref, srcp, refp, srcp_reg;
	SIFT3D_Detector detector;
	SIFT3D_Extractor extractor;
	Keypoint_store kp_src, kp_ref;
	SIFT3D_Descriptor_store desc_src, desc_ref;
	Mat_rm match_src, match_ref, A;
	Ransac ran;
	Affine aff_syn, aff_reg;
	int *matches;
	char *ref_path, *src_path;
	double nn_thresh;
	struct timespec reg_start, reg_end, diff;
	long elapsed_ms;
	int i, c, syn_mode, nx, ny, nz;

	// Default parameters
	nn_thresh = NN_THRESH_DEFAULT;
	syn_mode = 0;

	// Initialize the SIFT detector
	if (init_SIFT3D_Detector(&detector))
		err_exit("init sift detector\n");

	// Initialize the SIFT descriptor extractor 
	if (init_SIFT3D_Extractor(&extractor))
		err_exit("init sift extractor\n");

        // Parse the SIFT3D options and increment the argument list
        parse_args_SIFT3D_Detector(&detector, argc, argv, &optind, 0);

	// Parse the options	
	while ((c = getopt(argc, argv, "+sn:")) != -1)
		switch (c) {
		case 'n':
			nn_thresh = atof(optarg);
			break;
		case 's':
			syn_mode = 1;
			break;
	        case '?':
			if (optopt == 'n')
			  fprintf (stderr, "Option -%c requires an argument.\n", optopt);
			else if (isprint(optopt))
			  fprintf (stderr, "Unknown option `-%c'.\n", optopt);
			else
			  fprintf (stderr,
				   "Unknown option character `\\x%x'.\n",
				   optopt);
			return 1;
                        break;
	      	default:
			return 1;
	}

	// Parse the required arguments
	switch (argc - optind)  {
	case 1:
		puts("Registering synthetic data \n"); 
		ref_path = argv[optind];
		src_path = NULL;
		syn_mode = 1;
		break;
	case 2:
		puts("Registering real data \n"); 
		ref_path = argv[optind];
		src_path = argv[optind + 1];
		break;
      	default:
		puts("Usage: siftreg [source image] [reference image] \n" 
			 "Only use im1 for synthetic \n");
		return 1;
	}
	printf("nn_thresh: %f \n syn_mode: %d \n", nn_thresh, syn_mode);

	// Initialize data
	matches = NULL;
	init_Keypoint_store(&kp_src); 
	init_Keypoint_store(&kp_ref);
	init_SIFT3D_Descriptor_store(&desc_src); 
	init_SIFT3D_Descriptor_store(&desc_ref);
	init_im(&src);
	init_im(&srcp);
	init_im(&ref);
	init_im(&refp);
	init_im(&srcp_reg);
	init_Affine(&aff_syn, 3);
	init_Affine(&aff_reg, 3);
	init_Ransac(&ran);
	if (init_Mat_rm(&match_src, 0, 0, DOUBLE, FALSE) ||
		init_Mat_rm(&match_ref, 0, 0, DOUBLE, FALSE) ||
		init_Mat_rm(&A, 3, 4, DOUBLE, TRUE))
		err_exit("init data");

        // Set the RANSAC parameters
        set_min_inliers_Ransac(&ran, MIN_INLIERS);
        set_err_thresh_Ransac(&ran, ERR_THRESH);
        set_num_iter_Ransac(&ran, NUM_ITER);

	// Load the reference image
	if (read_nii(ref_path, &ref))
		err_exit("load ref image");

	if (syn_mode) {

		// Temporary source image
		Image src_temp;	
		init_im(&src_temp);
		if (src_path == NULL) {
			// Copy from the reference
			if (im_copy_data(&ref, &src_temp))
				err_exit("copy reference image\n");
		} else {
			// Load the specified source
			if (read_nii(src_path, &src_temp))
				err_exit("load source image\n");
		}

                // Make a rotation matrix
		double ang_deg = 10.0;
		double ang_rad = ang_deg * UTIL_PI / 180.0;
		MAT_RM_GET(&A, 0, 0, double) = cos(ang_rad);
		MAT_RM_GET(&A, 0, 1, double) = -sin(ang_rad);
		MAT_RM_GET(&A, 1, 0, double) = sin(ang_rad);
		MAT_RM_GET(&A, 1, 1, double) = cos(ang_rad);
		MAT_RM_GET(&A, 2, 2, double) = 1.0;
		if (Affine_set_mat(&A, &aff_syn))
			err_exit("set rotation matrix\n");

                // Rotate the center 
                const double xc = ((double) src_temp.nx - 1.0) / 2.0;
                const double yc = ((double) src_temp.ny - 1.0) / 2.0;
                const double zc = ((double) src_temp.nz - 1.0) / 2.0;
                double xtrans, ytrans, ztrans;
                if (apply_tform_xyz(xc, yc, zc, &xtrans, &ytrans, &ztrans, 
                                        AFFINE, &aff_syn))
                        err_exit("get center translation");

                // Apply the center translation and update the affine transformation
                MAT_RM_GET(&A, 0, 3, double) = xtrans - xc;
                MAT_RM_GET(&A, 1, 3, double) = ytrans - yc;
                MAT_RM_GET(&A, 2, 3, double) = ztrans - zc;
		if (Affine_set_mat(&A, &aff_syn))
			err_exit("set affine matrix\n");

		// Apply an affine transformation to the reference image
{'fraction_non_alphanumeric': 0.16585462861688377, 'fraction_numerical': 0.016505916277219847, 'mean_word_length': 2.9060194174757283, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3044086', 'n_tokens_mistral': 4105, 'n_tokens_neox': 3700, 'n_words': 926}