static int test(MRI *mri1, MRI *mri2, MRI *mri3, MATRIX *m_vol1_to_vol2_ras) { VECTOR *v_test, *v_vox ; float x_ras1, y_ras1, z_ras1, x_ras2, y_ras2, z_ras2, x_vox1, y_vox1, z_vox1, x_vox2, y_vox2, z_vox2 ; MATRIX *m_vol2_vox2ras, *m_vol2_ras2vox, *m_vol1_ras2vox, *m_vol1_vox2ras, *m_vol3_ras2vox, *m_vol3_vox2ras ; int val ; v_test = VectorAlloc(4, MATRIX_REAL) ; m_vol1_vox2ras = MRIgetVoxelToRasXform(mri1) ; m_vol2_vox2ras = MRIgetVoxelToRasXform(mri2) ; m_vol1_ras2vox = MRIgetRasToVoxelXform(mri1) ; m_vol2_ras2vox = MRIgetRasToVoxelXform(mri2) ; m_vol3_vox2ras = MRIgetVoxelToRasXform(mri3) ; m_vol3_ras2vox = MRIgetRasToVoxelXform(mri3) ; x_ras1 = 126.50 ; y_ras1 = -125.500 ; z_ras1 = 127.50 ; V3_X(v_test) = x_ras1 ; V3_Y(v_test) = y_ras1 ; V3_Z(v_test) = z_ras1 ; *MATRIX_RELT(v_test, 4, 1) = 1.0 ; v_vox = MatrixMultiply(m_vol1_ras2vox, v_test, NULL) ; x_vox1 = V3_X(v_vox) ; y_vox1 = V3_Y(v_vox) ; z_vox1 = V3_Z(v_vox) ; val = MRISvox(mri1, nint(x_vox1), nint(y_vox1), nint(z_vox1)) ; printf("VOL1: ras (%1.1f, %1.1f, %1.1f) --> VOX (%1.1f, %1.1f, %1.1f) = %d\n", x_ras1, y_ras1, z_ras1, x_vox1, y_vox1, z_vox1, val) ; x_ras2 = 76.5421 ; y_ras2 = 138.5352 ; z_ras2 = 96.0910 ; V3_X(v_test) = x_ras2 ; V3_Y(v_test) = y_ras2 ; V3_Z(v_test) = z_ras2 ; *MATRIX_RELT(v_test, 4, 1) = 1.0 ; v_vox = MatrixMultiply(m_vol2_ras2vox, v_test, NULL) ; x_vox2 = V3_X(v_vox) ; y_vox2 = V3_Y(v_vox) ; z_vox2 = V3_Z(v_vox) ; val = MRISvox(mri2, nint(x_vox2), nint(y_vox2), nint(z_vox2)) ; printf("VOL2: ras (%2.1f, %2.1f, %2.1f) --> VOX (%2.1f, %2.1f, %2.1f) = %d\n", x_ras2, y_ras2, z_ras2, x_vox2, y_vox2, z_vox2, val) ; MatrixFree(&v_test) ; return(NO_ERROR) ; }
static MRI * apply_bias(MRI *mri_orig, MRI *mri_norm, MRI *mri_bias) { MATRIX *m_vox2vox; VECTOR *v1, *v2; int x, y, z ; double xd, yd, zd, bias, val_orig, val_norm ; if (mri_norm == NULL) mri_norm = MRIclone(mri_orig, NULL) ; m_vox2vox = MRIgetVoxelToVoxelXform(mri_orig, mri_bias) ; v1 = VectorAlloc(4, MATRIX_REAL); v2 = VectorAlloc(4, MATRIX_REAL); VECTOR_ELT(v1, 4) = 1.0 ; VECTOR_ELT(v2, 4) = 1.0 ; for (x = 0 ; x < mri_orig->width ; x++) { V3_X(v1) = x ; for (y = 0 ; y < mri_orig->height ; y++) { V3_Y(v1) = y ; for (z = 0 ; z < mri_orig->depth ; z++) { V3_Z(v1) = z ; if (x == Gx && y == Gy && z == Gz) DiagBreak() ; val_orig = MRIgetVoxVal(mri_orig, x, y, z, 0) ; MatrixMultiply(m_vox2vox, v1, v2) ; xd = V3_X(v2) ; yd = V3_Y(v2) ; zd = V3_Z(v2); MRIsampleVolume(mri_bias, xd, yd, zd, &bias) ; val_norm = val_orig * bias ; if (mri_norm->type == MRI_UCHAR) { if (val_norm > 255) val_norm = 255 ; else if (val_norm < 0) val_norm = 0 ; } MRIsetVoxVal(mri_norm, x, y, z, 0, val_norm) ; } } } MatrixFree(&m_vox2vox) ; VectorFree(&v1) ; VectorFree(&v2) ; return(mri_norm) ; }
int main(int argc, char *argv[]) { char **av, *source_fname, *target_fname, *out_fname, fname[STRLEN] ; int ac, nargs, new_transform = 0, pad ; MRI *mri_target, *mri_source, *mri_orig_source ; MRI_REGION box ; struct timeb start ; int msec, minutes, seconds ; GCA_MORPH *gcam ; MATRIX *m_L/*, *m_I*/ ; LTA *lta ; /* initialize the morph params */ memset(&mp, 0, sizeof(GCA_MORPH_PARMS)); /* for nonlinear morph */ mp.l_jacobian = 1 ; mp.min_sigma = 0.4 ; mp.l_distance = 0 ; mp.l_log_likelihood = .025 ; mp.dt = 0.005 ; mp.noneg = True ; mp.exp_k = 20 ; mp.diag_write_snapshots = 1 ; mp.momentum = 0.9 ; if (FZERO(mp.l_smoothness)) mp.l_smoothness = 2 ; mp.sigma = 8 ; mp.relabel_avgs = -1 ; mp.navgs = 256 ; mp.levels = 6 ; mp.integration_type = GCAM_INTEGRATE_BOTH ; mp.nsmall = 1 ; mp.reset_avgs = -1 ; mp.npasses = 3 ; mp.regrid = regrid? True : False ; mp.tol = 0.1 ; mp.niterations = 1000 ; TimerStart(&start) ; setRandomSeed(-1L) ; DiagInit(NULL, NULL, NULL) ; ErrorInit(NULL, NULL, NULL) ; Progname = argv[0] ; 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_exit(1) ; source_fname = argv[1] ; target_fname = argv[2] ; out_fname = argv[3] ; FileNameOnly(out_fname, fname) ; FileNameRemoveExtension(fname, fname) ; strcpy(mp.base_name, fname) ; mri_source = MRIread(source_fname) ; if (!mri_source) ErrorExit(ERROR_NOFILE, "%s: could not read source label volume %s", Progname, source_fname) ; if (mri_source->type == MRI_INT) { MRI *mri_tmp = MRIchangeType(mri_source, MRI_FLOAT, 0, 1, 1) ; MRIfree(&mri_source); mri_source = mri_tmp ; } mri_target = MRIread(target_fname) ; if (!mri_target) ErrorExit(ERROR_NOFILE, "%s: could not read target label volume %s", Progname, target_fname) ; if (mri_target->type == MRI_INT) { MRI *mri_tmp = MRIchangeType(mri_target, MRI_FLOAT, 0, 1, 1) ; MRIfree(&mri_target); mri_target = mri_tmp ; } if (erosions > 0) { int n ; for (n = 0 ; n < erosions ; n++) { MRIerodeZero(mri_target, mri_target) ; MRIerodeZero(mri_source, mri_source) ; } } if (scale_values > 0) { MRIscalarMul(mri_source, mri_source, scale_values) ; MRIscalarMul(mri_target, mri_target, scale_values) ; } if (transform && transform->type == MORPH_3D_TYPE) TransformRas2Vox(transform, mri_source,NULL) ; if (use_aseg == 0) { if (match_peak_intensity_ratio) MRImatchIntensityRatio(mri_source, mri_target, mri_source, .8, 1.2, 100, 125) ; else if (match_mean_intensity) MRImatchMeanIntensity(mri_source, mri_target, mri_source) ; MRIboundingBox(mri_source, 0, &box) ; pad = (int)ceil(PADVOX * MAX(mri_target->xsize,MAX(mri_target->ysize,mri_target->zsize)) / MIN(mri_source->xsize,MIN(mri_source->ysize,mri_source->zsize))); #if 0 { MRI *mri_tmp ; if (pad < 1) pad = 1 ; printf("padding source with %d voxels...\n", pad) ; mri_tmp = MRIextractRegionAndPad(mri_source, NULL, &box, pad) ; if ((Gdiag & DIAG_WRITE) && DIAG_VERBOSE_ON) MRIwrite(mri_tmp, "t.mgz") ; MRIfree(&mri_source) ; mri_source = mri_tmp ; } #endif } mri_orig_source = MRIcopy(mri_source, NULL) ; mp.max_grad = 0.3*mri_source->xsize ; if (transform == NULL) transform = TransformAlloc(LINEAR_VOXEL_TO_VOXEL, NULL) ; if (transform->type != MORPH_3D_TYPE) // initializing m3d from a linear transform { new_transform = 1 ; lta = ((LTA *)(transform->xform)) ; if (lta->type != LINEAR_VOX_TO_VOX) { printf("converting ras xform to voxel xform\n") ; m_L = MRIrasXformToVoxelXform(mri_source, mri_target, lta->xforms[0].m_L, NULL) ; MatrixFree(<a->xforms[0].m_L) ; lta->type = LINEAR_VOX_TO_VOX ; } else { printf("using voxel xform\n") ; m_L = lta->xforms[0].m_L ; } #if 0 if (Gsx >= 0) // update debugging coords { VECTOR *v1, *v2 ; v1 = VectorAlloc(4, MATRIX_REAL) ; Gsx -= (box.x-pad) ; Gsy -= (box.y-pad) ; Gsz -= (box.z-pad) ; V3_X(v1) = Gsx ; V3_Y(v1) = Gsy ; V3_Z(v1) = Gsz ; VECTOR_ELT(v1,4) = 1.0 ; v2 = MatrixMultiply(m_L, v1, NULL) ; Gsx = nint(V3_X(v2)) ; Gsy = nint(V3_Y(v2)) ; Gsz = nint(V3_Z(v2)) ; MatrixFree(&v2) ; MatrixFree(&v1) ; printf("mapping by transform (%d, %d, %d) --> (%d, %d, %d) for rgb writing\n", Gx, Gy, Gz, Gsx, Gsy, Gsz) ; } #endif if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) write_snapshot(mri_target, mri_source, m_L, &mp, 0, 1, "linear_init"); lta->xforms[0].m_L = m_L ; printf("initializing GCAM with vox->vox matrix:\n") ; MatrixPrint(stdout, m_L) ; gcam = GCAMcreateFromIntensityImage(mri_source, mri_target, transform) ; #if 0 gcam->gca = gcaAllocMax(1, 1, 1, mri_target->width, mri_target->height, mri_target->depth, 0, 0) ; #endif GCAMinitVolGeom(gcam, mri_source, mri_target) ; if (use_aseg) { if (ribbon_name) { char fname[STRLEN], path[STRLEN], *str, *hemi ; int h, s, label ; MRI_SURFACE *mris_white, *mris_pial ; MRI *mri ; for (s = 0 ; s <= 1 ; s++) // source and target { if (s == 0) { str = source_surf ; mri = mri_source ; FileNamePath(mri->fname, path) ; strcat(path, "/../surf") ; } else { mri = mri_target ; FileNamePath(mri->fname, path) ; strcat(path, "/../elastic") ; str = target_surf ; } // sorry - these values come from FreeSurferColorLUT.txt MRIreplaceValueRange(mri, mri, 1000, 1034, Left_Cerebral_Cortex) ; MRIreplaceValueRange(mri, mri, 1100, 1180, Left_Cerebral_Cortex) ; MRIreplaceValueRange(mri, mri, 2000, 2034, Right_Cerebral_Cortex) ; MRIreplaceValueRange(mri, mri, 2100, 2180, Right_Cerebral_Cortex) ; for (h = LEFT_HEMISPHERE ; h <= RIGHT_HEMISPHERE ; h++) { if (h == LEFT_HEMISPHERE) { hemi = "lh" ; label = Left_Cerebral_Cortex ; } else { label = Right_Cerebral_Cortex ; hemi = "rh" ; } sprintf(fname, "%s/%s%s.white", path, hemi, str) ; mris_white = MRISread(fname) ; if (mris_white == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read surface %s", Progname, fname) ; MRISsaveVertexPositions(mris_white, WHITE_VERTICES) ; sprintf(fname, "%s/%s%s.pial", path, hemi, str) ; mris_pial = MRISread(fname) ; if (mris_pial == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read surface %s", Progname, fname) ; MRISsaveVertexPositions(mris_pial, PIAL_VERTICES) ; if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { sprintf(fname, "sb.mgz") ; MRIwrite(mri_source, fname) ; sprintf(fname, "tb.mgz") ; MRIwrite(mri_target, fname) ; } insert_ribbon_into_aseg(mri, mri, mris_white, mris_pial, h) ; if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { sprintf(fname, "sa.mgz") ; MRIwrite(mri_source, fname) ; sprintf(fname, "ta.mgz") ; MRIwrite(mri_target, fname) ; } MRISfree(&mris_white) ; MRISfree(&mris_pial) ; } } if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { sprintf(fname, "s.mgz") ; MRIwrite(mri_source, fname) ; sprintf(fname, "t.mgz") ; MRIwrite(mri_target, fname) ; } } GCAMinitLabels(gcam, mri_target) ; GCAMsetVariances(gcam, 1.0) ; mp.mri_dist_map = create_distance_transforms(mri_source, mri_target, NULL, 40.0, gcam) ; } } else /* use a previously create morph and integrate it some more */ { printf("using previously create gcam...\n") ; gcam = (GCA_MORPH *)(transform->xform) ; GCAMrasToVox(gcam, mri_source) ; if (use_aseg) { GCAMinitLabels(gcam, mri_target) ; GCAMsetVariances(gcam, 1.0) ; mp.mri_dist_map = create_distance_transforms(mri_source, mri_target, NULL, 40.0, gcam) ; } else GCAMaddIntensitiesFromImage(gcam, mri_target) ; } if (gcam->width != mri_source->width || gcam->height != mri_source->height || gcam->depth != mri_source->depth) ErrorExit(ERROR_BADPARM, "%s: warning gcam (%d, %d, %d), doesn't match source vol (%d, %d, %d)", Progname, gcam->width, gcam->height, gcam->depth, mri_source->width, mri_source->height, mri_source->depth) ; mp.mri_diag = mri_source ; mp.diag_morph_from_atlas = 0 ; mp.diag_write_snapshots = 1 ; mp.diag_sample_type = use_aseg ? SAMPLE_NEAREST : SAMPLE_TRILINEAR ; mp.diag_volume = use_aseg ? GCAM_LABEL : GCAM_MEANS ; if (renormalize) GCAMnormalizeIntensities(gcam, mri_target) ; if (mp.write_iterations != 0) { char fname[STRLEN] ; MRI *mri_gca ; if (getenv("DONT_COMPRESS")) sprintf(fname, "%s_target.mgh", mp.base_name) ; else sprintf(fname, "%s_target.mgz", mp.base_name) ; if (mp.diag_morph_from_atlas == 0) { printf("writing target volume to %s...\n", fname) ; MRIwrite(mri_target, fname) ; sprintf(fname, "%s_target", mp.base_name) ; MRIwriteImageViews(mri_target, fname, IMAGE_SIZE) ; } else { if (use_aseg) mri_gca = GCAMwriteMRI(gcam, NULL, GCAM_LABEL) ; else { mri_gca = MRIclone(mri_source, NULL) ; GCAMbuildMostLikelyVolume(gcam, mri_gca) ; } printf("writing target volume to %s...\n", fname) ; MRIwrite(mri_gca, fname) ; sprintf(fname, "%s_target", mp.base_name) ; MRIwriteImageViews(mri_gca, fname, IMAGE_SIZE) ; MRIfree(&mri_gca) ; } } if (nozero) { printf("disabling zero nodes\n") ; GCAMignoreZero(gcam, mri_target) ; } mp.mri = mri_target ; if (mp.regrid == True && new_transform == 0) GCAMregrid(gcam, mri_target, PAD, &mp, &mri_source) ; mp.write_fname = out_fname ; GCAMregister(gcam, mri_source, &mp) ; // atlas is target, morph target into register with it if (apply_transform) { MRI *mri_aligned ; char fname[STRLEN] ; FileNameRemoveExtension(out_fname, fname) ; strcat(fname, ".mgz") ; mri_aligned = GCAMmorphToAtlas(mp.mri, gcam, NULL, -1, mp.diag_sample_type) ; printf("writing transformed output volume to %s...\n", fname) ; MRIwrite(mri_aligned, fname) ; MRIfree(&mri_aligned) ; } printf("writing warp vector field to %s\n", out_fname) ; GCAMvoxToRas(gcam) ; GCAMwrite(gcam, out_fname) ; GCAMrasToVox(gcam, mri_source) ; msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; printf("registration took %d minutes and %d seconds.\n", minutes, seconds) ; exit(0) ; return(0) ; }
/* Actually no need to modify this function, since I will only use the float * type here, so the roundoff I added will never take effect. * What I need to modify is the MRIchangeType function! */ MRI *MRIlinearTransformInterpBSpline(MRI *mri_src, MRI *mri_dst, MATRIX *mA, int splinedegree) { int y1, y2, y3, width, height, depth ; VECTOR *v_X, *v_Y ; /* original and transformed coordinate systems */ MATRIX *mAinv ; /* inverse of mA */ double val, x1, x2, x3 ; MRI *mri_Bcoeff; mAinv = MatrixInverse(mA, NULL) ; /* will sample from dst back to src */ if (!mAinv) ErrorReturn(NULL, (ERROR_BADPARM, "MRIlinearTransformBSpline: xform is singular")) ; if (!mri_dst) mri_dst = MRIclone(mri_src, NULL) ; else MRIclear(mri_dst) ; /* set all values to zero */ if (mri_src->type != MRI_FLOAT) mri_Bcoeff = MRIchangeType(mri_src, MRI_FLOAT, 0, 1.0, 1); else mri_Bcoeff = MRIcopy(mri_src, NULL); /* convert between a representation based on image samples */ /* and a representation based on image B-spline coefficients */ if (SamplesToCoefficients(mri_Bcoeff, splinedegree)) { ErrorReturn(NULL, (ERROR_BADPARM, "Change of basis failed\n")); } printf("Direct B-spline Transform Finished. \n"); width = mri_src->width ; height = mri_src->height ; depth = mri_src->depth ; v_X = VectorAlloc(4, MATRIX_REAL) ; /* input (src) coordinates */ v_Y = VectorAlloc(4, MATRIX_REAL) ; /* transformed (dst) coordinates */ v_Y->rptr[4][1] = 1.0f ; for (y3 = 0 ; y3 < mri_dst->depth ; y3++) { V3_Z(v_Y) = y3 ; for (y2 = 0 ; y2 < mri_dst->height ; y2++) { V3_Y(v_Y) = y2 ; for (y1 = 0 ; y1 < mri_dst->width ; y1++) { V3_X(v_Y) = y1 ; MatrixMultiply(mAinv, v_Y, v_X) ; x1 = V3_X(v_X) ; x2 = V3_Y(v_X) ; x3 = V3_Z(v_X) ; if (x1 <= -0.5 || x1 >= (width - 0.5) || x2 <= -0.5 || x2 >= (height - 0.5) || x3 <= -0.5 || x3 >= (depth-0.5)) val = 0.0; else val = InterpolatedValue(mri_Bcoeff, x1, x2, x3, splinedegree); switch (mri_dst->type) { case MRI_UCHAR: if (val <-0.5) val = -0.5; if (val > 254.5) val = 254.5; MRIvox(mri_dst,y1,y2,y3) = (BUFTYPE)nint(val+0.5) ; break ; case MRI_SHORT: MRISvox(mri_dst,y1,y2,y3) = (short)nint(val+0.5) ; break ; case MRI_FLOAT: MRIFvox(mri_dst,y1,y2,y3) = (float)(val) ; break ; case MRI_INT: MRIIvox(mri_dst,y1,y2,y3) = nint(val+0.5) ; break ; default: ErrorReturn(NULL, (ERROR_UNSUPPORTED, "MRIlinearTransformBSpline: unsupported dst type %d", mri_dst->type)) ; break ; } } } } MatrixFree(&v_X) ; MatrixFree(&mAinv) ; MatrixFree(&v_Y) ; MRIfree(&mri_Bcoeff); return(mri_dst) ; }
static int remove_nonwm_voxels(MRI *mri_ctrl_src, MRI *mri_aseg, MRI *mri_ctrl_dst) { int x, y, z, label, removed = 0, xa, ya, za ; MATRIX *m_vox_to_vox ; VECTOR *v_src, *v_dst ; m_vox_to_vox = MRIgetVoxelToVoxelXform(mri_ctrl_src, mri_aseg) ; v_src = VectorAlloc(4,1) ; v_dst = VectorAlloc(4,1) ; VECTOR_ELT(v_src, 4) = VECTOR_ELT(v_dst,4) = 1 ; for (x = 0 ; x < mri_ctrl_src->width ; x++) for (y = 0 ; y < mri_ctrl_src->height ; y++) for (z = 0 ; z < mri_ctrl_src->depth ; z++) { if (x == Gx && y == Gy && z == Gz) { DiagBreak() ; } if (MRIgetVoxVal(mri_ctrl_src, x, y, z, 0) == 0) { continue ; } V3_X(v_src) = x ; V3_Y(v_src) = y ; V3_Z(v_src) = z ; MatrixMultiply(m_vox_to_vox, v_src, v_dst) ; xa = nint(V3_X(v_dst)) ; ya = nint(V3_Y(v_dst)) ; za = nint(V3_Z(v_dst)); label = MRIgetVoxVal(mri_aseg, xa, ya, za, 0) ; switch (label) { case Left_Thalamus_Proper: case Left_Lateral_Ventricle: case Left_Caudate: case Left_Putamen: case Left_Pallidum: case Left_Amygdala: case Left_Hippocampus: case Left_Cerebellum_Cortex: case Left_Inf_Lat_Vent: case Right_Thalamus_Proper: case Right_Lateral_Ventricle: case Right_Caudate: case Right_Putamen: case Right_Pallidum: case Right_Amygdala: case Right_Hippocampus: case Right_Cerebellum_Cortex: case Right_Inf_Lat_Vent: case Third_Ventricle: case Fourth_Ventricle: case Unknown: removed++ ; MRIsetVoxVal(mri_ctrl_dst, x, y, z, 0, CONTROL_NONE) ; break ; default: MRIsetVoxVal(mri_ctrl_dst, x, y, z, 0, CONTROL_MARKED) ; break ; } } printf("%d non wm control points removed\n", removed) ; return(NO_ERROR) ; }
int main(int argc, char *argv[]) { char **av, *label_name, *vol_name, *out_name ; int ac, nargs ; int msec, minutes, seconds, i ; LABEL *area ; struct timeb start ; MRI *mri, *mri_seg ; Real xw, yw, zw, xv, yv, zv, val; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mri_label_vals.c,v 1.16 2015/08/24 18:22:05 fischl Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; TimerStart(&start) ; 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(1) ; vol_name = argv[1] ; label_name = argv[2] ; out_name = argv[3] ; mri = MRIread(vol_name) ; if (!mri) ErrorExit(ERROR_NOFILE, "%s: could not read volume from %s",Progname, vol_name) ; if (scaleup_flag) { float scale, fov_x, fov_y, fov_z ; scale = 1.0/MIN(MIN(mri->xsize, mri->ysize),mri->zsize) ; fprintf(stderr, "scaling voxel sizes up by %2.2f\n", scale) ; mri->xsize *= scale ; mri->ysize *= scale ; mri->zsize *= scale ; fov_x = mri->xsize * mri->width; fov_y = mri->ysize * mri->height; fov_z = mri->zsize * mri->depth; mri->xend = fov_x / 2.0; mri->xstart = -mri->xend; mri->yend = fov_y / 2.0; mri->ystart = -mri->yend; mri->zend = fov_z / 2.0; mri->zstart = -mri->zend; mri->fov = (fov_x > fov_y ? (fov_x > fov_z ? fov_x : fov_z) : (fov_y > fov_z ? fov_y : fov_z) ); } if (segmentation_flag >= 0) { int x, y, z ; VECTOR *v_seg, *v_mri ; MATRIX *m_seg_to_mri ; v_seg = VectorAlloc(4, MATRIX_REAL) ; v_mri = VectorAlloc(4, MATRIX_REAL) ; VECTOR_ELT(v_seg, 4) = 1.0 ; VECTOR_ELT(v_mri, 4) = 1.0 ; mri_seg = MRIread(argv[2]) ; if (!mri_seg) ErrorExit(ERROR_NOFILE, "%s: could not read volume from %s",Progname, argv[2]) ; if (erode) { MRI *mri_tmp ; mri_tmp = MRIclone(mri_seg, NULL) ; MRIcopyLabel(mri_seg, mri_tmp, segmentation_flag) ; while (erode-- > 0) MRIerode(mri_tmp, mri_tmp) ; MRIcopy(mri_tmp, mri_seg) ; MRIfree(&mri_tmp) ; } m_seg_to_mri = MRIgetVoxelToVoxelXform(mri_seg, mri) ; for (x = 0 ; x < mri_seg->width ; x++) { V3_X(v_seg) = x ; for (y = 0 ; y < mri_seg->height ; y++) { V3_Y(v_seg) = y ; for (z = 0 ; z < mri_seg->depth ; z++) { V3_Z(v_seg) = z ; if (MRIvox(mri_seg, x, y, z) == segmentation_flag) { MatrixMultiply(m_seg_to_mri, v_seg, v_mri) ; xv = V3_X(v_mri) ; yv = V3_Y(v_mri) ; zv = V3_Z(v_mri) ; MRIsampleVolumeType(mri, xv, yv, zv, &val, SAMPLE_NEAREST); #if 0 if (val < .000001) { val *= 1000000; printf("%f*0.000001\n", val); } else #endif if (coords) printf("%2.1f %2.1f %2.1f %f\n", xv, yv, zv, val); else printf("%f\n", val); } } } } MatrixFree(&m_seg_to_mri) ; VectorFree(&v_seg) ; VectorFree(&v_mri) ; } else { if (cras == 1) fprintf(stderr,"using the label coordinates to be c_(r,a,s) != 0.\n"); if (surface_dir) { MRI_SURFACE *mris ; char fname[STRLEN] ; sprintf(fname, "%s/%s.white", surface_dir, hemi) ; mris = MRISread(fname) ; if (mris == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s...\n", Progname,fname) ; sprintf(fname, "%s/%s.thickness", surface_dir, hemi) ; if (MRISreadCurvatureFile(mris, fname) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read thickness file %s...\n", Progname,fname) ; if (annot_prefix) /* read an annotation in and print vals in it */ { #define MAX_ANNOT 10000 int vno, annot_counts[MAX_ANNOT], index ; VERTEX *v ; Real xw, yw, zw, xv, yv, zv, val ; float annot_means[MAX_ANNOT] ; FILE *fp ; memset(annot_means, 0, sizeof(annot_means)) ; memset(annot_counts, 0, sizeof(annot_counts)) ; if (MRISreadAnnotation(mris, label_name) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read annotation file %s...\n", Progname,fname) ; if (mris->ct == NULL) ErrorExit(ERROR_BADPARM, "%s: annot file does not contain a color table, specifiy one with -t ", Progname); for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->ripflag) continue ; CTABfindAnnotation(mris->ct, v->annotation, &index) ; if (index >= 0 && index < mris->ct->nentries) { annot_counts[index]++ ; xw = v->x + v->curv*.5*v->nx ; yw = v->y + v->curv*.5*v->ny ; zw = v->z + v->curv*.5*v->nz ; if (cras == 1) MRIworldToVoxel(mri, xw, yw, zw, &xv, &yv, &zv) ; else MRIsurfaceRASToVoxel(mri, xw, yw, zw, &xv, &yv, &zv); MRIsampleVolume(mri, xv, yv, zv, &val) ; annot_means[index] += val ; sprintf(fname, "%s-%s-%s.dat", annot_prefix, hemi, mris->ct->entries[index]->name) ; fp = fopen(fname, "a") ; fprintf(fp, "%f\n", val) ; fclose(fp) ; } } } else /* read label in and print vals in it */ { area = LabelRead(NULL, label_name) ; if (!area) ErrorExit(ERROR_NOFILE, "%s: could not read label from %s",Progname, label_name) ; } } else { area = LabelRead(NULL, label_name) ; if (!area) ErrorExit(ERROR_NOFILE, "%s: could not read label from %s",Progname, label_name) ; for (i = 0 ; i < area->n_points ; i++) { xw = area->lv[i].x ; yw = area->lv[i].y ; zw = area->lv[i].z ; if (cras == 1) MRIworldToVoxel(mri, xw, yw, zw, &xv, &yv, &zv) ; else MRIsurfaceRASToVoxel(mri, xw, yw, zw, &xv, &yv, &zv); MRIsampleVolumeType(mri, xv, yv, zv, &val, SAMPLE_NEAREST); #if 0 if (val < .000001) { val *= 1000000; printf("%f*0.000001\n", val); } else #endif printf("%f\n", val); } } } msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; if (DIAG_VERBOSE_ON) fprintf(stderr, "label value extractiong took %d minutes and %d seconds.\n", minutes, seconds) ; exit(0) ; return(0) ; }
MRI * add_aseg_structures_outside_ribbon(MRI *mri_src, MRI *mri_aseg, MRI *mri_dst, int wm_val, int gm_val, int csf_val) { VECTOR *v1, *v2 ; MATRIX *m_vox2vox ; int x, y, z, xa, ya, za, seg_label, aseg_label ; if (mri_dst == NULL) mri_dst = MRIcopy(mri_src, NULL) ; v1 = VectorAlloc(4, MATRIX_REAL) ; v2 = VectorAlloc(4, MATRIX_REAL) ; VECTOR_ELT(v1, 4) = 1.0 ; VECTOR_ELT(v2, 4) = 1.0 ; m_vox2vox = MRIgetVoxelToVoxelXform(mri_src, mri_aseg) ; for (x = 0 ; x < mri_dst->width ; x++) { V3_X(v1) = x ; for (y = 0 ; y < mri_dst->height ; y++) { V3_Y(v1) = y ; for (z = 0 ; z < mri_dst->depth ; z++) { if (x == Gx && y == Gy && z == Gz) DiagBreak() ; seg_label = nint(MRIgetVoxVal(mri_dst, x, y, z, 0)) ; V3_Z(v1) = z ; MatrixMultiply(m_vox2vox, v1, v2) ; xa = (int)(nint(V3_X(v2))) ; ya = (int)(nint(V3_Y(v2))) ; za = (int)(nint(V3_Z(v2))) ; if (xa < 0 || ya < 0 || za < 0 || xa >= mri_aseg->width || ya >= mri_aseg->height || za >= mri_aseg->depth) continue ; if (xa == Gx && ya == Gy && za == Gz) DiagBreak() ; aseg_label = nint(MRIgetVoxVal(mri_aseg, xa, ya, za, 0)) ; if (seg_label != 0 && !IS_MTL(aseg_label)) // already labeled and not amyg/hippo, skip it continue ; switch (aseg_label) { case Left_Cerebellum_White_Matter: case Right_Cerebellum_White_Matter: case Brain_Stem: MRIsetVoxVal(mri_dst, x, y, z, 0, wm_val) ; break ; case Left_Hippocampus: case Right_Hippocampus: case Left_Amygdala: case Right_Amygdala: case Left_Cerebellum_Cortex: case Right_Cerebellum_Cortex: case Left_Pallidum: case Right_Pallidum: case Left_Thalamus_Proper: case Right_Thalamus_Proper: case Right_Putamen: case Left_Putamen: case Right_Caudate: case Left_Caudate: case Left_Accumbens_area: case Right_Accumbens_area: // remove them from cortex MRIsetVoxVal(mri_dst, x, y, z, 0, gm_val) ; break ; default: break ; } } } } VectorFree(&v1) ; VectorFree(&v2) ; MatrixFree(&m_vox2vox) ; return(mri_dst) ; }
int MRIcomputePartialVolumeFractions(MRI *mri_src, MATRIX *m_vox2vox, MRI *mri_seg, MRI *mri_wm, MRI *mri_subcort_gm, MRI *mri_cortex, MRI *mri_csf, int wm_val, int subcort_gm_val, int cortex_val, int csf_val) { int x, y, z, xs, ys, zs, label ; VECTOR *v1, *v2 ; MRI *mri_counts ; float val, count ; MATRIX *m_inv ; m_inv = MatrixInverse(m_vox2vox, NULL) ; if (m_inv == NULL) { MatrixPrint(stdout, m_vox2vox) ; ErrorExit(ERROR_BADPARM, "MRIcomputePartialVolumeFractions: non-invertible vox2vox matrix"); } mri_counts = MRIcloneDifferentType(mri_src, MRI_INT) ; v1 = VectorAlloc(4, MATRIX_REAL) ; v2 = VectorAlloc(4, MATRIX_REAL) ; VECTOR_ELT(v1, 4) = 1.0 ; VECTOR_ELT(v2, 4) = 1.0 ; for (x = 0 ; x < mri_seg->width ; x++) { V3_X(v1) = x ; for (y = 0 ; y < mri_seg->height ; y++) { V3_Y(v1) = y ; for (z = 0 ; z < mri_seg->depth ; z++) { if (x == Gx && y == Gy && z == Gz) DiagBreak() ; V3_Z(v1) = z ; MatrixMultiply(m_vox2vox, v1, v2) ; xs = nint(V3_X(v2)) ; ys = nint(V3_Y(v2)) ; zs = nint(V3_Z(v2)) ; if (xs >= 0 && ys >= 0 && zs >= 0 && xs < mri_src->width && ys < mri_src->height && zs < mri_src->depth) { val = MRIgetVoxVal(mri_counts, xs, ys, zs, 0) ; MRIsetVoxVal(mri_counts, xs, ys, zs, 0, val+1) ; label = MRIgetVoxVal(mri_seg, x, y, z, 0) ; if (label == csf_val) { val = MRIgetVoxVal(mri_csf, xs, ys, zs, 0) ; MRIsetVoxVal(mri_csf, xs, ys, zs, 0, val+1) ; } else if (label == wm_val) { val = MRIgetVoxVal(mri_wm, xs, ys, zs, 0) ; MRIsetVoxVal(mri_wm, xs, ys, zs, 0, val+1) ; } else if (label == subcort_gm_val) { val = MRIgetVoxVal(mri_subcort_gm, xs, ys, zs, 0) ; MRIsetVoxVal(mri_subcort_gm, xs, ys, zs, 0, val+1) ; } else if (label == cortex_val) { val = MRIgetVoxVal(mri_cortex, xs, ys, zs, 0) ; MRIsetVoxVal(mri_cortex, xs, ys, zs, 0, val+1) ; } else DiagBreak() ; } } } } for (x = 0 ; x < mri_src->width ; x++) for (y = 0 ; y < mri_src->height ; y++) for (z = 0 ; z < mri_src->depth ; z++) { count = MRIgetVoxVal(mri_counts, x, y, z, 0) ; if (count >= 1) { if (x == Gx && y == Gy && z == Gz) DiagBreak() ; val = MRIgetVoxVal(mri_wm, x, y, z, 0) ; MRIsetVoxVal(mri_wm, x, y, z, 0, val/count) ; val = MRIgetVoxVal(mri_subcort_gm, x, y, z, 0) ; MRIsetVoxVal(mri_subcort_gm, x, y, z, 0, val/count) ; val = MRIgetVoxVal(mri_cortex, x, y, z, 0) ; MRIsetVoxVal(mri_cortex, x, y, z, 0, val/count) ; val = MRIgetVoxVal(mri_csf, x, y, z, 0) ; MRIsetVoxVal(mri_csf, x, y, z, 0, val/count) ; } else // sample in other direction { V3_X(v1) = x ; V3_Y(v1) = y ; V3_Z(v1) = z ; MatrixMultiply(m_inv, v1, v2) ; MatrixMultiply(m_inv, v1, v2) ; xs = nint(V3_X(v2)) ; ys = nint(V3_Y(v2)) ; zs = nint(V3_Z(v2)) ; if (xs >= 0 && ys >= 0 && zs >= 0 && xs < mri_seg->width && ys < mri_seg->height && zs < mri_seg->depth) { label = MRIgetVoxVal(mri_seg, xs, ys, zs, 0) ; if (label == csf_val) MRIsetVoxVal(mri_csf, x, y, z, 0, 1) ; else if (label == wm_val) MRIsetVoxVal(mri_wm, x, y, z, 0, 1) ; else if (label == subcort_gm_val) MRIsetVoxVal(mri_subcort_gm, x, y, z, 0, 1) ; else if (cortex_val) MRIsetVoxVal(mri_cortex, x, y, z, 0, 1) ; else DiagBreak() ; } } } VectorFree(&v1) ; VectorFree(&v2) ; MatrixFree(&m_inv) ; MRIfree(&mri_counts) ; return(NO_ERROR) ; }