static MRI * MRIsynthesizeWithFAF(MRI *mri_T1, MRI *mri_PD, MRI *mri_dst, double TR, double alpha, double TE, int nfaf, float *faf_coefs[3][2]) { int x, y, z, width, height, depth, n ; Real flash, T1, PD ; double xb, yb, zb, x0, y0, z0, w0x, w0y, w0z ; x0 = mri_T1->width/2 ; y0 = mri_T1->height/2 ; z0 = mri_PD->depth/2 ; w0x = 2/x0 ; w0y = 2/y0 ; w0z = 2/z0 ; if (!mri_dst) mri_dst = MRIclone(mri_T1, NULL) ; mri_dst->tr = TR ; mri_dst->flip_angle = alpha ; mri_dst->te = TE ; mri_dst->ti = 0 ; width = mri_T1->width ; height = mri_T1->height ; depth = mri_T1->depth ; for (x = 0 ; x < width ; x++) { for (xb = 1.0, n=1 ; n <= nfaf ; n++) xb += faf_coefs[0][0][n-1] * cos(w0x*n*(x-x0)) + faf_coefs[0][1][n-1] * sin(w0x*n*(x-x0)) ; for (y = 0 ; y < height ; y++) { for (yb = 1.0, n=1 ; n <= nfaf ; n++) yb += faf_coefs[1][0][n-1] * cos(w0y*n*(y-y0)) + faf_coefs[1][1][n-1] * sin(w0y*n*(y-y0)) ; for (z = 0 ; z < depth ; z++) { for (zb = 1.0, n=1 ; n <= nfaf ; n++) zb += faf_coefs[2][0][n-1] * cos(w0z*n*(z-z0)) + faf_coefs[2][1][n-1] * sin(w0z*n*(z-z0)) ; if (x == Gx && y == Gy && z == Gz) DiagBreak() ; MRIsampleVolume(mri_T1, x, y, z, &T1) ; if (T1 <= 0) T1 = 1 ; if (T1 < 900 && T1 > 600) DiagBreak() ; MRIsampleVolume(mri_PD, x, y, z, &PD) ; flash = FLASHforwardModel(T1, PD, TR, xb*yb*zb*alpha, TE) ; MRIsetVoxVal(mri_dst, x, y, z, 0, flash) ; } } } return(mri_dst) ; }
static void mrisExtractMidGrayValues(MRIS *mris, MRI *mri) { int n; float th; Real xw,yw,zw,xv,yv,zv,val; VERTEX *v; for (n=0;n<mris->nvertices;n++) { v = &mris->vertices[n] ; v->val=0; } MRIScomputeMetricProperties(mris); for (n=0;n<mris->nvertices;n++) { v = &mris->vertices[n] ; th=v->curv/2.0f; xw = v->x + th*v->nx; yw = v->y + th*v->ny; zw = v->z + th*v->nz; MRIsurfaceRASToVoxel(mri, xw, yw, zw, &xv, &yv, &zv) ; MRIsampleVolume(mri, xv, yv, zv, &val) ; v->val=val; } for (n=0;n<mris->nvertices;n++) { v = &mris->vertices[n] ; v->curv = v->val; } }
static double compute_surface_sse(MRI_SURFACE *mris, MRI *mri, float sample_dist) { int vno, nsamples ; VERTEX *v ; float sse, dist ; Real val, xw, yw, zw, x, y, z, nx, ny, nz, error ; for (nsamples = 0, sse = 0.0, vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->ripflag) continue ; if (!finite(v->x) || !finite(v->y) || !finite(v->z)) DiagBreak() ; // sample outside - want bright stuff out here nx = v->nx ; ny = v->ny ; nz = v->nz ; for (error = 0.0, dist = 0 ; dist <= sample_dist ; dist += DELTA) { x = v->x + dist*nx ; y = v->y + dist*ny ; z = v->z + dist*nz ; MRISvertexToVoxel(mris, v, mri, &xw, &yw, &zw); MRIsampleVolume(mri, xw, yw, zw, &val) ; val = 1000-val ; error += (val*val) ; nsamples++ ; } // sample inwards - want dark stuff for (dist = DELTA ; dist <= sample_dist ; dist += DELTA) { x = v->x - dist*nx ; y = v->y - dist*ny ; z = v->z - dist*nz ; MRISvertexToVoxel(mris, v, mri, &xw, &yw, &zw); MRIsampleVolume(mri, xw, yw, zw, &val) ; val = 0-val ; error += 100*(val*val) ; nsamples++ ; } sse += error / (float)nsamples ; } return(sse) ; }
static MRI * MRIsynthesize(MRI *mri_T1, MRI *mri_PD, MRI *mri_T2star, MRI *mri_dst, double TR, double alpha, double TE) { int x, y, z, width, height, depth ; Real flash, T1, PD ; if (!mri_dst) mri_dst = MRIclone(mri_T1, NULL) ; mri_dst->tr = TR ; mri_dst->flip_angle = alpha ; mri_dst->te = TE ; mri_dst->ti = 0 ; width = mri_T1->width ; height = mri_T1->height ; depth = mri_T1->depth ; for (x = 0 ; x < width ; x++) { for (y = 0 ; y < height ; y++) { for (z = 0 ; z < depth ; z++) { if (x == Gx && y == Gy && z == Gz) DiagBreak() ; MRIsampleVolume(mri_T1, x, y, z, &T1) ; if (T1 <= 0) T1 = 1 ; if (T1 < 900 && T1 > 600) DiagBreak() ; MRIsampleVolume(mri_PD, x, y, z, &PD) ; if (mri_T2star) { Real T2star ; MRIsampleVolume(mri_T2star, x, y, z, &T2star) ; flash = FLASHforwardModelT2star(T1, PD, T2star, TR, alpha, TE) ; } else flash = FLASHforwardModel(T1, PD, TR, alpha, TE) ; MRIsetVoxVal(mri_dst, x, y, z, 0, flash) ; if (!finite(flash)) DiagBreak() ; } } } return(mri_dst) ; }
static int compute_rigid_gradient(MRI_SURFACE *mris, MRI *mri, double *pdx, double *pdy, double *pdz) { int vno ; VERTEX *v ; Real val, xw, yw, zw, dx, dy, dz, delV, x, y, z, Ix, Iy, Iz, xv, yv, zv ; dx = dy = dz = 0.0 ; for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->ripflag) continue ; if (vno == Gdiag_no) DiagBreak() ; x = v->x ; y = v->y ; z = v->z ; MRISvertexToVoxel(mris, v, mri, &xw, &yw, &zw); MRIsampleVolume(mri, xw, yw, zw, &val) ; MRIsampleVolumeGradient(mri, xw, yw, zw, &Ix, &Iy, &Iz) ; // convert back to surface coords xw += Ix ; yw += Iy ; zw += Iz ; if (mris->useRealRAS) MRIvoxelToWorld(mri, xw, yw, zw, &xv, &yv, &zv) ; else MRIvoxelToSurfaceRAS(mri, xw, yw, zw, &xv, &yv, &zv) ; Ix = xv-v->x ; Iy = yv-v->y; Iz = zv-v->z ; delV = v->val - val ; dx += delV * Ix ; dy += delV * Iy ; dz += delV * Iz ; if (!finitep((float)dx)) DiagBreak() ; if (!finitep((float)dy)) DiagBreak() ; if (!finitep((float)dz)) DiagBreak() ; } dx /= mris->nvertices ; dy /= mris->nvertices ; dz /= mris->nvertices ; *pdx = dx ; *pdy = dy ; *pdz = dz ; return(NO_ERROR) ; }
static MRI * apply_bias(MRI *mri_orig, MRI *mri_norm, MRI *mri_bias) { MATRIX *m_vox2vox; VECTOR *v1, *v2; int x, y, z ; double xd, yd, zd, bias, val_orig, val_norm ; if (mri_norm == NULL) mri_norm = MRIclone(mri_orig, NULL) ; m_vox2vox = MRIgetVoxelToVoxelXform(mri_orig, mri_bias) ; v1 = VectorAlloc(4, MATRIX_REAL); v2 = VectorAlloc(4, MATRIX_REAL); VECTOR_ELT(v1, 4) = 1.0 ; VECTOR_ELT(v2, 4) = 1.0 ; for (x = 0 ; x < mri_orig->width ; x++) { V3_X(v1) = x ; for (y = 0 ; y < mri_orig->height ; y++) { V3_Y(v1) = y ; for (z = 0 ; z < mri_orig->depth ; z++) { V3_Z(v1) = z ; if (x == Gx && y == Gy && z == Gz) DiagBreak() ; val_orig = MRIgetVoxVal(mri_orig, x, y, z, 0) ; MatrixMultiply(m_vox2vox, v1, v2) ; xd = V3_X(v2) ; yd = V3_Y(v2) ; zd = V3_Z(v2); MRIsampleVolume(mri_bias, xd, yd, zd, &bias) ; val_norm = val_orig * bias ; if (mri_norm->type == MRI_UCHAR) { if (val_norm > 255) val_norm = 255 ; else if (val_norm < 0) val_norm = 0 ; } MRIsetVoxVal(mri_norm, x, y, z, 0, val_norm) ; } } } MatrixFree(&m_vox2vox) ; VectorFree(&v1) ; VectorFree(&v2) ; return(mri_norm) ; }
static double compute_surface_dist_sse(MRI_SURFACE *mris, MRI *mri) { double sse = 0.0 ; int vno ; VERTEX *v ; Real val, error, xw, yw, zw ; for (vno = 0, sse = 0.0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->ripflag) continue ; if (vno == Gdiag_no) DiagBreak() ; MRISvertexToVoxel(mris, v, mri, &xw, &yw, &zw); MRIsampleVolume(mri, xw, yw, zw, &val) ; error = v->val - val ; sse += error*error ; } return(sse) ; }
static MRI * MRIcomputeSurfaceDistanceIntensities(MRI_SURFACE *mris, MRI *mri_ribbon, MRI *mri_aparc, MRI *mri, MRI *mri_aseg, int whalf) { MRI *mri_features, *mri_binary, *mri_white_dist, *mri_pial_dist ; int vno, ngm, outside_of_ribbon, label0, label, ohemi_label, xi, yi, zi, xk, yk, zk, x0, y0, z0, hemi_label, assignable ; double xv, yv, zv, step_size, dist, thickness, wdist, pdist, snx, sny, snz, nx, ny, nz, xl, yl, zl, x, y, z, dot, angle ; VERTEX *v ; mri_features = MRIallocSequence(mris->nvertices, 1, 1, MRI_FLOAT, 1) ; // one samples inwards, one in ribbon, and one outside MRIcopyHeader(mri, mri_features) ; mri_binary = MRIcopy(mri_ribbon, NULL) ; mri_binary = MRIbinarize(mri_ribbon, NULL, 1, 0, 1) ; mri_pial_dist = MRIdistanceTransform(mri_binary, NULL, 1, max_pial_dist+1, DTRANS_MODE_SIGNED,NULL); if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) MRIwrite(mri_pial_dist, "pd.mgz") ; MRIclear(mri_binary) ; MRIcopyLabel(mri_ribbon, mri_binary, Left_Cerebral_White_Matter) ; MRIcopyLabel(mri_ribbon, mri_binary, Right_Cerebral_White_Matter) ; MRIbinarize(mri_binary, mri_binary, 1, 0, 1) ; mri_white_dist = MRIdistanceTransform(mri_binary, NULL, 1, max_white_dist+1, DTRANS_MODE_SIGNED,NULL); if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) MRIwrite(mri_white_dist, "wd.mgz") ; if (mris->hemisphere == LEFT_HEMISPHERE) { ohemi_label = Right_Cerebral_Cortex ; hemi_label = Left_Cerebral_Cortex ; } else { hemi_label = Right_Cerebral_Cortex ; ohemi_label = Left_Cerebral_Cortex ; } step_size = mri->xsize/2 ; for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (vno == Gdiag_no) DiagBreak() ; if (v->ripflag) continue ; // not cortex nx = v->pialx - v->whitex ; ny = v->pialy - v->whitey ; nz = v->pialz - v->whitez ; thickness = sqrt(nx*nx + ny*ny + nz*nz) ; if (FZERO(thickness)) continue ; // no cortex here x = (v->pialx + v->whitex)/2 ; y = (v->pialy + v->whitey)/2 ; z = (v->pialz + v->whitez)/2 ; // halfway between white and pial is x0 MRISsurfaceRASToVoxelCached(mris, mri_aseg, x, y, z, &xl, &yl, &zl) ; x0 = nint(xl); y0 = nint(yl) ; z0 = nint(zl) ; label0 = MRIgetVoxVal(mri_aparc, x0, y0, z0,0) ; // compute surface normal in voxel coords MRISsurfaceRASToVoxelCached(mris, mri_aseg, x+v->nx, y+v->ny, z+v->nz, &snx, &sny, &snz) ; snx -= xl ; sny -= yl ; snz -= zl ; for (ngm = 0, xk = -whalf ; xk <= whalf ; xk++) { xi = mri_aseg->xi[x0+xk] ; for (yk = -whalf ; yk <= whalf ; yk++) { yi = mri_aseg->yi[y0+yk] ; for (zk = -whalf ; zk <= whalf ; zk++) { zi = mri_aseg->zi[z0+zk] ; label = MRIgetVoxVal(mri_aseg, xi, yi, zi,0) ; if (xi == Gx && yi == Gy && zi == Gz) DiagBreak() ; if (label != hemi_label) continue ; label = MRIgetVoxVal(mri_aparc, xi, yi, zi,0) ; if (label && label != label0) // if outside the ribbon it won't be assigned to a parcel continue ; // constrain it to be in the same cortical parcel // search along vector connecting x0 to this point to make sure it is we don't perforate wm or leave and re-enter cortex nx = xi-x0 ; ny = yi-y0 ; nz = zi-z0 ; thickness = sqrt(nx*nx + ny*ny + nz*nz) ; assignable = 1 ; // assume this point should be counted if (thickness > 0) { nx /= thickness ; ny /= thickness ; nz /= thickness ; dot = nx*snx + ny*sny + nz*snz ; angle = acos(dot) ; if (FABS(angle) > angle_threshold) assignable = 0 ; outside_of_ribbon = 0 ; for (dist = 0 ; assignable && dist <= thickness ; dist += step_size) { xv = x0+nx*dist ; yv = y0+ny*dist ; zv = z0+nz*dist ; if (nint(xv) == Gx && nint(yv) == Gy && nint(zv) == Gz) DiagBreak() ; MRIsampleVolume(mri_pial_dist, xv, yv, zv, &pdist) ; MRIsampleVolume(mri_white_dist, xv, yv, zv, &wdist) ; label = MRIgetVoxVal(mri_aseg, xi, yi, zi,0) ; if (SKIP_LABEL(label) || label == ohemi_label) assignable = 0 ; if (wdist < 0) // entered wm - not assignable assignable = 0 ; else { if (pdist > 0) // outside pial surface outside_of_ribbon = 1 ; else { if (outside_of_ribbon) // left ribbon and reentered assignable = 0 ; } } } } // close of thickness > 0 if (assignable) ngm++ ; else DiagBreak() ; } } } MRIsetVoxVal(mri_features, vno, 0, 0, 0, ngm) ; } MRIfree(&mri_white_dist) ; MRIfree(&mri_pial_dist) ; MRIfree(&mri_binary) ; return(mri_features) ; }
int main(int argc, char *argv[]) { char **av, *label_name, *vol_name, *out_name ; int ac, nargs ; int msec, minutes, seconds, i ; LABEL *area ; struct timeb start ; MRI *mri, *mri_seg ; Real xw, yw, zw, xv, yv, zv, val; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mri_label_vals.c,v 1.16 2015/08/24 18:22:05 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 < 3) usage_exit(1) ; vol_name = argv[1] ; label_name = argv[2] ; out_name = argv[3] ; mri = MRIread(vol_name) ; if (!mri) ErrorExit(ERROR_NOFILE, "%s: could not read volume from %s",Progname, vol_name) ; if (scaleup_flag) { float scale, fov_x, fov_y, fov_z ; scale = 1.0/MIN(MIN(mri->xsize, mri->ysize),mri->zsize) ; fprintf(stderr, "scaling voxel sizes up by %2.2f\n", scale) ; mri->xsize *= scale ; mri->ysize *= scale ; mri->zsize *= scale ; fov_x = mri->xsize * mri->width; fov_y = mri->ysize * mri->height; fov_z = mri->zsize * mri->depth; mri->xend = fov_x / 2.0; mri->xstart = -mri->xend; mri->yend = fov_y / 2.0; mri->ystart = -mri->yend; mri->zend = fov_z / 2.0; mri->zstart = -mri->zend; mri->fov = (fov_x > fov_y ? (fov_x > fov_z ? fov_x : fov_z) : (fov_y > fov_z ? fov_y : fov_z) ); } if (segmentation_flag >= 0) { int x, y, z ; VECTOR *v_seg, *v_mri ; MATRIX *m_seg_to_mri ; v_seg = VectorAlloc(4, MATRIX_REAL) ; v_mri = VectorAlloc(4, MATRIX_REAL) ; VECTOR_ELT(v_seg, 4) = 1.0 ; VECTOR_ELT(v_mri, 4) = 1.0 ; mri_seg = MRIread(argv[2]) ; if (!mri_seg) ErrorExit(ERROR_NOFILE, "%s: could not read volume from %s",Progname, argv[2]) ; if (erode) { MRI *mri_tmp ; mri_tmp = MRIclone(mri_seg, NULL) ; MRIcopyLabel(mri_seg, mri_tmp, segmentation_flag) ; while (erode-- > 0) MRIerode(mri_tmp, mri_tmp) ; MRIcopy(mri_tmp, mri_seg) ; MRIfree(&mri_tmp) ; } m_seg_to_mri = MRIgetVoxelToVoxelXform(mri_seg, mri) ; for (x = 0 ; x < mri_seg->width ; x++) { V3_X(v_seg) = x ; for (y = 0 ; y < mri_seg->height ; y++) { V3_Y(v_seg) = y ; for (z = 0 ; z < mri_seg->depth ; z++) { V3_Z(v_seg) = z ; if (MRIvox(mri_seg, x, y, z) == segmentation_flag) { MatrixMultiply(m_seg_to_mri, v_seg, v_mri) ; xv = V3_X(v_mri) ; yv = V3_Y(v_mri) ; zv = V3_Z(v_mri) ; MRIsampleVolumeType(mri, xv, yv, zv, &val, SAMPLE_NEAREST); #if 0 if (val < .000001) { val *= 1000000; printf("%f*0.000001\n", val); } else #endif if (coords) printf("%2.1f %2.1f %2.1f %f\n", xv, yv, zv, val); else printf("%f\n", val); } } } } MatrixFree(&m_seg_to_mri) ; VectorFree(&v_seg) ; VectorFree(&v_mri) ; } else { if (cras == 1) fprintf(stderr,"using the label coordinates to be c_(r,a,s) != 0.\n"); if (surface_dir) { MRI_SURFACE *mris ; char fname[STRLEN] ; sprintf(fname, "%s/%s.white", surface_dir, hemi) ; mris = MRISread(fname) ; if (mris == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s...\n", Progname,fname) ; sprintf(fname, "%s/%s.thickness", surface_dir, hemi) ; if (MRISreadCurvatureFile(mris, fname) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read thickness file %s...\n", Progname,fname) ; if (annot_prefix) /* read an annotation in and print vals in it */ { #define MAX_ANNOT 10000 int vno, annot_counts[MAX_ANNOT], index ; VERTEX *v ; Real xw, yw, zw, xv, yv, zv, val ; float annot_means[MAX_ANNOT] ; FILE *fp ; memset(annot_means, 0, sizeof(annot_means)) ; memset(annot_counts, 0, sizeof(annot_counts)) ; if (MRISreadAnnotation(mris, label_name) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read annotation file %s...\n", Progname,fname) ; if (mris->ct == NULL) ErrorExit(ERROR_BADPARM, "%s: annot file does not contain a color table, specifiy one with -t ", Progname); for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->ripflag) continue ; CTABfindAnnotation(mris->ct, v->annotation, &index) ; if (index >= 0 && index < mris->ct->nentries) { annot_counts[index]++ ; xw = v->x + v->curv*.5*v->nx ; yw = v->y + v->curv*.5*v->ny ; zw = v->z + v->curv*.5*v->nz ; if (cras == 1) MRIworldToVoxel(mri, xw, yw, zw, &xv, &yv, &zv) ; else MRIsurfaceRASToVoxel(mri, xw, yw, zw, &xv, &yv, &zv); MRIsampleVolume(mri, xv, yv, zv, &val) ; annot_means[index] += val ; sprintf(fname, "%s-%s-%s.dat", annot_prefix, hemi, mris->ct->entries[index]->name) ; fp = fopen(fname, "a") ; fprintf(fp, "%f\n", val) ; fclose(fp) ; } } } else /* read label in and print vals in it */ { area = LabelRead(NULL, label_name) ; if (!area) ErrorExit(ERROR_NOFILE, "%s: could not read label from %s",Progname, label_name) ; } } else { area = LabelRead(NULL, label_name) ; if (!area) ErrorExit(ERROR_NOFILE, "%s: could not read label from %s",Progname, label_name) ; for (i = 0 ; i < area->n_points ; i++) { xw = area->lv[i].x ; yw = area->lv[i].y ; zw = area->lv[i].z ; if (cras == 1) MRIworldToVoxel(mri, xw, yw, zw, &xv, &yv, &zv) ; else MRIsurfaceRASToVoxel(mri, xw, yw, zw, &xv, &yv, &zv); MRIsampleVolumeType(mri, xv, yv, zv, &val, SAMPLE_NEAREST); #if 0 if (val < .000001) { val *= 1000000; printf("%f*0.000001\n", val); } else #endif printf("%f\n", val); } } } msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; if (DIAG_VERBOSE_ON) fprintf(stderr, "label value extractiong took %d minutes and %d seconds.\n", minutes, seconds) ; exit(0) ; return(0) ; }
static int transform_T1_values_using_joint_pdf(MRI *mri_T1, char *jpdf_name, int invert) { int x, y, z, nbins, i, j, **jpdf, max_j, max_count ; Real T1 ; FILE *fp ; float fstep, fmin, fmax, val ; char line[STRLEN], *cp, var_name[STRLEN] ; fstep = 10 ; fmin = 10; fmax = 5000 ; nbins = 500 ; fp = fopen(jpdf_name, "r") ; if (fp == NULL) ErrorExit(ERROR_BADPARM, "%s: could not read joint pdf file %s\n", Progname, jpdf_name) ; while ((cp = fgetl(line, STRLEN-1, fp)) != NULL) { sscanf(cp, "%s = %f", var_name, &val) ; if (stricmp(var_name, "nbins") == 0) nbins = nint(val) ; else if (stricmp(var_name, "fmin") == 0) fmin = val ; else if (stricmp(var_name, "fmax") == 0) fmax = val ; else if (stricmp(var_name, "fstep") == 0) fstep = val ; else if (stricmp(var_name, "joint_density") == 0) break ; else break ; } printf("using nbins = %d, fmin = %2.1f, fmax = %2.1f, fstep = %2.1f\n", nbins, fmin, fmax, fstep) ; jpdf = (int **)calloc(nbins, sizeof(int *)) ; if (!jpdf) ErrorExit(ERROR_NOMEMORY, "%s: could not allocate %d x %d bin jpdf lookup table\n", Progname, nbins, nbins); for (i = 0 ; i < nbins ; i++) { jpdf[i] = (int *)calloc(nbins, sizeof(int)) ; if (!jpdf[i]) ErrorExit(ERROR_NOMEMORY, "%s: could not allocate %d x %d bin jpdf lookup table\n", Progname, nbins, nbins); for (j = 0; j < nbins ; j++) { if (fscanf(fp, "%d", &jpdf[i][j]) != 1) ErrorExit(ERROR_BADFILE, "%s: could not scan element %d, %d from %s", Progname, i, j, jpdf_name) ; } fscanf(fp, " ;\n") ; } fclose(fp) ; for (x = 0 ; x < mri_T1->width ; x++) { for (y = 0 ; y < mri_T1->height ; y++) { for (z = 0 ; z < mri_T1->depth ; z++) { if (x == Gx && y == Gy && z == Gz) DiagBreak() ; MRIsampleVolume(mri_T1, x, y, z, &T1) ; if (T1 <= 0) T1 = 1 ; if (invert) { i = nint((T1-fmin)/fstep) ; if (i >= nbins) i = nbins-1 ; if (i < 0) i = 0 ; max_count = jpdf[i][max_j=0] ; for (j = 1 ; j < nbins ; j++) { if (jpdf[i][j] > max_count) { max_count = jpdf[i][j] ; max_j = j ; } } } else { i = nint((T1-fmin)/fstep) ; if (i >= nbins) i = nbins-1 ; if (i < 0) i = 0 ; max_count = jpdf[max_j=0][i] ; for (j = 1 ; j < nbins ; j++) { if (jpdf[i][j] > max_count) { max_count = jpdf[j][i] ; max_j = j ; } } } T1 = max_j * fstep + fmin ; MRIsetVoxVal(mri_T1, x, y, z, 0, T1) ; } } } return(NO_ERROR) ; }
static MRI * MRIfindInnerBoundary(MRI *mri_src, MRI *mri_grad, MRI *mri_dst, float dist) { int x, y, z, nsamples ; double x1, y1, z1, dx, dy, dz, dot, d, inside, outside, norm, val, xc, yc, zc, wt ; if (mri_dst == NULL) mri_dst = MRIclone(mri_src, NULL) ; xc = yc = zc = 0.0 ; for (wt = 0.0, x = 0 ; x < mri_src->width ; x++) { for (y = 0 ; y < mri_src->height ; y++) { for (z = 0 ; z < mri_src->depth ; z++) { val = MRIgetVoxVal(mri_src, x, y, z, 0) ; xc += val*x ; yc += val*y ; zc += val*z ; wt += val ; } } } xc /= wt ; yc /= wt ; zc /= wt ; for (x = 0 ; x < mri_src->width ; x++) { for (y = 0 ; y < mri_src->height ; y++) { for (z = 0 ; z < mri_src->depth ; z++) { if (x == Gx && y == Gy && z == Gz) DiagBreak() ; dx = MRIgetVoxVal(mri_grad, x, y, z, 0) ; dy = MRIgetVoxVal(mri_grad, x, y, z, 1) ; dz = MRIgetVoxVal(mri_grad, x, y, z, 2) ; norm = sqrt(dx*dx + dy*dy + dz*dz) ; if (DZERO(norm)) continue ; dx /= norm ; dy /= norm ; dz /= norm ; dot = dx * (x-xc) + dy * (y-yc) + dz * (z-zc); if (dot < 0) continue ; for (nsamples = 0, outside = 0, d = .1 ; d < dist ; d += 0.1, nsamples++) { x1 = x+d*dx ; y1 = y+d*dy ; z1 = z+d*dz ; MRIsampleVolume(mri_src, x1, y1, z1, &val) ; outside += val ; } for (nsamples = 0, inside = 0, d = .1 ; d < dist ; d += 0.1, nsamples++) { x1 = x-d*dx ; y1 = y-d*dy ; z1 = z-d*dz ; MRIsampleVolume(mri_src, x1, y1, z1, &val) ; inside += val ; } outside /= nsamples ; inside /= nsamples ; MRIsetVoxVal(mri_dst, x, y, z, 0, outside-inside) ; } } } return(mri_dst) ; }
int main(int argc, char *argv[]) { char **av, *out_name ; int ac, nargs ; int msec, minutes, seconds ; struct timeb start ; MRI_SURFACE *mris ; GCA_MORPH *gcam ; MRI *mri = NULL ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_interpolate_warp.c,v 1.5 2011/10/07 12:07:26 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 < 3) { usage_exit(1) ; } /* note that a "forward" morph means a retraction, so we reverse the order of the argvs here. This means that for every voxel in the inflated image we have a vector that points to where in the original image it came from, and *NOT* the reverse. */ mris = MRISread(argv[2]) ; if (mris == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read source surface %s\n", Progname,argv[2]) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; if (MRISreadVertexPositions(mris, argv[1]) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read target surface %s\n", Progname,argv[1]) ; if (like_vol_name == NULL) { mri = MRIallocSequence(mris->vg.width, mris->vg.height, mris->vg.depth, MRI_FLOAT, 3) ; MRIcopyVolGeomToMRI(mri, &mris->vg) ; } else { MRI *mri_tmp ; mri_tmp = MRIread(like_vol_name) ; if (mri_tmp == NULL) { ErrorExit(ERROR_NOFILE, "%s: could not like volume %s\n", like_vol_name) ; } mri = MRIallocSequence(mri_tmp->width, mri_tmp->height, mri_tmp->depth, MRI_FLOAT, 3) ; MRIcopyHeader(mri_tmp, mri) ; MRIfree(&mri_tmp) ; } if (Gdiag & DIAG_SHOW && DIAG_VERBOSE_ON) { double xv, yv, zv ; VERTEX *v = &mris->vertices[0] ; MRISsurfaceRASToVoxel(mris, mri, v->x, v->y, v->z, &xv, &yv, &zv) ; printf("v 0: sras (%f, %f, %f) --> vox (%f, %f, %f)\n", v->x,v->y,v->z,xv,yv,zv); MRISsurfaceRASToVoxelCached(mris, mri, v->x, v->y, v->z, &xv, &yv, &zv) ; printf("v 0: sras (%f, %f, %f) --> vox (%f, %f, %f)\n", v->x,v->y,v->z,xv,yv,zv); DiagBreak() ; } { MRI *mri_tmp ; mri_tmp = expand_mri_to_fit_surface(mris, mri) ; MRIfree(&mri) ; mri = mri_tmp ; } write_surface_warp_into_volume(mris, mri, niter) ; if (Gdiag & DIAG_WRITE && DIAG_VERBOSE_ON) MRIwrite(mri, "warp.mgz") ; gcam = GCAMalloc(mri->width, mri->height, mri->depth) ; GCAMinitVolGeom(gcam, mri, mri) ; GCAMremoveSingularitiesAndReadWarpFromMRI(gcam, mri) ; // GCAMreadWarpFromMRI(gcam, mri) ; // GCAsetVolGeom(gca, &gcam->atlas); #if 0 gcam->gca = gcaAllocMax(1, 1, 1, mri->width, mri->height, mri->depth, 0, 0) ; GCAMinit(gcam, mri, NULL, NULL, 0) ; #endif #if 0 GCAMinvert(gcam, mri) ; GCAMwriteInverseWarpToMRI(gcam, mri) ; GCAMremoveSingularitiesAndReadWarpFromMRI(gcam, mri) ; // should be inverse now #endif if (mri_in) { MRI *mri_warped, *mri_tmp ; printf("applying warp to %s and writing to %s\n", mri_in->fname, out_fname) ; mri_tmp = MRIextractRegionAndPad(mri_in, NULL, NULL, pad) ; MRIfree(&mri_in) ; mri_in = mri_tmp ; mri_warped = GCAMmorphToAtlas(mri_in, gcam, NULL, -1, SAMPLE_TRILINEAR) ; MRIwrite(mri_warped, out_fname) ; if (Gdiag_no >= 0) { double xi, yi, zi, xo, yo, zo, val; int xp, yp, zp ; GCA_MORPH_NODE *gcamn ; VERTEX *v = &mris->vertices[Gdiag_no] ; MRISsurfaceRASToVoxelCached(mris, mri, v->origx, v->origy, v->origz, &xi, &yi, &zi) ; MRISsurfaceRASToVoxelCached(mris, mri, v->x, v->y, v->z, &xo, &yo, &zo) ; printf("surface vertex %d: inflated (%2.0f, %2.0f, %2.0f), orig (%2.0f, %2.0f, %2.0f)\n", Gdiag_no, xi, yi, zi, xo, yo, zo) ; MRIsampleVolume(mri_in, xo, yo, zo, &val) ; xp = nint(xi) ; yp = nint(yi) ; zp = nint(zi) ; gcamn = &gcam->nodes[xp][yp][zp] ; printf("warp = (%2.1f, %2.1f, %2.1f), orig (%2.1f %2.1f %2.1f) = %2.1f \n", gcamn->x, gcamn->y, gcamn->z, gcamn->origx, gcamn->origy, gcamn->origz,val) ; DiagBreak() ; } } if (no_write == 0) { out_name = argv[3] ; GCAMwrite(gcam, out_name) ; } msec = TimerStop(&start) ; seconds = nint((float)msec/1000.0f) ; minutes = seconds / 60 ; seconds = seconds % 60 ; fprintf(stderr, "warp field calculation took %d minutes and %d seconds.\n", minutes, seconds) ; exit(0) ; return(0) ; }
static int augment_thicknesses(FCD_DATA *fcd, MRI *mri_pvals, double min_dist, double max_dist, double thresh) { int h, vno ; VERTEX *v ; MRI_SURFACE *mris ; MRI *mri_thickness ; double nx, ny, nz, x0, y0, z0, d, x, y, z, val ; MRI_SEGMENTATION *mriseg ; mriseg = MRIsegment(mri_pvals, thresh, 1e10) ; MRIeraseSmallSegments(mriseg, mri_pvals, 20) ; MRIremoveSmallSegments(mriseg, 100) ; if (Gdiag & DIAG_WRITE) { MRIwrite(mri_pvals, "pvals.mgz") ; } MRIsegmentFree(&mriseg) ; for (h = 0 ; h <= 1 ; h++) // do each hemi { if (h == 0) // left hemi { mri_thickness = fcd->lh_thickness_on_lh ; mris = fcd->mris_lh ; } else // right hemi { mri_thickness = fcd->rh_thickness_on_rh ; mris = fcd->mris_rh ; } for (vno = 0 ; vno < mris->nvertices ; vno++) { if (vno == Gdiag_no) { DiagBreak() ; } v = &mris->vertices[vno] ; if (v->ripflag) { continue ; } MRISvertexNormalInVoxelCoords(mris, mri_pvals, vno, &nx, &ny, &nz) ; MRISvertexToVoxel(mris, v, mri_pvals, &x0, &y0, &z0) ; for (d = 0 ; d <= max_dist ; d += 0.5) { x = x0+d*nx ; y = y0+d*ny ; z = z0+d*nz ; MRIsampleVolume(mri_pvals, x, y, z, &val) ; if (val < thresh) { break ; } } if (d > min_dist) // a string of unlikely values { val = MRIgetVoxVal(mri_thickness, vno, 0, 0, 0) ; MRIsetVoxVal(mri_thickness, vno, 0, 0, 0, val+d) ; } } } return(NO_ERROR) ; }