CLUSTER * MRISclusterKMeans(MRI_SURFACE *mris, MRI *mri_profiles, int k, char *start_fname, MRI_SURFACE *mris_ico) { int i, nsamples, iter, done, nchanged ; char fname[STRLEN] ; CLUSTER *ct ; nsamples = mri_profiles->nframes ; ct = calloc(k, sizeof(CLUSTER)) ; for (i = 0 ; i < k ; i++) { ct[i].v_mean = VectorAlloc(nsamples, MATRIX_REAL) ; ct[i].m_cov = MatrixIdentity(nsamples, NULL) ; ct[i].npoints = 0 ; } initialize_kmeans_centers(mris, mri_profiles, ct, k) ; done = iter = 0 ; do { nchanged = mark_clusters(mris, mri_profiles, ct, k) ; if (nchanged == 0) done = 1 ; compute_cluster_statistics(mris, mri_profiles, ct, k) ; sprintf(fname, "%s.clusters%6.6d.annot", mris->hemisphere == LEFT_HEMISPHERE ? "lh" : "rh", iter) ; printf("%6.6d: writing %s\n", iter, fname) ; MRISwriteAnnotation(mris, fname) ; sprintf(fname, "./%s.clusters%6.6d.indices", mris->hemisphere == LEFT_HEMISPHERE ? "lh" : "rh", iter) ; MRISwriteCurvature(mris, fname) ; if (iter++ > max_iterations) done = 1 ; } while (!done) ; return(ct) ; }
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 */ }
static CLUSTER * MRISclusterAgglomerative(MRI_SURFACE *mris, MRI *mri_profiles, int k, char *start_fname, MRI_SURFACE *mris_ico) { int i, nsamples, iter, done, vno, cluster ; char fname[STRLEN] ; CLUSTER *ct ; if (start_fname) { VERTEX *v ; if (MRISreadAnnotation(mris, start_fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read initial annotation file %s", Progname, start_fname) ; k = 0 ; for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->annotation == 0) { v->ripflag = 1; continue ; } CTABfindAnnotation(mris->ct, v->annotation, &cluster); if (cluster >= k) k = cluster+1 ; v->curv = cluster ; } printf("%d clusters detected...\n", k) ; ct = calloc(k, sizeof(CLUSTER)) ; if (!ct) ErrorExit(ERROR_BADPARM, "%s: could not allocate %d clusters", Progname, k) ; nsamples = mri_profiles->nframes ; for (i = 0 ; i < k ; i++) { ct[i].v_mean = VectorAlloc(nsamples, MATRIX_REAL) ; ct[i].m_cov = MatrixIdentity(nsamples, NULL) ; ct[i].npoints = 0 ; } for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->ripflag) continue ; add_vertex_to_cluster(mris, mri_profiles, ct, v->curv, vno); } } else { if (mris_ico) k = mris_ico->nvertices ; ct = calloc(k, sizeof(CLUSTER)) ; if (!ct) ErrorExit(ERROR_BADPARM, "%s: could not allocate %d clusters", Progname, k) ; nsamples = mri_profiles->nframes ; for (i = 0 ; i < k ; i++) { ct[i].v_mean = VectorAlloc(nsamples, MATRIX_REAL) ; ct[i].m_cov = MatrixIdentity(nsamples, NULL) ; ct[i].npoints = 0 ; } MRISsetCurvature(mris, -1) ; if (mris_ico) initialize_cluster_centers_with_ico(mris, mri_profiles, ct, mris_ico) ; else initialize_cluster_centers(mris, mri_profiles, ct, k) ; done = iter = 0 ; do { vno = find_most_likely_unmarked_vertex(mris, mri_profiles, ct, k, &cluster); if (vno < 0) break ; add_vertex_to_cluster(mris, mri_profiles, ct, cluster, vno); if (write_iters > 0 && ((iter % write_iters) == 0)) { sprintf(fname, "%s.clusters%6.6d.annot", mris->hemisphere == LEFT_HEMISPHERE ? "lh" : "rh", iter) ; printf("%6.6d: writing %s\n", iter, fname) ; MRISwriteAnnotation(mris, fname) ; sprintf(fname, "./%s.clusters%6.6d.indices", mris->hemisphere == LEFT_HEMISPHERE ? "lh" : "rh", iter) ; // MRISwriteCurvature(mris, fname) ; } if (write_iters == 0 && ((iter % 5000) == 0)) printf("%6.6d of %6.6d\n", iter, mris->nvertices-k) ; if (iter++ > mris->nvertices-k || iter > max_iterations) done = 1 ; } while (!done) ; } iter = done = 0 ; do { vno = find_most_likely_vertex_to_swap(mris, mri_profiles, ct, k, &cluster); if (vno < 0) break ; remove_vertex_from_cluster(mris, mri_profiles, ct, mris->vertices[vno].curv, vno) ; add_vertex_to_cluster(mris, mri_profiles, ct, cluster, vno); if (write_iters > 0 && ((iter % write_iters) == 0)) { sprintf(fname, "%s.more_clusters%6.6d.annot", mris->hemisphere == LEFT_HEMISPHERE ? "lh" : "rh", iter) ; printf("%6.6d: writing %s\n", iter, fname) ; MRISwriteAnnotation(mris, fname) ; sprintf(fname, "./%s.more_clusters%6.6d.indices", mris->hemisphere == LEFT_HEMISPHERE ? "lh" : "rh", iter) ; // MRISwriteCurvature(mris, fname) ; } if (write_iters == 0 && ((iter % 5000) == 0)) printf("%6.6d of %6.6d\n", iter, mris->nvertices) ; if (iter++ > mris->nvertices || iter > max_iterations) done = 1 ; } while (!done) ; return(ct); }
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[]) { int ac, nargs, *nunits, index ; MRI_SURFACE *mris ; char **av, fname[STRLEN], *annot_name, *out_fname, *cp, *subject, *hemi ; float area_thresh ; nargs = handle_version_option (argc, argv, vcid, "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; ErrorInit(NULL, NULL, NULL) ; DiagInit(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 < 6) usage_exit() ; subject = argv[1] ; hemi = argv[2] ; annot_name = argv[3] ; area_thresh = atof(argv[4]) ; out_fname = argv[5] ; 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.sphere", sdir, subject, hemi) ; mris = MRISread(fname) ; if (mris == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read surface from %s", Progname, fname) ; if (MRISreadAnnotation(mris, annot_name) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read annotation %s", Progname, annot_name) ; if (mris->ct == NULL) ErrorExit(ERROR_NOFILE, "%s: annotation %s must have embedded color table", Progname, annot_name) ; nunits = (int *)calloc(mris->ct->nentries, sizeof(int)) ; if (!nunits) ErrorExit(ERROR_BADPARM, "%s: could not allocate %d nunits table", Progname,mris->ct->nentries) ; MRIScomputeMetricProperties(mris) ; if (isdigit(*argv[4])) // area threshold specified { int vno ; VERTEX *v ; float *area ; area = (float *)calloc(mris->ct->nentries, sizeof(float)) ; if (!area) ErrorExit(ERROR_BADPARM, "%s: could not allocate %d area table", Progname,mris->ct->nentries) ; for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; CTABfindAnnotation(mris->ct, v->annotation, &index) ; if (index < 0 || v->ripflag) continue ; area[index] += v->area ; } for (index = 0 ; index < mris->ct->nentries ; index++) nunits[index] = (int)(area[index] / area_thresh)+1 ; free(area) ; } else // interpret it as a file with parcellation names and # of units { char line[STRLEN], *cp, name[STRLEN] ; FILE *fp ; int num ; printf("interpreting 4th command line arg as split file name\n") ; fp = fopen(argv[4], "r") ; if (fp == NULL) ErrorExit(ERROR_NOFILE, "%s: could not open parcellation division file %s", Progname,argv[4]) ; while ((cp = fgetl(line, STRLEN-1, fp)) != NULL) { if (sscanf(line, "%s %d", name, &num) != 2) ErrorExit(ERROR_BADFILE, "%s: could not parse name/num pair from '%s'", Progname, line) ; CTABfindName(mris->ct, name, &index) ; if (index < 0) ErrorExit(ERROR_BADFILE, "%s: could not find name '%s' in color table", Progname, name) ; nunits[index] = num ; } } for (index = 0 ; index < mris->ct->nentries ; index++) if (nunits[index] > 1) printf("dividing %s (%d) into %d parts\n", mris->ct->entries[index]->name, index, nunits[index]) ; MRISdivideAnnotation(mris, nunits) ; free(nunits) ; printf("saving annotation to %s\n", out_fname) ; MRISwriteAnnotation(mris, out_fname) ; return 0; }
/*---------------------------------------------------------------*/ int main(int argc, char *argv[]) { int nargs,n,err; MRIS *SurfReg[100]; MRI *SrcVal, *TrgVal; char *base; COLOR_TABLE *ctab=NULL; 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); // Load in surface registrations 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); } // Load in source data SrcVal = NULL; 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); } else if(DoSynthOnes != 0) { printf("INFO: filling input with all 1s\n"); MRIconst(SrcVal->width, SrcVal->height, SrcVal->depth, SrcVal->nframes, 1, SrcVal); } else if(AnnotFile) { printf("Loading annotation %s\n",AnnotFile); err = MRISreadAnnotation(SurfReg[0], AnnotFile); if(err) exit(1); SrcVal = MRISannotIndex2Seg(SurfReg[0]); ctab = CTABdeepCopy(SurfReg[0]->ct); } else if(LabelFile) { LABEL *srclabel; printf("Loading label %s\n",LabelFile); srclabel = LabelRead(NULL, LabelFile); if(srclabel == NULL) exit(1); SrcVal = MRISlabel2Mask(SurfReg[0],srclabel,NULL); printf(" %d points in input label\n",srclabel->n_points); LabelFree(&srclabel); } else { printf("Loading %s\n",SrcValFile); SrcVal = MRIread(SrcValFile); if(SrcVal==NULL) exit(1); } // Apply registration to source TrgVal = MRISapplyReg(SrcVal, SurfReg, nsurfs, ReverseMapFlag, DoJac, UseHash); if(TrgVal == NULL) exit(1); // Save output if(AnnotFile){ printf("Converting to target annot\n"); err = MRISseg2annot(SurfReg[nsurfs-1],TrgVal,ctab); if(err) exit(1); printf("Writing %s\n",TrgValFile); MRISwriteAnnotation(SurfReg[nsurfs-1], TrgValFile); } else if(LabelFile){ LABEL *label; label = MRISmask2Label(SurfReg[nsurfs-1], TrgVal, 0, 10e-5); printf(" %d points in output label\n",label->n_points); err = LabelWrite(label,TrgValFile); if(err){ printf("ERROR: writing label file %s\n",TrgValFile); exit(1); } LabelFree(&label); } else{ printf("Writing %s\n",TrgValFile); MRIwrite(TrgVal,TrgValFile); } printf("mris_apply_reg done\n"); return 0; }
/*---------------------------------------------------------------*/ int main(int argc, char *argv[]) { int nargs, nthlabel, n, vtxno, ano, index, nunhit; 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); // Make sure subject exists sprintf(tmpstr,"%s/%s",SUBJECTS_DIR,subject); if (!fio_IsDirectory(tmpstr)) { printf("ERROR: cannot find %s\n",tmpstr); exit(1); } if(AnnotPath == NULL){ // Get path to annot, make sure it does not exist sprintf(tmpstr,"%s/%s/label/%s.%s.annot", SUBJECTS_DIR,subject,hemi,AnnotName); if (fio_FileExistsReadable(tmpstr)) { printf("ERROR: %s already exists\n",tmpstr); exit(1); } AnnotPath = strcpyalloc(tmpstr); } // Read the surf sprintf(tmpstr,"%s/%s/surf/%s.orig",SUBJECTS_DIR,subject,hemi); printf("Loading %s\n",tmpstr); mris = MRISread(tmpstr); if (mris == NULL) exit(1); // Set up color table set_atable_from_ctable(ctab); mris->ct = ctab; // Set up something to keep track of nhits nhits = MRIalloc(mris->nvertices,1,1,MRI_INT); // Set up something to keep track of max stat for that vertex if (maxstatwinner) maxstat = MRIalloc(mris->nvertices,1,1,MRI_FLOAT); // Go thru each label for (nthlabel = 0; nthlabel < nlabels; nthlabel ++) { label = LabelRead(subject,LabelFiles[nthlabel]); if (label == NULL) { printf("ERROR: reading %s\n",LabelFiles[nthlabel]); exit(1); } index = nthlabel; if (MapUnhitToUnknown) index ++; ano = index_to_annotation(index); printf("%2d %2d %s\n",index,ano,index_to_name(index)); for (n = 0; n < label->n_points; n++) { vtxno = label->lv[n].vno; if (vtxno < 0 || vtxno > mris->nvertices) { printf("ERROR: %s, n=%d, vertex %d out of range\n", LabelFiles[nthlabel],n,vtxno); exit(1); } if(DoLabelThresh && label->lv[n].stat < LabelThresh) continue; if (maxstatwinner) { float stat = MRIgetVoxVal(maxstat,vtxno,0,0,0); if (label->lv[n].stat < stat) { if (verbose) { printf("Keeping prior label for vtxno %d " "(old_stat=%f > this_stat=%f)\n", vtxno,stat,label->lv[n].stat); } continue; } MRIsetVoxVal(maxstat,vtxno,0,0,0,label->lv[n].stat); } if (verbose) { if (MRIgetVoxVal(nhits,vtxno,0,0,0) > 0) { printf ("WARNING: vertex %d maps to multiple labels! (overwriting)\n", vtxno); } } MRIsetVoxVal(nhits,vtxno,0,0,0,MRIgetVoxVal(nhits,vtxno,0,0,0)+1); mris->vertices[vtxno].annotation = ano; //printf("%5d %2d %2d %s\n",vtxno,segid,ano,index_to_name(segid)); } // label ponts LabelFree(&label); }// Label nunhit = 0; if (MapUnhitToUnknown) { printf("Mapping unhit to unknown\n"); for (vtxno = 0; vtxno < mris->nvertices; vtxno++) { if (MRIgetVoxVal(nhits,vtxno,0,0,0) == 0) { ano = index_to_annotation(0); mris->vertices[vtxno].annotation = ano; nunhit ++; } } printf("Found %d unhit vertices\n",nunhit); } if (dilate_label_name) { dilate_label_into_unknown(mris, dilate_label_annot) ; } printf("Writing annot to %s\n",AnnotPath); MRISwriteAnnotation(mris, AnnotPath); if (NHitsFile != NULL) MRIwrite(nhits,NHitsFile); return 0; }