int main(int argc, char *argv[]) { char **av, *in_fname,fname[STRLEN],hemi[10], path[STRLEN], name[STRLEN],*cp ; int ac, nargs, nhandles ; MRI_SURFACE *mris ; double ici, fi, var ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_curvature.c,v 1.31 2011/03/02 00:04:30 nicks Exp $", "$Name: stable5 $"); 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 < 2) { usage_exit() ; } in_fname = argv[1] ; FileNamePath(in_fname, path) ; FileNameOnly(in_fname, name) ; cp = strchr(name, '.') ; if (!cp) ErrorExit(ERROR_BADPARM, "%s: could not scan hemisphere from '%s'", Progname, fname) ; strncpy(hemi, cp-2, 2) ; hemi[2] = 0 ; if (patch_flag) /* read the orig surface, then the patch file */ { sprintf(fname, "%s/%s.orig", path, hemi) ; mris = MRISfastRead(fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, in_fname) ; if (Gdiag & DIAG_SHOW) { fprintf(stderr, "reading patch file %s...\n", in_fname) ; } if (MRISreadPatch(mris, in_fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read patch file %s", Progname, in_fname) ; } else /* just read the surface normally */ { mris = MRISread(in_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, in_fname) ; } MRISsetNeighborhoodSize(mris, nbrs) ; if (nbhd_size > 0) { MRISsampleAtEachDistance(mris, nbhd_size, nbrs_per_distance) ; } if (max_mm > 0) { float ratio ; MRISstoreMetricProperties(mris) ; if (MRISreadCanonicalCoordinates(mris, "sphere") != NO_ERROR) { ErrorExit(ERROR_NOFILE, "%s: could not read canonical coordinates from ?h.sphere", Progname); } MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; MRISrestoreVertexPositions(mris, CANONICAL_VERTICES) ; MRIScomputeMetricProperties(mris) ; ratio = mris->orig_area / M_PI * mris->radius * mris->radius * 4.0 ; ratio = mris->orig_area / mris->total_area ; MRISscaleBrain(mris, mris, sqrt(ratio)) ; MRISsaveVertexPositions(mris, CANONICAL_VERTICES) ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRIScomputeMetricProperties(mris) ; MRIScomputeNeighbors(mris, max_mm) ; } if (param_file) { MRI_SP *mrisp ; mrisp = MRISPread(param_file) ; if (normalize_param) { MRISnormalizeFromParameterization(mrisp, mris, param_no) ; } else { MRISfromParameterization(mrisp, mris, param_no) ; } MRISPfree(&mrisp) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.param", path,name,suffix) ; fprintf(stderr, "writing parameterized curvature to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } else { MRIScomputeSecondFundamentalFormThresholded(mris, cthresh) ; nhandles = nint(1.0 - mris->Ktotal / (4.0*M_PI)) ; fprintf(stderr, "total integrated curvature = %2.3f*4pi (%2.3f) --> " "%d handles\n", (float)(mris->Ktotal/(4.0f*M_PI)), (float)mris->Ktotal, nhandles) ; #if 0 fprintf(stderr, "0: k1 = %2.3f, k2 = %2.3f, H = %2.3f, K = %2.3f\n", mris->vertices[0].k1, mris->vertices[0].k2, mris->vertices[0].H, mris->vertices[0].K) ; fprintf(stderr, "0: vnum = %d, v2num = %d, total=%d, area=%2.3f\n", mris->vertices[0].vnum, mris->vertices[0].v2num, mris->vertices[0].vtotal,mris->vertices[0].area) ; #endif MRIScomputeCurvatureIndices(mris, &ici, &fi); var = MRIStotalVariation(mris) ; fprintf(stderr,"ICI = %2.1f, FI = %2.1f, variation=%2.3f\n", ici, fi, var); if (diff_flag) { MRISuseCurvatureDifference(mris) ; MRISaverageCurvatures(mris, navgs) ; sprintf(fname, "%s/%s%s.diff", path,name,suffix) ; fprintf(stderr, "writing curvature difference to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } if (ratio_flag) { MRISuseCurvatureRatio(mris) ; MRISaverageCurvatures(mris, navgs) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.ratio", path,name,suffix) ; fprintf(stderr, "writing curvature ratio to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } if (contrast_flag) { MRISuseCurvatureContrast(mris) ; MRISaverageCurvatures(mris, navgs) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.contrast", path,name,suffix) ; fprintf(stderr, "writing curvature contrast to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } if (neg_flag) { int neg ; if (mris->patch) { mris->status = MRIS_PLANE ; } MRIScomputeMetricProperties(mris) ; neg = MRIScountNegativeTriangles(mris) ; MRISuseNegCurvature(mris) ; MRISaverageCurvatures(mris, navgs) ; sprintf(fname, "%s/%s%s.neg", path,name,suffix) ; fprintf(stderr, "writing negative vertex curvature to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "%d negative triangles\n", neg) ; fprintf(stderr, "done.\n") ; { int vno, fno ; VERTEX *v ; FACE *f ; for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->ripflag) { continue ; } neg = 0 ; for (fno = 0 ; fno < v->num ; fno++) { f = &mris->faces[v->f[fno]] ; if (f->area < 0.0f) { neg = 1 ; } } if (neg) { fprintf(stdout, "%d\n", vno) ; } } } } if (max_flag) { MRISuseCurvatureMax(mris) ; MRISaverageCurvatures(mris, navgs) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.max", path,name,suffix) ; fprintf(stderr, "writing curvature maxima to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } if (min_flag) { MRISuseCurvatureMin(mris) ; MRISaverageCurvatures(mris, navgs) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.min", path,name,suffix) ; fprintf(stderr, "writing curvature minima to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } if (stretch_flag) { MRISreadOriginalProperties(mris, NULL) ; MRISuseCurvatureStretch(mris) ; MRISaverageCurvatures(mris, navgs) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.stretch", path,name,suffix) ; fprintf(stderr, "writing curvature stretch to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } if (write_flag) { MRISuseGaussianCurvature(mris) ; if (cthresh > 0) { MRIShistoThresholdCurvature(mris, cthresh) ; } MRISaverageCurvatures(mris, navgs) ; sprintf(fname, "%s/%s%s.K", path,name, suffix) ; fprintf(stderr, "writing Gaussian curvature to %s...", fname) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } MRISwriteCurvature(mris, fname) ; MRISuseMeanCurvature(mris) ; if (cthresh > 0) { MRIShistoThresholdCurvature(mris, cthresh) ; } MRISaverageCurvatures(mris, navgs) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.H", path,name, suffix) ; fprintf(stderr, "done.\nwriting mean curvature to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } } exit(0) ; return(0) ; /* for ansi */ }
int main(int argc, char *argv[]) { char *in_fname, *out_fname, **av, *xform_fname, fname[STRLEN] ; MRI *mri_in, *mri_tmp ; int ac, nargs, msec, minutes, seconds; int input, ninputs ; struct timeb start ; TRANSFORM *transform = NULL ; char cmdline[CMD_LINE_LEN], line[STRLEN], *cp, subject[STRLEN], sdir[STRLEN], base_name[STRLEN] ; FILE *fp ; make_cmd_version_string (argc, argv, "$Id: mri_fuse_intensity_images.c,v 1.2 2011/06/02 14:05:10 fischl Exp $", "$Name: $", cmdline); /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mri_fuse_intensity_images.c,v 1.2 2011/06/02 14:05:10 fischl Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; setRandomSeed(-1L) ; Progname = argv[0] ; DiagInit(NULL, NULL, NULL) ; ErrorInit(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 < 5) ErrorExit (ERROR_BADPARM, "usage: %s [<options>] <longitudinal time point file> <in vol> <transform file> <out vol> \n", Progname) ; in_fname = argv[2] ; xform_fname = argv[3] ; out_fname = argv[4] ; transform = TransformRead(xform_fname) ; if (transform == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read transform from %s", Progname, xform_fname) ; TimerStart(&start) ; FileNamePath(argv[1], sdir) ; cp = strrchr(sdir, '/') ; if (cp) { strcpy(base_name, cp+1) ; *cp = 0 ; // remove last component of path, which is base subject name } ninputs = 0 ; fp = fopen(argv[1], "r") ; if (fp == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read time point file %s", Progname, argv[1]) ; do { cp = fgetl(line, STRLEN-1, fp) ; if (cp != NULL && strlen(cp) > 0) { subjects[ninputs] = (char *)calloc(strlen(cp)+1, sizeof(char)) ; strcpy(subjects[ninputs], cp) ; ninputs++ ; } } while (cp != NULL && strlen(cp) > 0) ; fclose(fp) ; printf("processing %d timepoints in SUBJECTS_DIR %s...\n", ninputs, sdir) ; for (input = 0 ; input < ninputs ; input++) { sprintf(subject, "%s.long.%s", subjects[input], base_name) ; printf("reading subject %s - %d of %d\n", subject, input+1, ninputs) ; sprintf(fname, "%s/%s/mri/%s", sdir, subject, in_fname) ; mri_tmp = MRIread(fname) ; if (!mri_tmp) ErrorExit(ERROR_NOFILE, "%s: could not read input MR volume from %s", Progname, fname) ; MRImakePositive(mri_tmp, mri_tmp) ; if (input == 0) { mri_in = MRIallocSequence(mri_tmp->width, mri_tmp->height, mri_tmp->depth, mri_tmp->type, ninputs) ; if (!mri_in) ErrorExit(ERROR_NOMEMORY, "%s: could not allocate input volume %dx%dx%dx%d", mri_tmp->width,mri_tmp->height,mri_tmp->depth,ninputs) ; MRIcopyHeader(mri_tmp, mri_in) ; } if (mask_fname) { int i ; 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) ; for (i = 1 ; i < WM_MIN_VAL ; i++) MRIreplaceValues(mri_mask, mri_mask, i, 0) ; MRImask(mri_tmp, mri_mask, mri_tmp, 0, 0) ; MRIfree(&mri_mask) ; } MRIcopyFrame(mri_tmp, mri_in, 0, input) ; MRIfree(&mri_tmp) ; } MRIaddCommandLine(mri_in, cmdline) ; // try to bring the images closer to each other at each voxel where they seem to come from the same distribution { MRI *mri_frame1, *mri_frame2 ; double rms_after ; mri_frame1 = MRIcopyFrame(mri_in, NULL, 0, 0) ; mri_frame2 = MRIcopyFrame(mri_in, NULL, 1, 0) ; rms_after = MRIrmsDiff(mri_frame1, mri_frame2) ; printf("RMS before intensity cohering = %2.2f\n", rms_after) ; MRIfree(&mri_frame1) ; MRIfree(&mri_frame2) ; if (0) normalize_timepoints(mri_in, 2.0, cross_time_sigma) ; else normalize_timepoints_with_parzen_window(mri_in, cross_time_sigma) ; mri_frame1 = MRIcopyFrame(mri_in, NULL, 0, 0) ; mri_frame2 = MRIcopyFrame(mri_in, NULL, 1, 0) ; rms_after = MRIrmsDiff(mri_frame1, mri_frame2) ; MRIfree(&mri_frame1) ; MRIfree(&mri_frame2) ; printf("RMS after intensity cohering = %2.2f (sigma=%2.2f)\n", rms_after, cross_time_sigma) ; } for (input = 0 ; input < ninputs ; input++) { sprintf(fname, "%s/%s.long.%s/mri/%s", sdir, subjects[input], base_name, out_fname) ; printf("writing normalized volume to %s...\n", fname) ; if (MRIwriteFrame(mri_in, fname, input) != NO_ERROR) ErrorExit(ERROR_BADFILE, "%s: could not write normalized volume to %s",Progname, fname); } MRIfree(&mri_in) ; printf("done.\n") ; msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; printf("normalization took %d minutes and %d seconds.\n", minutes, seconds) ; if (diag_fp) fclose(diag_fp) ; exit(0) ; return(0) ; }
int main(int argc, char *argv[]) { char **av, *avg_surf_name, *canon_surf_name, fname[STRLEN], *mdir, ico_fname[STRLEN], *hemi, *out_sname ; int ac, nargs, i, vno, n ; VERTEX *v ; MRI_SURFACE *mris_ico ; MRI_SP *mrisp_total ; LTA *lta ; VOL_GEOM vg; float average_surface_area = 0.0 ; MATRIX *XFM=NULL; GCA_MORPH *gcam=NULL; memset((void *) &vg, 0, sizeof (VOL_GEOM)); /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_make_average_surface.c,v 1.29 2011/03/02 00:04:33 nicks Exp $", "$Name: stable5 $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; mdir = getenv("FREESURFER_HOME") ; if (!mdir) ErrorExit(ERROR_BADPARM, "%s: no FREESURFER_HOME in environment.\n",Progname); ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (sdir == NULL) { sdir = getenv("SUBJECTS_DIR"); if (!sdir) ErrorExit(ERROR_BADPARM, "%s: no SUBJECTS_DIR in environment.\n",Progname); } if (sdirout == NULL) sdirout = sdir; if (argc < 6) usage_exit() ; hemi = argv[1] ; avg_surf_name = argv[2] ; canon_surf_name = argv[3] ; out_sname = argv[4] ; printf("---------------------------------------------------\n"); printf("hemi = %s\n",hemi); printf("avg_surf_name = %s\n",avg_surf_name); printf("canon_surf_name = %s\n",canon_surf_name); printf("out_sname = %s\n",out_sname); printf("xform = %s\n",xform_name); printf("---------------------------------------------------\n"); printf("\n\n"); fflush(stdout); #define SCALE 1 mrisp_total = MRISPalloc(SCALE, 3) ; for (n = 0, i = 5 ; i < argc ; i++) { MRI *mri; MRI_SURFACE *mris; MRI_SP *mrisp; printf("\n---------------------------------------------------\n"); printf("#@# processing subject %d/%d %s...\n", i-4,argc-5,argv[i]) ; fflush(stdout); // read sphere.reg sprintf(fname, "%s/%s/surf/%s.%s", sdir, argv[i], hemi, canon_surf_name) ; printf(" Reading %s\n",fname); fflush(stdout); mris = MRISread(fname) ; if (!mris) { ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, fname) ; exit(1); } // get "pial" surface vertex into ->origx, origy, origz if (MRISreadOriginalProperties(mris, orig_name) != NO_ERROR) ErrorExit(ERROR_BADFILE,"%s: could not read orig file for %s.\n", Progname, argv[1]); // read transform if (0) { sprintf(fname, "%s/%s/mri/transforms/%s", sdir, argv[i], xform_name) ; lta = LTAreadEx(fname) ; if (!lta) ErrorExit(ERROR_BADPARM, "%s: could not read transform from %s", Progname, fname) ; } // read T1 volume sprintf(fname, "%s/%s/mri/T1.mgz", sdir, argv[i]) ; if (fio_FileExistsReadable(fname)) mri = MRIreadHeader(fname,MRI_MGH_FILE); else { sprintf(fname, "%s/%s/mri/T1", sdir, argv[i]) ; mri = MRIreadHeader(fname, MRI_UCHAR); // MRI_CORONAL_SLICE_DIRECTORY) ; } printf(" Read %s\n",fname); fflush(stdout); if (!mri) ErrorExit(ERROR_BADPARM, "%s: could not read reference MRI volume from %s", Progname, fname) ; // save current vertex position into ->cx MRISsaveVertexPositions(mris, CANONICAL_VERTICES) ; // get the vertex position from ->origx, ... // (get the "pial" vertex position) MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRIScomputeMetricProperties(mris) ; printf(" Surface area: %2.1f cm^2\n", mris->total_area/100) ; fflush(stdout); average_surface_area += mris->total_area ; // this means that we transform "pial" surface if (xform_name) { if (!strcmp(xform_name,"talairach.xfm")) { printf(" Applying linear transform\n"); fflush(stdout); XFM = DevolveXFMWithSubjectsDir(argv[i], NULL, "talairach.xfm", sdir); if (XFM == NULL) exit(1); MRISmatrixMultiply(mris, XFM); MatrixFree(&XFM); } else if (!strcmp(xform_name,"talairach.m3z")) { printf(" Applying GCA Morph\n"); fflush(stdout); sprintf(fname, "%s/%s/mri/transforms/talairach.m3z", sdir, argv[i]) ; gcam = GCAMreadAndInvert(fname); if (gcam == NULL) exit(1); GCAMmorphSurf(mris, gcam); GCAMfree(&gcam); } else { printf("ERROR: don't know what to do with %s\n",xform_name); exit(1); } } // save transformed position in ->orig // (store "pial" vertices position in orig) MRIScomputeMetricProperties(mris) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; // get the vertex position from ->cx // (note that this is not transformed) sphere.reg vertices MRISrestoreVertexPositions(mris, CANONICAL_VERTICES) ; // mris contains sphere.reg in vertex and pial vertices in orig // map to a theta-phi space and accumulate values mrisp = MRIScoordsToParameterization(mris, NULL, SCALE, ORIGINAL_VERTICES) ; MRISPaccumulate(mrisp, mrisp_total, 0) ; MRISPaccumulate(mrisp, mrisp_total, 1) ; MRISPaccumulate(mrisp, mrisp_total, 2) ; MRISPfree(&mrisp) ; MRISfree(&mris) ; MRIfree(&mri) ; //LTAfree(<a) ; fflush(stdout); n++ ; } printf("Finished loading all data\n"); average_surface_area /= (float)n ; printf("Avg surf area = %g cm\n",average_surface_area/100.0); fflush(stdout); // mrisp_total lost info on the modified surface sprintf(ico_fname, "%s/lib/bem/ic%d.tri", mdir, ico_no) ; printf("Reading icosahedron from %s...\n", ico_fname) ; mris_ico = ICOread(ico_fname) ; if (!mris_ico) ErrorExit(ERROR_NOFILE, "%s: could not read icosahedron file %s\n", Progname,ico_fname) ; MRISscaleBrain(mris_ico, mris_ico, DEFAULT_RADIUS/MRISaverageRadius(mris_ico)) ; // save current ico position to ->cx, cy, cz MRISsaveVertexPositions(mris_ico, CANONICAL_VERTICES) ; // using mrisp_total to calculate position into ->origx, origy, origz // (orig is the "pial" vertices) MRIScoordsFromParameterization(mrisp_total, mris_ico, ORIGINAL_VERTICES) ; // copy geometry info memcpy((void *) &mris_ico->vg, (void *) &vg, sizeof (VOL_GEOM)); if (Gdiag_no >= 0 && Gdiag_no < mris_ico->nvertices) { int n ; VERTEX *vn ; v = &mris_ico->vertices[Gdiag_no] ; printf( "v %d: x = (%2.2f, %2.2f, %2.2f)\n", Gdiag_no, v->origx, v->origy, v->origz) ; for (n = 0 ; n < v->vnum ; n++) { vn = &mris_ico->vertices[v->v[n]] ; printf( "v %d: x = (%2.2f, %2.2f, %2.2f)\n", v->v[n], vn->origx, vn->origy, vn->origz) ; } } // write *h.sphere.reg sprintf(fname, "%s/%s/surf/%s.%s", sdirout, out_sname, hemi, canon_surf_name) ; if (Gdiag & DIAG_SHOW) printf("writing average canonical surface to %s\n", fname); MRISwrite(mris_ico, fname) ; // get "pial vertices" from orig MRISrestoreVertexPositions(mris_ico, ORIG_VERTICES); for (vno = 0 ; vno < mris_ico->nvertices ; vno++) { v = &mris_ico->vertices[vno] ; // n = number of subjects v->x /= (float)n ; v->y /= (float)n ; v->z /= (float)n ; } if (normalize_area) { MRIScomputeMetricProperties(mris_ico) ; printf("setting group surface area to be %2.1f cm^2 (scale=%2.2f)\n", average_surface_area/100.0, sqrt(average_surface_area/mris_ico->total_area)) ; #if 0 MRISscaleBrain(mris_ico, mris_ico, sqrt(average_surface_area/mris_ico->total_area)) ; #else mris_ico->group_avg_surface_area = average_surface_area ; #endif MRIScomputeMetricProperties(mris_ico) ; } sprintf(fname, "%s/%s/surf/%s.%s", sdirout,out_sname, hemi, avg_surf_name) ; printf("writing average %s surface to %s\n", avg_surf_name, fname); MRISwrite(mris_ico, fname) ; if (0) { char path[STRLEN] ; LTA *lta ; FileNamePath(fname, path) ; lta = LTAalloc(1, NULL) ; // write to a different location sprintf(fname, "%s/../mri/transforms/%s", path,xform_name) ; LTAwriteEx(lta, fname) ; LTAfree(<a) ; } MRISfree(&mris_ico) ; MRISPfree(&mrisp_total) ; printf("mris_make_average_surface done\n"); exit(0) ; return(0) ; /* for ansi */ }
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) ; }
int main(int argc, char *argv[]) { char **av, *in_fname, *out_fname, path[STRLEN], fname[STRLEN], hemi[STRLEN], *cp ; int ac, nargs ; MRI_SURFACE *mris ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_reverse.c,v 1.10 2011/03/02 00:04:33 nicks 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 < 3) usage_exit() ; in_fname = argv[1] ; out_fname = argv[2] ; if (patch_flag) { FileNamePath(in_fname, path) ; FileNameOnly(in_fname, hemi) ; cp = strchr(hemi, '.') ; if (cp) *cp = 0 ; else ErrorExit(ERROR_BADPARM, "%s: could not scan hemisphere from %s\n", in_fname) ; sprintf(fname, "%s/%s.%s", path, hemi, ORIG_NAME) ; mris = MRISread(fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, fname) ; if (MRISreadPatch(mris, in_fname) != NO_ERROR) ErrorExit(Gerror, "%s: could not read patch\n", Progname) ; } else { mris = MRISread(in_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, in_fname) ; } FileNamePath(out_fname, path) ; MRISreverse(mris, which, 1) ; if (Gdiag & DIAG_SHOW) fprintf(stderr, "writing reversed surface to %s\n", out_fname) ; mris->type = MRIS_TRIANGULAR_SURFACE ; if (patch_flag) MRISwritePatch(mris, out_fname) ; else MRISwrite(mris, out_fname) ; exit(0) ; return(0) ; /* for ansi */ }
int main(int argc, char *argv[]) { char **av, in_surf_fname[STRLEN], *in_patch_fname, *out_patch_fname, hemi[STRLEN] ; int ac, nargs; char path[STRLEN], out_surf_fname[STRLEN], *cp ; int msec, minutes, seconds ; struct timeb start ; MRI_SURFACE *mris_in, *mris_out ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_map_cuts.c,v 1.3 2011/03/02 00:04:33 nicks 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) ; in_patch_fname = argv[1] ; out_patch_fname = argv[2] ; FileNamePath(in_patch_fname, path) ; cp = strrchr(in_patch_fname, '/') ; if (!cp) cp = in_patch_fname ; cp = strchr(cp, '.') ; if (cp) { strncpy(hemi, cp-2, 2) ; hemi[2] = 0 ; } else strcpy(hemi, "lh") ; sprintf(in_surf_fname, "%s/%s.%s", path, hemi, orig_surf_name) ; FileNamePath(out_patch_fname, path) ; cp = strrchr(out_patch_fname, '/') ; if (!cp) cp = out_patch_fname ; cp = strchr(cp, '.') ; if (cp) { strncpy(hemi, cp-2, 2) ; hemi[2] = 0 ; } else strcpy(hemi, "lh") ; sprintf(out_surf_fname, "%s/%s.%s", path, hemi, orig_surf_name) ; mris_in = MRISread(in_surf_fname) ; mris_out = MRISread(out_surf_fname) ; MRISsaveVertexPositions(mris_in, CANONICAL_VERTICES) ; MRISsaveVertexPositions(mris_out, CANONICAL_VERTICES) ; if (MRISreadVertexPositions(mris_out, inf_surf_name) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not inflated surface %s", Progname, inf_surf_name) ; if (MRISreadPatch(mris_in, in_patch_fname) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read patch file %s", Progname, in_patch_fname) ; MRISmapCuts(mris_in, mris_out) ; if (dilate) { printf("dilating patch %d times\n", dilate) ; MRISdilateRipped(mris_out, dilate) ; printf("%d valid vertices (%2.1f %% of total)\n", MRISvalidVertices(mris_out), 100.0*MRISvalidVertices(mris_out)/mris_out->nvertices) ; } printf("writing output to %s\n", out_patch_fname) ; MRISwritePatch(mris_out, out_patch_fname) ; msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; fprintf(stderr, "cut mapping took %d minutes" " and %d seconds.\n", minutes, seconds) ; exit(0) ; return(0) ; }
int main(int argc, char *argv[]) { char *gca_fname, *in_fname, *out_fname, **av, *xform_fname, fname[STRLEN] ; MRI *mri_in, *mri_norm = NULL, *mri_tmp, *mri_ctrl = NULL ; GCA *gca ; int ac, nargs, nsamples, msec, minutes, seconds; int i, struct_samples, norm_samples = 0, n, input, ninputs ; struct timeb start ; GCA_SAMPLE *gcas, *gcas_norm = NULL, *gcas_struct ; TRANSFORM *transform = NULL ; char cmdline[CMD_LINE_LEN], line[STRLEN], *cp, subject[STRLEN], sdir[STRLEN], base_name[STRLEN] ; FILE *fp ; make_cmd_version_string (argc, argv, "$Id: mri_cal_normalize.c,v 1.2.2.1 2011/08/31 00:32:41 nicks Exp $", "$Name: stable5 $", cmdline); /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mri_cal_normalize.c,v 1.2.2.1 2011/08/31 00:32:41 nicks Exp $", "$Name: stable5 $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; setRandomSeed(-1L) ; Progname = argv[0] ; DiagInit(NULL, NULL, NULL) ; ErrorInit(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 < 6) ErrorExit (ERROR_BADPARM, "usage: %s [<options>] <longitudinal time point file> <in vol> <atlas> <transform file> <out vol> \n", Progname) ; in_fname = argv[2] ; gca_fname = argv[3] ; xform_fname = argv[4] ; out_fname = argv[5] ; transform = TransformRead(xform_fname) ; if (transform == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read transform from %s", Progname, xform_fname) ; if (read_ctrl_point_fname) { mri_ctrl = MRIread(read_ctrl_point_fname) ; if (mri_ctrl == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read precomputed control points from %s", Progname, read_ctrl_point_fname) ; } TimerStart(&start) ; printf("reading atlas from '%s'...\n", gca_fname) ; fflush(stdout) ; gca = GCAread(gca_fname) ; if (gca == NULL) ErrorExit(ERROR_NOFILE, "%s: could not open GCA %s.\n",Progname, gca_fname) ; GCAregularizeConditionalDensities(gca, .5) ; FileNamePath(argv[1], sdir) ; cp = strrchr(sdir, '/') ; if (cp) { strcpy(base_name, cp+1) ; *cp = 0 ; // remove last component of path, which is base subject name } ninputs = 0 ; fp = fopen(argv[1], "r") ; if (fp == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read time point file %s", argv[1]) ; do { cp = fgetl(line, STRLEN-1, fp) ; if (cp != NULL && strlen(cp) > 0) { subjects[ninputs] = (char *)calloc(strlen(cp)+1, sizeof(char)) ; strcpy(subjects[ninputs], cp) ; ninputs++ ; } } while (cp != NULL && strlen(cp) > 0) ; fclose(fp) ; printf("processing %d timepoints in SUBJECTS_DIR %s...\n", ninputs, sdir) ; for (input = 0 ; input < ninputs ; input++) { sprintf(subject, "%s.long.%s", subjects[input], base_name) ; printf("reading subject %s - %d of %d\n", subject, input+1, ninputs) ; sprintf(fname, "%s/%s/mri/%s", sdir, subject, in_fname) ; mri_tmp = MRIread(fname) ; if (!mri_tmp) ErrorExit(ERROR_NOFILE, "%s: could not read input MR volume from %s", Progname, fname) ; MRImakePositive(mri_tmp, mri_tmp) ; if (mri_tmp && ctrl_point_fname && !mri_ctrl) { mri_ctrl = MRIallocSequence(mri_tmp->width, mri_tmp->height, mri_tmp->depth,MRI_FLOAT, nregions*2) ; // labels and means MRIcopyHeader(mri_tmp, mri_ctrl) ; } if (input == 0) { mri_in = MRIallocSequence(mri_tmp->width, mri_tmp->height, mri_tmp->depth, mri_tmp->type, ninputs) ; if (!mri_in) ErrorExit(ERROR_NOMEMORY, "%s: could not allocate input volume %dx%dx%dx%d", mri_tmp->width,mri_tmp->height,mri_tmp->depth,ninputs) ; MRIcopyHeader(mri_tmp, mri_in) ; } if (mask_fname) { int i ; 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) ; for (i = 1 ; i < WM_MIN_VAL ; i++) MRIreplaceValues(mri_mask, mri_mask, i, 0) ; MRImask(mri_tmp, mri_mask, mri_tmp, 0, 0) ; MRIfree(&mri_mask) ; } MRIcopyFrame(mri_tmp, mri_in, 0, input) ; MRIfree(&mri_tmp) ; } MRIaddCommandLine(mri_in, cmdline) ; GCAhistoScaleImageIntensitiesLongitudinal(gca, mri_in, 1) ; { int j ; gcas = GCAfindAllSamples(gca, &nsamples, NULL, 1) ; printf("using %d sample points...\n", nsamples) ; GCAcomputeSampleCoords(gca, mri_in, gcas, nsamples, transform) ; if (sample_fname) GCAtransformAndWriteSamples (gca, mri_in, gcas, nsamples, sample_fname, transform) ; for (j = 0 ; j < 1 ; j++) { for (n = 1 ; n <= nregions ; n++) { for (norm_samples = i = 0 ; i < NSTRUCTURES ; i++) { if (normalization_structures[i] == Gdiag_no) DiagBreak() ; printf("finding control points in %s....\n", cma_label_to_name(normalization_structures[i])) ; gcas_struct = find_control_points(gca, gcas, nsamples, &struct_samples, n, normalization_structures[i], mri_in, transform, min_prior, ctl_point_pct) ; discard_unlikely_control_points(gca, gcas_struct, struct_samples, mri_in, transform, cma_label_to_name(normalization_structures[i])) ; if (mri_ctrl && ctrl_point_fname) // store the samples copy_ctrl_points_to_volume(gcas_struct, struct_samples, mri_ctrl, n-1) ; if (i) { GCA_SAMPLE *gcas_tmp ; gcas_tmp = gcas_concatenate(gcas_norm, gcas_struct, norm_samples, struct_samples) ; free(gcas_norm) ; norm_samples += struct_samples ; gcas_norm = gcas_tmp ; } else { gcas_norm = gcas_struct ; norm_samples = struct_samples ; } } printf("using %d total control points " "for intensity normalization...\n", norm_samples) ; if (normalized_transformed_sample_fname) GCAtransformAndWriteSamples(gca, mri_in, gcas_norm, norm_samples, normalized_transformed_sample_fname, transform) ; mri_norm = GCAnormalizeSamplesAllChannels(mri_in, gca, gcas_norm, file_only ? 0 :norm_samples, transform, ctl_point_fname, bias_sigma) ; if (Gdiag & DIAG_WRITE) { char fname[STRLEN] ; sprintf(fname, "norm%d.mgz", n) ; printf("writing normalized volume to %s...\n", fname) ; MRIwrite(mri_norm, fname) ; sprintf(fname, "norm_samples%d.mgz", n) ; GCAtransformAndWriteSamples(gca, mri_in, gcas_norm, norm_samples, fname, transform) ; } MRIcopy(mri_norm, mri_in) ; /* for next pass through */ MRIfree(&mri_norm) ; } } } // now do cross-time normalization to bring each timepoint closer to the mean at each location { MRI *mri_frame1, *mri_frame2, *mri_tmp ; double rms_before, rms_after ; int i ; mri_tmp = MRIcopy(mri_in, NULL) ; mri_frame1 = MRIcopyFrame(mri_in, NULL, 0, 0) ; mri_frame2 = MRIcopyFrame(mri_in, NULL, 1, 0) ; rms_before = MRIrmsDiff(mri_frame1, mri_frame2) ; printf("RMS before = %2.2f\n", rms_before) ; MRIfree(&mri_frame1) ; MRIfree(&mri_frame2) ; for (i = 50 ; i <= 50 ; i += 25) { MRIcopy(mri_tmp, mri_in) ; normalize_timepoints_with_samples(mri_in, gcas_norm, norm_samples, i) ; mri_frame1 = MRIcopyFrame(mri_in, NULL, 0, 0) ; mri_frame2 = MRIcopyFrame(mri_in, NULL, 1, 0) ; rms_after = MRIrmsDiff(mri_frame1, mri_frame2) ; MRIfree(&mri_frame1) ; MRIfree(&mri_frame2) ; printf("RMS after (%d) = %2.2f\n", i, rms_after) ; } } { MRI *mri_frame1, *mri_frame2 ; double rms_after ; int i ; mri_tmp = MRIcopy(mri_in, NULL) ; for (i = 10 ; i <= 10 ; i += 10) { MRIcopy(mri_tmp, mri_in) ; normalize_timepoints(mri_in, 2.0, i) ; mri_frame1 = MRIcopyFrame(mri_in, NULL, 0, 0) ; mri_frame2 = MRIcopyFrame(mri_in, NULL, 1, 0) ; rms_after = MRIrmsDiff(mri_frame1, mri_frame2) ; MRIfree(&mri_frame1) ; MRIfree(&mri_frame2) ; printf("RMS after intensity cohering = %2.2f\n", rms_after) ; } } for (input = 0 ; input < ninputs ; input++) { sprintf(fname, "%s/%s.long.%s/mri/%s", sdir, subjects[input], base_name, out_fname) ; printf("writing normalized volume to %s...\n", fname) ; if (MRIwriteFrame(mri_in, fname, input) != NO_ERROR) ErrorExit(ERROR_BADFILE, "%s: could not write normalized volume to %s",Progname, fname); } if (ctrl_point_fname) { printf("writing control points to %s\n", ctrl_point_fname) ; MRIwrite(mri_ctrl, ctrl_point_fname) ; MRIfree(&mri_ctrl) ; } MRIfree(&mri_in) ; printf("freeing GCA...") ; if (gca) GCAfree(&gca) ; printf("done.\n") ; msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; printf("normalization took %d minutes and %d seconds.\n", minutes, seconds) ; if (diag_fp) fclose(diag_fp) ; exit(0) ; return(0) ; }
int main(int argc, char *argv[]) { char **av, *surf_fname, *template_fname, *out_fname, fname[STRLEN],*cp; int ac, nargs,err, msec ; MRI_SURFACE *mris ; MRI_SP *mrisp_template ; char cmdline[CMD_LINE_LEN] ; struct timeb start ; make_cmd_version_string (argc, argv, "$Id: mris_register.c,v 1.59 2011/03/02 00:04:33 nicks Exp $", "$Name: stable5 $", cmdline); /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_register.c,v 1.59 2011/03/02 00:04:33 nicks Exp $", "$Name: stable5 $"); if (nargs && argc - nargs == 1) { exit (0); } argc -= nargs; TimerStart(&start) ; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; memset(&parms, 0, sizeof(parms)) ; parms.projection = PROJECT_SPHERE ; parms.flags |= IP_USE_CURVATURE ; parms.tol = 0.5 ; // was 1e-0*2.5 parms.min_averages = 0 ; parms.l_area = 0.0 ; parms.l_parea = 0.1f ; // used to be 0.2 parms.l_dist = 5.0 ; // used to be 0.5, and before that 0.1 parms.l_corr = 1.0f ; parms.l_nlarea = 1 ; parms.l_pcorr = 0.0f ; parms.niterations = 25 ; parms.n_averages = 1024 ; // used to be 256 parms.write_iterations = 100 ; parms.dt_increase = 1.01 /* DT_INCREASE */; parms.dt_decrease = 0.99 /* DT_DECREASE*/ ; parms.error_ratio = 1.03 /*ERROR_RATIO */; parms.dt_increase = 1.0 ; parms.dt_decrease = 1.0 ; parms.l_external = 10000 ; /* in case manual label is specified */ parms.error_ratio = 1.1 /*ERROR_RATIO */; parms.integration_type = INTEGRATE_ADAPTIVE ; parms.integration_type = INTEGRATE_MOMENTUM /*INTEGRATE_LINE_MINIMIZE*/ ; parms.integration_type = INTEGRATE_LINE_MINIMIZE ; parms.dt = 0.9 ; parms.momentum = 0.95 ; parms.desired_rms_height = -1.0 ; parms.nbhd_size = -10 ; parms.max_nbrs = 10 ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (nsigmas > 0) { MRISsetRegistrationSigmas(sigmas, nsigmas) ; } parms.which_norm = which_norm ; if (argc < 4) { usage_exit() ; } printf("%s\n", vcid) ; printf(" %s\n",MRISurfSrcVersion()); fflush(stdout); surf_fname = argv[1] ; template_fname = argv[2] ; out_fname = argv[3] ; if (parms.base_name[0] == 0) { FileNameOnly(out_fname, fname) ; cp = strchr(fname, '.') ; if (cp) { strcpy(parms.base_name, cp+1) ; } else { strcpy(parms.base_name, "sphere") ; } } fprintf(stderr, "reading surface from %s...\n", surf_fname) ; mris = MRISread(surf_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; if (parms.var_smoothness) { parms.vsmoothness = (float *)calloc(mris->nvertices, sizeof(float)) ; if (parms.vsmoothness == NULL) { ErrorExit(ERROR_NOMEMORY, "%s: could not allocate vsmoothness array", Progname) ; } parms.dist_error = (float *)calloc(mris->nvertices, sizeof(float)) ; if (parms.dist_error == NULL) { ErrorExit(ERROR_NOMEMORY, "%s: could not allocate dist_error array", Progname) ; } parms.area_error = (float *)calloc(mris->nvertices, sizeof(float)) ; if (parms.area_error == NULL) { ErrorExit(ERROR_NOMEMORY, "%s: could not allocate area_error array", Progname) ; } parms.geometry_error = (float *)calloc(mris->nvertices, sizeof(float)) ; if (parms.geometry_error == NULL) { ErrorExit(ERROR_NOMEMORY, "%s: could not allocate geometry_error array", Progname) ; } } MRISresetNeighborhoodSize(mris, 1) ; if (annot_name) { if (MRISreadAnnotation(mris, annot_name) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read annot file %s", Progname, annot_name) ; MRISripMedialWall(mris) ; } MRISsaveVertexPositions(mris, TMP2_VERTICES) ; MRISaddCommandLine(mris, cmdline) ; if (!FZERO(dalpha) || !FZERO(dbeta) || !FZERO(dgamma)) MRISrotate(mris, mris, RADIANS(dalpha), RADIANS(dbeta), RADIANS(dgamma)) ; if (curvature_fname[0]) { fprintf(stderr, "reading source curvature from %s\n",curvature_fname) ; MRISreadCurvatureFile(mris, curvature_fname) ; } if (single_surf) { char fname[STRLEN], *cp, surf_dir[STRLEN], hemi[10] ; MRI_SURFACE *mris_template ; int sno, tnbrs=3 ; FileNamePath(template_fname, surf_dir) ; cp = strrchr(template_fname, '/') ; if (cp == NULL) // no path - start from beginning of file name { cp = template_fname ; } cp = strchr(cp, '.') ; if (cp == NULL) ErrorExit(ERROR_NOFILE, "%s: could no scan hemi from %s", Progname, template_fname) ; strncpy(hemi, cp-2, 2) ; hemi[2] = 0 ; fprintf(stderr, "reading spherical surface %s...\n", template_fname) ; mris_template = MRISread(template_fname) ; if (mris_template == NULL) { ErrorExit(ERROR_NOFILE, "") ; } #if 0 if (reverse_flag) { MRISreverse(mris_template, REVERSE_X, 1) ; } #endif MRISsaveVertexPositions(mris_template, CANONICAL_VERTICES) ; MRIScomputeMetricProperties(mris_template) ; MRISstoreMetricProperties(mris_template) ; if (noverlays > 0) { mrisp_template = MRISPalloc(scale, IMAGES_PER_SURFACE*noverlays); for (sno = 0; sno < noverlays ; sno++) { sprintf(fname, "%s/../label/%s.%s", surf_dir, hemi, overlays[sno]) ; if (MRISreadValues(mris_template, fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read overlay from %s", Progname, fname) ; MRIScopyValuesToCurvature(mris_template) ; MRISaverageCurvatures(mris_template, navgs) ; MRISnormalizeCurvature(mris_template, which_norm) ; fprintf(stderr, "computing parameterization for overlay %s...\n", fname); MRIStoParameterization(mris_template, mrisp_template, scale, sno*3) ; MRISPsetFrameVal(mrisp_template, sno*3+1, 1.0) ; } } else { mrisp_template = MRISPalloc(scale, PARAM_IMAGES); for (sno = 0; sno < SURFACES ; sno++) { if (curvature_names[sno]) /* read in precomputed curvature file */ { sprintf(fname, "%s/%s.%s", surf_dir, hemi, curvature_names[sno]) ; if (MRISreadCurvatureFile(mris_template, fname) != NO_ERROR) ErrorExit(Gerror, "%s: could not read curvature file '%s'\n", Progname, fname) ; /* the two next lines were not in the original code */ MRISaverageCurvatures(mris_template, navgs) ; MRISnormalizeCurvature(mris_template, which_norm) ; } else /* compute curvature of surface */ { sprintf(fname, "%s/%s.%s", surf_dir, hemi, surface_names[sno]) ; if (MRISreadVertexPositions(mris_template, fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, fname) ; if (tnbrs > 1) { MRISresetNeighborhoodSize(mris_template, tnbrs) ; } MRIScomputeMetricProperties(mris_template) ; MRIScomputeSecondFundamentalForm(mris_template) ; MRISuseMeanCurvature(mris_template) ; MRISaverageCurvatures(mris_template, navgs) ; MRISrestoreVertexPositions(mris_template, CANONICAL_VERTICES) ; MRISnormalizeCurvature(mris_template, which_norm) ; } fprintf(stderr, "computing parameterization for surface %s...\n", fname); MRIStoParameterization(mris_template, mrisp_template, scale, sno*3) ; MRISPsetFrameVal(mrisp_template, sno*3+1, 1.0) ; } } } else { fprintf(stderr, "reading template parameterization from %s...\n", template_fname) ; mrisp_template = MRISPread(template_fname) ; if (!mrisp_template) ErrorExit(ERROR_NOFILE, "%s: could not open template file %s", Progname, template_fname) ; if (noverlays > 0) { if (mrisp_template->Ip->num_frame != IMAGES_PER_SURFACE*noverlays) ErrorExit(ERROR_BADPARM, "template frames (%d) doesn't match input (%d x %d) = %d\n", mrisp_template->Ip->num_frame, IMAGES_PER_SURFACE,noverlays, IMAGES_PER_SURFACE*noverlays) ; } } if (use_defaults) { if (*IMAGEFseq_pix(mrisp_template->Ip, 0, 0, 2) <= 1.0) /* 1st time */ { parms.l_dist = 5.0 ; parms.l_corr = 1.0 ; parms.l_parea = 0.2 ; } else /* subsequent alignments */ { parms.l_dist = 5.0 ; parms.l_corr = 1.0 ; parms.l_parea = 0.2 ; } } if (nbrs > 1) { MRISresetNeighborhoodSize(mris, nbrs) ; } MRISprojectOntoSphere(mris, mris, DEFAULT_RADIUS) ; mris->status = MRIS_PARAMETERIZED_SPHERE ; MRIScomputeMetricProperties(mris) ; if (!FZERO(parms.l_dist)) { MRISscaleDistances(mris, scale) ; } #if 0 MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; MRISzeroNegativeAreas(mris) ; MRISstoreMetricProperties(mris) ; #endif MRISstoreMeanCurvature(mris) ; /* use curvature from file */ MRISsetOriginalFileName(orig_name) ; if (inflated_name) { MRISsetInflatedFileName(inflated_name) ; } err = MRISreadOriginalProperties(mris, orig_name) ; if (err != 0) { printf("ERROR %d from MRISreadOriginalProperties().\n",err); exit(1); } if (MRISreadCanonicalCoordinates(mris, canon_name) != NO_ERROR) ErrorExit(ERROR_BADFILE, "%s: could not read canon surface %s", Progname, canon_name) ; if (reverse_flag) { MRISreverse(mris, REVERSE_X, 1) ; MRISsaveVertexPositions(mris, TMP_VERTICES) ; MRISrestoreVertexPositions(mris, CANONICAL_VERTICES) ; MRISreverse(mris, REVERSE_X, 0) ; MRISsaveVertexPositions(mris, CANONICAL_VERTICES) ; MRISrestoreVertexPositions(mris, TMP_VERTICES) ; MRIScomputeMetricProperties(mris) ; } #if 0 MRISsaveVertexPositions (mris, CANONICAL_VERTICES) ; // uniform spherical positions #endif if (starting_reg_fname) if (MRISreadVertexPositions(mris, starting_reg_fname) != NO_ERROR) { exit(Gerror) ; } if (multiframes) { if (use_initial_registration) MRISvectorRegister(mris, mrisp_template, &parms, max_passes, min_degrees, max_degrees, nangles) ; parms.l_corr=parms.l_pcorr=0.0f; #if 0 parms.l_dist = 0.0 ; parms.l_corr = 0.0 ; parms.l_parea = 0.0 ; parms.l_area = 0.0 ; parms.l_parea = 0.0f ; parms.l_dist = 0.0 ; parms.l_corr = 0.0f ; parms.l_nlarea = 0.0f ; parms.l_pcorr = 0.0f ; #endif MRISvectorRegister(mris, mrisp_template, &parms, max_passes, min_degrees, max_degrees, nangles) ; } else { double l_dist = parms.l_dist ; if (multi_scale > 0) { int i ; parms.l_dist = l_dist * pow(5.0, (multi_scale-1.0)) ; parms.flags |= IPFLAG_NOSCALE_TOL ; parms.flags &= ~IP_USE_CURVATURE ; for (i = 0 ; i < multi_scale ; i++) { printf("*************** round %d, l_dist = %2.3f **************\n", i, parms.l_dist) ; MRISregister(mris, mrisp_template, &parms, max_passes, min_degrees, max_degrees, nangles) ; parms.flags |= IP_NO_RIGID_ALIGN ; parms.flags &= ~IP_USE_INFLATED ; parms.l_dist /= 5 ; } if (parms.nbhd_size < 0) { parms.nbhd_size *= -1 ; printf("**** starting 2nd epoch, with long-range distances *****\n"); parms.l_dist = l_dist * pow(5.0, (multi_scale-2.0)) ; for (i = 1 ; i < multi_scale ; i++) { printf("*********** round %d, l_dist = %2.3f *************\n", i, parms.l_dist) ; MRISregister(mris, mrisp_template, &parms, max_passes, min_degrees, max_degrees, nangles) ; parms.l_dist /= 5 ; } } printf("****** final curvature registration ***************\n") ; if (parms.nbhd_size > 0) { parms.nbhd_size *= -1 ; // disable long-range stuff } parms.l_dist *= 5 ; parms.flags |= (IP_USE_CURVATURE | IP_NO_SULC); MRISregister(mris, mrisp_template, &parms, max_passes, min_degrees, max_degrees, nangles) ; } else MRISregister(mris, mrisp_template, &parms, max_passes, min_degrees, max_degrees, nangles) ; } if (remove_negative) { parms.niterations = 1000 ; MRISremoveOverlapWithSmoothing(mris,&parms) ; } fprintf(stderr, "writing registered surface to %s...\n", out_fname) ; MRISwrite(mris, out_fname) ; if (jacobian_fname) { MRIScomputeMetricProperties(mris) ; compute_area_ratios(mris) ; /* will put results in v->curv */ #if 0 MRISwriteArea(mris, jacobian_fname) ; #else MRISwriteCurvature(mris, jacobian_fname) ; #endif } msec = TimerStop(&start) ; if (Gdiag & DIAG_SHOW) printf("registration took %2.2f hours\n", (float)msec/(1000.0f*60.0f*60.0f)); MRISPfree(&mrisp_template) ; MRISfree(&mris) ; exit(0) ; return(0) ; /* for ansi */ }
int main(int argc, char *argv[]) { char **av, in_surf_fname[STRLEN], *in_patch_fname, *out_patch_fname, fname[STRLEN], path[STRLEN], *cp, hemi[10] ; int ac, nargs ; MRI_SURFACE *mris ; MRI *mri_vertices ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_flatten.c,v 1.42 2016/12/10 22:57:46 fischl Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Gdiag |= DIAG_SHOW ; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; Gdiag |= (DIAG_SHOW | DIAG_WRITE) ; memset(&parms, 0, sizeof(parms)) ; parms.dt = .1 ; parms.projection = PROJECT_PLANE ; parms.tol = 0.2 ; parms.n_averages = 1024 ; parms.l_dist = 1.0 ; parms.l_nlarea = 1.0 ; parms.niterations = 40 ; parms.area_coef_scale = 1.0 ; parms.dt_increase = 1.01 /* DT_INCREASE */; parms.dt_decrease = 0.98 /* DT_DECREASE*/ ; parms.error_ratio = 1.03 /*ERROR_RATIO */; parms.integration_type = INTEGRATE_LINE_MINIMIZE ; parms.momentum = 0.9 ; parms.desired_rms_height = -1.0 ; parms.base_name[0] = 0 ; parms.nbhd_size = 7 ; /* out to 7-connected neighbors */ parms.max_nbrs = 12 ; /* 12 at each distance */ ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc < 3) print_help() ; parms.base_dt = base_dt_scale * parms.dt ; in_patch_fname = argv[1] ; out_patch_fname = argv[2] ; FileNamePath(in_patch_fname, path) ; cp = strrchr(in_patch_fname, '/') ; if (!cp) cp = in_patch_fname ; cp = strchr(cp, '.') ; if (cp) { strncpy(hemi, cp-2, 2) ; hemi[2] = 0 ; } else strcpy(hemi, "lh") ; if (one_surf_flag) sprintf(in_surf_fname, "%s", in_patch_fname) ; else sprintf(in_surf_fname, "%s/%s.%s", path, hemi, original_surf_name) ; if (parms.base_name[0] == 0) { FileNameOnly(out_patch_fname, fname) ; cp = strchr(fname, '.') ; if (cp) strcpy(parms.base_name, cp+1) ; else strcpy(parms.base_name, "flattened") ; } mris = MRISread(in_surf_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, in_surf_fname) ; if (sphere_flag) { MRIScenter(mris, mris) ; mris->radius = MRISaverageRadius(mris) ; MRISstoreMetricProperties(mris) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; } if (Gdiag_no >= 0) { int n ; printf("vertex %d has %d nbrs before patch:\n", Gdiag_no, mris->vertices[Gdiag_no].vnum) ; for (n = 0 ; n < mris->vertices[Gdiag_no].vnum ; n++) printf("\t%d\n", mris->vertices[Gdiag_no].v[n]) ; } if (one_surf_flag) /* only have the 1 surface - no patch file */ { mris->patch = 1 ; mris->status = MRIS_PATCH ; if (!FEQUAL(rescale,1)) { MRISscaleBrain(mris, mris, rescale) ; MRIScomputeMetricProperties(mris) ; } MRISstoreMetricProperties(mris) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; } else { MRISresetNeighborhoodSize(mris, mris->vertices[0].nsize) ; // set back to max if (label_fname) // read in a label instead of a patch { LABEL *area ; area = LabelRead(NULL, label_fname) ; if (area == NULL) ErrorExit(ERROR_BADPARM, "%s: could not read label file %s", Progname, label_fname) ; LabelDilate(area, mris, dilate_label, CURRENT_VERTICES) ; MRISclearMarks(mris) ; LabelMark(area, mris) ; MRISripUnmarked(mris) ; MRISripFaces(mris); mris->patch = 1 ; mris->status = MRIS_CUT ; LabelFree(&area) ; printf("%d valid vertices (%2.1f %% of total)\n", MRISvalidVertices(mris), 100.0*MRISvalidVertices(mris)/mris->nvertices) ; } else { if (MRISreadPatch(mris, in_patch_fname) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read patch file %s", Progname, in_patch_fname) ; if (dilate) { printf("dilating patch %d times\n", dilate) ; MRISdilateRipped(mris, dilate) ; printf("%d valid vertices (%2.1f %% of total)\n", MRISvalidVertices(mris), 100.0*MRISvalidVertices(mris)/mris->nvertices) ; } } MRISremoveRipped(mris) ; MRISupdateSurface(mris) ; #if 0 mris->nsize = 1 ; // before recalculation of 2 and 3-nbrs { int vno ; VERTEX *v ; for (vno= 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; v->vtotal = v->vnum ; v->nsize = 1 ; } } MRISsetNeighborhoodSize(mris, nbrs) ; #endif } if (Gdiag_no >= 0) printf("vno %d is %sin patch\n", Gdiag_no, mris->vertices[Gdiag_no].ripflag ? "NOT " : "") ; if (Gdiag_no >= 0 && mris->vertices[Gdiag_no].ripflag == 0) { int n ; printf("vertex %d has %d nbrs after patch:\n", Gdiag_no, mris->vertices[Gdiag_no].vnum) ; for (n = 0 ; n < mris->vertices[Gdiag_no].vnum ; n++) printf("\t%d\n", mris->vertices[Gdiag_no].v[n]) ; } fprintf(stderr, "reading original vertex positions...\n") ; if (!FZERO(disturb)) mrisDisturbVertices(mris, disturb) ; if (parms.niterations > 0) { MRISresetNeighborhoodSize(mris, nbrs) ; if (!FZERO(parms.l_unfold) || !FZERO(parms.l_expand)) { static INTEGRATION_PARMS p2 ; sprintf(in_surf_fname, "%s/%s.%s", path, hemi, original_surf_name) ; if (stricmp(original_unfold_surf_name,"none") == 0) { printf("using current position of patch as initial position\n") ; MRISstoreMetricProperties(mris) ; /* use current positions */ } else if (!sphere_flag && !one_surf_flag) MRISreadOriginalProperties(mris, original_unfold_surf_name) ; *(&p2) = *(&parms) ; p2.l_dist = 0 ; p2.niterations = 100 ; p2.nbhd_size = p2.max_nbrs = 1 ; p2.n_averages = 0 ; p2.write_iterations = parms.write_iterations > 0 ? 25 : 0 ; p2.tol = -1 ; p2.dt = 0.5 ; p2.l_area = 0.0 ; p2.l_spring = 0.9 ; p2.l_convex = 0.9 ; p2.momentum = 0 ; p2.integration_type = INTEGRATE_MOMENTUM ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; #if 0 p2.flags |= IPFLAG_NO_SELF_INT_TEST ; printf("expanding surface....\n") ; MRISexpandSurface(mris, 4.0, &p2) ; // push it away from fissure #endif p2.niterations = 100 ; MRISunfold(mris, &p2, 1) ; p2.niterations = 300 ; p2.l_unfold *= 0.25 ; MRISunfold(mris, &p2, 1) ; p2.l_unfold *= 0.25 ; MRISunfold(mris, &p2, 1) ; #if 0 printf("smoothing unfolded surface..\n"); p2.niterations = 200 ; p2.l_unfold = 0 ; // just smooth it MRISunfold(mris, &p2, max_passes) ; #endif parms.start_t = p2.start_t ; parms.l_unfold = parms.l_convex = parms.l_boundary = parms.l_expand=0 ; MRIfree(&parms.mri_dist) ; } sprintf(in_surf_fname, "%s/%s.%s", path, hemi, original_surf_name) ; if (!sphere_flag && !one_surf_flag) MRISreadOriginalProperties(mris, original_surf_name) ; if (randomly_flatten) MRISflattenPatchRandomly(mris) ; else MRISflattenPatch(mris) ; /* optimize metric properties of flat map */ fprintf(stderr,"minimizing metric distortion induced by projection...\n"); MRISscaleBrain(mris, mris, scale) ; MRIScomputeMetricProperties(mris) ; MRISunfold(mris, &parms, max_passes) ; MRIScenter(mris, mris) ; fprintf(stderr, "writing flattened patch to %s\n", out_patch_fname) ; MRISwritePatch(mris, out_patch_fname) ; } if (plane_flag || sphere_flag) { char fname[STRLEN] ; FILE *fp ; #if 0 sprintf(fname, "%s.%s.out", mris->hemisphere == RIGHT_HEMISPHERE ? "rh" : "lh", parms.base_name); #else sprintf(fname, "flatten.log") ; #endif fp = fopen(fname, "a") ; if (plane_flag) MRIScomputeAnalyticDistanceError(mris, MRIS_PLANE, fp) ; else if (sphere_flag) MRIScomputeAnalyticDistanceError(mris, MRIS_SPHERE, fp) ; fclose(fp) ; } if (mri_overlay) { MRI *mri_flattened ; char fname[STRLEN] ; // if it is NxNx1x1 reshape it to be Nx1x1xN if ( mri_overlay->width == mri_overlay->height && mri_overlay->depth == 1 && mri_overlay->nframes == 1) { MRI *mri_tmp ; printf("reshaping to move 2nd dimension to time\n") ; mri_tmp = mri_reshape( mri_overlay, mri_overlay->width, 1, 1, mri_overlay->height); MRIfree( &mri_overlay ); mri_overlay = mri_tmp; } // put in some special code that knows about icosahedra if (mris->nvertices == 163842 || // ic7 mris->nvertices == 40962 || // ic6 mris->nvertices == 10242 || // ic5 mris->nvertices == 2562) // ic4 { int nvals, start_index, end_index ; MRI *mri_tmp ; printf("cross-hemispheric correlation matrix detected, reshaping...\n") ; nvals = mri_overlay->width * mri_overlay->height * mri_overlay->depth ; if (nvals == 2*mris->nvertices) // it's a corr matrix for both hemis { if (mris->hemisphere == LEFT_HEMISPHERE || mris->hemisphere == RIGHT_HEMISPHERE) { if (mris->hemisphere == LEFT_HEMISPHERE) { start_index = 0 ; end_index = mris->nvertices-1 ; } else { start_index = mris->nvertices ; end_index = 2*mris->nvertices-1 ; } mri_tmp = MRIextract(mri_overlay, NULL, start_index, 0, 0, mris->nvertices, 1, 1) ; MRIfree(&mri_overlay) ; mri_overlay = mri_tmp; } else // both hemis { } } } printf("resampling overlay (%d x %d x %d x %d) into flattened coordinates..\n", mri_overlay->width, mri_overlay->height, mri_overlay->depth, mri_overlay->nframes) ; if (synth_name) { LABEL *area_lh, *area_rh ; char fname[STRLEN], path[STRLEN], fname_no_path[STRLEN] ; int vno, n, vno2, n2 ; MRIsetValues(mri_overlay, 0) ; FileNameOnly(synth_name, fname_no_path) ; FileNamePath(synth_name, path) ; sprintf(fname, "%s/lh.%s", path, fname_no_path) ; area_lh = LabelRead(NULL, fname) ; if (area_lh == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read label from %s", Progname,fname) ; sprintf(fname, "%s/rh.%s", path, fname_no_path) ; area_rh = LabelRead(NULL, fname) ; if (area_rh == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read label from %s", Progname,fname) ; #if 0 for (n = 0 ; n < area_lh->n_points ; n++) { vno = area_lh->lv[n].vno ; MRIsetVoxVal(mri_overlay, vno, 0, 0, vno, 1) ; printf("synthesizing map with vno %d: (%2.1f, %2.1f)\n", vno, mris->vertices[vno].x, mris->vertices[vno].y) ; break ; } #else for (n = 0 ; n < area_lh->n_points ; n++) { vno = area_lh->lv[n].vno ; if (vno >= 0) { for (n2 = 0 ; n2 < area_lh->n_points ; n2++) { vno2 = area_lh->lv[n2].vno ; if (vno2 >= 0) MRIsetVoxVal(mri_overlay, vno, 0, 0, vno2, 1) ; } for (n2 = 0 ; n2 < area_rh->n_points ; n2++) { vno2 = area_rh->lv[n2].vno ; if (vno2 >= 0) MRIsetVoxVal(mri_overlay, vno, 0, 0, mris->nvertices+vno2, 1) ; } } } #endif } mri_flattened = MRIflattenOverlay(mris, mri_overlay, NULL, 1.0, label_overlay, &mri_vertices) ; printf("writing flattened overlay to %s\n", out_patch_fname) ; MRIwrite(mri_flattened, out_patch_fname) ; MRIfree(&mri_flattened) ; FileNameRemoveExtension(out_patch_fname, fname) ; strcat(fname, ".vnos.mgz") ; printf("writing flattened vertex #s to %s\n", fname) ; MRIwrite(mri_vertices, fname) ; MRIfree(&mri_vertices) ; } #if 0 sprintf(fname, "%s.area_error", out_fname) ; printf("writing area errors to %s\n", fname) ; MRISwriteAreaError(mris, fname) ; sprintf(fname, "%s.angle_error", out_fname) ; printf("writing angle errors to %s\n", fname) ; MRISwriteAngleError(mris, fname) ; MRISfree(&mris) ; #endif exit(0) ; return(0) ; /* for ansi */ }
int main(int argc, char *argv[]) { char **av, *in_fname, *out_fname, fname[STRLEN], path[STRLEN] ; int ac, nargs, start_t, pass ; MRI_SURFACE *mris ; char cmdline[CMD_LINE_LEN] ; make_cmd_version_string (argc, argv, "$Id: mris_smooth.c,v 1.28 2011/03/02 00:04:34 nicks Exp $", "$Name: stable5 $", cmdline); /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_smooth.c,v 1.28 2011/03/02 00:04:34 nicks Exp $", "$Name: stable5 $"); 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 < 3) { print_help() ; } in_fname = argv[1] ; out_fname = argv[2] ; FileNamePath(out_fname, path) ; mris = MRISfastRead(in_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, in_fname) ; MRISaddCommandLine(mris, cmdline) ; MRISremoveTriangleLinks(mris) ; fprintf(stderr, "smoothing surface tessellation for %d iterations...\n", niterations); MRIScomputeMetricProperties(mris) ; MRISstoreMetricProperties(mris) ; MRISsetNeighborhoodSize(mris, nbrs) ; #define DT 0.5 if (gaussian_norm > 0) { int i, done, start_avgs = gaussian_avgs, j ; done = 0; start_t = 0 ; pass = 0 ; do { for (i = start_t ; i < niterations+start_t ; i++) { MRIScomputeMetricProperties(mris) ; MRISsaveVertexPositions(mris, TMP_VERTICES) ; for (j = 0 ; j < 5 ; j++) { MRISaverageVertexPositions(mris, 2) ; // turn flat spikes into tubular ones MRIScomputeMetricProperties(mris) ; MRIScomputeSecondFundamentalForm(mris) ; MRIShistoThresholdGaussianCurvatureToMarked(mris, (float)(mris->nvertices-20)/mris->nvertices) ; } MRISrestoreVertexPositions(mris, TMP_VERTICES) ; MRIScomputeMetricProperties(mris) ; MRISsmoothSurfaceNormals(mris, gaussian_avgs) ; MRISclearMarks(mris) ; MRISthresholdGaussianCurvatureToMarked(mris, 10, 50); MRIScomputeSecondFundamentalForm(mris) ; MRIShistoThresholdGaussianCurvatureToMarked(mris, (float)(mris->nvertices-20)/mris->nvertices) ; MRISthresholdGaussianCurvatureToMarked(mris, 10, 50); if ((write_iterations > 0) && ((i % write_iterations) == 0)) { char fname[STRLEN] ; sprintf(fname, "%s%04d", out_fname, i) ; printf("writing snapshot to %s...\n", fname) ; MRISwrite(mris, fname) ; if (Gdiag & DIAG_WRITE) { MRISuseGaussianCurvature(mris) ; sprintf(fname, "%s_K%04d", out_fname, i) ; printf("writing curvature to %s...\n", fname) ; MRISwriteCurvature(mris, fname) ; sprintf(fname, "%s_marked%04d", out_fname, i) ; printf("writing marks to %s...\n", fname) ; MRISwriteMarked(mris, fname) ; } } for (j = 0 ; j <= 5*nint(1/DT) ; j++) { MRISmarkedSpringTerm(mris, l_spring) ; MRISaverageGradients(mris, gaussian_avgs) ; MRISmomentumTimeStep(mris, momentum, DT, 1, gaussian_avgs) ; MRISclearGradient(mris) ; MRIScomputeMetricProperties(mris) ; MRISsmoothSurfaceNormals(mris, gaussian_avgs) ; { int vno ; VERTEX *v ; for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->marked > 0) { v->K = 1.0/(v->marked) ; } else { v->K = 0 ; } } } } } MRISclearGradient(mris) ; if (gaussian_avgs == 2) { if (pass++ > 4) { done = 1 ; } else { int num = count_big_curvatures(mris, 2) ; printf("------------------------------------------------------\n") ; printf("------------------------------------------------------\n") ; printf("------------------ pass %d (num=%d) ------------------\n", pass, num) ; printf("------------------------------------------------------\n") ; printf("------------------------------------------------------\n") ; gaussian_avgs = start_avgs ; } } else { gaussian_avgs /= 2 ; if (done ==0) { printf("----------------- setting avgs to %d -----------------\n", gaussian_avgs) ; } } start_t = i ; } while (!done) ; #if 0 // more smoothing with principal curvatures gaussian_avgs = start_avgs ; printf("--------------------------------------------------------------------------\n") ; printf("--------------------------------------------------------------------------\n") ; printf("---------------------- starting threshold smoothing ----------------------\n") ; printf("--------------------------------------------------------------------------\n") ; printf("--------------------------------------------------------------------------\n") ; do { for (i = start_t ; i < niterations+start_t ; i++) { MRIScomputeMetricProperties(mris) ; MRIScomputeSecondFundamentalForm(mris) ; MRISsmoothSurfaceNormals(mris, 16) ; #define KTHRESH 1.5 // everything with kmin less than this will not move MRISthresholdPrincipalCurvatures(mris, KTHRESH) ; MRISspringTermWithGaussianCurvature(mris, gaussian_norm, l_spring) ; MRISaverageGradients(mris, gaussian_avgs) ; MRISmomentumTimeStep(mris, 0, 0.1, 1, gaussian_avgs) ; MRISclearGradient(mris) ; if ((write_iterations > 0) && (((i+1) % write_iterations) == 0)) { char fname[STRLEN] ; sprintf(fname, "%s%04d", out_fname, i+1) ; printf("writing snapshot to %s...\n", fname) ; MRISwrite(mris, fname) ; if (Gdiag & DIAG_WRITE/* && DIAG_VERBOSE_ON*/) { MRISuseGaussianCurvature(mris) ; sprintf(fname, "%s_K%04d", out_fname, i+1) ; printf("writing curvature to %s...\n", fname) ; MRISwriteCurvature(mris, fname) ; } } } MRISclearGradient(mris) ; done = (gaussian_avgs == 2) ; gaussian_avgs /= 2 ; if (done ==0) { printf("---------------------- setting avgs to %d ----------------------\n", gaussian_avgs) ; } start_t = i ; } while (!done) ; #endif } else { MRISaverageVertexPositions(mris, niterations) ; } fprintf(stderr, "smoothing complete - recomputing first and second " "fundamental forms...\n") ; MRIScomputeMetricProperties(mris) ; if (rescale) { MRISscaleBrainArea(mris) ; } MRIScomputeSecondFundamentalForm(mris) ; MRISuseMeanCurvature(mris) ; MRISaverageCurvatures(mris, navgs) ; if (normalize_flag) { MRISnormalizeCurvature(mris, which_norm) ; } sprintf(fname, "%s.%s", mris->hemisphere == LEFT_HEMISPHERE?"lh":"rh", curvature_fname); if (no_write == 0) { fprintf(stderr, "writing smoothed curvature to %s/%s\n", path,fname) ; MRISwriteCurvature(mris, fname) ; sprintf(fname, "%s.%s", mris->hemisphere == LEFT_HEMISPHERE?"lh":"rh", area_fname); fprintf(stderr, "writing smoothed area to %s/%s\n", path, fname) ; MRISwriteArea(mris, fname) ; } if (Gdiag & DIAG_SHOW) { fprintf(stderr, "writing smoothed surface to %s\n", out_fname) ; } MRISwrite(mris, out_fname) ; exit(0) ; return(0) ; /* for ansi */ }
int main(int argc, char *argv[]) { char *cp, **av, *in_fname, fname[100], path[100], name[100], hemi[100] ; int ac, nargs ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_errors.c,v 1.11 2011/03/02 00:04:31 nicks 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 < 2) usage_exit() ; in_fname = argv[1] ; #if 0 out_fname = argv[2] ; cp = strrchr(out_fname, '.') ; #endif if (patch_flag) /* read in orig surface before reading in patch */ { FileNamePath(in_fname, path) ; FileNameOnly(in_fname, name) ; cp = strchr(name, '.') ; if (cp) { strncpy(hemi, cp-2, 2) ; hemi[2] = 0 ; } else strcpy(hemi, "lh") ; sprintf(fname, "%s/%s.smoothwm", path, hemi) ; mris = MRISread(fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, fname) ; FileNameOnly(in_fname, name) ; MRISstoreMetricProperties(mris) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; if (MRISreadPatch(mris, name) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read patch file %s", Progname, name) ; } else { mris = MRISread(in_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, in_fname) ; MRISreadOriginalProperties(mris, "smoothwm") ; } MRISsaveVertexPositions(mris, TMP_VERTICES) ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRISsampleAtEachDistance(mris, nbhd_size, max_nbrs) ; MRIScomputeMetricProperties(mris) ; MRISstoreMetricProperties(mris) ; MRISrestoreVertexPositions(mris, TMP_VERTICES) ; MRIScomputeMetricProperties(mris) ; MRIScomputeDistanceErrors(mris, nbhd_size, max_nbrs) ; #if 0 if (write_flag) { MRISareaErrors(mris) ; MRISangleErrors(mris) ; } if (area_flag) { sprintf(fname, "%s.area_error", in_fname) ; printf("writing area errors to %s\n", fname) ; MRISwriteAreaError(mris, fname) ; sprintf(fname, "%s.angle_error", in_fname) ; printf("writing angle errors to %s\n", fname) ; MRISwriteAngleError(mris, fname) ; } #else sprintf(fname, "%s.distance_error", in_fname) ; fprintf(stderr, "writing errors to %s\n", fname) ; MRISwriteValues(mris, fname) ; #endif MRISfree(&mris) ; exit(0) ; return(0) ; /* for ansi */ }