static int initialize_cluster_centers(MRI_SURFACE *mris, MRI *mri_profiles, CLUSTER *ct, int k) { int i, j, done, vno, nsamples, vnos[MAX_CLUSTERS], iter ; double dist, min_dist ; VERTEX *v, *vn ; nsamples = mri_profiles->nframes ; min_dist = sqrt(mris->total_area/k) / 2 ; for (i = 0 ; i < k ; i++) { iter = 0 ; do { do { vno = (int)randomNumber(0, mris->nvertices-1) ; v = &mris->vertices[vno] ; } while (v->ripflag) ; // see if any of the other centers are too close to this one done = 1 ; for (j = 0 ; done && j < i ; j++) { if (j == i) continue ; vn = &mris->vertices[vnos[j]] ; dist = sqrt(SQR(vn->x-v->x)+SQR(vn->y-v->y)+SQR(vn->z-v->z)) ; if (dist < min_dist) { done = 0 ; break ; } } if (iter++ > mris->nvertices) done = 1 ; } while (!done) ; vnos[i] = vno ; for (j = 0 ; j < nsamples ; j++) VECTOR_ELT(ct[i].v_mean, j+1) = MRIgetVoxVal(mri_profiles, vno, 0, 0, j) ; } mris->ct = CTABalloc(k) ; for (i = 0 ; i < k ; i++) { mris->vertices[vnos[i]].curv = i ; ct[i].npoints++ ; ct[i].vno = vnos[i] ; CTABannotationAtIndex(mris->ct, i, &mris->vertices[vnos[i]].annotation) ; } return(NO_ERROR) ; }
/* ------------------------------------------------------------ int sclustAnnot(MRIS *surf, int NClusters) Convert clusters into annotation ------------------------------------------------------------*/ int sclustAnnot(MRIS *surf, int NClusters) { COLOR_TABLE *ct ; int vtxno, vtx_clusterno, annot, n; ct = CTABalloc(NClusters+1); surf->ct = ct; for(n=1; n < NClusters; n++) // no cluster 0 sprintf(surf->ct->entries[n]->name, "%s-%03d","cluster",n); for(vtxno = 0; vtxno < surf->nvertices; vtxno++) { vtx_clusterno = surf->vertices[vtxno].undefval; if(vtx_clusterno == 0 || vtx_clusterno > NClusters){ surf->vertices[vtxno].annotation = 0; continue; } CTABannotationAtIndex(surf->ct, vtx_clusterno, &annot); surf->vertices[vtxno].annotation = annot; } return(0); }
static int initialize_cluster_centers_with_ico(MRI_SURFACE *mris, MRI *mri_profiles, CLUSTER *ct, MRI_SURFACE *mris_ico) { int i, j, vno, nsamples, vnos[MAX_CLUSTERS], k ; double r1, r2, res ; float fmin ; MRIS_HASH_TABLE *mht ; VERTEX *vico ; k = mris_ico->nvertices ; MRISstoreRipFlags(mris) ; MRISunrip(mris) ; r1 = MRISaverageRadius(mris) ; r2 = MRISaverageRadius(mris_ico) ; MRISscaleBrain(mris_ico,mris_ico, r1/r2); res = sqrt(mris->total_area/mris->nvertices) ; mht = MHTfillVertexTableRes(mris, NULL, CURRENT_VERTICES, 2*res) ; nsamples = mri_profiles->nframes ; for (i = 0 ; i < mris_ico->nvertices ; i++) { vico = &mris_ico->vertices[i] ; vno = MRISfindClosestVertex(mris, vico->x, vico->y, vico->z, &fmin) ; if (vno < 0) continue ; vnos[i] = vno ; for (j = 0 ; j < nsamples ; j++) VECTOR_ELT(ct[i].v_mean, j+1) = MRIgetVoxVal(mri_profiles, vno, 0, 0, j) ; } mris->ct = CTABalloc(k) ; for (i = 0 ; i < k ; i++) { mris->vertices[vnos[i]].curv = i ; ct[i].npoints++ ; ct[i].vno = vnos[i] ; CTABannotationAtIndex(mris->ct, i, &mris->vertices[vnos[i]].annotation) ; } MRISrestoreRipFlags(mris) ; return(NO_ERROR) ; }
int main(int argc, char *argv[]) { char **av, fname[STRLEN] ; int ac, nargs, i ; char *subject, *cp, mdir[STRLEN], *out_fname, *name ; int r, g, b, nedits = 0 ; MRI *mri=NULL, *mri_edits=NULL, *mri_aseg_auto=NULL; // default output file name: out_fname = strcpyalloc("edits.mgz"); /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mri_compile_edits.c,v 1.6 2011/03/02 00:04:14 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(1) ; if (strlen(sdir) == 0) { cp = getenv("SUBJECTS_DIR") ; if (cp == NULL) ErrorExit (ERROR_BADPARM, "%s: SUBJECTS_DIR must be defined in the env or on cmdline " "with -sdir", Progname) ; strcpy(sdir, cp) ; } subject = argv[1] ; if (argv[2]) out_fname = argv[2] ; printf("Compiling volume edits for subject %s...\n", subject) ; fflush(stdout); sprintf(mdir, "%s/%s/mri", sdir, subject) ; /* * brain.mgz */ sprintf(fname, "%s/brain.mgz", mdir) ; mri = MRIread(fname) ; if (mri) { if(NULL == mri_edits) mri_edits = MRIclone(mri, NULL) ; int edits = MRIsetVoxelsWithValue(mri, mri_edits, WM_EDITED_OFF_VAL, EDIT_BRAIN_OFF) ; edits += MRIsetVoxelsWithValue(mri, mri_edits, WM_EDITED_ON_VAL, EDIT_BRAIN_ON) ; nedits += edits; MRIfree(&mri) ; if (edits) printf("Found %d edits in brain.mgz\n",edits); fflush(stdout); } /* * wm.mgz */ sprintf(fname, "%s/wm.mgz", mdir) ; mri = MRIread(fname) ; if (mri) { if(NULL == mri_edits) mri_edits = MRIclone(mri, NULL) ; int edits = MRIsetVoxelsWithValue(mri, mri_edits, WM_EDITED_OFF_VAL, EDIT_WM_OFF) ; edits += MRIsetVoxelsWithValue(mri, mri_edits, WM_EDITED_ON_VAL, EDIT_WM_ON) ; nedits += edits; MRIfree(&mri) ; if (edits) printf("Found %d edits in wm.mgz\n",edits); fflush(stdout); } /* * brainmask.mgz */ sprintf(fname, "%s/brainmask.mgz", mdir) ; mri = MRIread(fname) ; if (mri) { if(NULL == mri_edits) mri_edits = MRIclone(mri, NULL) ; int edits = MRIsetVoxelsWithValue(mri, mri_edits, WM_EDITED_OFF_VAL, EDIT_BRAINMASK_OFF) ; edits += MRIsetVoxelsWithValue(mri, mri_edits, WM_EDITED_ON_VAL, EDIT_BRAINMASK_ON) ; nedits += edits; MRIfree(&mri) ; if (edits) printf("Found %d edits in brainmask.mgz\n",edits); fflush(stdout); } /* * brain.finalsurfs.mgz */ sprintf(fname, "%s/brain.finalsurfs.mgz", mdir) ; mri = MRIread(fname) ; if (mri) { if(NULL == mri_edits) mri_edits = MRIclone(mri, NULL) ; int edits = MRIsetVoxelsWithValue(mri, mri_edits, WM_EDITED_OFF_VAL, EDIT_FINALSURFS_OFF) ; edits += MRIsetVoxelsWithValue(mri, mri_edits, WM_EDITED_ON_VAL, EDIT_FINALSURFS_ON) ; nedits += edits; MRIfree(&mri) ; if (edits) printf("Found %d edits in brain.finalsurfs.mgz\n",edits); fflush(stdout); } /* * aseg.mgz */ sprintf(fname, "%s/aseg.mgz", mdir) ; mri = MRIread(fname) ; if (mri) { if(NULL == mri_edits) mri_edits = MRIclone(mri, NULL) ; sprintf(fname, "%s/aseg.auto.mgz", mdir) ; mri_aseg_auto = MRIread(fname) ; if (mri_aseg_auto) { int edits = MRIsetDifferentVoxelsWithValue(mri, mri_aseg_auto, mri_edits, EDIT_ASEG_CHANGED); nedits += edits; MRIfree(&mri) ; MRIfree(&mri_aseg_auto) ; if (edits) printf("Found %d edits in aseg.mgz\n",edits); fflush(stdout); } } if (mri_edits) { mri_edits->ct = CTABalloc(CTAB_ENTRIES) ; strcpy (mri_edits->fname, "mri_compile_edits"); for (i = 0 ; i < CTAB_ENTRIES ; i++) { switch (i) { case EDIT_WM_OFF: name = "wm-OFF" ; r = 0 ; g = 0 ; b = 255 ; break ; case EDIT_WM_ON: name = "wm-ON" ; r = 255 ; g = 0 ; b = 0 ; break ; case EDIT_BRAIN_OFF: name = "brain-OFF" ; r = 0 ; g = 255 ; b = 255 ; break ; case EDIT_BRAIN_ON: name = "brain-ON" ; r = 255 ; g = 255 ; b = 0 ; break ; case EDIT_BRAINMASK_OFF: name = "brainmask-OFF" ; r = 0 ; g = 64 ; b = 255 ; break ; case EDIT_BRAINMASK_ON: name = "brainmask-ON" ; r = 255 ; g = 26 ; b = 0 ; break ; case EDIT_FINALSURFS_OFF: name = "brain.finalsurf-OFF" ; r = 0 ; g = 128 ; b = 255 ; break ; case EDIT_FINALSURFS_ON: name = "brain.finalsurfs-ON" ; r = 255 ; g = 128 ; b = 0 ; break ; case EDIT_ASEG_CHANGED: name = "aseg-CHANGED" ; r = 255 ; g = 255 ; b = 128 ; break ; default: continue ; } strcpy(mri_edits->ct->entries[i]->name, name) ; mri_edits->ct->entries[i]->ri = r ; mri_edits->ct->entries[i]->gi = g ; mri_edits->ct->entries[i]->bi = b ; mri_edits->ct->entries[i]->ai = 255; /* Now calculate the float versions. */ mri_edits->ct->entries[i]->rf = (float)mri_edits->ct->entries[i]->ri / 255.0; mri_edits->ct->entries[i]->gf = (float)mri_edits->ct->entries[i]->gi / 255.0; mri_edits->ct->entries[i]->bf = (float)mri_edits->ct->entries[i]->bi / 255.0; mri_edits->ct->entries[i]->af = (float)mri_edits->ct->entries[i]->ai / 255.0; } } // recon-all -show-edits greps on this text, so dont change it if (nedits && mri_edits) { printf("%d mri_compile_edits_found, saving results to %s\n", nedits, out_fname) ; MRIwrite(mri_edits, out_fname); printf("Edits can be viewed with command:\n"); printf("tkmedit %s T1.mgz -segmentation %s\n",subject,out_fname); } else { printf("0 mri_compile_edits_found\n") ; } exit(0) ; return(0) ; }