int main(int argc, char *argv[]) { char **av ; int ac, nargs, n ; MRI *mri_src, *mri_dst = NULL, *mri_bias, *mri_orig, *mri_aseg = NULL ; char *in_fname, *out_fname ; int msec, minutes, seconds ; struct timeb start ; char cmdline[CMD_LINE_LEN] ; make_cmd_version_string (argc, argv, "$Id: mri_normalize.c,v 1.80 2012/10/16 21:38:35 nicks Exp $", "$Name: $", cmdline); /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mri_normalize.c,v 1.80 2012/10/16 21:38:35 nicks Exp $", "$Name: $"); if (nargs && argc - nargs == 1) { exit (0); } argc -= nargs; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; mni.max_gradient = MAX_GRADIENT ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc < 3) { usage_exit(0) ; } if (argc < 1) { ErrorExit(ERROR_BADPARM, "%s: no input name specified", Progname) ; } in_fname = argv[1] ; if (argc < 2) { ErrorExit(ERROR_BADPARM, "%s: no output name specified", Progname) ; } out_fname = argv[2] ; if(verbose) { printf( "reading from %s...\n", in_fname) ; } mri_src = MRIread(in_fname) ; if (!mri_src) ErrorExit(ERROR_NO_FILE, "%s: could not open source file %s", Progname, in_fname) ; MRIaddCommandLine(mri_src, cmdline) ; if(nsurfs > 0) { MRI_SURFACE *mris ; MRI *mri_dist=NULL, *mri_dist_sup=NULL, *mri_ctrl, *mri_dist_one ; LTA *lta= NULL ; int i ; TRANSFORM *surface_xform ; if (control_point_fname) // do one pass with only file control points first { MRI3dUseFileControlPoints(mri_src, control_point_fname) ; mri_dst = MRI3dGentleNormalize(mri_src, NULL, DEFAULT_DESIRED_WHITE_MATTER_VALUE, NULL, intensity_above, intensity_below/2,1, bias_sigma, mri_not_control); } else { mri_dst = MRIcopy(mri_src, NULL) ; } for (i = 0 ; i < nsurfs ; i++) { mris = MRISread(surface_fnames[i]) ; if (mris == NULL) ErrorExit(ERROR_NOFILE,"%s: could not surface %s", Progname,surface_fnames[i]); surface_xform = surface_xforms[i] ; TransformInvert(surface_xform, NULL) ; if (surface_xform->type == MNI_TRANSFORM_TYPE || surface_xform->type == TRANSFORM_ARRAY_TYPE || surface_xform->type == REGISTER_DAT) { lta = (LTA *)(surface_xform->xform) ; #if 0 if (invert) { VOL_GEOM vgtmp; LT *lt; MATRIX *m_tmp = lta->xforms[0].m_L ; lta->xforms[0].m_L = MatrixInverse(lta->xforms[0].m_L, NULL) ; MatrixFree(&m_tmp) ; lt = <a->xforms[0]; if (lt->dst.valid == 0 || lt->src.valid == 0) { printf( "WARNING:***************************************************************\n"); printf( "WARNING:dst volume infor is invalid. Most likely produce wrong inverse.\n"); printf( "WARNING:***************************************************************\n"); } copyVolGeom(<->dst, &vgtmp); copyVolGeom(<->src, <->dst); copyVolGeom(&vgtmp, <->src); } #endif } if (stricmp(surface_xform_fnames[i], "identity.nofile") != 0) { MRIStransform(mris, NULL, surface_xform, NULL) ; } mri_dist_one = MRIcloneDifferentType(mri_dst, MRI_FLOAT) ; printf("computing distance transform\n") ; MRIScomputeDistanceToSurface(mris, mri_dist_one, mri_dist_one->xsize) ; if (i == 0) { mri_dist = MRIcopy(mri_dist_one, NULL) ; } else { MRIcombineDistanceTransforms(mri_dist_one, mri_dist, mri_dist) ; } // MRIminAbs(mri_dist_one, mri_dist, mri_dist) ; MRIfree(&mri_dist_one) ; } MRIscalarMul(mri_dist, mri_dist, -1) ; if (nonmax_suppress) { printf("computing nonmaximum suppression\n") ; mri_dist_sup = MRInonMaxSuppress(mri_dist, NULL, 0, 1) ; mri_ctrl = MRIcloneDifferentType(mri_dist_sup, MRI_UCHAR) ; MRIbinarize(mri_dist_sup, mri_ctrl, min_dist, CONTROL_NONE, CONTROL_MARKED) ; } else if (erode) { int i ; mri_ctrl = MRIcloneDifferentType(mri_dist, MRI_UCHAR) ; MRIbinarize(mri_dist, mri_ctrl, min_dist, CONTROL_NONE, CONTROL_MARKED) ; for (i = 0 ; i < erode ; i++) { MRIerode(mri_ctrl, mri_ctrl) ; } } else { mri_ctrl = MRIcloneDifferentType(mri_dist, MRI_UCHAR) ; MRIbinarize(mri_dist, mri_ctrl, min_dist, CONTROL_NONE, CONTROL_MARKED) ; } if (control_point_fname) { MRInormAddFileControlPoints(mri_ctrl, CONTROL_MARKED) ; } if (mask_sigma > 0) { MRI *mri_smooth, *mri_mag, *mri_grad ; mri_smooth = MRIgaussianSmooth(mri_dst, mask_sigma, 1, NULL) ; mri_mag = MRIcloneDifferentType(mri_dst, MRI_FLOAT) ; mri_grad = MRIsobel(mri_smooth, NULL, mri_mag) ; MRIbinarize(mri_mag, mri_mag, mask_thresh, 1, 0) ; MRImask(mri_ctrl, mri_mag, mri_ctrl, 0, CONTROL_NONE) ; MRIfree(&mri_grad) ; MRIfree(&mri_mag) ; MRIfree(&mri_smooth) ; } if (mask_orig_fname) { MRI *mri_orig ; mri_orig = MRIread(mask_orig_fname) ; MRIbinarize(mri_orig, mri_orig, mask_orig_thresh, 0, 1) ; MRImask(mri_ctrl, mri_orig, mri_ctrl, 0, CONTROL_NONE) ; MRIfree(&mri_orig) ; } if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { MRIwrite(mri_dist, "d.mgz"); MRIwrite(mri_dist_sup, "dm.mgz"); MRIwrite(mri_ctrl, "c.mgz"); } MRIeraseBorderPlanes(mri_ctrl, 4) ; if (aseg_fname) { mri_aseg = MRIread(aseg_fname) ; if (mri_aseg == NULL) { ErrorExit(ERROR_NOFILE, "%s: could not load aseg from %s", Progname, aseg_fname) ; } remove_nonwm_voxels(mri_ctrl, mri_aseg, mri_ctrl) ; MRIfree(&mri_aseg) ; } else { remove_surface_outliers(mri_ctrl, mri_dist, mri_dst, mri_ctrl) ; } mri_bias = MRIbuildBiasImage(mri_dst, mri_ctrl, NULL, 0.0) ; if (mri_dist) { MRIfree(&mri_dist) ; } if (mri_dist_sup) { MRIfree(&mri_dist_sup) ; } if (bias_sigma> 0) { MRI *mri_kernel = MRIgaussian1d(bias_sigma, -1) ; if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { MRIwrite(mri_bias, "b.mgz") ; } printf("smoothing bias field\n") ; MRIconvolveGaussian(mri_bias, mri_bias, mri_kernel) ; if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { MRIwrite(mri_bias, "bs.mgz") ; } MRIfree(&mri_kernel); } MRIfree(&mri_ctrl) ; mri_dst = MRIapplyBiasCorrectionSameGeometry (mri_dst, mri_bias, mri_dst, DEFAULT_DESIRED_WHITE_MATTER_VALUE) ; printf("writing normalized volume to %s\n", out_fname) ; MRIwrite(mri_dst, out_fname) ; exit(0) ; } // end if(surface_fname) if (!mriConformed(mri_src) && conform > 0) { printf("unconformed source detected - conforming...\n") ; mri_src = MRIconform(mri_src) ; } if (mask_fname) { MRI *mri_mask ; mri_mask = MRIread(mask_fname) ; if (!mri_mask) ErrorExit(ERROR_NOFILE, "%s: could not open mask volume %s.\n", Progname, mask_fname) ; MRImask(mri_src, mri_mask, mri_src, 0, 0) ; MRIfree(&mri_mask) ; } if (read_flag) { MRI *mri_ctrl ; double scale ; mri_bias = MRIread(bias_volume_fname) ; if (!mri_bias) ErrorExit (ERROR_BADPARM, "%s: could not read bias volume %s", Progname, bias_volume_fname) ; mri_ctrl = MRIread(control_volume_fname) ; if (!mri_ctrl) ErrorExit (ERROR_BADPARM, "%s: could not read control volume %s", Progname, control_volume_fname) ; MRIbinarize(mri_ctrl, mri_ctrl, 1, 0, 128) ; mri_dst = MRImultiply(mri_bias, mri_src, NULL) ; scale = MRImeanInLabel(mri_dst, mri_ctrl, 128) ; printf("mean in wm is %2.0f, scaling by %2.2f\n", scale, 110/scale) ; scale = 110/scale ; MRIscalarMul(mri_dst, mri_dst, scale) ; MRIwrite(mri_dst, out_fname) ; exit(0) ; } if(long_flag) { MRI *mri_ctrl ; double scale ; mri_bias = MRIread(long_bias_volume_fname) ; if (!mri_bias) ErrorExit (ERROR_BADPARM, "%s: could not read bias volume %s", Progname, long_bias_volume_fname) ; mri_ctrl = MRIread(long_control_volume_fname) ; if (!mri_ctrl) ErrorExit (ERROR_BADPARM, "%s: could not read control volume %s", Progname, long_control_volume_fname) ; MRIbinarize(mri_ctrl, mri_ctrl, 1, 0, CONTROL_MARKED) ; if (mri_ctrl->type != MRI_UCHAR) { MRI *mri_tmp ; mri_tmp = MRIchangeType(mri_ctrl, MRI_UCHAR, 0, 1,1); MRIfree(&mri_ctrl) ; mri_ctrl = mri_tmp ; } scale = MRImeanInLabel(mri_src, mri_ctrl, CONTROL_MARKED) ; printf("mean in wm is %2.0f, scaling by %2.2f\n", scale, 110/scale) ; scale = DEFAULT_DESIRED_WHITE_MATTER_VALUE/scale ; mri_dst = MRIscalarMul(mri_src, NULL, scale) ; MRIremoveWMOutliers(mri_dst, mri_ctrl, mri_ctrl, intensity_below/2) ; mri_bias = MRIbuildBiasImage(mri_dst, mri_ctrl, NULL, 0.0) ; MRIsoapBubble(mri_bias, mri_ctrl, mri_bias, 50, 1) ; MRIapplyBiasCorrectionSameGeometry(mri_dst, mri_bias, mri_dst, DEFAULT_DESIRED_WHITE_MATTER_VALUE); // MRIwrite(mri_dst, out_fname) ; // exit(0) ; } // end if(long_flag) if (grad_thresh > 0) { float thresh ; MRI *mri_mag, *mri_grad, *mri_smooth ; MRI *mri_kernel = MRIgaussian1d(.5, -1) ; mri_not_control = MRIcloneDifferentType(mri_src, MRI_UCHAR) ; switch (scan_type) { case MRI_MGH_MPRAGE: thresh = 15 ; break ; case MRI_WASHU_MPRAGE: thresh = 20 ; break ; case MRI_UNKNOWN: default: thresh = 12 ; break ; } mri_smooth = MRIconvolveGaussian(mri_src, NULL, mri_kernel) ; thresh = grad_thresh ; mri_mag = MRIcloneDifferentType(mri_src, MRI_FLOAT) ; mri_grad = MRIsobel(mri_smooth, NULL, mri_mag) ; MRIwrite(mri_mag, "m.mgz") ; MRIbinarize(mri_mag, mri_not_control, thresh, 0, 1) ; MRIwrite(mri_not_control, "nc.mgz") ; MRIfree(&mri_mag) ; MRIfree(&mri_grad) ; MRIfree(&mri_smooth) ; MRIfree(&mri_kernel) ; } #if 0 #if 0 if ((mri_src->type != MRI_UCHAR) || (!(mri_src->xsize == 1 && mri_src->ysize == 1 && mri_src->zsize == 1))) #else if (conform || (mri_src->type != MRI_UCHAR && conform > 0)) #endif { MRI *mri_tmp ; fprintf (stderr, "downsampling to 8 bits and scaling to isotropic voxels...\n") ; mri_tmp = MRIconform(mri_src) ; mri_src = mri_tmp ; } #endif if(aseg_fname) { printf("Reading aseg %s\n",aseg_fname); mri_aseg = MRIread(aseg_fname) ; if (mri_aseg == NULL) ErrorExit (ERROR_NOFILE, "%s: could not read aseg from file %s", Progname, aseg_fname) ; if (!mriConformed(mri_aseg)) { ErrorExit(ERROR_UNSUPPORTED, "%s: aseg volume %s must be conformed", Progname, aseg_fname) ; } } else { mri_aseg = NULL ; } if(verbose) { printf( "normalizing image...\n") ; } fflush(stdout); fflush(stderr); TimerStart(&start) ; if (control_point_fname) { MRI3dUseFileControlPoints(mri_src, control_point_fname) ; } // this just setup writing control-point volume saving if(control_volume_fname) { MRI3dWriteControlPoints(control_volume_fname) ; } /* first do a gentle normalization to get things in the right intensity range */ if(long_flag == 0) // if long, then this will already have been done with base control points { if(control_point_fname != NULL) /* do one pass with only file control points first */ mri_dst = MRI3dGentleNormalize(mri_src, NULL, DEFAULT_DESIRED_WHITE_MATTER_VALUE, NULL, intensity_above, intensity_below/2,1, bias_sigma, mri_not_control); else { mri_dst = MRIcopy(mri_src, NULL) ; } } fflush(stdout); fflush(stderr); if(mri_aseg) { MRI *mri_ctrl, *mri_bias ; int i ; printf("processing with aseg\n"); mri_ctrl = MRIclone(mri_aseg, NULL) ; for (i = 0 ; i < NWM_LABELS ; i++) { MRIcopyLabel(mri_aseg, mri_ctrl, aseg_wm_labels[i]) ; } printf("removing outliers in the aseg WM...\n") ; MRIremoveWMOutliersAndRetainMedialSurface(mri_dst, mri_ctrl, mri_ctrl, intensity_below) ; MRIbinarize(mri_ctrl, mri_ctrl, 1, CONTROL_NONE, CONTROL_MARKED) ; MRInormAddFileControlPoints(mri_ctrl, CONTROL_MARKED) ; if (interior_fname1) { MRIS *mris_interior1, *mris_interior2 ; mris_interior1 = MRISread(interior_fname1) ; if (mris_interior1 == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read white matter surface from %s\n", Progname, interior_fname1) ; mris_interior2 = MRISread(interior_fname2) ; if (mris_interior2 == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read white matter surface from %s\n", Progname, interior_fname2) ; add_interior_points(mri_ctrl, mri_dst, intensity_above, 1.25*intensity_below, mris_interior1, mris_interior2, mri_aseg, mri_ctrl) ; MRISfree(&mris_interior1) ; MRISfree(&mris_interior2) ; } if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { MRIwrite(mri_ctrl, "norm_ctrl.mgz") ; } printf("Building bias image\n"); fflush(stdout); fflush(stderr); mri_bias = MRIbuildBiasImage(mri_dst, mri_ctrl, NULL, 0.0) ; fflush(stdout); fflush(stderr); if (bias_sigma> 0) { printf("Smoothing with sigma %g\n",bias_sigma); MRI *mri_kernel = MRIgaussian1d(bias_sigma, -1) ; MRIconvolveGaussian(mri_bias, mri_bias, mri_kernel) ; MRIfree(&mri_kernel); fflush(stdout); fflush(stderr); } MRIfree(&mri_ctrl) ; MRIfree(&mri_aseg) ; printf("Applying bias correction\n"); mri_dst = MRIapplyBiasCorrectionSameGeometry (mri_dst, mri_bias, mri_dst, DEFAULT_DESIRED_WHITE_MATTER_VALUE) ; if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { MRIwrite(mri_dst, "norm_1.mgz") ; } fflush(stdout); fflush(stderr); } // if(mri_aseg) else { printf("processing without aseg, no1d=%d\n",no1d); if (!no1d) { printf("MRInormInit(): \n"); MRInormInit(mri_src, &mni, 0, 0, 0, 0, 0.0f) ; printf("MRInormalize(): \n"); mri_dst = MRInormalize(mri_src, NULL, &mni) ; if (!mri_dst) { no1d = 1 ; printf("1d normalization failed - trying no1d...\n") ; // ErrorExit(ERROR_BADPARM, "%s: normalization failed", Progname) ; } } if(no1d) { if ((file_only && nosnr) || ((gentle_flag != 0) && (control_point_fname != NULL))) { if (mri_dst == NULL) { mri_dst = MRIcopy(mri_src, NULL) ; } } else { if (nosnr) { if (interior_fname1) { MRIS *mris_interior1, *mris_interior2 ; MRI *mri_ctrl ; printf("computing initial normalization using surface interiors\n"); mri_ctrl = MRIcloneDifferentType(mri_src, MRI_UCHAR) ; mris_interior1 = MRISread(interior_fname1) ; if (mris_interior1 == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read white matter surface from %s\n", Progname, interior_fname1) ; mris_interior2 = MRISread(interior_fname2) ; if (mris_interior2 == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read white matter surface from %s\n", Progname, interior_fname2) ; add_interior_points(mri_ctrl, mri_dst, intensity_above, 1.25*intensity_below, mris_interior1, mris_interior2, mri_aseg, mri_ctrl) ; MRISfree(&mris_interior1) ; MRISfree(&mris_interior2) ; mri_bias = MRIbuildBiasImage(mri_dst, mri_ctrl, NULL, 0.0) ; if (bias_sigma> 0) { MRI *mri_kernel = MRIgaussian1d(bias_sigma, -1) ; MRIconvolveGaussian(mri_bias, mri_bias, mri_kernel) ; MRIfree(&mri_kernel); } mri_dst = MRIapplyBiasCorrectionSameGeometry (mri_src, mri_bias, mri_dst, DEFAULT_DESIRED_WHITE_MATTER_VALUE) ; MRIfree(&mri_ctrl) ; } else if (long_flag == 0) // no initial normalization specified { mri_dst = MRIcopy(mri_src, NULL) ; } } else { printf("computing initial normalization using SNR...\n") ; mri_dst = MRInormalizeHighSignalLowStd (mri_src, mri_dst, bias_sigma, DEFAULT_DESIRED_WHITE_MATTER_VALUE) ; } } if (!mri_dst) ErrorExit (ERROR_BADPARM, "%s: could not allocate volume", Progname) ; } } // else (not using aseg) fflush(stdout); fflush(stderr); if (file_only == 0) MRI3dGentleNormalize(mri_dst, NULL, DEFAULT_DESIRED_WHITE_MATTER_VALUE, mri_dst, intensity_above, intensity_below/2, file_only, bias_sigma, mri_not_control); mri_orig = MRIcopy(mri_dst, NULL) ; printf("\n"); printf("Iterating %d times\n",num_3d_iter); for (n = 0 ; n < num_3d_iter ; n++) { if(file_only) { break ; } printf( "---------------------------------\n"); printf( "3d normalization pass %d of %d\n", n+1, num_3d_iter) ; if (gentle_flag) MRI3dGentleNormalize(mri_dst, NULL, DEFAULT_DESIRED_WHITE_MATTER_VALUE, mri_dst, intensity_above/2, intensity_below/2, file_only, bias_sigma, mri_not_control); else MRI3dNormalize(mri_orig, mri_dst, DEFAULT_DESIRED_WHITE_MATTER_VALUE, mri_dst, intensity_above, intensity_below, file_only, prune, bias_sigma, scan_type, mri_not_control); } printf( "Done iterating ---------------------------------\n"); // this just setup writing control-point volume saving if(control_volume_fname) { MRI3dWriteControlPoints(control_volume_fname) ; } if(bias_volume_fname) { mri_bias = compute_bias(mri_src, mri_dst, NULL) ; printf("writing bias field to %s....\n", bias_volume_fname) ; MRIwrite(mri_bias, bias_volume_fname) ; MRIfree(&mri_bias) ; } if (verbose) { printf("writing output to %s\n", out_fname) ; } MRIwrite(mri_dst, out_fname) ; msec = TimerStop(&start) ; MRIfree(&mri_src); MRIfree(&mri_dst); seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; printf( "3D bias adjustment took %d minutes and %d seconds.\n", minutes, seconds) ; exit(0) ; return(0) ; }
int main(int argc, char *argv[]) { char **av; MRI *mri_T1, *mri_tmp, *mri_ctrl, *mri_in, *mri_out; MRI *mri_snr, *mri_bias; MRI *mri_mask1 = NULL; MRI *mri_mask2 = NULL; int ac, nargs; int width, height, depth, x, y, z; int mask1_set = 0; int mask2_set = 0; int i, j, k, cx, cy, cz, count; LTA *lta = 0; int transform_type; double mean, std, value, src, bias, norm; // HISTOGRAM *h; // float bin_size; // int nbins, bin_no; double mean1, std1, mean2, std2, count1, count2, slope, offset; VOL_GEOM vgtmp; LT *lt = NULL; MATRIX *m_tmp = NULL; Progname = argv[0]; nargs = handle_version_option (argc, argv, "$Id: mri_normalize_tp2.c,v 1.8 2011/03/02 00:04:23 nicks Exp $", "$Name: stable5 $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc != 3) usage(1); if (tp1_ctrl_fname == NULL || tp1_T1_fname == NULL) { printf("Use options to specify ctrl volume and T1 volume for tp1\n"); usage(1); } mri_in = MRIread(argv[1]) ; if (!mri_in) ErrorExit(ERROR_BADPARM, "%s: could not read input volume %s", Progname, argv[1]) ; mri_T1 = MRIread(tp1_T1_fname) ; if (!mri_T1) ErrorExit(ERROR_BADPARM, "%s: could not read T1 volume for tp1 %s", Progname, tp1_T1_fname) ; mri_ctrl = MRIread(tp1_ctrl_fname) ; if (!mri_ctrl) ErrorExit(ERROR_BADPARM, "%s: could not read control points volume for tp1 %s", Progname, tp1_ctrl_fname) ; if ((mri_in->width != mri_T1->width) || (mri_in->height != mri_T1->height) || (mri_in->depth != mri_T1->depth) || (mri_in->width != mri_ctrl->width) || (mri_in->height != mri_ctrl->height) || (mri_in->depth != mri_ctrl->depth) ) ErrorExit (ERROR_BADPARM, "%s: three input volumes have different sizes \n", Progname); if (mask1_fname) { mri_mask1 = MRIread(mask1_fname) ; if (!mri_mask1) ErrorExit(ERROR_BADPARM, "%s, could not read mask volume for tp1 %s", Progname, mask1_fname); mask1_set = 1; if ((mri_mask1->width != mri_in->width) || (mri_mask1->height != mri_in->height) || (mri_mask1->depth != mri_in->depth)) ErrorExit (ERROR_BADPARM, "%s: mask volumes have different sizes than other volumes \n", Progname); } if (mask2_fname) { mri_mask2 = MRIread(mask2_fname) ; if (!mri_mask2) ErrorExit (ERROR_BADPARM, "%s, could not read mask volume for tp2 %s", Progname, mask2_fname); mask2_set = 1; if ((mri_mask2->width != mri_T1->width) || (mri_mask2->height != mri_T1->height) || (mri_mask2->depth != mri_T1->depth) ) ErrorExit (ERROR_BADPARM, "%s: mask volumes have different sizes than other volumes \n", Progname); } width = mri_in->width ; height = mri_in->height ; depth = mri_in->depth ; //nbins = 200; //h = HISTOalloc(nbins); mri_out = MRIclone(mri_in, NULL) ; /* Read LTA transform and apply it to mri_ctrl */ if (xform_fname != NULL) { // read transform transform_type = TransformFileNameType(xform_fname); if (transform_type == MNI_TRANSFORM_TYPE || transform_type == TRANSFORM_ARRAY_TYPE || transform_type == REGISTER_DAT || transform_type == FSLREG_TYPE ) { printf("Reading transform ...\n"); lta = LTAreadEx(xform_fname) ; if (!lta) ErrorExit(ERROR_NOFILE, "%s: could not read transform file %s", Progname, xform_fname) ; if (transform_type == FSLREG_TYPE) { if (lta_src == 0 || lta_dst == 0) { fprintf (stderr, "ERROR: fslmat does not have information " "on the src and dst volumes\n"); fprintf (stderr, "ERROR: you must give options '-lta_src' and " "'-lta_dst' to specify the src and dst volume infos\n"); } LTAmodifySrcDstGeom (lta, lta_src, lta_dst); // add src and dst information LTAchangeType(lta, LINEAR_VOX_TO_VOX); //this is necessary } if (lta->xforms[0].src.valid == 0) { if (lta_src == 0) { fprintf (stderr, "The transform does not have the valid src volume info.\n"); fprintf (stderr, "Either you give src volume info by option -lta_src or\n"); fprintf(stderr, "make the transform to have the valid src info.\n"); ErrorExit(ERROR_BAD_PARM, "Bailing out...\n"); } else { LTAmodifySrcDstGeom(lta, lta_src, NULL); // add src information // getVolGeom(lta_src, <->src); } } if (lta->xforms[0].dst.valid == 0) { if (lta_dst == 0) { fprintf (stderr, "The transform does not have the valid dst volume info.\n"); fprintf (stderr, "Either you give src volume info by option -lta_dst or\n"); fprintf (stderr, "make the transform to have the valid dst info.\n"); fprintf (stderr, "If the dst was average_305, then you can set\n"); fprintf (stderr, "environmental variable USE_AVERAGE305 true\n"); fprintf (stderr, "without giving the dst volume for RAS-to-RAS transform.\n"); ErrorExit(ERROR_BAD_PARM, "Bailing out...\n"); } else { LTAmodifySrcDstGeom(lta, NULL, lta_dst); // add dst information } } } else { ErrorExit (ERROR_BADPARM, "transform is not of MNI, nor Register.dat, nor FSLMAT type"); } if (invert) { m_tmp = lta->xforms[0].m_L ; lta->xforms[0].m_L = MatrixInverse(lta->xforms[0].m_L, NULL) ; MatrixFree(&m_tmp) ; lt = <a->xforms[0]; if (lt->dst.valid == 0 || lt->src.valid == 0) { fprintf (stderr, "WARNING:***********************************************\n"); fprintf (stderr, "WARNING: dst volume infor is invalid. " "Most likely produce wrong inverse.\n"); fprintf (stderr, "WARNING:***********************************************\n"); } copyVolGeom(<->dst, &vgtmp); copyVolGeom(<->src, <->dst); copyVolGeom(&vgtmp, <->src); } // LTAchangeType(lta, LINEAR_VOX_TO_VOX); /* apply lta to the ctrl volume */ mri_tmp = MRIalloc(mri_ctrl->width, mri_ctrl->height, mri_ctrl->depth, mri_ctrl->type) ; MRIcopyHeader(mri_in, mri_tmp) ; // this function doesn't do NEAREST at all!! // I found the bug, in LTAtransformInterp() mri_tmp = LTAtransformInterp(mri_ctrl, mri_tmp, lta, SAMPLE_NEAREST); MRIfree(&mri_ctrl); mri_ctrl = mri_tmp; if (mask1_fname != NULL && mask2_fname == NULL) { printf("map mask for tp1 to get mask for tp2 ...\n"); mri_mask2 = MRIalloc(mri_in->width, mri_in->height, mri_in->depth, mri_mask1->type) ; MRIcopyHeader(mri_in, mri_mask2) ; mri_mask2 = LTAtransformInterp(mri_mask1, mri_mask2, lta, SAMPLE_NEAREST); mask2_set = 1; if (debug_flag) MRIwrite(mri_mask2, "mri_mask2.mgz"); } else if (mask2_fname != NULL && mask1_fname == NULL) { printf("map mask for tp2 to get mask for tp1 ...\n"); //need to invert lta first m_tmp = lta->xforms[0].m_L ; lta->xforms[0].m_L = MatrixInverse(lta->xforms[0].m_L, NULL) ; MatrixFree(&m_tmp) ; lt = <a->xforms[0]; copyVolGeom(<->dst, &vgtmp); copyVolGeom(<->src, <->dst); copyVolGeom(&vgtmp, <->src); mri_mask1 = MRIalloc(mri_T1->width, mri_T1->height, mri_T1->depth, mri_mask2->type) ; MRIcopyHeader(mri_T1, mri_mask1) ; mri_mask1 = LTAtransformInterp(mri_mask2, mri_mask1, lta, SAMPLE_NEAREST); mask1_set = 1; if (debug_flag) MRIwrite(mri_mask1, "mri_mask1.mgz"); } if (lta_src) MRIfree(<a_src); if (lta_dst) MRIfree(<a_dst); if (lta) LTAfree(<a); } /* if (xform_fname != NULL) */ if (debug_flag) { // MRIwrite(mri_snr, "snr.mgz"); MRIwrite(mri_ctrl, "ctrl.mgz"); } if (mask1_set == 0) { //create mask1 mri_mask1 = MRIalloc(mri_T1->width, mri_T1->height, mri_T1->depth, MRI_UCHAR) ; for (z=0; z < depth; z++) for (y=0; y< height; y++) for (x=0; x < width; x++) { if (MRIgetVoxVal(mri_T1, x, y, z, 0) < noise_threshold) { MRIvox(mri_mask1,x,y,z) = 0; } else MRIvox(mri_mask1,x,y,z) = 1; } } if (mask2_set == 0) { //create mask2 mri_mask2 = MRIalloc(mri_in->width, mri_in->height, mri_in->depth, MRI_UCHAR) ; for (z=0; z < depth; z++) for (y=0; y< height; y++) for (x=0; x < width; x++) { if (MRIgetVoxVal(mri_in, x, y, z, 0) < noise_threshold) { MRIvox(mri_mask2,x,y,z) = 0; } else MRIvox(mri_mask2,x,y,z) = 1; } } #if 0 /* compute the mean and std of T1 volume */ /* Using only high SNR points */ mri_snr = MRIalloc(mri_T1->width, mri_T1->height, mri_T1->depth, MRI_FLOAT) ; MRIcopyHeader(mri_T1, mri_snr) ; h->bin_size = bin_size = 0.5; for (bin_no = 0; bin_no < nbins; bin_no++) h->bins[bin_no] = (bin_no)*bin_size; for (z=0; z < depth; z++) for (y=0; y< height; y++) for (x=0; x < width; x++) { if (MRIgetVoxVal(mri_T1, x, y, z, 0) < noise_threshold) { MRIFvox(mri_snr,x,y,z) = 0; continue; } mean = 0; std = 0; count = 0; for (i=-1; i<=1; i++) for (j=-1; j<=1; j++) for (k=-1;k<=1;k++) { cx = x+i; cy = y+j, cz = z+k; if (cx < 0 || cx >= width || cy < 0 || cy >= height || cz < 0 || cz >= depth) continue; count++; value = MRIgetVoxVal(mri_T1, cx, cy, cz, 0); mean += value; std += value*value; } mean /= (count + 1e-30); std /= (count + 1e-30); std = std - mean *mean; if (std <= 0) std = 0; value = mean/sqrt(std); MRIFvox(mri_snr,x,y,z) = value; bin_no = nint((float)value/(float)bin_size); if (bin_no >= nbins) bin_no = nbins - 1; h->counts[bin_no]++; } for (num = 0.0f, b = h->nbins - 1; b >= 1; b --) { num += h->counts[b]; if (num > 20000) /* this may make me only use WM points, is it good to use only WM to compute scale of intensity?? */ break; } printf("using SNR threshold %2.3f at bin %d\n", h->bins[b], b); mean1 = 0; std1 = 0; count1 = 0; for (z=0; z < depth; z++) for (y=0; y< height; y++) for (x=0; x < width; x++) { if (MRIgetVoxVal(mri_T1, x, y, z, 0) < noise_threshold) { continue; } value = MRIFvox(mri_snr,x,y,z); if (value < h->bins[b]) continue; value = MRIgetVoxVal(mri_T1, x, y, z, 0); count1++; mean1 += value; std1 += value*value; } MRIfree(&mri_snr); #else printf("compute mean and std of tp1 volume within masked area...\n"); mean1 = 0; std1 = 0; count1 = 0; for (z=0; z < depth; z++) for (y=0; y< height; y++) for (x=0; x < width; x++) { if (MRIgetVoxVal(mri_mask1, x, y, z, 0) <= 1e-30) { continue; } value = MRIgetVoxVal(mri_T1, x, y, z, 0); count1++; mean1 += value; std1 += value*value; } #endif mean1 /= (count1 + 1e-30); std1 /= (count1 + 1e-30); std1 = std1 - mean1*mean1; if (std1 <= 0) printf("warning: negative std for T1 volume. \n"); else printf("mean and variance for tp1 volume are %g and %g\n", mean1, std1); printf("now compute SNR and stats for input volume ... \n"); mri_snr = MRIalloc(mri_in->width, mri_in->height, mri_in->depth, MRI_FLOAT) ; MRIcopyHeader(mri_in, mri_snr) ; //HISTOclear(h,h); //h->bin_size = bin_size = 0.5; //for (bin_no = 0; bin_no < nbins; bin_no++) // h->bins[bin_no] = (bin_no)*bin_size; for (z=0; z < depth; z++) for (y=0; y< height; y++) for (x=0; x < width; x++) { if (MRIgetVoxVal(mri_in, x, y, z, 0) < noise_threshold) { MRIFvox(mri_snr,x,y,z) = 0; continue; } mean = 0; std = 0; count = 0; for (i=-1; i<=1; i++) for (j=-1; j<=1; j++) for (k=-1;k<=1;k++) { cx = x+i; cy = y+j, cz = z+k; if (cx < 0 || cx >= width || cy < 0 || cy >= height || cz < 0 || cz >= depth) continue; count++; value = MRIgetVoxVal(mri_in, cx, cy, cz, 0); mean += value; std += value*value; } mean /= (count + 1e-30); std /= (count + 1e-30); std = std - mean *mean; if (std <= 0) std = 0; value = mean/sqrt(std); MRIFvox(mri_snr,x,y,z) = value; //bin_no = nint((float)value/(float)bin_size); //if (bin_no >= nbins) bin_no = nbins - 1; //h->counts[bin_no]++; } #if 0 for (num = 0.0f, b = h->nbins - 1; b >= 1; b --) { num += h->counts[b]; if (num > 20000) /* this may make me only use WM points, is it good to use only WM to compute scale of intensity?? */ break; } printf("using SNR threshold %2.3f at bin %d\n", h->bins[b], b); mean2 = 0; std2 = 0; count2 = 0; for (z=0; z < depth; z++) for (y=0; y< height; y++) for (x=0; x < width; x++) { if (MRIgetVoxVal(mri_in, x, y, z, 0) < noise_threshold) { continue; } value = MRIFvox(mri_snr,x,y,z); if (value >= h->bins[b]) { count2++; mean2 += value; std2 += value*value; } } #else printf("compute mean and std of tp2 volume within masked area\n"); /* somehow mri_watershed seems to leave some unzero voxels around image border, so I will skip image boundaries no, that's not a problem of most recent mri_watershed; something wrong previously */ mean2 = 0; std2 = 0; count2 = 0; for (z=0; z < depth; z++) for (y=0; y< height; y++) for (x=0; x < width; x++) { if (MRIgetVoxVal(mri_mask2, x, y, z, 0) <= 1e-30) { continue; } value = MRIgetVoxVal(mri_in, x, y, z, 0); count2++; mean2 += value; std2 += value*value; } #endif mean2 /= (count2 + 1e-30); std2 /= (count2 + 1e-30); std2 = std2 - mean2*mean2; if (std2 <= 0) printf("warning: negative std for input volume. \n"); else printf("mean and variance for input tp2 volume are %g and %g\n", mean2, std2); //compute intensity scale slope = sqrt(std1/std2); offset = mean1 - slope*mean2; printf("scale input volume by %g x + %g\n", slope, offset); // first change mri_in to FLOAT type mri_tmp = MRIchangeType(mri_in, MRI_FLOAT, 0, 1.0, 1); MRIfree(&mri_in); mri_in = mri_tmp; for (z=0; z < depth; z++) for (y=0; y< height; y++) for (x=0; x < width; x++) { value = MRIFvox(mri_in, x, y, z); MRIFvox(mri_in, x, y, z) = value*slope + offset; } // printf("compute SNR map of tp2 volume\n"); //already done above // mri_snr = MRIalloc(mri_ctrl->width, // mri_ctrl->height, mri_ctrl->depth, MRI_FLOAT) ; for (z=0; z < depth; z++) for (y=0; y< height; y++) for (x=0; x < width; x++) { if (MRIgetVoxVal(mri_in, x, y, z, 0) < noise_threshold) { // MRIFvox(mri_snr,x,y,z) = 0; continue; } value = MRIFvox(mri_snr,x,y,z); if (value < 20) MRIvox(mri_ctrl, x, y, z) = 0; else if (MRIvox(mri_ctrl, x, y, z) > 0) { MRIvox(mri_ctrl, x, y, z) = 1; } } if (debug_flag) { MRIwrite(mri_snr, "snr.mgz"); // MRIwrite(mri_ctrl, "ctrl.mgz"); } // SNR >= 20 seems a good threshold // Now use ctrl points to normalize tp2 printf("normalize tp2...\n"); mri_bias = MRIbuildBiasImage(mri_in, mri_ctrl, NULL, bias_sigma) ; for (z = 0 ; z < depth ; z++) { for (y = 0 ; y < height ; y++) { for (x = 0 ; x < width ; x++) { src = MRIgetVoxVal(mri_in, x, y, z, 0) ; bias = MRIgetVoxVal(mri_bias, x, y, z, 0) ; if (!bias) /* should never happen */ norm = (float)src ; else norm = (float)src * 110.0 / (float)bias ; if (norm > 255.0f && mri_out->type == MRI_UCHAR) norm = 255.0f ; else if (norm < 0.0f && mri_out->type == MRI_UCHAR) norm = 0.0f ; MRIsetVoxVal(mri_out, x, y, z, 0, norm) ; } } } printf("writing normalized volume to %s...\n", argv[2]) ; MRIwrite(mri_out, argv[2]); MRIfree(&mri_in); MRIfree(&mri_bias); MRIfree(&mri_out); MRIfree(&mri_T1); MRIfree(&mri_ctrl); MRIfree(&mri_snr); //HISTOfree(&h); exit(0); } /* end main() */
int main(int argc, char *argv[]) { char **av, *ltafn1, *ltafn2, *ltafn_total; LTA *lta1, *lta2, *lta_total; FILE *fo; MATRIX *r_to_i_1, *i_to_r_1, *i_to_r_2, *r_to_i_2; MATRIX *RAS_1_to_1, *RAS_2_to_2, *m_tmp; int nargs, ac; int type = 0; Progname = argv[0]; nargs = handle_version_option (argc, argv, "$Id: mri_concatenate_lta.c,v 1.10 2011/03/16 21:23:48 nicks Exp $", "$Name: stable5 $"); if (nargs && argc - nargs == 1) { exit (0); } argc -= nargs ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc != 4) { usage(1); } ltafn1 = argv[1]; ltafn2 = argv[2]; ltafn_total = argv[3]; printf("Read individual LTAs\n"); //lta1 = ltaReadFileEx(ltafn1); TRANSFORM * trans = TransformRead(ltafn1); lta1 = (LTA *)trans->xform ; if (!lta1) { ErrorExit(ERROR_BADFILE, "%s: can't read file %s",Progname, ltafn1); } if (invert1) { VOL_GEOM vgtmp; LT *lt; MATRIX *m_tmp = lta1->xforms[0].m_L ; lta1->xforms[0].m_L = MatrixInverse(lta1->xforms[0].m_L, NULL) ; MatrixFree(&m_tmp) ; lt = <a1->xforms[0]; if (lt->dst.valid == 0 || lt->src.valid == 0) { fprintf (stderr, "WARNING:********************************************************\n"); fprintf (stderr, "WARNING:dst or src volume is invalid. Inverse likely wrong.\n"); fprintf (stderr, "WARNING:********************************************************\n"); } copyVolGeom(<->dst, &vgtmp); copyVolGeom(<->src, <->dst); copyVolGeom(&vgtmp, <->src); } if (strcmp(ltafn2,"identity.nofile") == 0) { type = TransformFileNameType(ltafn_total); if (type == MNI_TRANSFORM_TYPE) { ltaMNIwrite(lta1, ltafn_total); } else { //change type to VOXEL_VOXEL if (lta1->type != out_type) { LTAchangeType(lta1, out_type); } printf("Writing LTA to file %s...\n", ltafn_total); fo = fopen(ltafn_total,"w"); if (fo==NULL) ErrorExit(ERROR_BADFILE, "%s: can't create file %s",Progname, ltafn_total); LTAprint(fo, lta1); fclose(fo); } LTAfree(<a1); printf("%s successful.\n", Progname); return 0; } type = TransformFileNameType(ltafn2); if (type == MNI_TRANSFORM_TYPE) { if (invert2 != 0) ErrorExit (ERROR_BADFILE, "%s: LTA2 is talairach.xfm, and shouldn't be inverted ", Progname); lta2 = ltaMNIreadEx(ltafn2) ; //the talairach xform is supposed to be linear_RAS_TO_RAS, right? Yes lta2->type = LINEAR_RAS_TO_RAS; if (tal_src_file == 0 && lta2->xforms[0].src.valid == 0) ErrorExit (ERROR_BADFILE, "%s: pls use -tal option to give talairach src and " "template filenames",Progname); if (tal_dst_file == 0 && lta2->xforms[0].dst.valid == 0) ErrorExit (ERROR_BADFILE, "%s: pls use -tal option to give talairach src and " "template filenames",Progname); if (tal_src_file != 0) { LTAmodifySrcDstGeom(lta2, tal_src, NULL); // add src and dst information } if (tal_dst_file != 0) { LTAmodifySrcDstGeom(lta2, NULL, tal_dst); // add src and dst information } } else { TRANSFORM * trans = TransformRead(ltafn2); lta2 = (LTA *)trans->xform ; //lta2 = ltaReadFileEx(ltafn2); } if (!lta2) { ErrorExit(ERROR_BADFILE, "%s: can't read file %s",Progname, ltafn2); } if (invert2) { VOL_GEOM vgtmp; LT *lt; MATRIX *m_tmp = lta2->xforms[0].m_L ; lta2->xforms[0].m_L = MatrixInverse(lta2->xforms[0].m_L, NULL) ; MatrixFree(&m_tmp) ; lt = <a2->xforms[0]; if (lt->dst.valid == 0 || lt->src.valid == 0) { fprintf (stderr, "WARNING:********************************************************\n"); fprintf (stderr, "WARNING:dst or src volume is invalid. Inverse likely wrong.\n"); fprintf (stderr, "WARNING:********************************************************\n"); } copyVolGeom(<->dst, &vgtmp); copyVolGeom(<->src, <->dst); copyVolGeom(&vgtmp, <->src); } if (vg_isEqual(<a1->xforms[0].dst, <a2->xforms[0].src) == 0) { /* ErrorExit(ERROR_BADFILE, "%s: dst volume of lta1 doesn't match src volume of lta2",Progname);*/ printf("Warning: dst volume of lta1 doesn't match src volume of lta2\n"); printf("Volume geometry for lta1-dst: \n"); vg_print(<a1->xforms[0].dst); printf("Volume geometry for lta2-src:\n"); vg_print(<a2->xforms[0].src); } printf("Combining the two LTAs to get a RAS-to-RAS from src " "of LTA1 to dst of LTA2...\n"); if (lta1->type == LINEAR_RAS_TO_RAS) { RAS_1_to_1 = MatrixCopy(lta1->xforms[0].m_L, NULL); } else if (lta1->type == LINEAR_VOX_TO_VOX) { r_to_i_1 = vg_r_to_i(<a1->xforms[0].src); i_to_r_1 = vg_i_to_r(<a1->xforms[0].dst); if (!r_to_i_1 || !i_to_r_1) ErrorExit(ERROR_BADFILE, "%s: failed to convert LTA1 to RAS_to_RAS",Progname); m_tmp = MatrixMultiply(lta1->xforms[0].m_L, r_to_i_1, NULL); RAS_1_to_1 = MatrixMultiply(i_to_r_1, m_tmp, NULL); MatrixFree(&m_tmp); } else { ErrorExit(ERROR_BADFILE, "%s: unknown transform type for LTA1",Progname); } if (lta2->type == LINEAR_RAS_TO_RAS) { RAS_2_to_2 = MatrixCopy(lta2->xforms[0].m_L, NULL); } else if (lta2->type == LINEAR_VOX_TO_VOX) { r_to_i_2 = vg_r_to_i(<a2->xforms[0].src); i_to_r_2 = vg_i_to_r(<a2->xforms[0].dst); if (!r_to_i_2 || !i_to_r_2) ErrorExit(ERROR_BADFILE, "%s: failed to convert LTA1 to RAS_to_RAS",Progname); m_tmp = MatrixMultiply(lta2->xforms[0].m_L, r_to_i_2, NULL); RAS_2_to_2 = MatrixMultiply(i_to_r_2, m_tmp, NULL); MatrixFree(&m_tmp); } else { ErrorExit(ERROR_BADFILE, "%s: unknown transform type for LTA1",Progname); } lta_total = LTAalloc(1, NULL); lta_total->type = LINEAR_RAS_TO_RAS; MatrixMultiply(RAS_2_to_2, RAS_1_to_1, lta_total->xforms[0].m_L); lta_total->xforms[0].src = lta1->xforms[0].src; lta_total->xforms[0].dst = lta2->xforms[0].dst; lta_total->xforms[0].x0 = 0; lta_total->xforms[0].y0 = 0; lta_total->xforms[0].z0 = 0; lta_total->xforms[0].sigma = 1.0f; type = TransformFileNameType(ltafn_total); if (type == MNI_TRANSFORM_TYPE) { ltaMNIwrite(lta_total, ltafn_total); } else { //change type to VOXEL_VOXEL if (lta_total->type != out_type) { LTAchangeType(lta_total, out_type); } printf("Writing combined LTA to file %s...\n", ltafn_total); fo = fopen(ltafn_total,"w"); if (fo==NULL) ErrorExit(ERROR_BADFILE, "%s: can't create file %s",Progname, ltafn_total); LTAprint(fo, lta_total); fclose(fo); } LTAfree(<a1); LTAfree(<a2); LTAfree(<a_total); MatrixFree(&RAS_1_to_1); MatrixFree(&RAS_2_to_2); if (tal_src) { MRIfree(&tal_src); } if (tal_dst) { MRIfree(&tal_dst); } printf("%s successful.\n", Progname); return(0); } /* end main() */
int main(int argc, char *argv[]) { char **av; MRI *mri_src, *mri_mask, *mri_dst ; int nargs, ac, nmask; int x, y, z; float value; MRI_REGION *region; LTA *lta = 0; int transform_type; MRI *mri_tmp; nargs = handle_version_option ( argc, argv, "$Id: mri_mask.c,v 1.18 2012/12/07 22:45:50 greve Exp $", "$Name: $" ); if (nargs && argc - nargs == 1) { exit (0); } argc -= nargs ; Progname = argv[0]; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc != 4) { printf("Incorrect number of arguments, argc = %d\n", argc); usage(1); } mri_src = MRIread(argv[1]) ; if (!mri_src) ErrorExit(ERROR_BADPARM, "%s: could not read source volume %s", Progname, argv[1]) ; mri_mask = MRIread(argv[2]) ; if (!mri_mask) ErrorExit(ERROR_BADPARM, "%s: could not read mask volume %s", Progname, argv[2]) ; if(mri_src->width != mri_mask->width) { printf("ERROR: dimension mismatch between source and mask\n"); exit(1); } printf("DoAbs = %d\n",DoAbs); /* Read LTA transform and apply it to mri_mask */ if (xform_fname != NULL) { printf("Apply the given LTA xfrom to the mask volume\n"); // read transform transform_type = TransformFileNameType(xform_fname); if (transform_type == MNI_TRANSFORM_TYPE || transform_type == TRANSFORM_ARRAY_TYPE || transform_type == REGISTER_DAT || transform_type == FSLREG_TYPE ) { printf("Reading transform ...\n"); lta = LTAreadEx(xform_fname) ; if (!lta) ErrorExit(ERROR_NOFILE, "%s: could not read transform file %s", Progname, xform_fname) ; if (transform_type == FSLREG_TYPE) { if (lta_src == 0 || lta_dst == 0) { fprintf(stderr, "ERROR: fslmat does not have information on " "the src and dst volumes\n"); fprintf(stderr, "ERROR: you must give options '-lta_src' " "and '-lta_dst' to specify the src and dst volume infos\n"); } LTAmodifySrcDstGeom(lta, lta_src, lta_dst); // add src and dst information LTAchangeType(lta, LINEAR_VOX_TO_VOX); } if (lta->xforms[0].src.valid == 0) { if (lta_src == 0) { fprintf(stderr, "The transform does not have the valid src volume info.\n"); fprintf(stderr, "Either you give src volume info by option -lta_src or\n"); fprintf(stderr, "make the transform to have the valid src info.\n"); ErrorExit(ERROR_BAD_PARM, "Bailing out...\n"); } else { LTAmodifySrcDstGeom(lta, lta_src, NULL); // add src information // getVolGeom(lta_src, <->src); } } if (lta->xforms[0].dst.valid == 0) { if (lta_dst == 0) { fprintf(stderr, "The transform does not have the valid dst volume info.\n"); fprintf(stderr, "Either you give src volume info by option -lta_dst or\n"); fprintf(stderr, "make the transform to have the valid dst info.\n"); fprintf(stderr, "If the dst was average_305, then you can set\n"); fprintf(stderr, "environmental variable USE_AVERAGE305 true\n"); fprintf(stderr, "without giving the dst volume for RAS-to-RAS transform.\n"); ErrorExit(ERROR_BAD_PARM, "Bailing out...\n"); } else { LTAmodifySrcDstGeom(lta, NULL, lta_dst); // add dst information } } } else { ErrorExit(ERROR_BADPARM, "transform is not of MNI, nor Register.dat, nor FSLMAT type"); } if (invert) { VOL_GEOM vgtmp; LT *lt; MATRIX *m_tmp = lta->xforms[0].m_L ; lta->xforms[0].m_L = MatrixInverse(lta->xforms[0].m_L, NULL) ; MatrixFree(&m_tmp) ; lt = <a->xforms[0]; if (lt->dst.valid == 0 || lt->src.valid == 0) { fprintf(stderr, "WARNING:**************************************" "*************************\n"); fprintf(stderr, "WARNING:dst volume information is invalid. " "Most likely produced wrong inverse.\n"); fprintf(stderr, "WARNING:**************************************" "*************************\n"); } copyVolGeom(<->dst, &vgtmp); copyVolGeom(<->src, <->dst); copyVolGeom(&vgtmp, <->src); } // LTAchangeType(lta, LINEAR_VOX_TO_VOX); mri_tmp = MRIalloc(mri_src->width, mri_src->height, mri_src->depth, mri_mask->type) ; MRIcopyHeader(mri_src, mri_tmp) ; mri_tmp = LTAtransformInterp(mri_mask, mri_tmp, lta, InterpMethod); // mri_tmp = //MRIlinearTransformInterp // ( // mri_mask, mri_tmp, lta->xforms[0].m_L, InterpMethod // ); MRIfree(&mri_mask); mri_mask = mri_tmp; if (lta_src) { MRIfree(<a_src); } if (lta_dst) { MRIfree(<a_dst); } if (lta) { LTAfree(<a); } } /* if (xform_fname != NULL) */ // Threshold mask nmask = 0; for (z = 0 ; z <mri_mask->depth ; z++) { for (y = 0 ; y < mri_mask->height ; y++) { for (x = 0 ; x < mri_mask->width ; x++) { value = MRIgetVoxVal(mri_mask, x, y, z, 0); if(DoAbs) { value = fabs(value); } if(value <= threshold) { MRIsetVoxVal(mri_mask,x,y,z,0,0); } else { nmask ++; } } } } printf("Found %d voxels in mask (pct=%6.2f)\n",nmask, 100.0*nmask/(mri_mask->width*mri_mask->height*mri_mask->depth)); if(DoBB){ printf("Computing bounding box, npad = %d\n",nPadBB); region = REGIONgetBoundingBox(mri_mask,nPadBB); REGIONprint(stdout, region); mri_tmp = MRIextractRegion(mri_mask, NULL, region); if(mri_tmp == NULL) exit(1); MRIfree(&mri_mask); mri_mask = mri_tmp; mri_tmp = MRIextractRegion(mri_src, NULL, region); if(mri_tmp == NULL) exit(1); MRIfree(&mri_src); mri_src = mri_tmp; } int mask=0; float out_val=0; if (do_transfer) { mask = (int)transfer_val; out_val = transfer_val; } mri_dst = MRImask(mri_src, mri_mask, NULL, mask, out_val) ; if (!mri_dst) { ErrorExit(Gerror, "%s: stripping failed", Progname) ; } if (keep_mask_deletion_edits) { mri_dst = MRImask(mri_dst, mri_mask, NULL, 1, 1) ; // keep voxels = 1 if (!mri_dst) ErrorExit(Gerror, "%s: stripping failed on keep_mask_deletion_edits", Progname) ; } printf("Writing masked volume to %s...", argv[3]) ; MRIwrite(mri_dst, argv[3]); printf("done.\n") ; MRIfree(&mri_src); MRIfree(&mri_mask); MRIfree(&mri_dst); exit(0); } /* end main() */