static int con_and_fill_pathx(char* fname, char* ofname, char* surfaceFname, int seed) { printf("Reading %s\n",surfaceFname); MRIS* mris = MRISread(surfaceFname); if(mris == NULL) exit(1); return con_and_fill_pathy(fname, ofname, mris, seed); }
/*---------------------------------------------------------------*/ int main(int argc, char *argv[]) { int nargs; char *surf1_fname ; char *surf2_fname ; char *out_fname ; MRI_SURFACE *mris1, *mris2 ; nargs = handle_version_option (argc, argv, vcid, "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; cmdline = argv2cmdline(argc,argv); uname(&uts); getcwd(cwd,2000); Progname = argv[0] ; argc --; argv++; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; if (argc == 0) usage_exit(); parse_commandline(argc, argv); check_options(); if (checkoptsonly) return(0); dump_options(stdout); SUBJECTS_DIR = getenv("SUBJECTS_DIR"); if (SUBJECTS_DIR == NULL) { printf("ERROR: SUBJECTS_DIR not defined in environment\n"); exit(1); } surf1_fname = argv[0] ; surf2_fname = argv[1] ; out_fname = argv[2] ; mris1 = MRISread(surf1_fname) ; if (mris1 == NULL) ErrorExit(ERROR_NOFILE, "could not read surface 1 from %s", surf1_fname) ; mris2 = MRISread(surf2_fname) ; if (mris2 == NULL) ErrorExit(ERROR_NOFILE, "could not read surface 2 from %s", surf2_fname) ; compute_surface_distance(mris1, mris2, mris1) ; MRISwriteValues(mris1, out_fname) ; return 0; }
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[]) { MRI_SURFACE *mris ; char **av, *in_fname, *out_fname ; int ac, nargs ; MRI *mri_distance ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_distance_map.c,v 1.2 2011/03/02 00:04:31 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) usage_exit() ; in_fname = argv[1] ; out_fname = argv[2] ; mris = MRISread(in_fname) ; if (mris == NULL) ErrorExit(ERROR_NOFILE, "%s: could not load surface %s", Progname, out_fname) ; mri_distance = MRIScomputeDistanceMap(mris, NULL, ref_vertex_no) ; MRIwrite(mri_distance, out_fname) ; MRISfree(&mris) ; exit(0) ; return(0) ; /* for ansi */ }
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 */ }
int main(int argc, char *argv[]) { char **av, *out_fname, *in_fname ; int ac, nargs ; MRI_SURFACE *mris ; MRI *mri_interior, *mri_template = NULL, *mri_buffer = NULL; char cmdline[CMD_LINE_LEN] ; make_cmd_version_string (argc, argv, "$Id: mris_fill.c,v 1.6 2011/03/02 00:04:32 nicks Exp $", "$Name: $", cmdline); /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_fill.c,v 1.6 2011/03/02 00:04:32 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] ; fprintf(stderr, "reading surface from %s...\n", in_fname) ; mris = MRISread(in_fname) ; if (template)
/* --------------------------------------------- */ static void check_options(void) { struct timeval tv; FILE *fp; char tmpstr[1000]; if(volid == NULL && !NoOutput) { printf("A volume path must be supplied unless --no-output\n"); exit(1); } if (seed < 0) { gettimeofday(&tv, NULL); seed = tv.tv_sec + tv.tv_usec; } if (seedfile != NULL) { fp = fopen(seedfile,"w"); if (fp == NULL) { printf("ERROR: cannot open seed file %s\n",seedfile); exit(1); } fprintf(fp,"%ld\n",seed); fclose(fp); } if(DoCurv && nframes > 1){ printf("ERROR: cannot have more than 1 frame with curv output\n"); exit(1); } if(DoCurv){ sprintf(tmpstr,"%s/%s/surf/%s.orig",getenv("SUBJECTS_DIR"),subject,hemi); printf("Loading %s\n",tmpstr); surf = MRISread(tmpstr); if(!surf) exit(1); } if(!DoCurv) getfmtid(volid); return; }
/*---------------------------------------------------------------*/ int main(int argc, char *argv[]) { int nargs, n, err; char tmpstr[2000], *signstr=NULL,*SUBJECTS_DIR, fname[2000]; //char *OutDir = NULL; RFS *rfs; int nSmoothsPrev, nSmoothsDelta; MRI *z, *zabs=NULL, *sig=NULL, *p=NULL; int FreeMask = 0; int nthSign, nthFWHM, nthThresh; double sigmax, zmax, threshadj, csize, csizeavg, searchspace,avgvtxarea; int csizen; int nClusters, cmax,rmax,smax; SURFCLUSTERSUM *SurfClustList; struct timeb mytimer; LABEL *clabel; FILE *fp, *fpLog=NULL; nargs = handle_version_option (argc, argv, vcid, "$Name: stable5 $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; cmdline = argv2cmdline(argc,argv); uname(&uts); getcwd(cwd,2000); Progname = argv[0] ; argc --; argv++; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; if (argc == 0) usage_exit(); parse_commandline(argc, argv); check_options(); if (checkoptsonly) return(0); dump_options(stdout); if(LogFile){ fpLog = fopen(LogFile,"w"); if(fpLog == NULL){ printf("ERROR: opening %s\n",LogFile); exit(1); } dump_options(fpLog); } if(SynthSeed < 0) SynthSeed = PDFtodSeed(); srand48(SynthSeed); SUBJECTS_DIR = getenv("SUBJECTS_DIR"); // Create output directory printf("Creating %s\n",OutTop); err = fio_mkdirp(OutTop,0777); if(err) exit(1); for(nthFWHM=0; nthFWHM < nFWHMList; nthFWHM++){ for(nthThresh = 0; nthThresh < nThreshList; nthThresh++){ for(nthSign = 0; nthSign < nSignList; nthSign++){ if(SignList[nthSign] == 0) signstr = "abs"; if(SignList[nthSign] == +1) signstr = "pos"; if(SignList[nthSign] == -1) signstr = "neg"; sprintf(tmpstr,"%s/fwhm%02d/%s/th%02d", OutTop,(int)round(FWHMList[nthFWHM]), signstr,(int)round(10*ThreshList[nthThresh])); sprintf(fname,"%s/%s.csd",tmpstr,csdbase); if(fio_FileExistsReadable(fname)){ printf("ERROR: output file %s exists\n",fname); if(fpLog) fprintf(fpLog,"ERROR: output file %s exists\n",fname); exit(1); } err = fio_mkdirp(tmpstr,0777); if(err) exit(1); } } } // Load the target surface sprintf(tmpstr,"%s/%s/surf/%s.%s",SUBJECTS_DIR,subject,hemi,surfname); printf("Loading %s\n",tmpstr); surf = MRISread(tmpstr); if(!surf) return(1); // Handle masking if(LabelFile){ printf("Loading label file %s\n",LabelFile); sprintf(tmpstr,"%s/%s/label/%s.%s.label", SUBJECTS_DIR,subject,hemi,LabelFile); if(!fio_FileExistsReadable(tmpstr)){ printf(" Cannot find label file %s\n",tmpstr); sprintf(tmpstr,"%s",LabelFile); printf(" Trying label file %s\n",tmpstr); if(!fio_FileExistsReadable(tmpstr)){ printf(" ERROR: cannot read or find label file %s\n",LabelFile); exit(1); } } printf("Loading %s\n",tmpstr); clabel = LabelRead(NULL, tmpstr); mask = MRISlabel2Mask(surf, clabel, NULL); FreeMask = 1; } if(MaskFile){ printf("Loading %s\n",MaskFile); mask = MRIread(MaskFile); if(mask == NULL) exit(1); } if(mask && SaveMask){ sprintf(tmpstr,"%s/mask.mgh",OutTop); printf("Saving mask to %s\n",tmpstr); err = MRIwrite(mask,tmpstr); if(err) exit(1); } // Compute search space searchspace = 0; nmask = 0; for(n=0; n < surf->nvertices; n++){ if(mask && MRIgetVoxVal(mask,n,0,0,0) < 0.5) continue; searchspace += surf->vertices[n].area; nmask++; } printf("Found %d voxels in mask\n",nmask); if(surf->group_avg_surface_area > 0) searchspace *= (surf->group_avg_surface_area/surf->total_area); printf("search space %g mm2\n",searchspace); avgvtxarea = searchspace/nmask; printf("average vertex area %g mm2\n",avgvtxarea); // Determine how many iterations are needed for each FWHM nSmoothsList = (int *) calloc(sizeof(int),nFWHMList); for(nthFWHM=0; nthFWHM < nFWHMList; nthFWHM++){ nSmoothsList[nthFWHM] = MRISfwhm2niters(FWHMList[nthFWHM], surf); printf("%2d %5.1f %4d\n",nthFWHM,FWHMList[nthFWHM],nSmoothsList[nthFWHM]); if(fpLog) fprintf(fpLog,"%2d %5.1f %4d\n",nthFWHM,FWHMList[nthFWHM],nSmoothsList[nthFWHM]); } printf("\n"); // Allocate the CSDs for(nthFWHM=0; nthFWHM < nFWHMList; nthFWHM++){ for(nthThresh = 0; nthThresh < nThreshList; nthThresh++){ for(nthSign = 0; nthSign < nSignList; nthSign++){ csd = CSDalloc(); sprintf(csd->simtype,"%s","null-z"); sprintf(csd->anattype,"%s","surface"); sprintf(csd->subject,"%s",subject); sprintf(csd->hemi,"%s",hemi); sprintf(csd->contrast,"%s","NA"); csd->seed = SynthSeed; csd->nreps = nRepetitions; csd->thresh = ThreshList[nthThresh]; csd->threshsign = SignList[nthSign]; csd->nullfwhm = FWHMList[nthFWHM]; csd->varfwhm = -1; csd->searchspace = searchspace; CSDallocData(csd); csdList[nthFWHM][nthThresh][nthSign] = csd; } } } // Alloc the z map z = MRIallocSequence(surf->nvertices, 1,1, MRI_FLOAT, 1); // Set up the random field specification rfs = RFspecInit(SynthSeed,NULL); rfs->name = strcpyalloc("gaussian"); rfs->params[0] = 0; rfs->params[1] = 1; printf("Thresholds (%d): ",nThreshList); for(n=0; n < nThreshList; n++) printf("%5.2f ",ThreshList[n]); printf("\n"); printf("Signs (%d): ",nSignList); for(n=0; n < nSignList; n++) printf("%2d ",SignList[n]); printf("\n"); printf("FWHM (%d): ",nFWHMList); for(n=0; n < nFWHMList; n++) printf("%5.2f ",FWHMList[n]); printf("\n"); // Start the simulation loop printf("\n\nStarting Simulation over %d Repetitions\n",nRepetitions); if(fpLog) fprintf(fpLog,"\n\nStarting Simulation over %d Repetitions\n",nRepetitions); TimerStart(&mytimer) ; for(nthRep = 0; nthRep < nRepetitions; nthRep++){ msecTime = TimerStop(&mytimer) ; printf("%5d %7.1f ",nthRep,(msecTime/1000.0)/60); if(fpLog) { fprintf(fpLog,"%5d %7.1f ",nthRep,(msecTime/1000.0)/60); fflush(fpLog); } // Synthesize an unsmoothed z map RFsynth(z,rfs,mask); nSmoothsPrev = 0; // Loop through FWHMs for(nthFWHM=0; nthFWHM < nFWHMList; nthFWHM++){ printf("%d ",nthFWHM); if(fpLog) { fprintf(fpLog,"%d ",nthFWHM); fflush(fpLog); } nSmoothsDelta = nSmoothsList[nthFWHM] - nSmoothsPrev; nSmoothsPrev = nSmoothsList[nthFWHM]; // Incrementally smooth z MRISsmoothMRI(surf, z, nSmoothsDelta, mask, z); // smooth z // Rescale RFrescale(z,rfs,mask,z); // Slightly tortured way to get the right p-values because // RFstat2P() computes one-sided, but I handle sidedness // during thresholding. // First, use zabs to get a two-sided pval bet 0 and 0.5 zabs = MRIabs(z,zabs); p = RFstat2P(zabs,rfs,mask,0,p); // Next, mult pvals by 2 to get two-sided bet 0 and 1 MRIscalarMul(p,p,2.0); sig = MRIlog10(p,NULL,sig,1); // sig = -log10(p) for(nthThresh = 0; nthThresh < nThreshList; nthThresh++){ for(nthSign = 0; nthSign < nSignList; nthSign++){ csd = csdList[nthFWHM][nthThresh][nthSign]; // If test is not ABS then apply the sign if(csd->threshsign != 0) MRIsetSign(sig,z,0); // Get the max stats sigmax = MRIframeMax(sig,0,mask,csd->threshsign, &cmax,&rmax,&smax); zmax = MRIgetVoxVal(z,cmax,rmax,smax,0); if(csd->threshsign == 0){ zmax = fabs(zmax); sigmax = fabs(sigmax); } // Mask if(mask) MRImask(sig,mask,sig,0.0,0.0); // Surface clustering MRIScopyMRI(surf, sig, 0, "val"); if(csd->threshsign == 0) threshadj = csd->thresh; else threshadj = csd->thresh - log10(2.0); // one-sided test SurfClustList = sclustMapSurfClusters(surf,threshadj,-1,csd->threshsign, 0,&nClusters,NULL); // Actual area of cluster with max area csize = sclustMaxClusterArea(SurfClustList, nClusters); // Number of vertices of cluster with max number of vertices. // Note: this may be a different cluster from above! csizen = sclustMaxClusterCount(SurfClustList, nClusters); // Area of this cluster based on average vertex area. This just scales // the number of vertices. csizeavg = csizen * avgvtxarea; if(UseAvgVtxArea) csize = csizeavg; // Store results csd->nClusters[nthRep] = nClusters; csd->MaxClusterSize[nthRep] = csize; csd->MaxSig[nthRep] = sigmax; csd->MaxStat[nthRep] = zmax; } // Sign } // Thresh } // FWHM printf("\n"); if(fpLog) fprintf(fpLog,"\n"); if(SaveEachIter || fio_FileExistsReadable(SaveFile)) SaveOutput(); if(fio_FileExistsReadable(StopFile)) { printf("Found stop file %s\n",StopFile); goto finish; } } // Simulation Repetition finish: SaveOutput(); msecTime = TimerStop(&mytimer) ; printf("Total Sim Time %g min (%g per rep)\n", msecTime/(1000*60.0),(msecTime/(1000*60.0))/nthRep); if(fpLog) fprintf(fpLog,"Total Sim Time %g min (%g per rep)\n", msecTime/(1000*60.0),(msecTime/(1000*60.0))/nthRep); if(DoneFile){ fp = fopen(DoneFile,"w"); fprintf(fp,"%g\n",msecTime/(1000*60.0)); fclose(fp); } printf("mri_mcsim done\n"); if(fpLog){ fprintf(fpLog,"mri_mcsim done\n"); fclose(fpLog); } exit(0); }
int main(int argc, char *argv[]) { char **av, *hemi, *subject_name, *cp, fname[STRLEN]; char *parc_name, *annot_name ; int ac, nargs, vno, i ; MRI_SURFACE *mris ; MRI *mri_parc ; VERTEX *v ; double d ; Real x, y, z, xw, yw, zw ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_sample_parc.c,v 1.31 2016/12/11 14:33:38 fischl Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc < 4) usage_exit() ; subject_name = argv[1] ; hemi = argv[2] ; parc_name = argv[3] ; annot_name = argv[4] ; if (strlen(sdir) == 0) /* if not specified explicitly as option */ { cp = getenv("SUBJECTS_DIR") ; if (!cp) ErrorExit(ERROR_BADPARM, "%s: SUBJECTS_DIR not defined in environment.\n", Progname) ; strcpy(sdir, cp) ; } if (parc_name[0] == '/') // full path specified strcpy(fname, parc_name) ; else sprintf(fname, "%s/%s/mri/%s", sdir, subject_name, parc_name) ; printf("reading parcellation volume from %s...\n", fname) ; mri_parc = MRIread(fname) ; if (!mri_parc) ErrorExit(ERROR_NOFILE, "%s: could not read input volume %s", Progname, fname) ; if (mask_fname) { MRI *mri_mask, *mri_tmp ; mri_tmp = MRIread(mask_fname) ; if (mri_tmp == NULL) ErrorExit(ERROR_BADPARM, "%s: could not load mask volume %s", Progname, mask_fname) ; mri_mask = MRIclone(mri_tmp, NULL) ; MRIcopyLabel(mri_tmp, mri_mask, mask_val) ; MRIdilate(mri_mask, mri_mask) ; MRIdilate(mri_mask, mri_mask) ; MRIdilate(mri_mask, mri_mask) ; MRIdilate(mri_mask, mri_mask) ; MRIfree(&mri_tmp) ; mri_tmp = MRIclone(mri_parc, NULL) ; MRIcopyLabeledVoxels(mri_parc, mri_mask, mri_tmp, mask_val) ; MRIfree(&mri_parc) ; mri_parc = mri_tmp ; if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) MRIwrite(mri_parc, "p.mgz") ; MRIfree(&mri_mask) ; } for (i = 0 ; i < ntrans ; i++) { MRIreplaceValues(mri_parc, mri_parc, trans_in[i], trans_out[i]) ; } sprintf(fname, "%s/%s/surf/%s.%s", sdir, subject_name, hemi, surf_name) ; printf("reading input surface %s...\n", fname) ; mris = MRISread(fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, fname) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; MRIScomputeMetricProperties(mris) ; if (avgs > 0) MRISaverageVertexPositions(mris, avgs) ; if (FZERO(proj_mm)) { if (MRISreadCurvatureFile(mris, thickness_name) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read thickness file %s", Progname, thickness_name) ; } if (color_table_fname) { mris->ct = CTABreadASCII(color_table_fname) ; if (mris->ct == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read color file %s", Progname, color_table_fname) ; } if (sample_from_vol_to_surf) // sample from volume to surface */ { MRIsampleParcellationToSurface(mris, mri_parc) ; } else /* sample from surface to volume */ { for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->ripflag) continue ; if (vno == Gdiag_no) DiagBreak() ; if (!FZERO(proj_mm)) d = proj_mm ; else d = v->curv*proj_frac ; /* halfway out */ x = v->x+d*v->nx ; y = v->y+d*v->ny ; z = v->z+d*v->nz ; MRIsurfaceRASToVoxel(mri_parc, x, y, z, &xw, &yw, &zw) ; v->annotation = v->val = MRIfindNearestNonzero(mri_parc, wsize, xw, yw, zw, ((float)wsize-1)/2) ; if (v->val == 0xffffffff) DiagBreak() ; } } if (replace_label) replace_vertices_with_label(mris, mri_parc, replace_label, proj_mm); if (unknown_label >= 0) { LABEL **labels, *label ; int nlabels, i, biggest_label, most_vertices, nzero ; #define TMP_LABEL 1000 for (nzero = vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->annotation == 0) { v->annotation = TMP_LABEL; nzero++ ; } } printf("%d unknown vertices found\n", nzero) ; MRISsegmentAnnotated(mris, &labels, &nlabels, 10) ; most_vertices = 0 ; biggest_label = -1 ; for (i = 0 ; i < nlabels ; i++) { label = labels[i] ; if (mris->vertices[label->lv[0].vno].annotation == TMP_LABEL) { if (label->n_points > most_vertices) { biggest_label = i ; most_vertices = label->n_points ; } } } if (biggest_label >= 0) { label = labels[biggest_label] ; printf("replacing label # %d with %d vertices " "(vno=%d) with label %d\n", biggest_label, label->n_points, label->lv[0].vno, unknown_label) ; for (i = 0 ; i < label->n_points ; i++) { v = &mris->vertices[label->lv[i].vno] ; v->annotation = v->val = unknown_label ; } } for (nzero = vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->annotation == TMP_LABEL) { v->annotation = 0; nzero++ ; } } printf("after replacement, %d unknown vertices found\n", nzero) ; MRISmodeFilterZeroVals(mris) ; /* get rid of the rest of the unknowns by mode filtering */ for (i = 0 ; i < nlabels ; i++) LabelFree(&labels[i]) ; free(labels) ; } MRIScopyValsToAnnotations(mris) ; if (fix_topology != 0) fix_label_topology(mris, fix_topology) ; if (mode_filter) { printf("mode filtering sample labels...\n") ; #if 0 MRISmodeFilterZeroVals(mris) ; #else MRISmodeFilterVals(mris, mode_filter) ; #endif for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->ripflag) continue ; v->annotation = v->val ; } } /* this will fill in the v->annotation field from the v->val ones */ translate_indices_to_annotations(mris, translation_fname) ; if (label_index >= 0) { int index ; LABEL *area ; printf("writing label to %s...\n", annot_name) ; MRISclearMarks(mris) ; for (vno = 0 ; vno < mris->nvertices ; vno++) { if (vno == Gdiag_no) DiagBreak() ; v = &mris->vertices[vno] ; if (v->annotation > 0) DiagBreak() ; CTABfindAnnotation(mris->ct, v->annotation, &index); if (index == label_index) v->marked = 1 ; } area = LabelFromMarkedSurface(mris) ; if (nclose > 0) { LabelDilate(area, mris, nclose, CURRENT_VERTICES) ; LabelErode(area, mris, nclose) ; } LabelWrite(area, annot_name) ; } else { printf("writing annotation to %s...\n", annot_name) ; MRISwriteAnnotation(mris, annot_name) ; } /* MRISreadAnnotation(mris, fname) ;*/ exit(0) ; return(0) ; /* for ansi */ }
int main(int argc, char *argv[]) { char **av, *output_fname ; int ac, nargs, msec, mode=-1 ; LABEL *area = NULL ; MRI_SURFACE *mris ; struct timeb then ; MRI *mri_dist ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_distance_transform.c,v 1.5 2013/04/12 20:59:17 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) ; 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() ; TimerStart(&then) ; mris = MRISread(argv[1]) ; if (mris == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read surface %s", Progname, argv[1]) ; if (vol) { /* mri_template = MRIread(argv[2]) ; if (!mri_template) ErrorExit(ERROR_NOFILE, "%s: could not read MRI volume from %s\n", Progname, argv[2]) ; */ } else { area = LabelRead(NULL, argv[2]) ; if (area == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read label %s", Progname, argv[2]) ; if (anterior_dist > 0) LabelCropAnterior(area, anterior_dist) ; if (posterior_dist > 0) LabelCropPosterior(area, posterior_dist) ; } if (stricmp(argv[3], "signed") == 0) mode = DTRANS_MODE_SIGNED ; else if (stricmp(argv[3], "unsigned") == 0) mode = DTRANS_MODE_UNSIGNED ; else if (stricmp(argv[3], "outside") == 0) mode = DTRANS_MODE_OUTSIDE ; else { print_usage() ; ErrorExit(ERROR_BADPARM, "unrecognized mode choice %s\n", argv[3]) ; } output_fname = argv[4] ; MRIScomputeMetricProperties(mris) ; if (vol) { mri_dist = MRIScomputeDistanceToSurface(mris, NULL, 0.25) ; MRIwrite(mri_dist, argv[4]) ; } else { MRIScomputeSecondFundamentalForm(mris) ; if (normalize > 0) { normalize = sqrt(mris->total_area) ; printf("normalizing surface distances by sqrt(%2.1f) = %2.1f\n", mris->total_area,normalize) ; } if (divide > 1) { int i ; char fname[STRLEN], ext[STRLEN], base_name[STRLEN] ; LABEL *area_division ; FileNameExtension(output_fname, ext) ; FileNameRemoveExtension(output_fname, base_name) ; LabelMark(area, mris) ; MRIScopyMarksToAnnotation(mris) ; MRISsaveVertexPositions(mris, TMP_VERTICES) ; if (MRISreadVertexPositions(mris, divide_surf_name) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read vertex coords from %s", Progname, divide_surf_name) ; MRIScomputeSecondFundamentalForm(mris) ; MRISdivideAnnotationUnit(mris, 1, divide) ; MRISrestoreVertexPositions(mris, TMP_VERTICES) ; MRIScomputeSecondFundamentalForm(mris) ; // MRISdivideAnnotationUnit sets the marked to be in [0,divide-1], make it [1,divide] // make sure they are oriented along original a/p direction #define MAX_UNITS 100 { double cx[MAX_UNITS], cy[MAX_UNITS], cz[MAX_UNITS], min_a ; int index, num[MAX_UNITS], new_index[MAX_UNITS], j, min_i ; VERTEX *v ; memset(num, 0, sizeof(num[0])*divide) ; memset(cx, 0, sizeof(cx[0])*divide) ; memset(cy, 0, sizeof(cy[0])*divide) ; memset(cz, 0, sizeof(cz[0])*divide) ; for (i = 0 ; i < area->n_points ; i++) { if (area->lv[i].vno < 0 || area->lv[i].deleted > 0) continue ; v = &mris->vertices[area->lv[i].vno] ; v->marked++ ; index = v->marked ; cx[index] += v->x ; cy[index] += v->y ; cz[index] += v->z ; num[index]++ ; } memset(new_index, 0, sizeof(new_index[0])*divide) ; for (i = 1 ; i <= divide ; i++) cy[i] /= num[i] ; // order them from posterior to anterior for (j = 1 ; j <= divide ; j++) { min_a = 1e10 ; min_i = 0 ; for (i = 1 ; i <= divide ; i++) { if (cy[i] < min_a) { min_a = cy[i] ; min_i = i ; } } cy[min_i] = 1e10 ; // make it biggest so it won't be considered again new_index[j] = min_i ; } for (i = 0 ; i < area->n_points ; i++) { if (area->lv[i].vno < 0 || area->lv[i].deleted > 0) continue ; v = &mris->vertices[area->lv[i].vno] ; v->marked = new_index[v->marked] ; } } for (i = 1 ; i <= divide ; i++) { area_division = LabelFromMarkValue(mris, i) ; printf("performing distance transform on division %d with %d vertices\n", i, area_division->n_points) ; if (output_label) { sprintf(fname, "%s%d.label", base_name, i) ; printf("writing %dth subdivision to %s\n", i, fname) ; LabelWrite(area_division, fname); } MRISdistanceTransform(mris, area_division, mode) ; sprintf(fname, "%s%d.%s", base_name, i, ext) ; if (normalize > 0) MRISmulVal(mris, 1.0/normalize) ; MRISwriteValues(mris, fname) ; } } else { MRISdistanceTransform(mris, area, mode) ; if (normalize > 0) MRISmulVal(mris, 1.0/normalize) ; MRISwriteValues(mris, output_fname) ; } } msec = TimerStop(&then) ; fprintf(stderr,"distance transform took %2.1f minutes\n", (float)msec/(60*1000.0f)); exit(0) ; return(0) ; /* for ansi */ }
int main(int argc, char *argv[]) { char **av, surf_fname[100], *template_fname, *out_fname, *surf_dir, *hemi, *sphere_name ; int ac, nargs ; MRI_SURFACE *mris ; MRI_SP *mrisp, *mrisp_template ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_add_template.c,v 1.8 2011/03/02 00:04:26 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 < 5) usage_exit() ; surf_dir = argv[1] ; hemi = argv[2] ; sphere_name = argv[3] ; out_fname = template_fname = argv[4] ; if (argc > 5) out_fname = argv[5] ; sprintf(surf_fname, "%s/%s.%s", surf_dir, hemi, sphere_name) ; fprintf(stderr, "reading new surface %s...\n", surf_fname) ; mris = MRISread(surf_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; if (!FileExists(template_fname)) /* first time - create it */ { fprintf(stderr, "creating new parameterization...\n") ; mrisp_template = MRISPalloc(scale, PARAM_IMAGES); } 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) ; } /* first read in inflated surface and use it to build the first template set. */ sprintf(surf_fname, "%s/%s.%s", surf_dir, hemi, INFLATED_NAME) ; if (MRISreadVertexPositions(mris, surf_fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; MRISsetNeighborhoodSize(mris, nbrs) ; MRIScomputeMetricProperties(mris) ; MRIScomputeSecondFundamentalForm(mris) ; MRISuseMeanCurvature(mris) ; MRISaverageCurvatures(mris, navgs) ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRISnormalizeCurvature(mris, which_norm) ; fprintf(stderr, "computing parameterization for surface %s...\n",surf_fname); mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; MRISPcombine(mrisp, mrisp_template, 0) ; MRISPfree(&mrisp) ; /* now do the same thing with the smoothwm curvatures. */ sprintf(surf_fname, "%s/%s.%s", surf_dir, hemi, SMOOTH_NAME) ; if (MRISreadVertexPositions(mris, surf_fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; MRIScomputeMetricProperties(mris) ; if (curvature_fname[0]) MRISreadCurvatureFile(mris, curvature_fname) ; else { MRIScomputeSecondFundamentalForm(mris) ; MRISuseMeanCurvature(mris) ; } MRISaverageCurvatures(mris, navgs) ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; if (curvature_fname[0]) fprintf(stderr, "computing parameterization for surface %s (%s)...\n", surf_fname, curvature_fname); else fprintf(stderr, "computing parameterization for surface %s...\n", surf_fname); MRISnormalizeCurvature(mris, which_norm) ; mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; MRISPcombine(mrisp, mrisp_template, 3) ; fprintf(stderr, "writing updated template to %s...\n", out_fname) ; MRISPwrite(mrisp_template, out_fname) ; MRISPfree(&mrisp) ; MRISPfree(&mrisp_template) ; MRISfree(&mris) ; 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_name, *out_prefix, *fname; int nargs, ac, i, nsubjects, total, index; double scalar, std, tmp, maxV, minV, meanV; MRI *SrcVals[2], *AvgVals; MRI_SURFACE *BaseSurf; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_diff_on_surface.c,v 1.3 2011/03/02 00:04:55 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 ; } /* command line: <surf> <datafile 1> <datafile 2> <output prefix> */ if (argc != 5) usage_exit(); surf_name = argv[1]; out_prefix = argv[argc - 1]; if (srctypestring == NULL || trgtypestring == NULL) { printf("Please specify both input and output data type!\n"); usage_exit(); } printf("Reading underlying surface file\n"); BaseSurf = MRISread(surf_name); if (!BaseSurf) ErrorExit(ERROR_NOFILE, "%s:could not read surface %s", Progname, surf_name); printf("Base surface has %d vertices\n", BaseSurf->nvertices); /* Read in the first data file */ fname = argv[2]; /* only two data types are supported */ if (!strcmp(srctypestring,"curv")) { /* curvature file */ if (MRISreadCurvatureFile(BaseSurf, fname) != 0) { printf("ERROR: reading curvature file\n"); exit(1); } SrcVals[0] = MRIcopyMRIS(NULL, BaseSurf, 0, "curv"); } else if (!strcmp(srctypestring,"paint") || !strcmp(srctypestring,"w")) { MRISreadValues(BaseSurf,fname); SrcVals[0] = MRIcopyMRIS(NULL, BaseSurf, 0, "val"); } else { printf("ERROR: unknown data file format\n"); exit(1); } if (SrcVals[0] == NULL) { fprintf(stderr, "ERROR loading data values from %s\n", fname); } /* Read in the second data file */ fname = argv[3]; /* only two data types are supported */ if (!strcmp(srctypestring,"curv")) { /* curvature file */ if (MRISreadCurvatureFile(BaseSurf, fname) != 0) { printf("ERROR: reading curvature file\n"); exit(1); } SrcVals[1] = MRIcopyMRIS(NULL, BaseSurf, 0, "curv"); } else if (!strcmp(srctypestring,"paint") || !strcmp(srctypestring,"w")) { MRISreadValues(BaseSurf,fname); SrcVals[1] = MRIcopyMRIS(NULL, BaseSurf, 0, "val"); } else { printf("ERROR: unknown data file format\n"); exit(1); } if (SrcVals[1] == NULL) { fprintf(stderr, "ERROR loading data values from %s\n", fname); } if (debugflag) { for (i=0; i < 2; i++) { printf("Data%d at vertex %d has value %g\n",i, debugvtx, MRIFseq_vox(SrcVals[i], debugvtx, 0, 0, 0)); } } #if 0 AvgVals = MRIclone(SrcVals[0], NULL); if (negflag) /* Add the two data sets */ AvgVals = MRIadd(SrcVals[0], SrcVals[1], AvgVals); else /* Data1 - Data2 */ AvgVals = MRIsubtract(SrcVals[0], SrcVals[1], AvgVals); #endif AvgVals = MRIcopy(SrcVals[0], NULL); if (negflag) { for (index=0; index < BaseSurf->nvertices; index++) { MRIFseq_vox(AvgVals, index, 0, 0, 0) = MRIFseq_vox(SrcVals[0], index, 0, 0, 0) + MRIFseq_vox(SrcVals[1], index, 0, 0, 0); } } else { for (index=0; index < BaseSurf->nvertices; index++) { MRIFseq_vox(AvgVals, index, 0, 0, 0) = MRIFseq_vox(SrcVals[0], index, 0, 0, 0) - MRIFseq_vox(SrcVals[1], index, 0, 0, 0); } } maxV = -1000.0; minV = 1000.0; meanV=0.0; for (index=0; index < BaseSurf->nvertices; index++) { scalar = MRIFseq_vox(AvgVals, index, 0, 0, 0); if (maxV < scalar) maxV = scalar; if (minV > scalar) minV = scalar; meanV += scalar; } meanV /= BaseSurf->nvertices; printf("Output max = %g, min = %g, mean = %g\n", maxV, minV, meanV); if (debugflag) { printf("Output at vertex %d has value %g\n", debugvtx, MRIFseq_vox(AvgVals, debugvtx, 0, 0, 0)); } if (pathflag) sprintf(fname, "%s", out_prefix); else { if (negflag) sprintf(fname, "%s.sum.w", out_prefix) ; else sprintf(fname, "%s.diff.w", out_prefix) ; } if (!strcmp(trgtypestring,"paint") || !strcmp(trgtypestring,"w")) { /* This function will remove a zero-valued vertices */ /* Make sense, since default value is considered as zero */ /* But it will confuse the processing with matlab! */ /* So I copy the data to the curv field to force every value is * written out */ /* MRIScopyMRI(BaseSurf, AvgVals, framesave, "val");*/ /* MRISwriteValues(BaseSurf,fname); */ MRIScopyMRI(BaseSurf, AvgVals, framesave, "curv"); MRISwriteCurvatureToWFile(BaseSurf,fname); } else { fprintf(stderr, "ERROR unknown output file format.\n"); } /* Free memories */ MRISfree(&BaseSurf); MRIfree(&AvgVals); for (i=0; i < 2; i++) { MRIfree(&SrcVals[i]); } return 0; }
/*---------------------------------------------------------------------- Parameters: Description: ----------------------------------------------------------------------*/ int main(int argc, char *argv[]) { char **av, *subject_name, *cp, *hemi, *surf_name, *annot_name, fname[STRLEN], *name ; int ac, nargs, msec, minutes, label, seconds, i ; double area, total_area ; struct timeb start ; MRIS *mris ; FILE *log_fp ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_compute_overlap.c,v 1.6 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) ; 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) ; subject_name = argv[1] ; hemi = argv[2] ; surf_name = argv[3] ; annot_name = argv[4] ; if (strlen(sdir) == 0) { cp = getenv("SUBJECTS_DIR") ; if (!cp) ErrorExit (ERROR_BADPARM, "%s: SUBJECTS_DIR not defined in env or cmd line", Progname) ; strcpy(sdir, cp) ; } sprintf(fname, "%s/%s/surf/%s.%s", sdir, subject_name, hemi, surf_name) ; mris = MRISread(fname) ; if (!mris) ErrorExit (ERROR_NOFILE, "%s: could not read surface from %s", Progname,fname) ; MRIScomputeMetricProperties(mris) ; #if 0 if (in_label >= 0) MRIreplaceValues(mri, mri, in_label, out_label) ; #endif if (compute_pct) total_area = mris->total_area ; else total_area = 1 ; if (MRISreadAnnotation(mris, annot_name) != NO_ERROR) ErrorExit (ERROR_NOFILE, "%s: could not read annot file %s", Progname, annot_name) ; for (i = 5 ; i < argc ; i++) { label = atoi(argv[i]) ; name = annotation_to_name(index_to_annotation(label), NULL) ; printf("processing label %s (%d)...\n", name, label) ; area = MRISannotArea(mris, label) ; if (log_fname) { char fname[STRLEN] ; sprintf(fname, log_fname, label) ; printf("logging to %s...\n", fname) ; log_fp = fopen(fname, "a+") ; if (!log_fp) ErrorExit(ERROR_BADFILE, "%s: could not open %s for writing", Progname, fname) ; } else log_fp = NULL ; if (compute_pct) { printf("%2.3f mm^2 in label %d (%s), " "%%%2.6f of total cortical area (%2.2f)\n", area, label, name, 100.0*(float)area/(float)total_area, total_area) ; if (log_fp) { fprintf(log_fp,"%2.6f\n", 100.0*area/(float)total_area) ; fclose(log_fp) ; } } else { printf("%2.0f mm^2 in label %s (%d)\n", area, name, label) ; if (log_fp) { fprintf(log_fp,"%f\n", area) ; fclose(log_fp) ; } } } msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; if (DIAG_VERBOSE_ON) printf("area calculation took %d minutes and %d seconds.\n", minutes, seconds) ; exit(0) ; return(0) ; }
int main(int argc, char *argv[]) { char **av, fname[STRLEN], *cp ; int ac, nargs ; char *subject, *out_fname, *hemi, *ohemi ; int msec, minutes, seconds ; struct timeb start ; MRI *mri, *mri_features, *mri_ribbon, *mri_aseg, *mri_aparc ; MRI_SURFACE *mris, *mris_contra ; LABEL *cortex ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mri_extract_fcd_features.c,v 1.1 2016/06/15 17:51:09 fischl Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; TimerStart(&start) ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc < 4) usage_exit(1) ; subject = argv[1] ; hemi = argv[2] ; if (strcmp(hemi, "lh") == 0) ohemi = "rh" ; else ohemi = "lh" ; out_fname = argv[3] ; printf("reading data for subject %s and writing output to %s\n", subject, out_fname) ; if (!strlen(sdir)) { cp = getenv("SUBJECTS_DIR") ; if (!cp) ErrorExit(ERROR_BADPARM, "%s: SUBJECTS_DIR not defined in environment.\n", Progname) ; strcpy(sdir, cp) ; } sprintf(fname, "%s/%s/surf/%s.%s", sdir, subject, hemi, white_name) ; printf("reading %s\n", fname) ; mris = MRISread(fname) ; if (mris == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read surface from %s\n", Progname, fname) ; MRISsaveVertexPositions(mris, WHITE_VERTICES) ; sprintf(fname, "%s/%s/surf/%s.%s", sdir, subject, ohemi, white_name) ; printf("reading %s\n", fname) ; mris_contra = MRISread(fname) ; if (mris_contra == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read surface from %s\n", Progname, fname) ; MRISsaveVertexPositions(mris_contra, WHITE_VERTICES) ; sprintf(fname, "%s/%s/mri/%s", sdir, subject, ribbon_name) ; printf("reading %s\n", fname) ; mri_ribbon = MRIread(fname) ; if (mri_ribbon == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read ribbon from %s\n", Progname, fname) ; sprintf(fname, "%s/%s/mri/%s", sdir, subject, aparc_name) ; printf("reading %s\n", fname) ; mri_aparc = MRIread(fname) ; if (mri_aparc == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read ribbon from %s\n", Progname, fname) ; sprintf(fname, "%s/%s/mri/%s", sdir, subject, aseg_name) ; printf("reading %s\n", fname) ; mri_aseg = MRIread(fname) ; if (mri_aseg == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read aseg from %s\n", Progname, fname) ; sprintf(fname, "%s/%s/surf/%s.%s", sdir, subject, hemi, pial_name) ; if (MRISreadPialCoordinates(mris, fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read pial coordinates from %s\n", Progname, fname) ; sprintf(fname, "%s/%s/surf/%s.%s", sdir, subject, hemi, sphere_name) ; if (MRISreadCanonicalCoordinates(mris, fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read left/right spherical coordinates from %s\n", Progname, fname) ; sprintf(fname, "%s/%s/surf/%s.%s", sdir, subject, ohemi, pial_name) ; if (MRISreadPialCoordinates(mris_contra, fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read pial coordinates from %s\n", Progname, fname) ; sprintf(fname, "%s/%s/label/%s.%s", sdir, subject, hemi, cortex_label) ; cortex = LabelRead(NULL, fname) ; if (cortex == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read cortical label from %s\n", Progname, fname) ; LabelRipRestOfSurface(cortex, mris) ; LabelFree(&cortex) ; sprintf(fname, "%s/%s/surf/%s.%s", sdir, subject, ohemi, sphere_name) ; if (MRISreadCanonicalCoordinates(mris_contra, fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read left/right spherical coordinates from %s\n", Progname, fname) ; sprintf(fname, "%s/%s/mri/%s", sdir, subject, vol_name) ; printf("reading %s\n", fname) ; mri = MRIread(fname) ; if (mri == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read volume from %s\n", Progname, fname) ; if (0) mri_features = MRIcomputeSurfaceDistanceProbabilities(mris, mri_ribbon, mri, mri_aseg) ; else { MRI *mri_ohemi_features, *mri_ohemi_mapped_to_hemi_features ; mri_features = MRIcomputeSurfaceDistanceIntensities(mris, mri_ribbon, mri_aparc, mri, mri_aseg, whalf) ; mri_ohemi_features = MRIcomputeSurfaceDistanceIntensities(mris_contra, mri_ribbon, mri_aparc, mri, mri_aseg, whalf) ; mri_ohemi_mapped_to_hemi_features = MRISmapToSurface(mris_contra, mris, mri_ohemi_features, NULL) ; // map contra feature to this surface // MRIwrite(mri_ohemi_mapped_to_hemi_features, "test.mgz") ; MRIsubtract(mri_features, mri_ohemi_mapped_to_hemi_features, mri_features) ; } if (navgs > 0) { MRI *mri_tmp ; mri_tmp = MRISsmoothMRI(mris, mri_features, navgs, NULL, NULL); MRIfree(&mri_features) ; mri_features = mri_tmp ; } printf("writing output to %s\n", out_fname) ; if (Gdiag_no >= 0) printf("feature(%d) = %f\n", Gdiag_no, MRIgetVoxVal(mri_features, Gdiag_no, 0, 0, 0)) ; MRIwrite(mri_features, out_fname) ; msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; fprintf(stderr, "feature extraction took %d minutes and %d seconds.\n", minutes, seconds) ; exit(0) ; return(0) ; }
int main(int argc, char *argv[]) { char **av, *in_fname; int ac, nargs; MRIS *mris; int msec, minutes, seconds, nv, nf, ne, eno ; struct timeb start ; double total_volume; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_volume.c,v 1.6 2011/03/02 00:04:34 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 < 2) usage_exit(1) ; // printf("command line parsing finished\n"); /*** Read in the input surfaces ***/ in_fname = argv[1] ; if (verbose) printf("reading %s...\n", in_fname) ; mris = MRISread(in_fname) ; if(mris == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read surface %s", Progname, in_fname) ; eno = MRIScomputeEulerNumber(mris, &nv, &nf, &ne) ; if (eno != 2) ErrorExit(ERROR_BADPARM, "%s: surface %s has an incorrect topology (eno=%d)", Progname, in_fname, eno) ; if(verbose) printf("surface file read in.\n"); total_volume = MRISvolumeInSurf(mris); msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; if (verbose) printf("Volume computation took %d minutes and %d seconds.\n", minutes, seconds) ; if (verbose) printf("total volume surrounded by the surface is %g\n", total_volume); else printf("%lf\n", total_volume); MRISfree(&mris); exit(0); }
/*---------------------------------------------------------------*/ int main(int argc, char *argv[]) { int nargs,n; MRIS *SurfReg[100]; MRI *SrcVal, *TrgVal; char *base; nargs = handle_version_option (argc, argv, vcid, "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; cmdline = argv2cmdline(argc,argv); uname(&uts); getcwd(cwd,2000); Progname = argv[0] ; argc --; argv++; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; if (argc == 0) usage_exit(); parse_commandline(argc, argv); check_options(); if (checkoptsonly) return(0); dump_options(stdout); printf("Loading %s\n",SrcValFile); SrcVal = MRIread(SrcValFile); if(SrcVal==NULL) exit(1); if(DoSynthRand) { if (SynthSeed < 0) SynthSeed = PDFtodSeed(); printf("INFO: synthesizing, seed = %d\n",SynthSeed); srand48(SynthSeed); MRIrandn(SrcVal->width, SrcVal->height, SrcVal->depth, SrcVal->nframes,0, 1, SrcVal); } if(DoSynthOnes != 0) { printf("INFO: filling input with all 1s\n"); MRIconst(SrcVal->width, SrcVal->height, SrcVal->depth, SrcVal->nframes, 1, SrcVal); } for(n=0; n<nsurfs; n++) { printf("%d Loading %s\n",n+1,SurfRegFile[n]); base = fio_basename(SurfRegFile[n],".tri"); if(strcmp(base,"ic7")==0) { // Have to do it this way to rescale. Need to find a better more robust way. printf(" reading as ico 7, rescaling radius to 100\n"); SurfReg[n] = ReadIcoByOrder(7, 100); } else SurfReg[n] = MRISread(SurfRegFile[n]); free(base); if(SurfReg[n]==NULL) exit(1); } TrgVal = MRISapplyReg(SrcVal, SurfReg, nsurfs,ReverseMapFlag,DoJac,UseHash); if(TrgVal == NULL) exit(1); printf("Writing %s\n",TrgValFile); MRIwrite(TrgVal,TrgValFile); printf("mris_apply_reg done\n"); return 0; }
int main(int argc, char *argv[]) { char *inner_mris_fname,*outer_mris_fname,*input_mri_pref,*output_mri_pref; MRI *mri=0,*mri_src=0; MRI_SURFACE *inner_mris=0,*outer_mris=0; int nargs; LABEL *area = NULL ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mri_ribbon.c,v 1.15 2011/03/02 00:04:24 nicks Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Progname=argv[0]; if (argc > 1) while (*argv[1] == '-') { int nargs = 0 ; switch (toupper(argv[1][1])) { case 'L': printf("cropping ribbon to label file %s\n", argv[2]) ; nargs = 1 ; area = LabelRead(NULL, argv[2]) ; if (area == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read label file %s\n", argv[2]) ; break ; default: break ; } argc -= (nargs+1) ; if (argc == 1) break; argv += (nargs+1) ; } /* Set command-line parameters */ if (argc!=5) { printf("Usage: mri_ribbon [-l fname.label] \\ \n" " inner_surface_fname \\ \n" " outer_surface_fname \\ \n" " input_volume_pref \\ \n" " output_volume_pref\n"); exit(1); } inner_mris_fname=argv[1]; outer_mris_fname=argv[2]; input_mri_pref=argv[3]; output_mri_pref=argv[4]; /* Read surface information from inner surface file */ printf("Reading surface file %s.\n",inner_mris_fname); inner_mris=MRISread(inner_mris_fname); if (!inner_mris) { printf("Could not read surface file %s.\n",inner_mris_fname); exit(1); } /* Read surface information from outer surface file */ printf("Reading surface file %s.\n",outer_mris_fname); outer_mris=MRISread(outer_mris_fname); if (!outer_mris) { printf("Could not read surface file %s.\n",outer_mris_fname); exit(1); } /* Read example volume from file */ printf("Reading MRI volume directory %s.\n",input_mri_pref); mri_src=MRIread(input_mri_pref); if (!mri_src) { printf("Could not read MRI volume directory %s.\n",input_mri_pref); exit(1); } /* Extract ribbon */ printf("Extracting ribbon.\n"); mri=MRISribbon(inner_mris,outer_mris,mri_src,NULL); if (area) MRIcropVolumeToLabel(mri, mri, area, inner_mris, outer_mris) ; /* Save MRI volume to directory */ printf("Writing volume file %s.\n",output_mri_pref); MRIwrite(mri,output_mri_pref); MRIfree(&mri); MRIfree(&mri_src); MRISfree(&inner_mris); MRISfree(&outer_mris); return 0; }
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[]) { 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.3 2011/03/02 00:04:11 nicks Exp $", "$Name: stable5 $"); 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.inflated",subjects_dir,subject_name,hemi); 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) ; if (nerode) LabelErode(label, mris, nerode) ; if (nclose) { LabelDilate(label, mris, nclose) ; LabelErode(label, mris, nclose) ; } LabelRipRestOfSurface(label, mris) ; MRISripFaces(mris) ; MRISwritePatch(mris, out_fname) ; if (verbose) fprintf(stderr, "done.\n") ; exit(0) ; return(0) ; /* 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, *surf_fname, *profile_fname, *seg_fname ; int ac, nargs ; MRI_SURFACE *mris, *mris_ico = NULL ; // LABEL *label = NULL ; MRI *mri_profiles ; CLUSTER *ct ; setRandomSeed(10L) ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_cluster_profiles.c,v 1.4 2011/03/02 00:04:34 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 < 4) usage_exit() ; profile_fname = argv[1] ; surf_fname = argv[2] ; seg_fname = argv[3] ; mris = MRISread(surf_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; MRISsetNeighborhoodSize(mris, 2) ; if (MRISreadAnnotation(mris, "ad_aparc") != NO_ERROR) { if (MRISreadAnnotation(mris, "aparc") != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read annotation file ad_aparc", Progname) ; } printf("reading intensity profile volume from %s...\n", profile_fname) ; mri_profiles = MRIread(profile_fname) ; if (!mri_profiles) ErrorExit(ERROR_NOFILE, "%s: could not read intensity volume %s", Progname, profile_fname) ; rip_vertices_out_of_fov(mris, mri_profiles) ; rip_bad_vertices(mris, mri_profiles) ; MRISclearAnnotations(mris) ; #if 0 if (nlabels > 0) { int l ; char label_name[STRLEN] ; LABEL *ltotal = NULL ; for (l = 0 ; l < nlabels ; l++) { sprintf(label_name, "%s/%s/label/%s.%s.label", sdir, sname, hemi,label_names[l]) ; label = LabelRead(NULL, label_name) ; if (!label) ErrorExit(ERROR_NOFILE, "%s: could not read label file %s...\n", Progname, label_name) ; if (num_erode > 0) { printf("eroding label %d times, npoints went from %d ", num_erode,label->n_points) ; LabelErode(label, mris, num_erode) ; printf("to %d ", label->n_points) ; } ltotal = LabelCombine(label, ltotal) ; } if (nlabels == 0) ltotal = LabelInFOV(mris, mri, MIN_BORDER_DIST) ; LabelRipRestOfSurfaceWithThreshold(ltotal, mris, thresh) ; } #endif if (navgs > 0) { printf("smoothing profiles %d times\n", navgs) ; MRISsmoothFrames(mris, mri_profiles, navgs) ; } if (ico_fname) { mris_ico = MRISread(ico_fname) ; if (mris_ico == NULL) ErrorExit(ERROR_BADPARM, "%s: could not read icosahedron from %s...\n", Progname, ico_fname) ; } ct = MRIScluster(mris, mri_profiles, cluster_type, k, start_fname, mris_ico) ; printf("writing cortical intensity clusters to %s...\n", seg_fname) ; MRISwriteAnnotation(mris, seg_fname) ; { int vno ; VERTEX *v ; int c, i ; char fname[STRLEN], ext[STRLEN] ; // write average profiles into mri_profiles and write it out for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->ripflag) continue ; c = v->curv ; if (c < 0) continue ; for (i = 0 ; i < mri_profiles->nframes ; i++) MRIsetVoxVal(mri_profiles, vno, 0, 0, i, VECTOR_ELT(ct[c].v_mean,i+1)) ; } FileNameExtension(seg_fname, ext) ; FileNameRemoveExtension(seg_fname, fname) ; strcat(fname, "_cluster_avg.mgz") ; printf("writing average cluster profiles to %s...\n", fname) ; MRIwrite(mri_profiles, fname) ; } MRIfree(&mri_profiles) ; exit(0) ; return(0) ; /* for ansi */ }
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 **av, *cp, fname[STRLEN], *subject, *out_fname ; int ac, nargs, msec, minutes, n, seconds, nsubjects, i, sno, nfeatures, correct ; struct timeb start ; LABEL *cortex_label, *training_label ; RANDOM_FOREST *rf ; double **training_data ; int *training_classes, ntraining ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_rf_train.c,v 1.1 2012/06/07 12:09:47 fischl Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; setRandomSeed(0L); 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) ; if (strlen(sdir) == 0) { cp = getenv("SUBJECTS_DIR") ; if (!cp) ErrorExit(ERROR_BADPARM, "%s: SUBJECTS_DIR not defined in env or cmd line",Progname) ; strcpy(sdir, cp) ; } nsubjects = argc-(ARGC_OFFSET+1) ; printf("training random forest classifier using %d subjects\n", nsubjects) ; for (n = ARGC_OFFSET ; n < argc-1 ; n++) { subject = argv[n] ; sno = n-ARGC_OFFSET ; printf("processing subject %s: %d of %d\n", subject, sno+1, nsubjects) ; sprintf(fname, "%s/%s/label/lh.%s.label", sdir, subject, label_name) ; if (FileExists(fname) == 0) { sprintf(fname, "%s/%s/label/rh.%s.label", sdir, subject, label_name) ; if (FileExists(fname) == 0) ErrorExit(ERROR_NOFILE, "%s: subject %s has no training label for either hemisphere", Progname, subject) ; hemi = "rh" ; } else hemi = "lh" ; training_label = LabelRead(NULL, fname) ; if (training_label == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read training label %s\n", Progname, fname) ; sprintf(fname, "%s/%s/surf/%s.%s", sdir, subject, hemi, surf_name) ; mris[sno] = MRISread(fname) ; if (!mris[sno]) ErrorExit(ERROR_NOFILE, "%s: could not read surface from %s",Progname, fname) ; MRIScomputeMetricProperties(mris[sno]) ; if (nbhd_size > mris[sno]->nsize) MRISsetNeighborhoodSize(mris[sno], nbhd_size) ; sprintf(fname, "%s/%s/label/%s.%s", sdir, subject, hemi, cortex_label_name) ; cortex_label = LabelRead(NULL, fname) ; if (cortex_label == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read cortex label %s\n", Progname, fname) ; LabelRipRestOfSurface(cortex_label, mris[sno]) ; MRISclearMarks(mris[sno]) ; LabelFillUnassignedVertices(mris[sno], training_label, CURRENT_VERTICES); LabelDilate(training_label, mris[sno], ndilates) ; LabelMark(training_label, mris[sno]) ; LabelFree(&training_label) ; for (i = 0 ; i < noverlays ; i++) { sprintf(fname, "%s/%s/surf/%s.%s", sdir, subject, hemi, overlay_names[i]) ; mri_overlays[sno][i] = MRIread(fname) ; if (mri_overlays[sno][i] == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read overlay %s (%d)\n", Progname, fname, i) ; } LabelFree(&cortex_label) ; } nfeatures = noverlays*(nbhd_size+1) ; rf = RFalloc(ntrees, nfeatures, 2, max_depth, class_names, 100) ; rf->feature_names = (char **)calloc(nfeatures, sizeof(char *)) ; for (i = 0 ; i < noverlays ; i++) { rf->feature_names[i] = (char *)calloc(strlen(overlay_names[i])+1, sizeof(char)) ; strcpy(rf->feature_names[i], overlay_names[i]) ; } assemble_training_data_and_free_mris(mris, mri_overlays, nsubjects, noverlays, &training_classes, &training_data, &ntraining) ; RFtrain(rf, 1.0, training_fraction, training_classes, training_data, ntraining); correct = RFcomputeOutOfBagCorrect(rf, training_classes, training_data, ntraining); printf("out of bag accuracy = %2.1f (%d of %d)\n", (float)correct*100.0f/ntraining, correct, ntraining) ; RFevaluateFeatures(rf, stdout) ; out_fname = argv[argc-1] ; printf("writing output to %s\n", out_fname) ; RFwrite(rf, out_fname) ; msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; printf("random forest training took %d minutes and %d seconds.\n", minutes, seconds) ; exit(0) ; return(0) ; }
/*--------------------------------------------------*/ int main(int argc, char **argv) { int nargs, err, asegid, c, r, s, nctx, annot,vtxno,nripped; int annotid, IsCortex=0, IsWM=0, IsHypo=0, hemi=0, segval=0; int RibbonVal=0,nbrute=0; float dmin=0.0, lhRibbonVal=0, rhRibbonVal=0; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, vcid, "$Name: stable5 $"); if (nargs && argc - nargs == 1) { exit (0); } argc -= nargs; Progname = argv[0] ; argc --; argv++; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; if (argc == 0) { usage_exit(); } SUBJECTS_DIR = getenv("SUBJECTS_DIR"); if (SUBJECTS_DIR==NULL) { printf("ERROR: SUBJECTS_DIR not defined in environment\n"); exit(1); } parse_commandline(argc, argv); check_options(); dump_options(stdout); /* ------ Load subject's lh white surface ------ */ sprintf(tmpstr,"%s/%s/surf/lh.white",SUBJECTS_DIR,subject); printf("\nReading lh white surface \n %s\n",tmpstr); lhwhite = MRISread(tmpstr); if (lhwhite == NULL) { fprintf(stderr,"ERROR: could not read %s\n",tmpstr); exit(1); } /* ------ Load subject's lh pial surface ------ */ sprintf(tmpstr,"%s/%s/surf/lh.pial",SUBJECTS_DIR,subject); printf("\nReading lh pial surface \n %s\n",tmpstr); lhpial = MRISread(tmpstr); if (lhpial == NULL) { fprintf(stderr,"ERROR: could not read %s\n",tmpstr); exit(1); } if (lhwhite->nvertices != lhpial->nvertices) { printf("ERROR: lh white and pial have a different number of " "vertices (%d,%d)\n", lhwhite->nvertices,lhpial->nvertices); exit(1); } /* ------ Load lh annotation ------ */ sprintf(annotfile,"%s/%s/label/lh.%s.annot",SUBJECTS_DIR,subject,annotname); printf("\nLoading lh annotations from %s\n",annotfile); err = MRISreadAnnotation(lhwhite, annotfile); if (err) { printf("ERROR: MRISreadAnnotation() failed %s\n",annotfile); exit(1); } /* ------ Load subject's rh white surface ------ */ sprintf(tmpstr,"%s/%s/surf/rh.white",SUBJECTS_DIR,subject); printf("\nReading rh white surface \n %s\n",tmpstr); rhwhite = MRISread(tmpstr); if (rhwhite == NULL) { fprintf(stderr,"ERROR: could not read %s\n",tmpstr); exit(1); } /* ------ Load subject's rh pial surface ------ */ sprintf(tmpstr,"%s/%s/surf/rh.pial",SUBJECTS_DIR,subject); printf("\nReading rh pial surface \n %s\n",tmpstr); rhpial = MRISread(tmpstr); if (rhpial == NULL) { fprintf(stderr,"ERROR: could not read %s\n",tmpstr); exit(1); } if (rhwhite->nvertices != rhpial->nvertices) { printf("ERROR: rh white and pial have a different " "number of vertices (%d,%d)\n", rhwhite->nvertices,rhpial->nvertices); exit(1); } /* ------ Load rh annotation ------ */ sprintf(annotfile,"%s/%s/label/rh.%s.annot",SUBJECTS_DIR,subject,annotname); printf("\nLoading rh annotations from %s\n",annotfile); err = MRISreadAnnotation(rhwhite, annotfile); if (err) { printf("ERROR: MRISreadAnnotation() failed %s\n",annotfile); exit(1); } if (lhwhite->ct) { printf("Have color table for lh white annotation\n"); } if (rhwhite->ct) { printf("Have color table for rh white annotation\n"); } //print_annotation_table(stdout); if (UseRibbon) { sprintf(tmpstr,"%s/%s/mri/lh.ribbon.mgz",SUBJECTS_DIR,subject); printf("Loading lh ribbon mask from %s\n",tmpstr); lhRibbon = MRIread(tmpstr); if (lhRibbon == NULL) { printf("ERROR: loading %s\n",tmpstr); exit(1); } sprintf(tmpstr,"%s/%s/mri/rh.ribbon.mgz",SUBJECTS_DIR,subject); printf("Loading rh ribbon mask from %s\n",tmpstr); rhRibbon = MRIread(tmpstr); if (rhRibbon == NULL) { printf("ERROR: loading %s\n",tmpstr); exit(1); } } if (UseNewRibbon) { sprintf(tmpstr,"%s/%s/mri/ribbon.mgz",SUBJECTS_DIR,subject); printf("Loading ribbon segmentation from %s\n",tmpstr); RibbonSeg = MRIread(tmpstr); if (RibbonSeg == NULL) { printf("ERROR: loading %s\n",tmpstr); exit(1); } } if (LabelHypoAsWM) { sprintf(tmpstr,"%s/%s/mri/filled.mgz",SUBJECTS_DIR,subject); printf("Loading filled from %s\n",tmpstr); filled = MRIread(tmpstr); if (filled == NULL) { printf("ERROR: loading filled %s\n",tmpstr); exit(1); } } // ------------ Rip ----------------------- if (RipUnknown) { printf("Ripping vertices labeled as unkown\n"); nripped = 0; for (vtxno = 0; vtxno < lhwhite->nvertices; vtxno++) { annot = lhwhite->vertices[vtxno].annotation; CTABfindAnnotation(lhwhite->ct, annot, &annotid); // Sometimes the annotation will be "none" indicated by // annotid = -1. We interpret this as "unknown". if (annotid == 0 || annotid == -1) { lhwhite->vertices[vtxno].ripflag = 1; lhpial->vertices[vtxno].ripflag = 1; nripped++; } } printf("Ripped %d vertices from left hemi\n",nripped); nripped = 0; for (vtxno = 0; vtxno < rhwhite->nvertices; vtxno++) { annot = rhwhite->vertices[vtxno].annotation; CTABfindAnnotation(rhwhite->ct, annot, &annotid); if (annotid == 0 || annotid == -1) { rhwhite->vertices[vtxno].ripflag = 1; rhpial->vertices[vtxno].ripflag = 1; nripped++; } } printf("Ripped %d vertices from right hemi\n",nripped); } printf("\n"); printf("Building hash of lh white\n"); lhwhite_hash = MHTfillVertexTableRes(lhwhite, NULL,CURRENT_VERTICES,hashres); printf("\n"); printf("Building hash of lh pial\n"); lhpial_hash = MHTfillVertexTableRes(lhpial, NULL,CURRENT_VERTICES,hashres); printf("\n"); printf("Building hash of rh white\n"); rhwhite_hash = MHTfillVertexTableRes(rhwhite, NULL,CURRENT_VERTICES,hashres); printf("\n"); printf("Building hash of rh pial\n"); rhpial_hash = MHTfillVertexTableRes(rhpial, NULL,CURRENT_VERTICES,hashres); /* ------ Load ASeg ------ */ sprintf(tmpstr,"%s/%s/mri/aseg.mgz",SUBJECTS_DIR,subject); if (!fio_FileExistsReadable(tmpstr)) { sprintf(tmpstr,"%s/%s/mri/aseg.mgh",SUBJECTS_DIR,subject); if (!fio_FileExistsReadable(tmpstr)) { sprintf(tmpstr,"%s/%s/mri/aseg/COR-.info",SUBJECTS_DIR,subject); if (!fio_FileExistsReadable(tmpstr)) { printf("ERROR: cannot find aseg\n"); exit(1); } else { sprintf(tmpstr,"%s/%s/mri/aseg/",SUBJECTS_DIR,subject); } } } printf("\nLoading aseg from %s\n",tmpstr); ASeg = MRIread(tmpstr); if (ASeg == NULL) { printf("ERROR: loading aseg %s\n",tmpstr); exit(1); } mritmp = MRIchangeType(ASeg,MRI_INT,0,0,1); MRIfree(&ASeg); ASeg = mritmp; if (CtxSegFile) { printf("Loading Ctx Seg File %s\n",CtxSegFile); CtxSeg = MRIread(CtxSegFile); if (CtxSeg == NULL) { exit(1); } } AParc = MRIclone(ASeg,NULL); if (OutDistFile != NULL) { Dist = MRIclone(ASeg,NULL); mritmp = MRIchangeType(Dist,MRI_FLOAT,0,0,0); if (mritmp == NULL) { printf("ERROR: could change type\n"); exit(1); } MRIfree(&Dist); Dist = mritmp; } Vox2RAS = MRIxfmCRS2XYZtkreg(ASeg); printf("ASeg Vox2RAS: -----------\n"); MatrixPrint(stdout,Vox2RAS); printf("-------------------------\n"); CRS = MatrixAlloc(4,1,MATRIX_REAL); CRS->rptr[4][1] = 1; RAS = MatrixAlloc(4,1,MATRIX_REAL); RAS->rptr[4][1] = 1; if (crsTest) { printf("Testing point %d %d %d\n",ctest,rtest,stest); err = FindClosestLRWPVertexNo(ctest,rtest,stest, &lhwvtx, &lhpvtx, &rhwvtx, &rhpvtx, Vox2RAS, lhwhite, lhpial, rhwhite, rhpial, lhwhite_hash, lhpial_hash, rhwhite_hash, rhpial_hash); printf("Result: err = %d\n",err); exit(err); } printf("\nLabeling Slice\n"); nctx = 0; annot = 0; annotid = 0; nbrute = 0; // Go through each voxel in the aseg for (c=0; c < ASeg->width; c++) { printf("%3d ",c); if (c%20 ==19) { printf("\n"); } fflush(stdout); for (r=0; r < ASeg->height; r++) { for (s=0; s < ASeg->depth; s++) { asegid = MRIgetVoxVal(ASeg,c,r,s,0); if (asegid == 3 || asegid == 42) { IsCortex = 1; } else { IsCortex = 0; } if (asegid >= 77 && asegid <= 82) { IsHypo = 1; } else { IsHypo = 0; } if (asegid == 2 || asegid == 41) { IsWM = 1; } else { IsWM = 0; } if (IsHypo && LabelHypoAsWM && MRIgetVoxVal(filled,c,r,s,0)) { IsWM = 1; } // integrate surface information // // Only Do This for GM,WM or Unknown labels in the ASEG !!! // // priority is given to the ribbon computed from the surface // namely // ribbon=GM => GM // aseg=GM AND ribbon=WM => WM // ribbon=UNKNOWN => UNKNOWN if (UseNewRibbon && ( IsCortex || IsWM || asegid==0 ) ) { RibbonVal = MRIgetVoxVal(RibbonSeg,c,r,s,0); MRIsetVoxVal(ASeg,c,r,s,0, RibbonVal); if (RibbonVal==2 || RibbonVal==41) { IsWM = 1; IsCortex = 0; } else if (RibbonVal==3 || RibbonVal==42) { IsWM = 0; IsCortex = 1; } if (RibbonVal==0) { IsWM = 0; IsCortex = 0; } } // If it's not labeled as cortex or wm in the aseg, skip if (!IsCortex && !IsWM) { continue; } // If it's wm but not labeling wm, skip if (IsWM && !LabelWM) { continue; } // Check whether this point is in the ribbon if (UseRibbon) { lhRibbonVal = MRIgetVoxVal(lhRibbon,c,r,s,0); rhRibbonVal = MRIgetVoxVal(rhRibbon,c,r,s,0); if (IsCortex) { // ASeg says it's in cortex if (lhRibbonVal < 0.5 && rhRibbonVal < 0.5) { // but it is not part of the ribbon, // so set it to unknown (0) and go to the next voxel. MRIsetVoxVal(ASeg,c,r,s,0,0); continue; } } } // Convert the CRS to RAS CRS->rptr[1][1] = c; CRS->rptr[2][1] = r; CRS->rptr[3][1] = s; RAS = MatrixMultiply(Vox2RAS,CRS,RAS); vtx.x = RAS->rptr[1][1]; vtx.y = RAS->rptr[2][1]; vtx.z = RAS->rptr[3][1]; // Get the index of the closest vertex in the // lh.white, lh.pial, rh.white, rh.pial if (UseHash) { lhwvtx = MHTfindClosestVertexNo(lhwhite_hash,lhwhite,&vtx,&dlhw); lhpvtx = MHTfindClosestVertexNo(lhpial_hash, lhpial, &vtx,&dlhp); rhwvtx = MHTfindClosestVertexNo(rhwhite_hash,rhwhite,&vtx,&drhw); rhpvtx = MHTfindClosestVertexNo(rhpial_hash, rhpial, &vtx,&drhp); if (lhwvtx < 0 && lhpvtx < 0 && rhwvtx < 0 && rhpvtx < 0) { /* printf(" Could not map to any surface with hash table:\n"); printf(" crs = %d %d %d, ras = %6.4f %6.4f %6.4f \n", c,r,s,vtx.x,vtx.y,vtx.z); printf(" Using brute force search %d ... \n",nbrute); fflush(stdout); */ lhwvtx = MRISfindClosestVertex(lhwhite,vtx.x,vtx.y,vtx.z,&dlhw); lhpvtx = MRISfindClosestVertex(lhpial,vtx.x,vtx.y,vtx.z,&dlhp); rhwvtx = MRISfindClosestVertex(rhwhite,vtx.x,vtx.y,vtx.z,&drhw); rhpvtx = MRISfindClosestVertex(rhpial,vtx.x,vtx.y,vtx.z,&drhp); nbrute ++; //exit(1); } } else { lhwvtx = MRISfindClosestVertex(lhwhite,vtx.x,vtx.y,vtx.z,&dlhw); lhpvtx = MRISfindClosestVertex(lhpial,vtx.x,vtx.y,vtx.z,&dlhp); rhwvtx = MRISfindClosestVertex(rhwhite,vtx.x,vtx.y,vtx.z,&drhw); rhpvtx = MRISfindClosestVertex(rhpial,vtx.x,vtx.y,vtx.z,&drhp); } if (lhwvtx < 0) { dlhw = 1000000000000000.0; } if (lhpvtx < 0) { dlhp = 1000000000000000.0; } if (rhwvtx < 0) { drhw = 1000000000000000.0; } if (rhpvtx < 0) { drhp = 1000000000000000.0; } if (dlhw <= dlhp && dlhw < drhw && dlhw < drhp && lhwvtx >= 0) { annot = lhwhite->vertices[lhwvtx].annotation; hemi = 1; if (lhwhite->ct) { CTABfindAnnotation(lhwhite->ct, annot, &annotid); } else { annotid = annotation_to_index(annot); } dmin = dlhw; } if (dlhp < dlhw && dlhp < drhw && dlhp < drhp && lhpvtx >= 0) { annot = lhwhite->vertices[lhpvtx].annotation; hemi = 1; if (lhwhite->ct) { CTABfindAnnotation(lhwhite->ct, annot, &annotid); } else { annotid = annotation_to_index(annot); } dmin = dlhp; } if (drhw < dlhp && drhw < dlhw && drhw <= drhp && rhwvtx >= 0) { annot = rhwhite->vertices[rhwvtx].annotation; hemi = 2; if (rhwhite->ct) { CTABfindAnnotation(rhwhite->ct, annot, &annotid); } else { annotid = annotation_to_index(annot); } dmin = drhw; } if (drhp < dlhp && drhp < drhw && drhp < dlhw && rhpvtx >= 0) { annot = rhwhite->vertices[rhpvtx].annotation; hemi = 2; if (rhwhite->ct) { CTABfindAnnotation(rhwhite->ct, annot, &annotid); } else { annotid = annotation_to_index(annot); } dmin = drhp; } // Sometimes the annotation will be "none" indicated by // annotid = -1. We interpret this as "unknown". if (annotid == -1) { annotid = 0; } // why was this here in the first place? /* if (annotid == 0 && lhwvtx >= 0 && lhpvtx >= 0 && rhwvtx >= 0 && rhpvtx >= 0) { printf("%d %d %d %d\n", lhwhite->vertices[lhwvtx].ripflag, lhpial->vertices[lhpvtx].ripflag, rhwhite->vertices[rhwvtx].ripflag, rhpial->vertices[rhpvtx].ripflag); } */ if ( IsCortex && hemi == 1) { segval = annotid+1000 + baseoffset; //ctx-lh } if ( IsCortex && hemi == 2) { segval = annotid+2000 + baseoffset; //ctx-rh } if (!IsCortex && hemi == 1) { segval = annotid+3000 + baseoffset; // wm-lh } if (!IsCortex && hemi == 2) { segval = annotid+4000 + baseoffset; // wm-rh } if (!IsCortex && dmin > dmaxctx && hemi == 1) { segval = 5001; } if (!IsCortex && dmin > dmaxctx && hemi == 2) { segval = 5002; } // This is a hack for getting the right cortical seg with --rip-unknown // The aparc+aseg should be passed as CtxSeg. if (IsCortex && CtxSeg) { segval = MRIgetVoxVal(CtxSeg,c,r,s,0); } MRIsetVoxVal(ASeg,c,r,s,0,segval); MRIsetVoxVal(AParc,c,r,s,0,annot); if (OutDistFile != NULL) { MRIsetVoxVal(Dist,c,r,s,0,dmin); } if (debug || annotid == -1) { // Gets here when there is no label at the found vertex. // This is different than having a vertex labeled as "unknown" if (!debug) { continue; } printf("\n"); printf("Found closest vertex, but it has no label.\n"); printf("aseg id = %d\n",asegid); printf("crs = %d %d %d, ras = %6.4f %6.4f %6.4f \n", c,r,s,vtx.x,vtx.y,vtx.z); if (lhwvtx > 0) printf("lhw %d %7.5f %6.4f %6.4f %6.4f\n", lhwvtx, dlhw, lhwhite->vertices[lhwvtx].x, lhwhite->vertices[lhwvtx].y, lhwhite->vertices[lhwvtx].z); if (lhpvtx > 0) printf("lhp %d %7.5f %6.4f %6.4f %6.4f\n", lhpvtx, dlhp, lhpial->vertices[lhpvtx].x, lhpial->vertices[lhpvtx].y, lhpial->vertices[lhpvtx].z); if (rhwvtx > 0) printf("rhw %d %7.5f %6.4f %6.4f %6.4f\n", rhwvtx, drhw, rhwhite->vertices[rhwvtx].x, rhwhite->vertices[rhwvtx].y, rhwhite->vertices[rhwvtx].z); if (rhpvtx > 0) printf("rhp %d %7.5f %6.4f %6.4f %6.4f\n", rhpvtx, drhp, rhpial->vertices[rhpvtx].x, rhpial->vertices[rhpvtx].y, rhpial->vertices[rhpvtx].z); printf("annot = %d, annotid = %d\n",annot,annotid); CTABprintASCII(lhwhite->ct,stdout); continue; } nctx++; } } } printf("nctx = %d\n",nctx); printf("Used brute-force search on %d voxels\n",nbrute); if (FixParaHipWM) { /* This is a bit of a hack. There are some vertices that have been ripped because they are "unkown". When the above alorithm finds these, it searches for the closest known vertex. If this is less than dmax away, then the wm voxel gets labeled accordingly. However, there are often some voxels near ventralDC that are just close enough in 3d space to parahip to get labeled even though they are very far away along the surface. These voxels end up forming an island. CCSegment() will eliminate any islands. Unforunately, CCSegment() uses 6-neighbor (face) definition of connectedness, so some voxels may be eliminated. */ printf("Fixing Parahip LH WM\n"); CCSegment(ASeg, 3016, 5001); //3016 = lhphwm, 5001 = unsegmented WM left printf("Fixing Parahip RH WM\n"); CCSegment(ASeg, 4016, 5002); //4016 = rhphwm, 5002 = unsegmented WM right } printf("Writing output aseg to %s\n",OutASegFile); MRIwrite(ASeg,OutASegFile); if (OutAParcFile != NULL) { printf("Writing output aparc to %s\n",OutAParcFile); MRIwrite(AParc,OutAParcFile); } if (OutDistFile != NULL) { printf("Writing output dist file to %s\n",OutDistFile); MRIwrite(Dist,OutDistFile); } return(0); }
int main(int argc, char *argv[]) { char **av ; int ac, nargs, n ; MRI *mri_src, *mri_dst = NULL, *mri_bias, *mri_orig, *mri_aseg = NULL ; char *in_fname, *out_fname ; int msec, minutes, seconds ; struct timeb start ; char cmdline[CMD_LINE_LEN] ; make_cmd_version_string (argc, argv, "$Id: mri_normalize.c,v 1.80 2012/10/16 21:38:35 nicks Exp $", "$Name: $", cmdline); /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mri_normalize.c,v 1.80 2012/10/16 21:38:35 nicks Exp $", "$Name: $"); if (nargs && argc - nargs == 1) { exit (0); } argc -= nargs; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; mni.max_gradient = MAX_GRADIENT ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc < 3) { usage_exit(0) ; } if (argc < 1) { ErrorExit(ERROR_BADPARM, "%s: no input name specified", Progname) ; } in_fname = argv[1] ; if (argc < 2) { ErrorExit(ERROR_BADPARM, "%s: no output name specified", Progname) ; } out_fname = argv[2] ; if(verbose) { printf( "reading from %s...\n", in_fname) ; } mri_src = MRIread(in_fname) ; if (!mri_src) ErrorExit(ERROR_NO_FILE, "%s: could not open source file %s", Progname, in_fname) ; MRIaddCommandLine(mri_src, cmdline) ; if(nsurfs > 0) { MRI_SURFACE *mris ; MRI *mri_dist=NULL, *mri_dist_sup=NULL, *mri_ctrl, *mri_dist_one ; LTA *lta= NULL ; int i ; TRANSFORM *surface_xform ; if (control_point_fname) // do one pass with only file control points first { MRI3dUseFileControlPoints(mri_src, control_point_fname) ; mri_dst = MRI3dGentleNormalize(mri_src, NULL, DEFAULT_DESIRED_WHITE_MATTER_VALUE, NULL, intensity_above, intensity_below/2,1, bias_sigma, mri_not_control); } else { mri_dst = MRIcopy(mri_src, NULL) ; } for (i = 0 ; i < nsurfs ; i++) { mris = MRISread(surface_fnames[i]) ; if (mris == NULL) ErrorExit(ERROR_NOFILE,"%s: could not surface %s", Progname,surface_fnames[i]); surface_xform = surface_xforms[i] ; TransformInvert(surface_xform, NULL) ; if (surface_xform->type == MNI_TRANSFORM_TYPE || surface_xform->type == TRANSFORM_ARRAY_TYPE || surface_xform->type == REGISTER_DAT) { lta = (LTA *)(surface_xform->xform) ; #if 0 if (invert) { VOL_GEOM vgtmp; LT *lt; MATRIX *m_tmp = lta->xforms[0].m_L ; lta->xforms[0].m_L = MatrixInverse(lta->xforms[0].m_L, NULL) ; MatrixFree(&m_tmp) ; lt = <a->xforms[0]; if (lt->dst.valid == 0 || lt->src.valid == 0) { printf( "WARNING:***************************************************************\n"); printf( "WARNING:dst volume infor is invalid. Most likely produce wrong inverse.\n"); printf( "WARNING:***************************************************************\n"); } copyVolGeom(<->dst, &vgtmp); copyVolGeom(<->src, <->dst); copyVolGeom(&vgtmp, <->src); } #endif } if (stricmp(surface_xform_fnames[i], "identity.nofile") != 0) { MRIStransform(mris, NULL, surface_xform, NULL) ; } mri_dist_one = MRIcloneDifferentType(mri_dst, MRI_FLOAT) ; printf("computing distance transform\n") ; MRIScomputeDistanceToSurface(mris, mri_dist_one, mri_dist_one->xsize) ; if (i == 0) { mri_dist = MRIcopy(mri_dist_one, NULL) ; } else { MRIcombineDistanceTransforms(mri_dist_one, mri_dist, mri_dist) ; } // MRIminAbs(mri_dist_one, mri_dist, mri_dist) ; MRIfree(&mri_dist_one) ; } MRIscalarMul(mri_dist, mri_dist, -1) ; if (nonmax_suppress) { printf("computing nonmaximum suppression\n") ; mri_dist_sup = MRInonMaxSuppress(mri_dist, NULL, 0, 1) ; mri_ctrl = MRIcloneDifferentType(mri_dist_sup, MRI_UCHAR) ; MRIbinarize(mri_dist_sup, mri_ctrl, min_dist, CONTROL_NONE, CONTROL_MARKED) ; } else if (erode) { int i ; mri_ctrl = MRIcloneDifferentType(mri_dist, MRI_UCHAR) ; MRIbinarize(mri_dist, mri_ctrl, min_dist, CONTROL_NONE, CONTROL_MARKED) ; for (i = 0 ; i < erode ; i++) { MRIerode(mri_ctrl, mri_ctrl) ; } } else { mri_ctrl = MRIcloneDifferentType(mri_dist, MRI_UCHAR) ; MRIbinarize(mri_dist, mri_ctrl, min_dist, CONTROL_NONE, CONTROL_MARKED) ; } if (control_point_fname) { MRInormAddFileControlPoints(mri_ctrl, CONTROL_MARKED) ; } if (mask_sigma > 0) { MRI *mri_smooth, *mri_mag, *mri_grad ; mri_smooth = MRIgaussianSmooth(mri_dst, mask_sigma, 1, NULL) ; mri_mag = MRIcloneDifferentType(mri_dst, MRI_FLOAT) ; mri_grad = MRIsobel(mri_smooth, NULL, mri_mag) ; MRIbinarize(mri_mag, mri_mag, mask_thresh, 1, 0) ; MRImask(mri_ctrl, mri_mag, mri_ctrl, 0, CONTROL_NONE) ; MRIfree(&mri_grad) ; MRIfree(&mri_mag) ; MRIfree(&mri_smooth) ; } if (mask_orig_fname) { MRI *mri_orig ; mri_orig = MRIread(mask_orig_fname) ; MRIbinarize(mri_orig, mri_orig, mask_orig_thresh, 0, 1) ; MRImask(mri_ctrl, mri_orig, mri_ctrl, 0, CONTROL_NONE) ; MRIfree(&mri_orig) ; } if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { MRIwrite(mri_dist, "d.mgz"); MRIwrite(mri_dist_sup, "dm.mgz"); MRIwrite(mri_ctrl, "c.mgz"); } MRIeraseBorderPlanes(mri_ctrl, 4) ; if (aseg_fname) { mri_aseg = MRIread(aseg_fname) ; if (mri_aseg == NULL) { ErrorExit(ERROR_NOFILE, "%s: could not load aseg from %s", Progname, aseg_fname) ; } remove_nonwm_voxels(mri_ctrl, mri_aseg, mri_ctrl) ; MRIfree(&mri_aseg) ; } else { remove_surface_outliers(mri_ctrl, mri_dist, mri_dst, mri_ctrl) ; } mri_bias = MRIbuildBiasImage(mri_dst, mri_ctrl, NULL, 0.0) ; if (mri_dist) { MRIfree(&mri_dist) ; } if (mri_dist_sup) { MRIfree(&mri_dist_sup) ; } if (bias_sigma> 0) { MRI *mri_kernel = MRIgaussian1d(bias_sigma, -1) ; if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { MRIwrite(mri_bias, "b.mgz") ; } printf("smoothing bias field\n") ; MRIconvolveGaussian(mri_bias, mri_bias, mri_kernel) ; if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { MRIwrite(mri_bias, "bs.mgz") ; } MRIfree(&mri_kernel); } MRIfree(&mri_ctrl) ; mri_dst = MRIapplyBiasCorrectionSameGeometry (mri_dst, mri_bias, mri_dst, DEFAULT_DESIRED_WHITE_MATTER_VALUE) ; printf("writing normalized volume to %s\n", out_fname) ; MRIwrite(mri_dst, out_fname) ; exit(0) ; } // end if(surface_fname) if (!mriConformed(mri_src) && conform > 0) { printf("unconformed source detected - conforming...\n") ; mri_src = MRIconform(mri_src) ; } if (mask_fname) { MRI *mri_mask ; mri_mask = MRIread(mask_fname) ; if (!mri_mask) ErrorExit(ERROR_NOFILE, "%s: could not open mask volume %s.\n", Progname, mask_fname) ; MRImask(mri_src, mri_mask, mri_src, 0, 0) ; MRIfree(&mri_mask) ; } if (read_flag) { MRI *mri_ctrl ; double scale ; mri_bias = MRIread(bias_volume_fname) ; if (!mri_bias) ErrorExit (ERROR_BADPARM, "%s: could not read bias volume %s", Progname, bias_volume_fname) ; mri_ctrl = MRIread(control_volume_fname) ; if (!mri_ctrl) ErrorExit (ERROR_BADPARM, "%s: could not read control volume %s", Progname, control_volume_fname) ; MRIbinarize(mri_ctrl, mri_ctrl, 1, 0, 128) ; mri_dst = MRImultiply(mri_bias, mri_src, NULL) ; scale = MRImeanInLabel(mri_dst, mri_ctrl, 128) ; printf("mean in wm is %2.0f, scaling by %2.2f\n", scale, 110/scale) ; scale = 110/scale ; MRIscalarMul(mri_dst, mri_dst, scale) ; MRIwrite(mri_dst, out_fname) ; exit(0) ; } if(long_flag) { MRI *mri_ctrl ; double scale ; mri_bias = MRIread(long_bias_volume_fname) ; if (!mri_bias) ErrorExit (ERROR_BADPARM, "%s: could not read bias volume %s", Progname, long_bias_volume_fname) ; mri_ctrl = MRIread(long_control_volume_fname) ; if (!mri_ctrl) ErrorExit (ERROR_BADPARM, "%s: could not read control volume %s", Progname, long_control_volume_fname) ; MRIbinarize(mri_ctrl, mri_ctrl, 1, 0, CONTROL_MARKED) ; if (mri_ctrl->type != MRI_UCHAR) { MRI *mri_tmp ; mri_tmp = MRIchangeType(mri_ctrl, MRI_UCHAR, 0, 1,1); MRIfree(&mri_ctrl) ; mri_ctrl = mri_tmp ; } scale = MRImeanInLabel(mri_src, mri_ctrl, CONTROL_MARKED) ; printf("mean in wm is %2.0f, scaling by %2.2f\n", scale, 110/scale) ; scale = DEFAULT_DESIRED_WHITE_MATTER_VALUE/scale ; mri_dst = MRIscalarMul(mri_src, NULL, scale) ; MRIremoveWMOutliers(mri_dst, mri_ctrl, mri_ctrl, intensity_below/2) ; mri_bias = MRIbuildBiasImage(mri_dst, mri_ctrl, NULL, 0.0) ; MRIsoapBubble(mri_bias, mri_ctrl, mri_bias, 50, 1) ; MRIapplyBiasCorrectionSameGeometry(mri_dst, mri_bias, mri_dst, DEFAULT_DESIRED_WHITE_MATTER_VALUE); // MRIwrite(mri_dst, out_fname) ; // exit(0) ; } // end if(long_flag) if (grad_thresh > 0) { float thresh ; MRI *mri_mag, *mri_grad, *mri_smooth ; MRI *mri_kernel = MRIgaussian1d(.5, -1) ; mri_not_control = MRIcloneDifferentType(mri_src, MRI_UCHAR) ; switch (scan_type) { case MRI_MGH_MPRAGE: thresh = 15 ; break ; case MRI_WASHU_MPRAGE: thresh = 20 ; break ; case MRI_UNKNOWN: default: thresh = 12 ; break ; } mri_smooth = MRIconvolveGaussian(mri_src, NULL, mri_kernel) ; thresh = grad_thresh ; mri_mag = MRIcloneDifferentType(mri_src, MRI_FLOAT) ; mri_grad = MRIsobel(mri_smooth, NULL, mri_mag) ; MRIwrite(mri_mag, "m.mgz") ; MRIbinarize(mri_mag, mri_not_control, thresh, 0, 1) ; MRIwrite(mri_not_control, "nc.mgz") ; MRIfree(&mri_mag) ; MRIfree(&mri_grad) ; MRIfree(&mri_smooth) ; MRIfree(&mri_kernel) ; } #if 0 #if 0 if ((mri_src->type != MRI_UCHAR) || (!(mri_src->xsize == 1 && mri_src->ysize == 1 && mri_src->zsize == 1))) #else if (conform || (mri_src->type != MRI_UCHAR && conform > 0)) #endif { MRI *mri_tmp ; fprintf (stderr, "downsampling to 8 bits and scaling to isotropic voxels...\n") ; mri_tmp = MRIconform(mri_src) ; mri_src = mri_tmp ; } #endif if(aseg_fname) { printf("Reading aseg %s\n",aseg_fname); mri_aseg = MRIread(aseg_fname) ; if (mri_aseg == NULL) ErrorExit (ERROR_NOFILE, "%s: could not read aseg from file %s", Progname, aseg_fname) ; if (!mriConformed(mri_aseg)) { ErrorExit(ERROR_UNSUPPORTED, "%s: aseg volume %s must be conformed", Progname, aseg_fname) ; } } else { mri_aseg = NULL ; } if(verbose) { printf( "normalizing image...\n") ; } fflush(stdout); fflush(stderr); TimerStart(&start) ; if (control_point_fname) { MRI3dUseFileControlPoints(mri_src, control_point_fname) ; } // this just setup writing control-point volume saving if(control_volume_fname) { MRI3dWriteControlPoints(control_volume_fname) ; } /* first do a gentle normalization to get things in the right intensity range */ if(long_flag == 0) // if long, then this will already have been done with base control points { if(control_point_fname != NULL) /* do one pass with only file control points first */ mri_dst = MRI3dGentleNormalize(mri_src, NULL, DEFAULT_DESIRED_WHITE_MATTER_VALUE, NULL, intensity_above, intensity_below/2,1, bias_sigma, mri_not_control); else { mri_dst = MRIcopy(mri_src, NULL) ; } } fflush(stdout); fflush(stderr); if(mri_aseg) { MRI *mri_ctrl, *mri_bias ; int i ; printf("processing with aseg\n"); mri_ctrl = MRIclone(mri_aseg, NULL) ; for (i = 0 ; i < NWM_LABELS ; i++) { MRIcopyLabel(mri_aseg, mri_ctrl, aseg_wm_labels[i]) ; } printf("removing outliers in the aseg WM...\n") ; MRIremoveWMOutliersAndRetainMedialSurface(mri_dst, mri_ctrl, mri_ctrl, intensity_below) ; MRIbinarize(mri_ctrl, mri_ctrl, 1, CONTROL_NONE, CONTROL_MARKED) ; MRInormAddFileControlPoints(mri_ctrl, CONTROL_MARKED) ; if (interior_fname1) { MRIS *mris_interior1, *mris_interior2 ; mris_interior1 = MRISread(interior_fname1) ; if (mris_interior1 == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read white matter surface from %s\n", Progname, interior_fname1) ; mris_interior2 = MRISread(interior_fname2) ; if (mris_interior2 == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read white matter surface from %s\n", Progname, interior_fname2) ; add_interior_points(mri_ctrl, mri_dst, intensity_above, 1.25*intensity_below, mris_interior1, mris_interior2, mri_aseg, mri_ctrl) ; MRISfree(&mris_interior1) ; MRISfree(&mris_interior2) ; } if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { MRIwrite(mri_ctrl, "norm_ctrl.mgz") ; } printf("Building bias image\n"); fflush(stdout); fflush(stderr); mri_bias = MRIbuildBiasImage(mri_dst, mri_ctrl, NULL, 0.0) ; fflush(stdout); fflush(stderr); if (bias_sigma> 0) { printf("Smoothing with sigma %g\n",bias_sigma); MRI *mri_kernel = MRIgaussian1d(bias_sigma, -1) ; MRIconvolveGaussian(mri_bias, mri_bias, mri_kernel) ; MRIfree(&mri_kernel); fflush(stdout); fflush(stderr); } MRIfree(&mri_ctrl) ; MRIfree(&mri_aseg) ; printf("Applying bias correction\n"); mri_dst = MRIapplyBiasCorrectionSameGeometry (mri_dst, mri_bias, mri_dst, DEFAULT_DESIRED_WHITE_MATTER_VALUE) ; if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) { MRIwrite(mri_dst, "norm_1.mgz") ; } fflush(stdout); fflush(stderr); } // if(mri_aseg) else { printf("processing without aseg, no1d=%d\n",no1d); if (!no1d) { printf("MRInormInit(): \n"); MRInormInit(mri_src, &mni, 0, 0, 0, 0, 0.0f) ; printf("MRInormalize(): \n"); mri_dst = MRInormalize(mri_src, NULL, &mni) ; if (!mri_dst) { no1d = 1 ; printf("1d normalization failed - trying no1d...\n") ; // ErrorExit(ERROR_BADPARM, "%s: normalization failed", Progname) ; } } if(no1d) { if ((file_only && nosnr) || ((gentle_flag != 0) && (control_point_fname != NULL))) { if (mri_dst == NULL) { mri_dst = MRIcopy(mri_src, NULL) ; } } else { if (nosnr) { if (interior_fname1) { MRIS *mris_interior1, *mris_interior2 ; MRI *mri_ctrl ; printf("computing initial normalization using surface interiors\n"); mri_ctrl = MRIcloneDifferentType(mri_src, MRI_UCHAR) ; mris_interior1 = MRISread(interior_fname1) ; if (mris_interior1 == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read white matter surface from %s\n", Progname, interior_fname1) ; mris_interior2 = MRISread(interior_fname2) ; if (mris_interior2 == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read white matter surface from %s\n", Progname, interior_fname2) ; add_interior_points(mri_ctrl, mri_dst, intensity_above, 1.25*intensity_below, mris_interior1, mris_interior2, mri_aseg, mri_ctrl) ; MRISfree(&mris_interior1) ; MRISfree(&mris_interior2) ; mri_bias = MRIbuildBiasImage(mri_dst, mri_ctrl, NULL, 0.0) ; if (bias_sigma> 0) { MRI *mri_kernel = MRIgaussian1d(bias_sigma, -1) ; MRIconvolveGaussian(mri_bias, mri_bias, mri_kernel) ; MRIfree(&mri_kernel); } mri_dst = MRIapplyBiasCorrectionSameGeometry (mri_src, mri_bias, mri_dst, DEFAULT_DESIRED_WHITE_MATTER_VALUE) ; MRIfree(&mri_ctrl) ; } else if (long_flag == 0) // no initial normalization specified { mri_dst = MRIcopy(mri_src, NULL) ; } } else { printf("computing initial normalization using SNR...\n") ; mri_dst = MRInormalizeHighSignalLowStd (mri_src, mri_dst, bias_sigma, DEFAULT_DESIRED_WHITE_MATTER_VALUE) ; } } if (!mri_dst) ErrorExit (ERROR_BADPARM, "%s: could not allocate volume", Progname) ; } } // else (not using aseg) fflush(stdout); fflush(stderr); if (file_only == 0) MRI3dGentleNormalize(mri_dst, NULL, DEFAULT_DESIRED_WHITE_MATTER_VALUE, mri_dst, intensity_above, intensity_below/2, file_only, bias_sigma, mri_not_control); mri_orig = MRIcopy(mri_dst, NULL) ; printf("\n"); printf("Iterating %d times\n",num_3d_iter); for (n = 0 ; n < num_3d_iter ; n++) { if(file_only) { break ; } printf( "---------------------------------\n"); printf( "3d normalization pass %d of %d\n", n+1, num_3d_iter) ; if (gentle_flag) MRI3dGentleNormalize(mri_dst, NULL, DEFAULT_DESIRED_WHITE_MATTER_VALUE, mri_dst, intensity_above/2, intensity_below/2, file_only, bias_sigma, mri_not_control); else MRI3dNormalize(mri_orig, mri_dst, DEFAULT_DESIRED_WHITE_MATTER_VALUE, mri_dst, intensity_above, intensity_below, file_only, prune, bias_sigma, scan_type, mri_not_control); } printf( "Done iterating ---------------------------------\n"); // this just setup writing control-point volume saving if(control_volume_fname) { MRI3dWriteControlPoints(control_volume_fname) ; } if(bias_volume_fname) { mri_bias = compute_bias(mri_src, mri_dst, NULL) ; printf("writing bias field to %s....\n", bias_volume_fname) ; MRIwrite(mri_bias, bias_volume_fname) ; MRIfree(&mri_bias) ; } if (verbose) { printf("writing output to %s\n", out_fname) ; } MRIwrite(mri_dst, out_fname) ; msec = TimerStop(&start) ; MRIfree(&mri_src); MRIfree(&mri_dst); seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; printf( "3D bias adjustment took %d minutes and %d seconds.\n", minutes, seconds) ; exit(0) ; return(0) ; }
int main(int argc, char *argv[]) { char *inner_mris_fname_lh,*outer_mris_fname_lh, *inner_mris_fname_rh,*outer_mris_fname_rh, *input_mri_pref,*output_mri_pref,*mask_mri_pref; MRI *mri,*mri_src,*mri_mask; MRI_SURFACE *inner_mris_lh,*outer_mris_lh,*inner_mris_rh,*outer_mris_rh; int nargs; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mri_partial_ribbon.c,v 1.11 2011/03/02 00:04:23 nicks Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; /* Set command-line parameters */ if (argc < 7) { usage(); exit(-1); } Progname=argv[0]; inner_mris_fname_lh=argv[1]; // lh.white outer_mris_fname_lh=argv[2]; // lh.pial inner_mris_fname_rh=argv[3]; // rh.white outer_mris_fname_rh=argv[4]; // rh.pial input_mri_pref=argv[5]; // volume output_mri_pref=argv[6]; // output if (argc==8) mask_mri_pref=argv[7]; // cma mask else mask_mri_pref=NULL; /* Read surface information from left inner surface file */ printf("Reading left inner surface file %s.\n",inner_mris_fname_lh); inner_mris_lh=MRISread(inner_mris_fname_lh); if (!inner_mris_lh) { fprintf(stderr,"Could not read surface file %s.\n",inner_mris_fname_lh); exit(1); } /* Read surface information from left outer surface file */ printf("Reading left outer surface file %s.\n",outer_mris_fname_lh); outer_mris_lh=MRISread(outer_mris_fname_lh); if (!outer_mris_lh) { fprintf(stderr,"Could not read surface file %s.\n",outer_mris_fname_lh); exit(1); } /* Read surface information from right inner surface file */ printf("Reading right inner surface file %s.\n",inner_mris_fname_rh); inner_mris_rh=MRISread(inner_mris_fname_rh); if (!inner_mris_rh) { fprintf(stderr,"Could not read surface file %s.\n",inner_mris_fname_rh); exit(1); } /* Read surface information from right outer surface file */ printf("Reading right outer surface file %s.\n",outer_mris_fname_rh); outer_mris_rh=MRISread(outer_mris_fname_rh); if (!outer_mris_rh) { fprintf(stderr,"Could not read surface file %s.\n",outer_mris_fname_rh); exit(1); } /* Read example volume from file */ printf("Reading MRI volume directory %s.\n",input_mri_pref); mri_src=MRIread(input_mri_pref); if (!mri_src) { fprintf(stderr,"Could not read MRI volume directory %s.\n",input_mri_pref); exit(1); } /* Read example volume from file */ if (argc==8) { printf("Reading MRI volume directory %s.\n",mask_mri_pref); mri_mask=MRIread(mask_mri_pref); if (!mri_mask) { fprintf(stderr,"Could not read MRI volume directory %s.\n", mask_mri_pref); exit(1); } } else mri_mask=NULL; /* Extract ribbon */ printf("Extracting ribbon.\n"); mri=MRISpartialribbon(inner_mris_lh, outer_mris_lh, inner_mris_rh, outer_mris_rh, mri_src, NULL, mri_mask); /* Save MRI volume to directory */ printf("Writing volume file %s.\n",output_mri_pref); MRIwrite(mri,output_mri_pref); MRIfree(&mri); MRIfree(&mri_src); if (mri_mask) MRIfree(&mri_mask); MRISfree(&inner_mris_lh); MRISfree(&outer_mris_lh); MRISfree(&inner_mris_rh); MRISfree(&outer_mris_rh); return 0; }
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) ; }