int main(int argc, char *argv[]) { char **av, *in_fname, *out_fname ; int ac, nargs ; MRI_SURFACE *mris ; float alpha, beta, gamma ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_rotate.c,v 1.6 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) ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc < 6) usage_exit() ; in_fname = argv[1] ; if (sscanf(argv[2], "%f", &alpha) != 1) ErrorExit(ERROR_BADPARM, "%s: could not scan alpha from %s", Progname, argv[2]) ; if (sscanf(argv[3], "%f", &beta) != 1) ErrorExit(ERROR_BADPARM, "%s: could not scan beta from %s", Progname, argv[3]) ; if (sscanf(argv[4], "%f", &gamma) != 1) ErrorExit(ERROR_BADPARM, "%s: could not scan gamma from %s", Progname, argv[4]) ; out_fname = argv[5] ; mris = MRISfastRead(in_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, in_fname) ; alpha = RADIANS(alpha) ; beta = RADIANS(beta) ; gamma = RADIANS(gamma) ; MRIScenter(mris, mris) ; MRISrotate(mris, mris, alpha, beta, gamma) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not rotate surface", Progname) ; if (Gdiag & DIAG_SHOW) fprintf(stderr, "writing rotated surface to %s\n", out_fname) ; MRISwrite(mris, out_fname) ; exit(0) ; return(0) ; /* for ansi */ }
int main(int argc, char *argv[]) { char **av ; int ac, nargs, nsize ; MRI_SURFACE *mris ; MRI *mri ; nargs = handle_version_option (argc, argv, "$Id: mris_nudge.c,v 1.2 2011/03/02 00:04:31 nicks Exp $", "$Name: stable5 $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Gx = Gy = Gz = -1 ; 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 < 7) usage_exit(1) ; mris = MRISread(argv[1]) ; if (mris == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read surface from %s", Progname, argv[1]) ; MRIScomputeMetricProperties(mris) ; MRISstoreMetricProperties(mris) ; mri = MRIread(argv[2]) ; if (mri == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read volume from %s", Progname, argv[2]) ; target_vnos[nvertices] = atoi(argv[3]) ; target_vals[nvertices] = atof(argv[4]) ; nsize = atoi(argv[5]) ; printf("nudging %d vertex region around vertex %d to target val %2.1f\n", nsize, target_vnos[nvertices], target_vals[nvertices]) ; nvertices++ ; MRISerodeRipped(mris, nsize) ; MRISrepositionSurface(mris, mri, target_vnos, target_vals, nvertices, nsize, sigma) ; MRISunrip(mris) ; printf("writing repositioned surface to %s\n", argv[6]) ; MRISwrite(mris, argv[6]) ; return(0) ; }
int main(int argc, char *argv[]) { MRIS *mris_in,*mris_out; Progname=argv[0]; if (argc < 3) { usage_exit(-1); } mris_in=MRISread(argv[1]); mris_out=MRISextractMainComponent(mris_in,0,1,0); MRISwrite(mris_out,argv[2]); MRISfree(&mris_out); MRISfree(&mris_in); fprintf(stderr,"\ndone\n\n"); return 0; }
int main(int argc, char *argv[]) { char **av, *in_fname, *out_fname ; int ac, nargs ; MRI_SURFACE *mris ; float radius, scale ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_rescale.c,v 1.5 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] ; mris = MRISread(in_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, in_fname) ; radius = MRISaverageRadius(mris) ; scale = DEFAULT_RADIUS / radius ; MRISscaleBrain(mris, mris, scale) ; MRISwrite(mris, out_fname) ; exit(0) ; return(0) ; /* for ansi */ }
static int MRISfindOptimalRigidPosition(MRI_SURFACE *mris, MRI *mri, INTEGRATION_PARMS *parms) { double dx, dy, dz, old_sse, sse, pct_change, dt, dx_total, dy_total, dz_total ; int i ; i = 0 ; sse = compute_surface_dist_sse(mris, mri) ; dt = .1 ; dx_total = dy_total = dz_total = 0; do { old_sse = sse ; compute_rigid_gradient(mris, mri, &dx, &dy, &dz) ; apply_rigid_gradient(mris, dx*dt, dy*dt, dz*dt) ; sse = compute_surface_dist_sse(mris, mri) ; dx_total += dt*dx ; dy_total += dt*dy ; dz_total += dt*dz ; pct_change = 100.0 * ((old_sse - sse) / old_sse) ; if (pct_change < 0) { dt *=-1 ; apply_rigid_gradient(mris, dx*dt, dy*dt, dz*dt) ; dx_total += dt*dx ; dy_total += dt*dy ; dz_total += dt*dz ; } if (Gdiag & DIAG_WRITE) { char fname[STRLEN] ; sprintf(fname, "lh.%4.4d", ++parms->start_t) ; MRISwrite(mris, fname) ; } if (FZERO(sse)) break ; } while (pct_change > .01); printf("after rigid positioning, delta = (%2.0f, %2.0f, %2.0f)\n", dx_total, dy_total, dz_total) ; return(NO_ERROR) ; }
int main(int argc, char *argv[]) { char **av, *in_surf_fname, *out_fname, fname[STRLEN], *cp ; int ac, nargs, msec, err ; MRI_SURFACE *mris ; struct timeb then ; float max_dim ; char cmdline[CMD_LINE_LEN] ; make_cmd_version_string (argc, argv, "$Id: mris_sphere.c,v 1.57 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_sphere.c,v 1.57 2011/03/02 00:04:34 nicks Exp $", "$Name: stable5 $"); if (nargs && argc - nargs == 1) { exit (0); } argc -= nargs; #ifdef FS_CUDA /* print GPU device info */ MRISCdeviceInfo(); #endif // FS_CUDA TimerStart(&then) ; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; memset(&parms, 0, sizeof(parms)) ; parms.dt = .05 ; parms.projection = PROJECT_ELLIPSOID ; parms.tol = .5 /*1e-1*/ ; parms.n_averages = 1024 ; parms.min_averages = 0 ; parms.l_angle = 0.0 /* L_ANGLE */ ; parms.l_area = 0.0 /* L_AREA */ ; parms.l_neg = 0.0 ; parms.l_dist = 1.0 ; parms.l_spring = 0.0 ; parms.l_area = 1.0 ; parms.l_boundary = 0.0 ; parms.l_curv = 0.0 ; parms.niterations = 25 ; parms.write_iterations = 1000 ; parms.a = parms.b = parms.c = 0.0f ; /* ellipsoid parameters */ parms.dt_increase = 1.01 /* DT_INCREASE */; parms.dt_decrease = 0.99 /* 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.Hdesired = 0.0 ; /* a flat surface */ parms.nbhd_size = 7 ; parms.max_nbrs = 8 ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } parms.scale = scale ; if (argc != 3) // catches args beyond the expected two { usage_exit() ; } parms.base_dt = base_dt_scale * parms.dt ; in_surf_fname = argv[1] ; out_fname = argv[2] ; printf("%s\n",vcid); printf(" %s\n",MRISurfSrcVersion()); fflush(stdout); 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") ; } } mris = MRISread(in_surf_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, in_surf_fname) ; MRISaddCommandLine(mris, cmdline) ; fprintf(stderr, "reading original vertex positions...\n") ; if (!FZERO(disturb)) { mrisDisturbVertices(mris, disturb) ; } if (quick == 0) { // don't need original properties unless preserving metric err = MRISreadOriginalProperties(mris, orig_name) ; if(err) { exit(1); } } if (smooth_avgs > 0) { MRISsaveVertexPositions(mris, TMP_VERTICES) ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRISaverageVertexPositions(mris, smooth_avgs) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; MRISrestoreVertexPositions(mris, TMP_VERTICES) ; } if (!FZERO(ralpha) || !FZERO(rbeta) || !FZERO(rgamma)) { MRISrotate(mris,mris,RADIANS(ralpha),RADIANS(rbeta),RADIANS(rgamma)) ; // if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) MRISwrite(mris, "rot") ; } fprintf(stderr, "unfolding cortex into spherical form...\n"); if (talairach) { MRIStalairachTransform(mris, mris) ; MRISwrite(mris, "tal") ; } if (xform_fname) { LTA *lta ; MRI *mri ; TRANSFORM transform ; lta = LTAread(xform_fname) ; if (lta == NULL) { ErrorExit(ERROR_NOFILE, "%s: could not load %s", xform_fname) ; } mri = MRIread(vol_fname) ; if (mri == NULL) { ErrorExit(ERROR_NOFILE, "%s: could not load %s", vol_fname) ; } transform.type = lta->type ; transform.xform = (void *)lta ; MRIStransform(mris, mri, &transform, mri) ; MRIfree(&mri) ; LTAfree(<a) ; MRISwrite(mris, "xfm") ; } #if 0 max_dim = MAX(abs(mris->xlo), abs(mris->xhi)) ; max_dim = MAX(abs(max_dim), abs(mris->ylo)) ; max_dim = MAX(abs(max_dim), abs(mris->yhi)) ; max_dim = MAX(abs(max_dim), abs(mris->zlo)) ; max_dim = MAX(abs(max_dim), abs(mris->zhi)) ; #else max_dim = MAX(abs(mris->xhi-mris->xlo), abs(mris->yhi-mris->ylo)) ; max_dim = MAX(max_dim,abs(mris->zhi-mris->zlo)) ; #endif if (max_dim > .75*DEFAULT_RADIUS) { float ratio = .75*DEFAULT_RADIUS / (max_dim) ; printf("scaling brain by %2.3f...\n", ratio) ; MRISscaleBrain(mris, mris, ratio) ; } if (target_radius < 0) { target_radius = sqrt(mris->total_area / (4*M_PI)) ; printf("setting target radius to be %2.3f to match surface areas\n", target_radius) ; } // MRISsampleAtEachDistance(mris, parms.nbhd_size, parms.max_nbrs) ; if (!load && inflate) { INTEGRATION_PARMS inflation_parms ; MRIScenter(mris, mris) ; memset(&inflation_parms, 0, sizeof(INTEGRATION_PARMS)) ; strcpy(inflation_parms.base_name, parms.base_name) ; inflation_parms.write_iterations = parms.write_iterations ; inflation_parms.niterations = inflate_iterations ; inflation_parms.l_spring_norm = l_spring_norm ; inflation_parms.l_spring = inflate_spring ; inflation_parms.l_nlarea = inflate_nlarea ; inflation_parms.l_area = inflate_area ; inflation_parms.n_averages = inflate_avgs ; inflation_parms.l_expand = l_expand ; inflation_parms.l_tspring = inflate_tspring ; inflation_parms.l_sphere = l_sphere ; inflation_parms.l_convex = l_convex ; #define SCALE_UP 2 inflation_parms.a = SCALE_UP*DEFAULT_RADIUS ; inflation_parms.tol = inflate_tol ; inflation_parms.integration_type = INTEGRATE_MOMENTUM ; inflation_parms.momentum = 0.9 ; inflation_parms.dt = inflate_dt ; /* store the inflated positions in the v->c? field so that they can be used in the repulsive term. */ /* inflation_parms.l_repulse_ratio = .1 ;*/ MRISsaveVertexPositions(mris, CANONICAL_VERTICES) ; if (l_expand > 0) { MRISexpandSurface(mris, target_radius/2, &inflation_parms, 0, 1) ; l_expand = parms.l_expand = 0 ; } MRIScenter(mris, mris) ; mris->x0 = mris->xctr ; mris->y0 = mris->yctr ; mris->z0 = mris->zctr ; MRISinflateToSphere(mris, &inflation_parms) ; if (inflation_parms.l_expand > 0) { inflation_parms.l_expand = 0 ; inflation_parms.niterations += (inflate_iterations*.1) ; MRISinflateToSphere(mris, &inflation_parms) ; } MRISscaleBrain(mris, mris, target_radius/(DEFAULT_RADIUS*SCALE_UP)) ; parms.start_t = inflation_parms.start_t ; MRISresetNeighborhoodSize(mris, nbrs) ; } if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { MRISwrite(mris, "before") ; } MRISprojectOntoSphere(mris, mris, target_radius) ; if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { MRISwrite(mris, "after") ; } fprintf(stderr,"surface projected - minimizing metric distortion...\n"); MRISsetNeighborhoodSize(mris, nbrs) ; if (quick) { if (!load) { #if 0 parms.n_averages = 32 ; parms.tol = .1 ; parms.l_parea = parms.l_dist = 0.0 ; parms.l_nlarea = 1 ; #endif MRISprintTessellationStats(mris, stderr) ; MRISquickSphere(mris, &parms, max_passes) ; } } else { MRISunfold(mris, &parms, max_passes) ; } if (remove_negative) { parms.niterations = 1000 ; MRISremoveOverlapWithSmoothing(mris,&parms) ; } if (!load) { fprintf(stderr, "writing spherical brain to %s\n", out_fname) ; MRISwrite(mris, out_fname) ; } msec = TimerStop(&then) ; fprintf(stderr, "spherical transformation took %2.2f hours\n", (float)msec/(1000.0f*60.0f*60.0f)); exit(0) ; return(0) ; /* for ansi */ }
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[]) { int nargs, msec, order, i, number, vno, nnum, m, k, b1, b2, cno, flag=0, fno; struct timeb then ; MRIS *mris_in, *mris_out, *mris_high; MRI_SP *mrisp ; VERTEX *vm_out, *vm_high, *v; float s_jkm, area; Progname = argv[0] ; DiagInit(NULL, NULL, NULL) ; ErrorInit(NULL, NULL, NULL) ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc < 4) ErrorExit(ERROR_BADPARM, "usage: %s <input surface> <orig surface> <finest order> <output surface>", Progname); TimerStart(&then) ; order = atoi (argv[3]); fprintf(stdout, "Set %s as the finest scale level\n", argv[3]); if (order > 7) ErrorExit(ERROR_BADPARM, "the highest order is 7\n"); /*Spherical Wavelet Analysis*/ if (ANALYSIS&&!CURV) { mris_in = MRISread(argv[1]) ; if (!mris_in) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, argv[1]) ; fprintf(stdout, "Reading input spherical surface from %s\n", argv[1]); MRISreadOriginalProperties(mris_in, argv[2]) ; fprintf(stdout, "Reading original surface from %s orig area is %f\n", argv[2],mris_in->orig_area); mris_out = ReadIcoByOrder(order, 100); for (m = 0; m<mris_out->nvertices; m++) mris_out->vertices[m].nsize=1; mrisp = MRISPalloc(1, 3); #if 1 MRIScoordsToParameterization(mris_in, mrisp, 1, ORIGINAL_VERTICES) ; MRISPblur(mrisp, mrisp, 1, 0); MRISPblur(mrisp, mrisp, 1, 1); MRISPblur(mrisp, mrisp, 1, 2); MRIScoordsFromParameterization(mrisp, mris_out) ; #else MRISreadOriginalProperties(mris_out, argv[2]) ; #endif #if 1 /*just to test if the parameterization is correct */ MRISsaveVertexPositions(mris_out, TMP_VERTICES) ; MRISrestoreVertexPositions(mris_out, ORIGINAL_VERTICES) ; MRISupdateSurface(mris_out); fprintf(stderr, "original area becomes %f\n", mris_out->total_area); center_brain(mris_out, mris_out); MRISscaleBrain(mris_out, mris_out, sqrt(100000.0f/mris_out->total_area)) ; MRISupdateSurface(mris_out); for (fno=0; fno<mris_out->nfaces; fno++) area += mris_out->faces[fno].area; fprintf(stderr, "original area becomes %f\n", area); //MRISwrite(mris_out, "/space/xrt/1/users/btquinn/buckner_paper/010223_61223/surf/lh.sampled") ; MRISsaveVertexPositions(mris_out, ORIGINAL_VERTICES) ; MRISrestoreVertexPositions(mris_out, TMP_VERTICES) ; #endif /* Initialize Ij,k*/ for (vno = 0 ; vno<mris_out->nvertices; vno++) { vm_out = &mris_out->vertices[vno]; vm_out->val = 1; } /*Iteratively compute Ij,k*/ for (i=order;i>0;i--) { mris_high = ReadIcoByOrder(i, 100); //higher order surface for (m = 0; m<mris_high->nvertices; m++) mris_high->vertices[m].nsize=1; MRISsetNeighborhoodSize(mris_high, 3) ; number = IcoNVtxsFromOrder(i-1); //the start of m vertices for (m = number; m<mris_high->nvertices; m++) { vm_out = &mris_out->vertices[m]; vm_high = &mris_high->vertices[m]; flag=0; for (nnum=0; nnum<vm_high->vnum; nnum++) if ( vm_high->v[nnum]<number ) //A(j,m) { k = vm_high->v[nnum]; v = &mris_out->vertices[k]; v->val += 0.5*vm_out->val ; } for (; nnum<vm_high->v2num; nnum++) if ( vm_high->v[nnum]<number ) //B(j,m) { k = vm_high->v[nnum]; if (flag==0) b1=k; else b2=k; flag++; v = &mris_out->vertices[k]; v->val += 0.125*vm_out->val ; } for (; nnum<vm_high->v3num; nnum++) if ( vm_high->v[nnum]<number ) //C(j,m) { k = vm_high->v[nnum]; flag=0; //C has to be a second-order neighbor of B for (cno=mris_high->vertices[b1].vnum; cno<mris_high->vertices[b1].v2num;cno++) if (mris_high->vertices[b1].v[cno]==k) flag=1; for (cno=mris_high->vertices[b2].vnum; cno<mris_high->vertices[b2].v2num;cno++) if (mris_high->vertices[b2].v[cno]==k) flag=1; if (flag) { v = &mris_out->vertices[k]; v->val -= 0.0625*vm_out->val ; } } } } /*Analysis Stage I:*/ for (i=order;i>0;i--) { mris_high = ReadIcoByOrder(i, 100); //higher order surface for (m = 0; m<mris_high->nvertices; m++) mris_high->vertices[m].nsize=1; MRISsetNeighborhoodSize(mris_high, 3) ; number = IcoNVtxsFromOrder(i-1); //the start of m vertices /* compute Yj,m for each m vertices */ for (m = number; m<mris_high->nvertices; m++) { vm_out = &mris_out->vertices[m]; vm_high = &mris_high->vertices[m]; flag=0; for (nnum=0; nnum<vm_high->vnum; nnum++) //first order neighborhood if ( vm_high->v[nnum]<number ) //neighbor A(j,m) { k = vm_high->v[nnum] ; v = &mris_out->vertices[k]; vm_out->origx -= 0.5*v->origx; vm_out->origy -= 0.5*v->origy; vm_out->origz -= 0.5*v->origz; } for (; nnum<vm_high->v2num; nnum++) //second order neighborhood if ( vm_high->v[nnum]<number ) //neighbor B(j,m) { k = vm_high->v[nnum] ; if (flag==0) b1=k; else b2=k; flag++; v = &mris_out->vertices[k]; vm_out->origx -= 0.125*v->origx; vm_out->origy -= 0.125*v->origy; vm_out->origz -= 0.125*v->origz; } for (; nnum<vm_high->v3num; nnum++) if ( vm_high->v[nnum]<number ) //neighbor C(j,m) { k = vm_high->v[nnum] ; flag=0; //C has to be a second-order neighbor of B for (cno=mris_high->vertices[b1].vnum; cno<mris_high->vertices[b1].v2num;cno++) if (mris_high->vertices[b1].v[cno]==k) flag=1; for (cno=mris_high->vertices[b2].vnum; cno<mris_high->vertices[b2].v2num;cno++) if (mris_high->vertices[b2].v[cno]==k) flag=1; if (flag) { v = &mris_out->vertices[k]; vm_out->origx += 0.0625*v->origx; vm_out->origy += 0.0625*v->origy; vm_out->origz += 0.0625*v->origz; } } } /*Analysis Stage II: */ /*Compute Lamda(j,k) using the Yita(j,m)*/ for (m = number; m<mris_high->nvertices; m++) { vm_out = &mris_out->vertices[m]; vm_high = &mris_high->vertices[m]; for (nnum=0; nnum<vm_high->vnum; nnum++) if ( vm_high->v[nnum]<number ) //A(j,m) { k = vm_high->v[nnum]; v = &mris_out->vertices[k]; s_jkm = vm_out->val/2/v->val; v->origx += s_jkm*vm_out->origx; v->origy += s_jkm*vm_out->origy; v->origz += s_jkm*vm_out->origz; } } } MRISsaveVertexPositions(mris_out, TMP_VERTICES) ; MRISrestoreVertexPositions(mris_out, ORIGINAL_VERTICES) ; #if 0 for (m=0;m<mris_out->nvertices;m++) if (mris_out->vertices[m].z>6) fprintf(stdout, "%d %f %f %f\n", m,mris_out->vertices[m].x, mris_out->vertices[m].y, mris_out->vertices[m].z); //mris_high = ReadIcoByOrder(0, 100); //for (m=0;m<mris_high->nvertices;m++) //{mris_high->vertices[m].x=mris_out->vertices[m].x; //mris_high->vertices[m].y=mris_out->vertices[m].y; //mris_high->vertices[m].z=mris_out->vertices[m].z; //} //MRISwrite(mris_high, "/space/xrt/1/users/btquinn/buckner_paper/010223_61223/surf/lh.sampled") ; #endif fprintf(stdout, "Writing wavelets coefficient of original surface to %s\n", argv[4]); MRISwrite(mris_out,argv[4] ) ; MRISrestoreVertexPositions(mris_out, TMP_VERTICES) ; MRISPfree(&mrisp) ; MRISfree(&mris_in) ; /*End of Analysis*/ } else if (ANALYSIS&&CURV) { mris_in = MRISread(argv[1]) ; if (!mris_in) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, argv[1]) ; fprintf(stdout, "Reading input spherical surface from %s\n", argv[1]); MRISreadCurvatureFile(mris_in, argv[2]) ; fprintf(stdout, "Reading input from %s\n", argv[2]); mris_out = ReadIcoByOrder(order, 100); for (m = 0; m<mris_out->nvertices; m++) mris_out->vertices[m].nsize=1; //mrisp = MRISPalloc(1, 3); mrisp = MRIStoParameterization(mris_in, NULL, 1, 0) ; //MRISPblur(mrisp, mrisp, 1, 0); MRISfromParameterization(mrisp, mris_out, 0) ; //MRISwriteCurvature(mris_out,"/space/xrt/1/users/btquinn/buckner_paper/010223_61223/surf/lh.thickness.sampled"); /* Initialize Ij,k*/ for (vno = 0 ; vno<mris_out->nvertices; vno++) { vm_out = &mris_out->vertices[vno]; vm_out->val = 1; } /*Iteratively compute Ij,k*/ for (i=order;i>0;i--) { mris_high = ReadIcoByOrder(i, 100); //higher order surface for (m = 0; m<mris_high->nvertices; m++) mris_high->vertices[m].nsize=1; MRISsetNeighborhoodSize(mris_high, 3) ; number = IcoNVtxsFromOrder(i-1); //the start of m vertices for (m = number; m<mris_high->nvertices; m++) { vm_out = &mris_out->vertices[m]; vm_high = &mris_high->vertices[m]; flag=0; for (nnum=0; nnum<vm_high->vnum; nnum++) if ( vm_high->v[nnum]<number ) //A(j,m) { k = vm_high->v[nnum]; v = &mris_out->vertices[k]; v->val += 0.5*vm_out->val ; } for (; nnum<vm_high->v2num; nnum++) if ( vm_high->v[nnum]<number ) //B(j,m) { k = vm_high->v[nnum]; if (flag==0) b1=k; else b2=k; flag++; v = &mris_out->vertices[k]; v->val += 0.125*vm_out->val ; } for (; nnum<vm_high->v3num; nnum++) if ( vm_high->v[nnum]<number ) //C(j,m) { k = vm_high->v[nnum]; flag=0; //C has to be a second-order neighbor of B for (cno=mris_high->vertices[b1].vnum; cno<mris_high->vertices[b1].v2num;cno++) if (mris_high->vertices[b1].v[cno]==k) flag=1; for (cno=mris_high->vertices[b2].vnum; cno<mris_high->vertices[b2].v2num;cno++) if (mris_high->vertices[b2].v[cno]==k) flag=1; if (flag) { v = &mris_out->vertices[k]; v->val -= 0.0625*vm_out->val ; } } } } /*Analysis Stage I:*/ for (i=order;i>0;i--) { mris_high = ReadIcoByOrder(i, 100); //higher order surface for (m = 0; m<mris_high->nvertices; m++) mris_high->vertices[m].nsize=1; MRISsetNeighborhoodSize(mris_high, 3) ; number = IcoNVtxsFromOrder(i-1); //the start of m vertices /* compute Yj,m for each m vertices */ for (m = number; m<mris_high->nvertices; m++) { vm_out = &mris_out->vertices[m]; vm_high = &mris_high->vertices[m]; flag=0; for (nnum=0; nnum<vm_high->vnum; nnum++) //first order neighborhood if ( vm_high->v[nnum]<number ) //neighbor A(j,m) { k = vm_high->v[nnum] ; v = &mris_out->vertices[k]; vm_out->curv -= 0.5*v->curv; } for (; nnum<vm_high->v2num; nnum++) //second order neighborhood if ( vm_high->v[nnum]<number ) //neighbor B(j,m) { k = vm_high->v[nnum] ; if (flag==0) b1=k; else b2=k; flag++; v = &mris_out->vertices[k]; vm_out->curv -= 0.125*v->curv; } for (; nnum<vm_high->v3num; nnum++) if ( vm_high->v[nnum]<number ) //neighbor C(j,m) { k = vm_high->v[nnum] ; flag=0; //C has to be a second-order neighbor of B for (cno=mris_high->vertices[b1].vnum; cno<mris_high->vertices[b1].v2num;cno++) if (mris_high->vertices[b1].v[cno]==k) flag=1; for (cno=mris_high->vertices[b2].vnum; cno<mris_high->vertices[b2].v2num;cno++) if (mris_high->vertices[b2].v[cno]==k) flag=1; if (flag) { v = &mris_out->vertices[k]; vm_out->curv += 0.0625*v->curv; } } } /*Analysis Stage II: */ /*Compute Lamda(j,k) using the Yita(j,m)*/ for (m = number; m<mris_high->nvertices; m++) { vm_out = &mris_out->vertices[m]; vm_high = &mris_high->vertices[m]; for (nnum=0; nnum<vm_high->vnum; nnum++) if ( vm_high->v[nnum]<number ) //A(j,m) { k = vm_high->v[nnum]; v = &mris_out->vertices[k]; s_jkm = vm_out->val/2/v->val; v->curv += s_jkm*vm_out->curv; } } } fprintf(stdout, "Writing wavelets coefficient of original surface to %s\n", argv[4]); MRISwriteCurvature(mris_out,argv[4] ) ; MRISPfree(&mrisp) ; MRISfree(&mris_in) ; /*End of Analysis*/ } else if (SYNTHESIS) /*Spherical Wavelet Synthesis*/ { mris_out = ReadIcoByOrder(order, 100); //higher order surface fprintf(stdout, "Creating a %d order spherical surface\n", order); MRISreadOriginalProperties(mris_out, argv[1]) ; fprintf(stdout, "Reading wavelet coefficients from %s\n", argv[1]); for (m = 0; m<mris_out->nvertices; m++) mris_out->vertices[m].nsize=1; MRISsetNeighborhoodSize(mris_out, 3) ; if (COMPARE) { mris_in = MRISread(fname); for (i=1; i<IcoNVtxsFromOrder(order-1); i++) { if (mris_out->vertices[i].origx==0) area = fabs(mris_out->vertices[i].origx-mris_in->vertices[i].x); else area = fabs((mris_out->vertices[i].origx-mris_in->vertices[i].x)/mris_out->vertices[i].origx); if ( area>5 ) { mris_out->vertices[i].origx = mris_in->vertices[i].x ; fprintf(stdout, "%d %f\n", i, area); } if (mris_out->vertices[i].origy==0) area = fabs(mris_out->vertices[i].origy-mris_in->vertices[i].y); else area = fabs((mris_out->vertices[i].origy-mris_in->vertices[i].y)/mris_out->vertices[i].origy); if ( area>5 ) { mris_out->vertices[i].origy = mris_in->vertices[i].y ; fprintf(stdout, "%d %f\n", i, area); } if (mris_out->vertices[i].origz==0) area = fabs(mris_out->vertices[i].origz-mris_in->vertices[i].z); else area = fabs((mris_out->vertices[i].origz-mris_in->vertices[i].z)/mris_out->vertices[i].origz); if ( area>5 ) { mris_out->vertices[i].origz = mris_in->vertices[i].z ; fprintf(stdout, "%d %f\n", i, area); } } MRISfree(&mris_in); } fprintf(stdout, "Recover the surface using %s order coefficients\n",argv[2]); number = IcoNVtxsFromOrder(atoi(argv[2])); for (m = number; m<mris_out->nvertices; m++) { mris_out->vertices[m].origx = 0; mris_out->vertices[m].origy = 0; mris_out->vertices[m].origz = 0; } /*Initialize Ij,k*/ for (vno = 0; vno<mris_out->nvertices; vno++) { vm_out = &mris_out->vertices[vno]; vm_out->val = 1; } /*Iteratively compute Ij,k*/ for (i=order;i>0;i--) { mris_high = ReadIcoByOrder(i, 100); //higher order surface for (m = 0; m<mris_high->nvertices; m++) mris_high->vertices[m].nsize=1; MRISsetNeighborhoodSize(mris_high, 3) ; number = IcoNVtxsFromOrder(i-1); //the start of m vertices for (m = number; m<mris_high->nvertices; m++) { vm_out = &mris_out->vertices[m]; vm_high = &mris_high->vertices[m]; flag=0; for (nnum=0; nnum<vm_high->vnum; nnum++) if ( vm_high->v[nnum]<number ) //A(j,m) { k = vm_high->v[nnum]; v = &mris_out->vertices[k]; v->val += 0.5*vm_out->val ; } for (; nnum<vm_high->v2num; nnum++) if ( vm_high->v[nnum]<number ) //B(j,m) { k = vm_high->v[nnum]; if (flag==0) b1=k; else b2=k; flag++; v = &mris_out->vertices[k]; v->val += 0.125*vm_out->val ; } for (; nnum<vm_high->v3num; nnum++) if ( vm_high->v[nnum]<number ) //C(j,m) { k = vm_high->v[nnum]; flag=0; //C has to be a second-order neighbor of B for (cno=mris_high->vertices[b1].vnum; cno<mris_high->vertices[b1].v2num;cno++) if (mris_high->vertices[b1].v[cno]==k) flag=1; for (cno=mris_high->vertices[b2].vnum; cno<mris_high->vertices[b2].v2num;cno++) if (mris_high->vertices[b2].v[cno]==k) flag=1; if (flag) { v = &mris_out->vertices[k]; v->val -= 0.0625*vm_out->val ; } } } } for (i=1;i<=order;i++) { mris_high = ReadIcoByOrder(i, 100); //higher order surface for (m = 0; m<mris_high->nvertices; m++) mris_high->vertices[m].nsize=1; MRISsetNeighborhoodSize(mris_high, 3) ; number = IcoNVtxsFromOrder(i-1); //the start of m vertices /* Synthesis Stage I */ /* Compute Lamda(j+1,k) using the Yita(j,m) */ for (m = number; m<mris_high->nvertices; m++) { vm_out = &mris_out->vertices[m]; vm_high = &mris_high->vertices[m]; for (nnum=0; nnum<vm_high->vnum; nnum++) if ( vm_high->v[nnum]<number ) //A(j,m) { k = vm_high->v[nnum]; v = &mris_out->vertices[k]; s_jkm = vm_out->val/2/v->val; v->origx -= s_jkm*vm_out->origx; v->origy -= s_jkm*vm_out->origy; v->origz -= s_jkm*vm_out->origz; } } /* compute Lamda(j+1,m) for each m vertices */ for (m = number; m<mris_high->nvertices; m++) { vm_out = &mris_out->vertices[m]; vm_high = &mris_high->vertices[m]; flag=0; for (nnum=0; nnum<vm_high->vnum; nnum++) //first order neighborhood if ( vm_high->v[nnum]<number ) //neighbor A(j,m) { k = vm_high->v[nnum] ; v = &mris_out->vertices[k]; vm_out->origx += 0.5*v->origx; vm_out->origy += 0.5*v->origy; vm_out->origz += 0.5*v->origz; } for (; nnum<vm_high->v2num; nnum++) //second order neighborhood if ( vm_high->v[nnum]<number ) //neighbor B(j,m) { k = vm_high->v[nnum] ; if (flag==0) b1=k; else b2=k; flag++; v = &mris_out->vertices[k]; vm_out->origx += 0.125*v->origx; vm_out->origy += 0.125*v->origy; vm_out->origz += 0.125*v->origz; } for (; nnum<vm_high->v3num; nnum++) //third order neighborhood if ( vm_high->v[nnum]<number ) //neighbor C(j,m) { k = vm_high->v[nnum] ; flag=0; //C has to be a second-order neighbor of B for (cno=mris_high->vertices[b1].vnum; cno<mris_high->vertices[b1].v2num;cno++) if (mris_high->vertices[b1].v[cno]==k) flag=1; for (cno=mris_high->vertices[b2].vnum; cno<mris_high->vertices[b2].v2num;cno++) if (mris_high->vertices[b2].v[cno]==k) flag=1; if (flag) { v = &mris_out->vertices[k]; vm_out->origx -= 0.0625*v->origx; vm_out->origy -= 0.0625*v->origy; vm_out->origz -= 0.0625*v->origz; } } } } MRISsaveVertexPositions(mris_out, TMP_VERTICES) ; MRISrestoreVertexPositions(mris_out, ORIGINAL_VERTICES) ; fprintf(stdout, "Writing recovered surface to %s\n", argv[4]); MRISwrite(mris_out, argv[4]) ; #if 0 mris_high = ReadIcoByOrder(4, 100); for (m=0;m<mris_high->nvertices;m++) { mris_high->vertices[m].x=mris_out->vertices[m].x; mris_high->vertices[m].y=mris_out->vertices[m].y; mris_high->vertices[m].z=mris_out->vertices[m].z; } MRISwrite(mris_high, "/space/xrt/1/users/btquinn/buckner_paper/010223_61223/surf/lh.wavelet.recon") ; #endif MRISrestoreVertexPositions(mris_out, TMP_VERTICES) ; /*End of Synthesis*/ } MRISfree(&mris_out); MRISfree(&mris_high) ; msec = TimerStop(&then) ; fprintf(stdout, "spherical wavelet took %2.1f minutes\n", (float)msec/(1000.0f*60.0f)); exit(0) ; return(0) ; }
int main(int argc, char *argv[]) { char **av, surf_fname[STRLEN], *template_fname, *hemi, *sphere_name, *cp, *subject, fname[STRLEN] ; int ac, nargs, ino, sno, nbad = 0, failed, n,nfields; VERTEX *v; VALS_VP *vp; MRI_SURFACE *mris ; MRI_SP *mrisp, /* *mrisp_aligned,*/ *mrisp_template ; INTEGRATION_PARMS parms ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_make_template.c,v 1.27 2011/03/02 00:04:33 nicks Exp $", "$Name: stable5 $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; memset(&parms, 0, sizeof(parms)) ; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; /* setting default values for vectorial registration */ setParms(&parms); ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv,&parms) ; argc -= nargs ; argv += nargs ; } if (argc < 5) usage_exit() ; /* multiframe registration */ if (multiframes) parms.flags |= IP_USE_MULTIFRAMES; if (!strlen(subjects_dir)) /* not specified on command line*/ { cp = getenv("SUBJECTS_DIR") ; if (!cp) ErrorExit(ERROR_BADPARM, "%s: SUBJECTS_DIR not defined in environment.\n", Progname) ; strcpy(subjects_dir, cp) ; } hemi = argv[1] ; sphere_name = argv[2] ; template_fname = argv[argc-1] ; if (1 || !FileExists(template_fname)) /* first time - create it */ { fprintf(stderr, "creating new parameterization...\n") ; if (multiframes) { mrisp_template = MRISPalloc(scale, atlas_size * IMAGES_PER_SURFACE ); /* if (no_rot) /\* don't do rigid alignment *\/ */ /* mrisp_aligned = NULL ; */ /* else */ /* mrisp_aligned = MRISPalloc(scale, PARAM_FRAMES); */ } else { mrisp_template = MRISPalloc(scale, PARAM_IMAGES); /* if (no_rot) /\* don't do rigid alignment *\/ */ /* mrisp_aligned = NULL ; */ /* else */ /* mrisp_aligned = MRISPalloc(scale, PARAM_IMAGES); */ } } else { fprintf(stderr, "reading template parameterization from %s...\n", template_fname) ; /* mrisp_aligned = NULL ; */ mrisp_template = MRISPread(template_fname) ; if (!mrisp_template) ErrorExit(ERROR_NOFILE, "%s: could not open template file %s", Progname, template_fname) ; } argv += 3 ; argc -= 3 ; for (ino = 0 ; ino < argc-1 ; ino++) { failed = 0 ; subject = argv[ino] ; fprintf(stderr, "\nprocessing subject %s (%d of %d)\n", subject, ino+1, argc-1) ; sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, sphere_name) ; fprintf(stderr, "reading spherical surface %s...\n", surf_fname) ; mris = MRISread(surf_fname) ; if (!mris) { nbad++ ; ErrorPrintf(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; exit(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, CANONICAL_VERTICES) ; MRIScomputeMetricProperties(mris) ; MRISstoreMetricProperties(mris) ; if (Gdiag & DIAG_WRITE) { char *cp1 ; FileNameOnly(template_fname, fname) ; cp = strchr(fname, '.') ; if (cp) { cp1 = strrchr(fname, '.') ; if (cp1 && cp1 != cp) strncpy(parms.base_name, cp+1, cp1-cp-1) ; else strcpy(parms.base_name, cp+1) ; } else strcpy(parms.base_name, "template") ; sprintf(fname, "%s.%s.out", hemi, parms.base_name); parms.fp = fopen(fname, "w") ; printf("writing output to '%s'\n", fname) ; } /* multiframe registration */ if (multiframes) { nfields=parms.nfields; for ( n = 0; n < mris->nvertices ; n++) /* allocate the VALS_VP structure */ { v=&mris->vertices[n]; vp=calloc(1,sizeof(VALS_VP)); vp->nvals=nfields; vp->orig_vals=(float*)malloc(nfields*sizeof(float)); /* before blurring */ vp->vals=(float*)malloc(nfields*sizeof(float)); /* values used by MRISintegrate */ v->vp=(void*)vp; } /* load the different fields */ for (n = 0 ; n < parms.nfields ; n++) { if (parms.fields[n].name != NULL) { sprintf(surf_fname, "%s/%s/%s/%s.%s", subjects_dir, subject, overlay_dir, hemi, parms.fields[n].name) ; printf("reading overlay file %s...\n", surf_fname) ; if (MRISreadValues(mris, surf_fname) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read overlay file %s", Progname, surf_fname) ; MRIScopyValuesToCurvature(mris) ; } else if (ReturnFieldName(parms.fields[n].field)) { /* read in precomputed curvature file */ sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, ReturnFieldName(parms.fields[n].field)) ; // fprintf(stderr,"\nreading field %d from %s(type=%d,frame=%d)\n",parms.fields[n].field,surf_fname,parms.fields[n].type,parms.fields[n].frame); if (MRISreadCurvatureFile(mris, surf_fname) != NO_ERROR) { fprintf(stderr,"\n\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); fprintf(stderr, "%s: could not read curvature file '%s'\n", Progname, surf_fname) ; failed = 1; break; } } else { /* compute curvature of surface */ sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, surface_names[parms.fields[n].field]) ; /*if(parms.fields[n].field==0) sprintf(fname, "inflated") ; else sprintf(fname, "smoothwm") ;*/ //fprintf(stderr,"\ngenerating field %d(type=%d,frame=%d) (from %s)\n",parms.fields[n].field,parms.fields[n].type,parms.fields[n].frame,surf_fname); // MRISsaveVertexPositions(mris, TMP_VERTICES) ; if (MRISreadVertexPositions(mris, surf_fname) != NO_ERROR) { fprintf(stderr,"\n\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); ErrorPrintf(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; fprintf(stderr,"setting up correlation coefficient to zero\n"); parms.fields[n].l_corr=parms.fields[n].l_pcorr=0.0; failed=1; break; } if (nbrs > 1) MRISsetNeighborhoodSize(mris, nbrs) ; MRIScomputeMetricProperties(mris) ; MRIScomputeSecondFundamentalForm(mris) ; MRISuseMeanCurvature(mris) ; MRISaverageCurvatures(mris, navgs) ; MRISrestoreVertexPositions(mris, CANONICAL_VERTICES) ; } /* if(parms.fields[n].field!=SULC_CORR_FRAME)*/ MRISnormalizeField(mris,parms.fields[n].type, parms.fields[n].which_norm); /* normalize values */ MRISsetCurvaturesToOrigValues(mris,n); MRISsetCurvaturesToValues(mris,n); } if (failed) { fprintf(stderr,"\n\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); fprintf(stderr,"Subject %s Failed",subject); fprintf(stderr,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n\n"); /* free cal structure*/ for ( n = 0; n < mris->nvertices ; n++) { v=&mris->vertices[n]; vp=(VALS_VP*)v->vp; free(vp->orig_vals); free(vp->vals); free(vp); v->vp=NULL; } /* free surface */ MRISfree(&mris); /* go onto the next subject */ continue; } } if (multiframes && (!no_rot)) { /* rigid body alignment */ parms.frame_no = 3 ; /* don't use single field correlation functions */ parms.l_corr = parms.l_pcorr = 0.0f ; parms.mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; parms.mrisp_template = mrisp_template ; MRISrigidBodyAlignVectorGlobal(mris, &parms, 1.0, 64.0, 8) ; if (Gdiag & DIAG_WRITE) MRISwrite(mris, "sphere.rot.global") ; MRISrigidBodyAlignVectorLocal(mris, &parms) ; if (Gdiag & DIAG_WRITE) MRISwrite(mris, "sphere.rot.local") ; MRISPfree(&parms.mrisp) ; MRISsaveVertexPositions(mris, CANONICAL_VERTICES) ; }; if ((!multiframes) && (!no_rot) && ino > 0) { /* rigid body alignment */ sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, "sulc") ; if (MRISreadCurvatureFile(mris, surf_fname) != NO_ERROR) { ErrorPrintf(Gerror, "%s: could not read curvature file '%s'\n", Progname, surf_fname) ; nbad++ ; MRISfree(&mris) ; continue ; } parms.frame_no = 3 ; /* use sulc for rigid registration */ parms.mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; parms.mrisp_template = mrisp_template ; parms.l_corr = 1.0f ; MRISrigidBodyAlignGlobal(mris, &parms, 1.0, 64.0, 8) ; if (Gdiag & DIAG_WRITE) MRISwrite(mris, "sphere.rot.global") ; MRISrigidBodyAlignLocal(mris, &parms) ; if (Gdiag & DIAG_WRITE) MRISwrite(mris, "sphere.rot.local") ; MRISPfree(&parms.mrisp) ; MRISsaveVertexPositions(mris, CANONICAL_VERTICES) ; } if (multiframes) { for (n = 0; n < parms.nfields ; n++) { MRISsetOrigValuesToCurvatures(mris,n); MRISaverageCurvatures(mris, parms.fields[n].navgs) ; mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; MRISPcombine(mrisp, mrisp_template, parms.fields[n].frame * IMAGES_PER_SURFACE) ; MRISPfree(&mrisp) ; } /* free the VALS_VP structure */ for ( n = 0; n < mris->nvertices ; n++) { v=&mris->vertices[n]; vp=(VALS_VP*)v->vp; free(vp->orig_vals); free(vp->vals); free(vp); v->vp=NULL; } MRISfree(&mris) ; } else { for (sno = 0; sno < SURFACES ; sno++) { if (curvature_names[sno]) /* read in precomputed curvature file */ { sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, curvature_names[sno]) ; if (MRISreadCurvatureFile(mris, surf_fname) != NO_ERROR) { nbad++ ; ErrorPrintf(Gerror, "%s: could not read curvature file '%s'\n", Progname, surf_fname) ; failed = 1 ; break ; } /* the two next lines were not in the original code */ MRISaverageCurvatures(mris, navgs) ; MRISnormalizeCurvature(mris, which_norm) ; } else /* compute curvature of surface */ { sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, surface_names[sno]) ; if (MRISreadVertexPositions(mris, surf_fname) != NO_ERROR) { ErrorPrintf(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; nbad++ ; failed = 1 ; break ; } if (nbrs > 1) MRISsetNeighborhoodSize(mris, nbrs) ; MRIScomputeMetricProperties(mris) ; MRIScomputeSecondFundamentalForm(mris) ; MRISuseMeanCurvature(mris) ; MRISaverageCurvatures(mris, navgs) ; MRISrestoreVertexPositions(mris, CANONICAL_VERTICES) ; MRISnormalizeCurvature(mris, which_norm) ; } fprintf(stderr, "computing parameterization for surface %s...\n", surf_fname); if (failed) { continue ; MRISfree(&mris) ; } mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; MRISPcombine(mrisp, mrisp_template, sno*3) ; MRISPfree(&mrisp) ; } MRISfree(&mris) ; } } #if 0 if (mrisp_aligned) /* new parameterization - use rigid alignment */ { MRI_SP *mrisp_tmp ; if (Gdiag & DIAG_WRITE) { char *cp1 ; FileNameOnly(template_fname, fname) ; cp = strchr(fname, '.') ; if (cp) { cp1 = strrchr(fname, '.') ; if (cp1 && cp1 != cp) strncpy(parms.base_name, cp+1, cp1-cp-1) ; else strcpy(parms.base_name, cp+1) ; } else strcpy(parms.base_name, "template") ; sprintf(fname, "%s.%s.out", hemi, parms.base_name); parms.fp = fopen(fname, "w") ; printf("writing output to '%s'\n", fname) ; } for (ino = 0 ; ino < argc-1 ; ino++) { subject = argv[ino] ; if (Gdiag & DIAG_WRITE) fprintf(parms.fp, "processing subject %s\n", subject) ; fprintf(stderr, "processing subject %s\n", subject) ; sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, sphere_name) ; fprintf(stderr, "reading spherical surface %s...\n", surf_fname) ; mris = MRISread(surf_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; MRIScomputeMetricProperties(mris) ; MRISstoreMetricProperties(mris) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, "sulc") ; if (MRISreadCurvatureFile(mris, surf_fname) != NO_ERROR) ErrorExit(Gerror, "%s: could not read curvature file '%s'\n", Progname, surf_fname) ; parms.frame_no = 3 ; parms.mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; parms.mrisp_template = mrisp_template ; parms.l_corr = 1.0f ; MRISrigidBodyAlignGlobal(mris, &parms, 1.0, 32.0, 8) ; if (Gdiag & DIAG_WRITE) MRISwrite(mris, "sphere.rot.global") ; MRISrigidBodyAlignLocal(mris, &parms) ; if (Gdiag & DIAG_WRITE) MRISwrite(mris, "sphere.rot.local") ; MRISPfree(&parms.mrisp) ; #if 0 /* write out rotated surface */ sprintf(surf_fname, "%s.rot", mris->fname) ; fprintf(stderr, "writing out rigidly aligned surface to '%s'\n", surf_fname) ; MRISwrite(mris, surf_fname) ; #endif /* now generate new parameterization using the optimal alignment */ for (sno = 0; sno < SURFACES ; sno++) { if (curvature_names[sno]) /* read in precomputed curvature file */ { sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, curvature_names[sno]) ; if (MRISreadCurvatureFile(mris, surf_fname) != NO_ERROR) ErrorExit(Gerror, "%s: could not read curvature file '%s'\n", Progname, surf_fname) ; } else /* compute curvature of surface */ { sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, surface_names[sno]) ; if (MRISreadVertexPositions(mris, surf_fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; if (nbrs > 1) MRISsetNeighborhoodSize(mris, nbrs) ; MRIScomputeMetricProperties(mris) ; MRIScomputeSecondFundamentalForm(mris) ; MRISuseMeanCurvature(mris) ; MRISaverageCurvatures(mris, navgs) ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRISnormalizeCurvature(mris) ; } fprintf(stderr, "computing parameterization for surface %s...\n", surf_fname); mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; MRISPcombine(mrisp, mrisp_aligned, sno*3) ; MRISPfree(&mrisp) ; } MRISfree(&mris) ; } if (Gdiag & DIAG_WRITE) fclose(parms.fp) ; mrisp_tmp = mrisp_aligned ; mrisp_aligned = mrisp_template ; mrisp_template = mrisp_tmp ; MRISPfree(&mrisp_aligned) ; } #endif fprintf(stderr, "writing updated template with %d subjects to %s...\n", argc-1-nbad, template_fname) ; MRISPwrite(mrisp_template, template_fname) ; MRISPfree(&mrisp_template) ; exit(0) ; return(0) ; /* for ansi */ }
int main(int argc, char *argv[]) { int ac, nargs ; char **av, *cp, surf_name[100], *hemi, *subject_name, *label_name, *out_fname ; MRI_SURFACE *mris ; LABEL *label ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: label2patch.c,v 1.7 2016/12/10 22:57:39 fischl Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; /* read in command-line options */ ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc < 4) print_usage() ; subject_name = argv[1] ; hemi = argv[2] ; label_name = argv[3] ; out_fname = argv[4] ; if (strlen(subjects_dir) == 0) { cp = getenv("SUBJECTS_DIR") ; if (!cp) ErrorExit(ERROR_BADPARM, "no subjects directory in environment.\n") ; strcpy(subjects_dir, cp) ; } sprintf(surf_name,"%s/%s/surf/%s.%s",subjects_dir,subject_name,hemi,surface); fprintf(stderr, "reading %s...\n", surf_name) ; mris = MRISread(surf_name) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s\n", surf_name) ; MRIScomputeMetricProperties(mris) ; label = LabelRead(subject_name, label_name) ; if (ndilate) LabelDilate(label, mris, ndilate, CURRENT_VERTICES) ; if (nerode) LabelErode(label, mris, nerode) ; if (nclose) { LabelDilate(label, mris, nclose, CURRENT_VERTICES) ; LabelErode(label, mris, nclose) ; } LabelRipRestOfSurface(label, mris) ; MRISripFaces(mris) ; if (writesurf) { MRISwrite(mris, out_fname) ; } else MRISwritePatch(mris, out_fname) ; if (verbose) fprintf(stderr, "done.\n") ; exit(0) ; return(0) ; /* ansi */ }
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[]) { MRIS *mris; char *in_orig_fname=NULL, *in_seg_fname=NULL,*out_fname=NULL; MRI *mri_orig=NULL,*mri_seg=NULL,*mri_out=NULL; int nargs,n; char fname[512]; Progname=argv[0]; fprintf(stderr,"\n"); MRI_TOPOLOGY_PARMSdefault(&parms); for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (parms.tesselation_mode==-1) parms.tesselation_mode=parms.connectivity; if (argc<4) { fprintf(stderr, "\nUsage: %s options input_orig_file input_segmented_file output_folder\n", Progname); exit(1); }; in_orig_fname=argv[argc-3]; in_seg_fname = argv[argc-2]; out_fname = argv[argc-1]; fprintf(stderr,"************************************************************" "\nThe input orig volume is %s" "\nThe input segmented volume is %s" "\nThe output volume is %s" "\nIf this is incorrect, please exit quickly the program (Ctl-C)\n",in_orig_fname,in_seg_fname,out_fname); for (n=0;n<parms.nlabels;n++) fprintf(stderr,"label = %d: %s \n",parms.labels[n],cma_label_to_name(parms.labels[n])); if (parms.using_gca_maps) fprintf(stderr,"mixing parameters: alpha=%1.3f , beta=%1.3f \n",parms.alpha,parms.beta); else { parms.beta=1.0f; parms.alpha=1.0f; } fprintf(stderr,"connectivity = %d\n",parms.connectivity); mri_orig=MRIread(in_orig_fname); if (!mri_orig && parms.using_gca_maps) Error("orig volume: orig volume could not be read\n"); mri_seg=MRIread(in_seg_fname); if (!mri_seg) Error("segmented volume: segmented volume could not be read\n"); //check euler characteristic of initial surface if (parms.initial_surface_file) { int i,j,k,val,euler,pnvertices, pnfaces, pnedges; MRI *mri_tmp; mri_tmp=MRIclone(mri_seg,NULL); for (k=0;k<mri_seg->depth;k++) for (j=0;j<mri_seg->height;j++) for (i=0;i<mri_seg->width;i++) for (n=0;n<parms.nlabels;n++) { val=MRIgetVoxVal(mri_seg,i,j,k, 0); if (val==parms.labels[n]) { MRIsetVoxVal(mri_tmp,i,j,k,0,1); break; } } mris=MRIScreateSurfaceFromVolume(mri_tmp,1,parms.connectivity); euler=MRIScomputeEulerNumber(mris,&pnvertices,&pnfaces,&pnedges); fprintf(stderr,"\ninitial euler characteristic = %d, %d vertices, %d faces, %d edges" ,euler,pnvertices,pnfaces,pnedges); MRISwrite(mris,parms.initial_surface_file); MRISfree(&mris); MRIfree(&mri_tmp); } mri_out=MRIcorrectTopology(mri_orig,mri_seg,NULL,&parms); if (parms.nlabels == 1) { MRI *mri_tmp ; // turn off all voxels that are going to be on in the output MRImask(mri_seg, mri_out, mri_seg, 1, 0) ; /* whatever ones are left are now incorrect and should be labeled something else */ resegment_erased_voxels(mri_orig, mri_seg, mri_seg, parms.labels[0]) ; MRIreplaceValues(mri_out, mri_out, 1, parms.labels[0]) ; mri_tmp = MRIcopy(mri_seg, NULL) ; MRIcopyLabel(mri_out, mri_tmp, parms.labels[0]) ; MRIfree(&mri_out) ; mri_out = mri_tmp ; // check_volume(mri_save, mri_out, parms.labels[0]) ; } MRIwrite(mri_out,out_fname); ////TEMPORARY VALIDATION STUFF ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////// #if 0 //validation of the algo { FILE *f; MRIS *mristb[20],*mrisr; int n,i,j,k,depth,height,width,count,count2; int tab[20]={4,43,51,12,52,13,54,18,53,17,49,10,50,11};//,6,7,10,11,12,13,17,18,43,44,45,46,49,50,51,52,53,54}; MRI *mri_val=MRIclone(parms.mri_seg,NULL); parms.nlabels=1; depth=parms.mri_seg->depth; height=parms.mri_seg->height; width=parms.mri_seg->width; for (n=0;n<14;n++) { MRIfree(&parms.mri_output); MRIfree(&parms.mri_bin); MRIfree(&parms.mri_dist); MRIfree(&parms.mri_fcost); MRIfree(&parms.mri_bcost); MRIfree(&parms.mri_fprior); MRIfree(&parms.mri_bprior); MRIfree(&parms.mri_labeled); segmentationFree(&parms.F_Bseg); segmentationFree(&parms.F_Rseg); segmentationFree(&parms.B_Bseg); segmentationFree(&parms.B_Rseg); CCSfree(&parms.F_Bccs); CCSfree(&parms.F_Rccs); CCSfree(&parms.B_Bccs); CCSfree(&parms.B_Rccs); parms.labels[0]=tab[n]; MRIcorrectTopology(parms.mri_orig,parms.mri_seg,&parms.mri_output,mris ,parms.labels,parms.nblabels,parms.f_c,parms); MRISwrite(*mris,"./tmp"); mristb[n]=MRISread("./tmp"); #if 0 count=0; count2=0; for (k=0;k<depth;k++) for (j=0;j<height;j++) for (i=0;i<width;i++) { if (MRIvox(parms.mri_seg,i,j,k)==parms.labels[0]) count2++; if (MRIvox(parms.mri_output,i,j,k)==1) { MRIvox(mri_val,i,j,k)++; if (MRIvox(parms.mri_seg,i,j,k)!=parms.labels[0]) count++; } else if (MRIvox(parms.mri_seg,i,j,k)==parms.labels[0]) count++; } fprintf(stderr,"\n yeh %d %d %f \n",count,count2,100.*count/count2); sprintf(fname,"./label%d",tab[n]); f=fopen(fname,"a+"); fprintf(f,"\n %d %d %f ",count,count2,(float)100.*count/count2); fclose(f); #endif #if 0 sprintf(fname,"./surf%d",n); MRISwrite(mristb[n],fname); MRISsmoothSurface2(mristb[n],5,0.5,0); MRISsmoothSurface2(mristb[n],5,0.25,2); MRISsmoothSurface2(mristb[n],10,0.05,5); sprintf(fname,"./surfsmooth%d",n); mristb[n]->type=MRIS_TRIANGULAR_SURFACE;//MRIS_BINARY_QUADRANGLE_FILE; MRISwrite(mristb[n],fname); MRISsetNeighborhoodSize(mristb[n],3) ; MRIScomputeMetricProperties(mristb[n]) ; MRIScomputeSecondFundamentalForm(mristb[n]) ; MRISuseMeanCurvature(mristb[n]); MRISaverageCurvatures(mristb[n],2) ; MRISnormalizeCurvature(mristb[n], NORM_MEAN) ; sprintf(fname,"./curv%d",n); MRISwriteCurvature(mristb[n],fname); #endif } #if 0 mrisr=MRISconcatenateQuadSurfaces(n,mristb); mrisr->type=MRIS_TRIANGULAR_SURFACE; MRISwrite(mrisr,"./lh.ZURFACE"); // for(k=0;k<mrisr->nvertices;k++) // mrisr->vertices[k].curv=0.3; //MRISnormalizeCurvature(mrisr, NORM_MEAN) ; MRISwriteCurvature(mrisr,"./ZURFACE_CURVATURE"); for (k=0;k<mrisr->nvertices;k++) mrisr->vertices[k].curv=mrisr->vertices[k].val; MRISwriteCurvature(mrisr,"./ZURFACE_VAL"); #endif n=0; count=0; for (k=0;k<depth;k++) for (j=0;j<height;j++) for (i=0;i<width;i++) { if (MRIgetVoxVal(mri_val,i,j,k,0)>=1) { n++; if (MRIsetVoxVal(mri_val,i,j,k,0)>1) count++; } } // sprintf(fname,"./labeltotal"); /// f=fopen(fname,"a+"); //fprintf(f,"\n %s %d %d %f ",in_seg_fname,count,n,(float)100.*count/n); //fclose(f); #if 0 MRIwrite(mri_val,"/tmp/tmp"); #endif fprintf(stderr,"\n WE HAVE %d %d %f \n",count,n,100.*count/n); } #endif ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// if (parms.final_surface_file) { int euler,pnvertices, pnfaces, pnedges; mris=MRIScreateSurfaceFromVolume(mri_out,1,parms.connectivity); euler=MRIScomputeEulerNumber(mris,&pnvertices,&pnfaces,&pnedges); fprintf(stderr,"\nfinal euler characteristic = %d, %d vertices, %d faces, %d edges" ,euler,pnvertices,pnfaces,pnedges); sprintf(fname,"%s",parms.final_surface_file); MRISwrite(mris,fname); #if 0 MRISsmoothSurface(mris,7,0.2); strcat(fname,"_smooth"); MRISwrite(mris,fname); if (parms.fit) { sprintf(fname,parms.surfname); strcat(fname,"_fit"); MRISmatchSurfaceToLabel(parms.mris,parms.mri_output,1,NULL,NULL,parms.f_c); MRISwrite(parms.mris,fname); } #endif MRISfree(&mris); } if (mri_out) MRIfree(&mri_out); if (mri_orig) MRIfree(&mri_orig); if (mri_seg) MRIfree(&mri_seg); fprintf(stderr,"\n"); return NO_ERROR; }
static int MRISrepositionToInnerSkull(MRI_SURFACE *mris, MRI *mri_smooth, INTEGRATION_PARMS *parms) { MRI *mri_dist, *mri_bin, *mri_kernel, *mri_bin_smooth, *mri_dist_smooth ; float l_spring, sigma ; int i, ic_order, avgs ; parms->niterations = 1000 ; if (parms->momentum < 0.0) parms->momentum = 0.0 /*0.75*/ ; mri_bin = MRIbinarize(mri_smooth, NULL, 15, 0, TARGET_VAL) ; mri_dist = MRIdistanceTransform(mri_bin, NULL, TARGET_VAL, 10*mri_bin->width, DTRANS_MODE_SIGNED, NULL) ; MRIwrite(mri_bin, "bin.mgz") ; MRIwrite(mri_dist, "dist.mgz") ; MRISscaleBrain(mris, mris, 0.5) ; // start inside mri_kernel = MRIgaussian1d(2, 0) ; mri_bin_smooth = MRIconvolveGaussian(mri_bin, NULL, mri_kernel) ; MRIwrite(mri_bin_smooth, "bin_smooth.mgz") ; MRISfindOptimalRigidPosition(mris, mri_bin_smooth, parms) ; MRIfree(&mri_kernel) ; MRIfree(&mri_bin_smooth) ; avgs = parms->n_averages = 32 ; l_spring = parms->l_spring_norm ; for (ic_order = 3 ; ic_order <= 3 ; ic_order++) { if (ic_order != ic_init) { MRI_SURFACE *mris_new ; char fname[STRLEN], *mdir ; mdir = getenv("FREESURFER_HOME") ; if (!mdir) ErrorExit(ERROR_BADPARM, "FREESURFER_HOME not defined in environment") ; sprintf(fname, "%s/lib/bem/ic%d.tri", mdir, ic_order) ; mris_new = MRISread(fname) ; MRISupsampleIco(mris, mris_new) ; MRISfree(&mris) ; mris = mris_new ; } printf("********************** using ICO order %d *********************\n", ic_order) ; parms->n_averages = avgs ; parms->l_spring_norm = l_spring ; for (sigma = 16.0, i = 0 ; i < 7 ; i++, sigma /= 2) { printf("******************** pass %d, sigma = %2.2f, avgs = %d ******************\n", i+1, sigma, parms->n_averages) ; parms->sigma = sigma ; MRISsetVals(mris,parms->sigma) ; MRIScopyValToVal2(mris) ; MRISsetVals(mris, 0) ; // 0 mm from fat parms->mri_brain = mri_dist ; mri_kernel = MRIgaussian1d(sigma, 0) ; mri_dist_smooth = MRIconvolveGaussian(mri_dist, NULL, mri_kernel) ; MRIfree(&mri_kernel) ; if (i == 0) { MRIwrite(mri_dist_smooth, "dist_smooth.mgz") ; MRISwrite(mris, "lh.0000") ; } MRISsetVals(mris, 0) ; MRISpositionSurface(mris, mri_dist, mri_dist_smooth, parms) ; parms->l_spring_norm /= 2; parms->n_averages /= 2 ; } } MRIfree(&mri_bin) ; MRIfree(&mri_dist) ; return(NO_ERROR) ; }
int MRISpositionOptimalSphere(MRI_SURFACE *mris, MRI *mri_inner, float sample_dist) { double r, rmin, rmax, min_sse, min_r, min_x0, min_y0, min_z0, x0, y0, z0, sse, xmin, xmax, ymin, ymax, zmin, zmax, delta_r, delta_x, delta_y, delta_z ; int scale ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; MRIScomputeMetricProperties(mris) ; rmin = mris->radius*.5 ; rmax = mris->radius*1.5 ; min_x0 = mris->xctr ; min_y0 = mris->yctr ; min_z0 = mris->zctr ; min_r = mris->radius ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRISprojectOntoTranslatedSphere(mris, mris, min_r, min_x0, min_y0, min_z0) ; min_sse = compute_surface_sse(mris, mri_inner, sample_dist) ; delta_r = DELTA_R ; for (r = rmin ; r <= rmax ; r += delta_r) { MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRISprojectOntoTranslatedSphere(mris, mris, r, min_x0, min_y0, min_z0) ; sse = compute_surface_sse(mris, mri_inner, sample_dist) ; if (sse < min_sse) { min_sse = sse ; min_r = r ; printf("new min sse %2.0f, found at r=%2.1f mm, c = (%2.1f, %2.1f, %2.1f)\n", min_sse, min_r, min_x0, min_y0, min_z0) ; } } MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRISprojectOntoTranslatedSphere(mris, mris, min_r, min_x0, min_y0, min_z0) ; MRISwrite(mris, "lh.minr") ; for (scale = 8 ; scale >= 1 ; scale /=2) { printf("scale = %d\n", scale) ; xmin = min_x0-scale ; xmax = min_x0+scale ; ymin = min_y0-scale ; ymax = min_y0+scale ; zmin = min_z0-scale ; zmax = min_z0+scale ; rmin = min_r*(1.0-scale/128.0) ; rmax = min_r*(1.0+scale/128.0) ; delta_x = mri_inner->xsize*scale/16 ; delta_y = mri_inner->ysize*scale/16 ; delta_z = mri_inner->zsize*scale/16 ; delta_r = DELTA_R*scale ; if (delta_x > (xmax-xmin)/3) delta_x = (xmax-xmin)/3 ; if (delta_y > (ymax-ymin)/3) delta_y = (ymax-ymin)/3 ; if (delta_z > (zmax-zmin)/3) delta_z = (zmax-zmin)/3 ; if (delta_r > (rmax-rmin)/3) delta_r = (rmax-rmin)/3 ; for (x0 = xmin ; x0 <= xmax ; x0 += delta_x) { for (y0 = ymin ; y0 <= ymax ; y0 += delta_y) { for (z0 = zmin ; z0 <= zmax ; z0 += delta_z) { for (r = rmin ; r <= rmax ; r += delta_r) { MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRISprojectOntoTranslatedSphere(mris, mris, r, x0, y0, z0) ; sse = compute_surface_sse(mris, mri_inner, sample_dist) ; if (sse < min_sse) { min_sse = sse ; min_r = r ; min_x0 = x0 ; min_y0 = y0 ; min_z0 = z0 ; printf("new min sse %2.0f, found at r=%2.1f mm, c = (%2.1f, %2.1f, %2.1f)\n", min_sse, min_r, min_x0, min_y0, min_z0) ; } } } } } { char fname[STRLEN] ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRISprojectOntoTranslatedSphere(mris, mris, min_r, min_x0, min_y0, min_z0) ; sprintf(fname, "lh.min_scale%d", scale) ; MRISwrite(mris, fname) ; } } MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRISprojectOntoTranslatedSphere(mris, mris, min_r, min_x0, min_y0, min_z0) ; return(NO_ERROR) ; }
int main(int argc, char *argv[]) { char **av, fname[STRLEN], *T1_fname, *PD_fname, *output_dir, *mdir ; int ac, nargs, msec, s ; MRI_SURFACE *mris ; MRI *mri_flash1, *mri_flash2, *mri_masked, *mri_masked_smooth, *mri_kernel, *mri_mean, *mri_dif, *mri_binary, *mri_distance ; MRI *mri_smooth, *mri_grad, *mri_inner ; struct timeb then ; double l_spring ; MRI_SEGMENTATION *mriseg ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_AA_shrinkwrap.c,v 1.5 2011/03/02 00:04:34 nicks Exp $", "$Name: stable5 $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Gdiag |= DIAG_SHOW ; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; memset(&parms, 0, sizeof(parms)) ; parms.projection = NO_PROJECTION ; parms.tol = 0.05 ; parms.check_tol = 1 ; parms.ignore_energy = 1 ; parms.dt = 0.5f ; parms.base_dt = BASE_DT_SCALE*parms.dt ; parms.l_spring_norm = 1 ; parms.l_shrinkwrap = 0 ; parms.l_intensity = 1 ; parms.niterations = 0 ; parms.write_iterations = 0 /*WRITE_ITERATIONS */; parms.integration_type = INTEGRATE_MOMENTUM ; parms.momentum = 0.0 /*0.8*/ ; parms.l_intensity = 1 ; parms.dt_increase = 1.0 /* DT_INCREASE */; parms.dt_decrease = 0.50 /* DT_DECREASE*/ ; parms.error_ratio = 50.0 /*ERROR_RATIO */; /* parms.integration_type = INTEGRATE_LINE_MINIMIZE ;*/ parms.l_surf_repulse = 0.0 ; parms.l_repulse = 0 /*1*/ ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } mdir = getenv("FREESURFER_HOME") ; if (!mdir) ErrorExit(ERROR_BADPARM, "FREESURFER_HOME not defined in environment") ; if (argc < 4) usage_exit() ; /* set default parameters for white and gray matter surfaces */ parms.niterations = 1000 ; if (parms.momentum < 0.0) parms.momentum = 0.0 /*0.75*/ ; TimerStart(&then) ; T1_fname = argv[1] ; PD_fname = argv[2] ; output_dir = argv[3] ; fprintf(stderr, "reading volume %s...\n", T1_fname) ; mri_flash1 = MRIread(T1_fname) ; if (!mri_flash1) ErrorExit(ERROR_NOFILE, "%s: could not read input volume %s", Progname, T1_fname) ; mri_flash2 = MRIread(PD_fname) ; if (!mri_flash2) ErrorExit(ERROR_NOFILE, "%s: could not read input volume %s", Progname, T1_fname) ; // setMRIforSurface(mri_flash1); sprintf(fname, "%s/lib/bem/ic%d.tri", mdir, ic_init) ; mris = MRISread(fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read icosahedron %s", Progname, fname) ; mri_mean = MRImean(mri_flash1, NULL, 5) ; MRIwrite(mri_mean, "mean.mgz") ; mri_dif = MRIabsdiff(mri_flash1, mri_flash2, NULL) ; MRIwrite(mri_dif, "dif.mgz") ; mriseg = MRIsegment(mri_mean, 30, 100000) ; s = MRIsegmentMax(mriseg) ; mri_masked = MRIsegmentToImage(mri_flash1, NULL, mriseg, s) ; MRIwrite(mri_masked, "mask.mgz") ; MRIsegmentFree(&mriseg) ; // MRIthresholdMask(mri_dif, mri_masked, mri_dif, 1, 0) ; // MRIwrite(mri_dif, "dif_masked.mgz") ; mri_kernel = MRIgaussian1d(2, 0) ; mri_smooth = MRIconvolveGaussian(mri_dif, NULL, mri_kernel) ; MRIwrite(mri_smooth, "smooth.mgz") ; MRIScopyVolGeomFromMRI(mris, mri_smooth) ; mris->useRealRAS = 1 ; initialize_surface_position(mris, mri_dif, 1, &parms) ; MRISwrite(mris, "init") ; MRISrepositionToInnerSkull(mris, mri_smooth, &parms) ; exit(0) ; mri_grad = MRIsobel(mri_smooth, NULL, NULL) ; MRIwrite(mri_grad, "grad.mgz") ; mri_inner = MRIfindInnerBoundary(mri_dif, mri_grad, NULL, 5.0) ; MRIwrite(mri_inner, "inner.mgz") ; MRIbinarize(mri_inner, mri_inner, 10, 0, 128) ; MRISpositionOptimalSphere(mris, mri_inner, 6) ; MRISwrite(mris, "optimal") ; exit(0) ; parms.sigma = 4 / mri_flash1->xsize ; // mri_dist = create_distance_map(mri_masked, NULL, BORDER_VAL, OUTSIDE_BORDER_STEP) ; MRISsetVals(mris,parms.sigma) ; MRIScopyValToVal2(mris) ; MRISsetVals(mris, 0) ; sprintf(parms.base_name, "%s_inner_skull%s%s", "test", output_suffix, suffix) ; parms.mri_brain = mri_masked ; l_spring = parms.l_spring_norm ; mri_kernel = MRIgaussian1d(parms.sigma, 0) ; mri_binary = MRIbinarize(mri_dif, mri_binary, 40, 0, 128) ; MRIwrite(mri_binary, "bin.mgz") ; mri_distance = MRIdistanceTransform(mri_binary, NULL, 128, 100, DTRANS_MODE_SIGNED, NULL) ; MRIwrite(mri_distance, "dist.mgz") ; mri_masked_smooth = MRIconvolveGaussian(mri_distance, NULL, mri_kernel) ; MRIfree(&mri_kernel) ; MRIwrite(mri_masked_smooth, "dif_smooth.mgz") ; MRISwrite(mris, "inner_skull.tri") ; msec = TimerStop(&then) ; fprintf(stderr,"positioning took %2.1f minutes\n", (float)msec/(60*1000.0f)); 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[]) { tesselation_parms *parms; MRIS **mris_table, *mris,*mris_corrected; MRI *mri; char cmdline[CMD_LINE_LEN] ; make_cmd_version_string (argc, argv, "$Id: mri_mc.c,v 1.22 2011/03/02 00:04:23 nicks Exp $", "$Name: stable5 $", cmdline); Progname=argv[0]; if (argc > 1 && (stricmp(argv[1], "-d") == 0)) { downsample = atoi(argv[2]) ; argc -= 2; argv += 2 ; printf("downsampling input volume %d times\n", downsample) ; } if (argc < 4) { fprintf(stderr,"\n\nUSAGE: mri_mc input_volume " "label_value output_surface [connectivity]"); fprintf(stderr, "\noption connectivity: 1=6+,2=18,3=6,4=26 (default=1)\n\n"); exit(-1); } parms=(tesselation_parms*)calloc(1,sizeof(tesselation_parms)); if (!parms) ErrorExit(ERROR_NOMEMORY, "tesselation parms\n") ; mri=MRIread(argv[1]); if (downsample > 0) { MRI *mri_tmp ; mri_tmp = MRIdownsample2(mri, NULL) ; MRIfree(&mri) ; mri = mri_tmp ; } { MRI *mri_tmp ; mri_tmp = MRIalloc(mri->width+2, mri->height+2, mri->depth+2, mri->type) ; MRIextractInto(mri, mri_tmp, 0, 0, 0, mri->width, mri->height, mri->depth, 1, 1, 1) ; MRIfree(&mri) ; mri = mri_tmp ; } MRIreInitCache(mri); if (mri->type != MRI_UCHAR) { MRI *mri_tmp ; float min_val, max_val ; MRIvalRange(mri, &min_val, &max_val) ; if (min_val < 0 || max_val > 255) ErrorExit (ERROR_UNSUPPORTED, "%s: input volume (val range [%2.1f %2.1f]) must be " "convertible to UCHAR", Progname, min_val, max_val) ; printf("changing type of input volume to 8 bits/voxel...\n") ; mri_tmp = MRIchangeType(mri, MRI_UCHAR, 0.0, 0.999, TRUE) ; MRIfree(&mri) ; mri = mri_tmp ; } parms->mri=mri; parms->number_of_labels=1; //only one single label parms->label_values=(int*)malloc(sizeof(int)); parms->label_values[0]=atoi(argv[2]);//label; parms->ind=0; mris_table=(MRIS**)malloc(sizeof(MRIS*)); //final surface information parms->mris_table=mris_table; if ((!parms->label_values) || (!mris_table)) ErrorExit(ERROR_NOMEMORY, "labels/surfaces tables\n") ; if (argc==5) parms->connectivity=atoi(argv[4]);//connectivity; else parms->connectivity=1; initTesselationParms(parms); generateMCtesselation(parms); free(parms->label_values); mris=parms->mris_table[0]; free(parms->mris_table); freeTesselationParms(&parms); { float dist,max_e=0.0; int n,p,vn0,vn2; VERTEX *v,*vp; fprintf(stderr,"computing the maximum edge length..."); for (n = 0 ; n < mris->nvertices ; n++) { v=&mris->vertices[n]; for (p = 0 ; p < v->vnum ; p++) { vp = &mris->vertices[v->v[p]]; dist=SQR(vp->x-v->x)+SQR(vp->y-v->y)+SQR(vp->z-v->z); if (dist>max_e) max_e=dist; } } fprintf(stderr,"%f mm",sqrt(max_e)); fprintf(stderr,"\nreversing orientation of faces..."); for (n = 0 ; n < mris->nfaces ; n++) { vn0=mris->faces[n].v[0]; vn2=mris->faces[n].v[2]; /* vertex 0 becomes vertex 2 */ v=&mris->vertices[vn0]; for (p = 0 ; p < v->num ; p++) if (v->f[p]==n) v->n[p]=2; mris->faces[n].v[2]=vn0; /* vertex 2 becomes vertex 0 */ v=&mris->vertices[vn2]; for (p = 0 ; p < v->num ; p++) if (v->f[p]==n) v->n[p]=0; mris->faces[n].v[0]=vn2; } } fprintf(stderr,"\nchecking orientation of surface..."); MRISmarkOrientationChanges(mris); mris_corrected=MRISextractMainComponent(mris,0,1,0); MRISfree(&mris); fprintf(stderr,"\nwriting out surface..."); MRISaddCommandLine(mris_corrected, cmdline) ; if (mriConformed(mri) == 0) { printf("input volume is not conformed - using useRealRAS=1\n") ; mris_corrected->useRealRAS = 1 ; } // getVolGeom(mri, &mris_corrected->vg); MRISwrite(mris_corrected,argv[3]); fprintf(stderr,"done\n"); MRIfree(&mri); MRISfree(&mris_corrected); 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 */ }