/*------------------------------------------------------------ int CountLabelHits(): This constructs a mask only from the label as a way to count the number of functional voxels in the label itself. ------------------------------------------------------------*/ int CountLabelHits(MRI *SrcVol, MATRIX *Qsrc, MATRIX *Fsrc, MATRIX *Wsrc, MATRIX *Dsrc, MATRIX *Msrc2lbl, LABEL *Label, float labelfillthresh, int float2int) { MRI * LabelMskVol; int nlabelhits, nfinalhits; int r,c,s; float val; LabelMskVol = label2mask_linear(mSrcVol, Qsrc, Fsrc, Wsrc, Dsrc, NULL, Msrc2lbl, Label, labelfillthresh, float2int, &nlabelhits, &nfinalhits); nlabelhits = 0; for (r=0;r<LabelMskVol->height;r++) { for (c=0;c<LabelMskVol->width;c++) { for (s=0;s<LabelMskVol->depth;s++) { val = MRIFseq_vox(LabelMskVol,c,r,s,0); if (val > 0.5) nlabelhits ++; } } } MRIfree(&LabelMskVol); return(nlabelhits); }
// map mri data to image data, assuming same data type void VolumeFilter::MapMRIToVolume( MRI* mri, LayerMRI* layer ) { vtkImageData* image = layer->GetImageData(); int zX = mri->width; int zY = mri->height; int zZ = mri->depth; int zFrames = mri->nframes; for ( int nZ = 0; nZ < zZ; nZ++ ) { for ( int nY = 0; nY < zY; nY++ ) { for ( int nX = 0; nX < zX; nX++ ) { for ( int nFrame = 0; nFrame < zFrames; nFrame++ ) { switch ( mri->type ) { case MRI_UCHAR: image->SetScalarComponentFromDouble(nX, nY, nZ, nFrame, MRIseq_vox( mri, nX, nY, nZ, nFrame ) ); break; case MRI_INT: image->SetScalarComponentFromDouble(nX, nY, nZ, nFrame, MRIIseq_vox( mri, nX, nY, nZ, nFrame ) ); break; case MRI_LONG: image->SetScalarComponentFromDouble(nX, nY, nZ, nFrame, MRILseq_vox( mri, nX, nY, nZ, nFrame ) ); break; case MRI_FLOAT: image->SetScalarComponentFromDouble(nX, nY, nZ, nFrame, MRIFseq_vox( mri, nX, nY, nZ, nFrame ) ); break; case MRI_SHORT: image->SetScalarComponentFromDouble(nX, nY, nZ, nFrame, MRISseq_vox( mri, nX, nY, nZ, nFrame ) ); break; default: break; } } } } exec_progress_callback(nZ, zZ, 0, 1); } }
/*------------------------------------------------------*/ MRI *afniRead(const char *fname, int read_volume) { FILE *fp; char header_fname[STRLEN]; char *c; MRI *mri, *header; int big_endian_flag; long brik_file_length; long nvoxels; int bytes_per_voxel; int i, j, k; int swap_flag; AF af; float scaling = 1.; void *pmem = 0; float *pf; short *ps; unsigned char *pc; float det; float xfov, yfov, zfov; float fMin = 0.; float fMax = 0.; float flMin = 0.; float flMax = 0.; int initialized = 0; int frame; int bytes; strcpy(header_fname, fname); c = strrchr(header_fname, '.'); if (c == NULL) { errno = 0; ErrorReturn(NULL, (ERROR_BADPARM, "afniRead(): bad file name %s", fname)); } if (strcmp(c, ".BRIK") != 0) { errno = 0; ErrorReturn(NULL, (ERROR_BADPARM, "afniRead(): bad file name %s", fname)); } sprintf(c, ".HEAD"); if ((fp = fopen(header_fname, "r")) == NULL) { errno = 0; ErrorReturn(NULL, (ERROR_BADFILE, "afniRead(): error opening file %s", header_fname)); } // initialize AFNI structure AFinit(&af); // read header file if (!readAFNIHeader(fp, &af)) return (NULL); printAFNIHeader(&af); // well, we don't have time if (af.numtypes != 1) // should be the same as af.dataset_rank[1] = subbricks { errno = 0; // ErrorReturn(NULL, (ERROR_UNSUPPORTED, "afniRead(): nframes = %d (only 1 frame supported)", af.numtypes)); printf("INFO: number of frames dataset_rank[1] = %d : numtypes = %d \n", af.dataset_rank[1], af.numtypes); } // byteorder_string : required field if (strcmp(af.byteorder_string, "MSB_FIRST") == 0) big_endian_flag = 1; else if (strcmp(af.byteorder_string, "LSB_FIRST") == 0) big_endian_flag = 0; else { errno = 0; ErrorReturn(NULL, (ERROR_BADPARM, "read_afni_header(): unrecognized byte order string %s", af.byteorder_string)); } // brick_types : required field if (af.brick_types[0] == 2 // int || af.brick_types[0] > 3 )// 4 = double, 5 = complex, 6 = rgb { errno = 0; ErrorReturn(NULL, (ERROR_BADPARM, "afniRead(): unsupported data type %d, Must be 0 (byte), 1(short), or 3(float)", af.brick_types[0])); } ////////////////////////////////////////////////////////////////////////////////// // now we allocate space for MRI // dataset_dimensions : required field header = MRIallocHeader(af.dataset_dimensions[0], af.dataset_dimensions[1], af.dataset_dimensions[2], MRI_UCHAR, af.dataset_rank[1] ); // set number of frames header->nframes = af.dataset_rank[1]; // direction cosines (use orient_specific) // orient_specific : required field header->x_r = afni_orientations[af.orient_specific[0]][0]; header->x_a = afni_orientations[af.orient_specific[0]][1]; header->x_s = afni_orientations[af.orient_specific[0]][2]; header->y_r = afni_orientations[af.orient_specific[1]][0]; header->y_a = afni_orientations[af.orient_specific[1]][1]; header->y_s = afni_orientations[af.orient_specific[1]][2]; header->z_r = afni_orientations[af.orient_specific[2]][0]; header->z_a = afni_orientations[af.orient_specific[2]][1]; header->z_s = afni_orientations[af.orient_specific[2]][2]; /* --- quick determinant check --- */ det = + header->x_r * (header->y_a * header->z_s - header->z_a * header->y_s) - header->x_a * (header->y_r * header->z_s - header->z_r * header->y_s) + header->x_s * (header->y_r * header->z_a - header->z_r * header->y_a); if (det == 0) { MRIfree(&header); errno = 0; ErrorReturn(NULL, (ERROR_BADPARM, "read_afni_header(): error in orientations %d, %d, %d (direction cosine matrix has determinant zero)", af.orient_specific[0], af.orient_specific[1], af.orient_specific[2])); } // sizes use delta // delta : required field header->xsize = af.delta[0]; header->ysize = af.delta[1]; header->zsize = af.delta[2]; // uses origin // origin : required field header->c_r = header->x_r * (header->xsize * (header->width-1.0)/2.0 + af.origin[0]) + header->y_r * (header->ysize * (header->height-1.0)/2.0 + af.origin[1]) + header->z_r * (header->zsize * (header->depth-1.0)/2.0 + af.origin[2]); header->c_a = header->x_a * (header->xsize * (header->width-1.0)/2.0 + af.origin[0]) + header->y_a * (header->ysize * (header->height-1.0)/2.0 + af.origin[1]) + header->z_a * (header->zsize * (header->depth-1.0)/2.0 + af.origin[2]); header->c_s = header->x_s * (header->xsize * (header->width-1.0)/2.0 + af.origin[0]) + header->y_s * (header->ysize * (header->height-1.0)/2.0 + af.origin[1]) + header->z_s * (header->zsize * (header->depth-1.0)/2.0 + af.origin[2]); header->ras_good_flag = 1; if (header->xsize < 0) header->xsize = -header->xsize; if (header->ysize < 0) header->ysize = -header->ysize; if (header->zsize < 0) header->zsize = -header->zsize; header->imnr0 = 1; header->imnr1 = header->depth; header->ps = header->xsize; header->thick = header->zsize; header->xend = (header->width / 2.0) * header->xsize; header->xstart = -header->xend; header->yend = (header->height / 2.0) * header->ysize; header->ystart = -header->yend; header->zend = (header->depth / 2.0) * header->zsize; header->zstart = -header->zend; xfov = header->xend - header->xstart; yfov = header->yend - header->ystart; zfov = header->zend - header->zstart; header->fov = ( xfov > yfov ? (xfov > zfov ? xfov : zfov ) : (yfov > zfov ? yfov : zfov ) ); #if (BYTE_ORDER==LITTLE_ENDIAN) //#ifdef Linux swap_flag = big_endian_flag; #else swap_flag = !big_endian_flag; #endif if ((fp = fopen(fname, "r")) == NULL) { MRIfree(&header); errno = 0; ErrorReturn(NULL, (ERROR_BADFILE, "afniRead(): error opening file %s", header_fname)); } fseek(fp, 0, SEEK_END); brik_file_length = ftell(fp); fseek(fp, 0, SEEK_SET); // number of voxels consecutive nvoxels = header->width * header->height * header->depth * header->nframes; if (brik_file_length % nvoxels) { fclose(fp); MRIfree(&header); errno = 0; ErrorReturn(NULL, (ERROR_BADFILE, "afniRead(): BRIK file length (%d) is not divisible by the number of voxels (%d)", brik_file_length, nvoxels)); } // bytes_per_voxel = brik_file_length / nvoxels; // this assumes one frame bytes_per_voxel = af.brick_types[0] + 1; // 0(byte)-> 1, 1(short) -> 2, 3(float)->4 bytes = header->width*header->height*header->depth*bytes_per_voxel; // this check is for nframes = 1 case which is the one we are supporting if (bytes_per_voxel != brik_file_length/nvoxels) { fclose(fp); MRIfree(&header); errno = 0; ErrorReturn(NULL, (ERROR_UNSUPPORTED, "afniRead(): type info stored in header does not agree with the file size: %d != %d/%d", bytes_per_voxel, brik_file_length/nvoxels)); } // if brick_float_facs != 0 then we scale values to be float if (af.numfacs != 0 && af.brick_float_facs[0] != 0.) { header->type = MRI_FLOAT; scaling = af.brick_float_facs[0]; } else { if (bytes_per_voxel == 1) header->type = MRI_UCHAR; else if (bytes_per_voxel == 2) header->type = MRI_SHORT; else if (bytes_per_voxel == 4) header->type = MRI_FLOAT; else { fclose(fp); MRIfree(&header); errno = 0; ErrorReturn(NULL, (ERROR_UNSUPPORTED, "afniRead(): don't know what to do with %d bytes per voxel", bytes_per_voxel)); } } /////////////////////////////////////////////////////////////////////// if (read_volume) { // mri = MRIalloc(header->width, header->height, header->depth, header->type); mri = MRIallocSequence(header->width, header->height, header->depth, header->type, header->nframes) ; MRIcopyHeader(header, mri); for (frame = 0; frame < header->nframes; ++frame) { initialized = 0; for (k = 0;k < mri->depth;k++) { for (j = 0;j < mri->height;j++) { if (af.brick_float_facs[frame]) scaling = af.brick_float_facs[frame]; else scaling = 1.; { pmem = (void *) malloc(bytes_per_voxel*mri->width); if (pmem) { if (fread(pmem, bytes_per_voxel, mri->width, fp) != mri->width) { fclose(fp); MRIfree(&header); errno = 0; ErrorReturn(NULL, (ERROR_BADFILE, "afniRead(): error reading from file %s", fname)); } // swap bytes if (swap_flag) { if (bytes_per_voxel == 2) // short { swab(pmem, pmem, mri->width * 2); } else if (bytes_per_voxel == 4) // float { pf = (float *) pmem; for (i = 0;i < mri->width;i++, pf++) *pf = swapFloat(*pf); } } // now scaling if (bytes_per_voxel == 1) // byte { pc = (unsigned char *) pmem; for (i=0; i < mri->width; i++) { if (scaling == 1.) MRIseq_vox(mri, i, j, k, frame) = *pc; else MRIFseq_vox(mri, i, j, k, frame) = ((float) (*pc))*scaling; ++pc; } findMinMaxByte((unsigned char *) pmem, mri->width, &flMin, &flMax); } if (bytes_per_voxel == 2) // short { ps = (short *) pmem; for (i=0; i < mri->width; i++) { // if (*ps != 0) // printf("%d ", *ps); if (scaling == 1.) MRISseq_vox(mri, i, j, k, frame) = *ps; else MRIFseq_vox(mri, i, j, k, frame) = ((float) (*ps))*scaling; ++ps; } findMinMaxShort((short *) pmem, mri->width, &flMin, &flMax); } else if (bytes_per_voxel == 4) // float { pf = (float *) pmem; for (i=0; i < mri->width; i++) { MRIFseq_vox(mri, i, j, k, frame) = (*pf)*scaling; ++pf; } findMinMaxFloat((float *) pmem, mri->width, &flMin, &flMax); } free(pmem); // if (initialized == 0) { fMin = flMin; fMax = flMax; initialized = 1; } else { if (flMin < fMin) fMin = flMin; if (flMax > fMax) fMax = flMax; // printf("\n fmin =%f, fmax = %f, local min = %f, max = %f\n", fMin, fMax, flMin, flMax); } } else { fclose(fp); MRIfree(&header); errno = 0; ErrorReturn(NULL, (ERROR_BADFILE, "afniRead(): could not allocate memory for reading %s", fname)); } } } // height } // depth // valid only for nframs == 1 { printf("BRICK_STATS min = %f <--> actual min = %f\n", af.brick_stats[0+2*frame], fMin*scaling); printf("BRICK_STATS max = %f <--> actual max = %f\n", af.brick_stats[1+2*frame], fMax*scaling); } } // nframes } else // not reading volume mri = MRIcopy(header, NULL); strcpy(mri->fname, fname); fclose(fp); MRIfree(&header); AFclean(&af); return(mri); } /* end afniRead() */
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; }
MRI* VolumeFilter::CreateMRIFromVolume( LayerMRI* layer ) { vtkImageData* image = layer->GetImageData(); int mri_type = MRI_FLOAT; switch ( image->GetScalarType() ) { case VTK_CHAR: case VTK_SIGNED_CHAR: case VTK_UNSIGNED_CHAR: mri_type = MRI_UCHAR; break; case VTK_INT: case VTK_UNSIGNED_INT: mri_type = MRI_INT; break; case VTK_LONG: case VTK_UNSIGNED_LONG: mri_type = MRI_LONG; break; case VTK_SHORT: case VTK_UNSIGNED_SHORT: mri_type = MRI_SHORT; break; default: break; } int* dim = image->GetDimensions(); int zFrames = image->GetNumberOfScalarComponents(); MRI* mri = MRIallocSequence( dim[0], dim[1], dim[2], mri_type, zFrames ); if ( !mri ) { cerr << "Can not allocate memory for MRI.\n"; return NULL; } for ( int j = 0; j < mri->height; j++ ) { for ( int k = 0; k < mri->depth; k++ ) { for ( int i = 0; i < mri->width; i++ ) { for ( int nFrame = 0; nFrame < mri->nframes; nFrame++ ) { switch ( mri->type ) { case MRI_UCHAR: MRIseq_vox( mri, i, j, k, nFrame ) = (unsigned char)image->GetScalarComponentAsDouble(i, j, k, nFrame); break; case MRI_INT: MRIIseq_vox( mri, i, j, k, nFrame ) = (int)image->GetScalarComponentAsDouble(i, j, k, nFrame); break; case MRI_LONG: MRILseq_vox( mri, i, j, k, nFrame ) = (int)image->GetScalarComponentAsDouble(i, j, k, nFrame); break; case MRI_FLOAT: MRIFseq_vox( mri, i, j, k, nFrame ) = (int)image->GetScalarComponentAsDouble(i, j, k, nFrame); break; case MRI_SHORT: MRISseq_vox( mri, i, j, k, nFrame ) = (int)image->GetScalarComponentAsDouble(i, j, k, nFrame); break; default: break; } } } } exec_progress_callback(j, mri->height, 0, 1); } return mri; }
/*---------------------------------------------------------------*/ int main(int argc, char **argv) { int c,r,s,f; double val,rval; FILE *fp; MRI *mritmp; Progname = argv[0] ; argc --; argv++; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; /* assign default geometry */ cdircos[0] = 1.0; cdircos[1] = 0.0; cdircos[2] = 0.0; rdircos[0] = 0.0; rdircos[1] = 1.0; rdircos[2] = 0.0; sdircos[0] = 0.0; sdircos[1] = 0.0; sdircos[2] = 1.0; res[0] = 1.0; res[1] = 1.0; res[2] = 1.0; cras[0] = 0.0; cras[1] = 0.0; cras[2] = 0.0; res[3] = 2.0; /* TR */ if (argc == 0) usage_exit(); parse_commandline(argc, argv); check_options(); dump_options(stdout); if(tempid != NULL) { printf("INFO: reading template header\n"); if(! DoCurv) mritemp = MRIreadHeader(tempid,tempfmtid); else mritemp = MRIread(tempid); if (mritemp == NULL) { printf("ERROR: reading %s header\n",tempid); exit(1); } if(NewVoxSizeSpeced){ dim[0] = round(mritemp->width*mritemp->xsize/res[0]); dim[1] = round(mritemp->height*mritemp->ysize/res[1]); dim[2] = round(mritemp->depth*mritemp->zsize/res[2]); dim[3] = mritemp->nframes; res[3] = mritemp->tr; dimSpeced = 1; } if(dimSpeced){ mritmp = MRIallocSequence(dim[0],dim[1],dim[2],MRI_FLOAT,dim[3]); MRIcopyHeader(mritemp,mritmp); MRIfree(&mritemp); mritemp = mritmp; } if(resSpeced){ mritemp->xsize = res[0]; mritemp->ysize = res[1]; mritemp->zsize = res[2]; mritemp->tr = res[3]; } dim[0] = mritemp->width; dim[1] = mritemp->height; dim[2] = mritemp->depth; if (nframes > 0) dim[3] = nframes; else dim[3] = mritemp->nframes; mritemp->nframes = dim[3]; } if(mritemp) { if(SpikeTP >= mritemp->nframes){ printf("ERROR: SpikeTP = %d >= mritemp->nframes = %d\n", SpikeTP,mritemp->nframes); exit(1); } } printf("Synthesizing\n"); srand48(seed); if (strcmp(pdfname,"gaussian")==0) mri = MRIrandn(dim[0], dim[1], dim[2], dim[3], gausmean, gausstd, NULL); else if (strcmp(pdfname,"uniform")==0) mri = MRIdrand48(dim[0], dim[1], dim[2], dim[3], 0, 1, NULL); else if (strcmp(pdfname,"const")==0) mri = MRIconst(dim[0], dim[1], dim[2], dim[3], ValueA, NULL); else if (strcmp(pdfname,"sphere")==0) { if(voxradius < 0) voxradius = sqrt( pow(dim[0]/2.0,2)+pow(dim[1]/2.0,2)+pow(dim[2]/2.0,2) )/2.0; printf("voxradius = %lf\n",voxradius); mri = MRIsphereMask(dim[0], dim[1], dim[2], dim[3], dim[0]/2.0, dim[1]/2.0, dim[2]/2.0, voxradius, ValueA, NULL); } else if (strcmp(pdfname,"delta")==0) { mri = MRIconst(dim[0], dim[1], dim[2], dim[3], delta_off_value, NULL); if (delta_crsf_speced == 0) { delta_crsf[0] = dim[0]/2; delta_crsf[1] = dim[1]/2; delta_crsf[2] = dim[2]/2; delta_crsf[3] = dim[3]/2; } printf("delta set to %g at %d %d %d %d\n",delta_value,delta_crsf[0], delta_crsf[1],delta_crsf[2],delta_crsf[3]); MRIFseq_vox(mri, delta_crsf[0], delta_crsf[1], delta_crsf[2], delta_crsf[3]) = delta_value; } else if (strcmp(pdfname,"chi2")==0) { rfs = RFspecInit(seed,NULL); rfs->name = strcpyalloc("chi2"); rfs->params[0] = dendof; mri = MRIconst(dim[0], dim[1], dim[2], dim[3], 0, NULL); printf("Synthesizing chi2 with dof=%d\n",dendof); RFsynth(mri,rfs,NULL); } else if (strcmp(pdfname,"z")==0) { printf("Synthesizing z \n"); rfs = RFspecInit(seed,NULL); rfs->name = strcpyalloc("gaussian"); rfs->params[0] = 0; // mean rfs->params[1] = 1; // std mri = MRIconst(dim[0], dim[1], dim[2], dim[3], 0, NULL); RFsynth(mri,rfs,NULL); } else if (strcmp(pdfname,"t")==0) { printf("Synthesizing t with dof=%d\n",dendof); rfs = RFspecInit(seed,NULL); rfs->name = strcpyalloc("t"); rfs->params[0] = dendof; mri = MRIconst(dim[0], dim[1], dim[2], dim[3], 0, NULL); RFsynth(mri,rfs,NULL); } else if (strcmp(pdfname,"tr")==0) { printf("Synthesizing t with dof=%d as ratio of z/sqrt(chi2)\n",dendof); rfs = RFspecInit(seed,NULL); // numerator rfs->name = strcpyalloc("gaussian"); rfs->params[0] = 0; // mean rfs->params[1] = 1; // std mri = MRIconst(dim[0], dim[1], dim[2], dim[3], 0, NULL); RFsynth(mri,rfs,NULL); // denominator rfs->name = strcpyalloc("chi2"); rfs->params[0] = dendof; mri2 = MRIconst(dim[0], dim[1], dim[2], dim[3], 0, NULL); RFsynth(mri2,rfs,NULL); fMRIsqrt(mri2,mri2); // sqrt of chi2 mri = MRIdivide(mri,mri2,mri); MRIscalarMul(mri, mri, sqrt(dendof)) ; MRIfree(&mri2); } else if (strcmp(pdfname,"F")==0) { printf("Synthesizing F with num=%d den=%d\n",numdof,dendof); rfs = RFspecInit(seed,NULL); rfs->name = strcpyalloc("F"); rfs->params[0] = numdof; rfs->params[1] = dendof; mri = MRIconst(dim[0], dim[1], dim[2], dim[3], 0, NULL); RFsynth(mri,rfs,NULL); } else if (strcmp(pdfname,"Fr")==0) { printf("Synthesizing F with num=%d den=%d as ratio of two chi2\n", numdof,dendof); rfs = RFspecInit(seed,NULL); rfs->name = strcpyalloc("chi2"); // numerator rfs->params[0] = numdof; mri = MRIconst(dim[0], dim[1], dim[2], dim[3], 0, NULL); RFsynth(mri,rfs,NULL); // denominator rfs->params[0] = dendof; mri2 = MRIconst(dim[0], dim[1], dim[2], dim[3], 0, NULL); RFsynth(mri2,rfs,NULL); mri = MRIdivide(mri,mri2,mri); MRIscalarMul(mri, mri, (double)dendof/numdof) ; MRIfree(&mri2); } else if (strcmp(pdfname,"voxcrs")==0) { // three frames. 1st=col, 2nd=row, 3rd=slice printf("Filling with vox CRS\n"); mri = MRIconst(dim[0], dim[1], dim[2], 3, 0, NULL); for(c=0; c < mri->width; c ++){ for(r=0; r < mri->height; r ++){ for(s=0; s < mri->depth; s ++){ MRIsetVoxVal(mri,c,r,s,0,c); MRIsetVoxVal(mri,c,r,s,1,r); MRIsetVoxVal(mri,c,r,s,2,s); } } } } else if (strcmp(pdfname,"boundingbox")==0) { printf("Setting bounding box \n"); if(mritemp == NULL) mritemp = MRIconst(dim[0], dim[1], dim[2], dim[3], 0, NULL); mri = MRIsetBoundingBox(mritemp,&boundingbox,ValueA,ValueB); if(!mri) exit(1); } else if (strcmp(pdfname,"checker")==0) { printf("Checker \n"); mri=MRIchecker(mritemp,NULL); if(!mri) exit(1); } else if (strcmp(pdfname,"sliceno")==0) { printf("SliceNo \n"); if(mritemp == NULL){ printf("ERROR: need --temp with sliceno\n"); exit(1); } mri=MRIsliceNo(mritemp,NULL); if(!mri) exit(1); } else if (strcmp(pdfname,"indexno")==0) { printf("IndexNo \n"); if(mritemp == NULL){ printf("ERROR: need --temp with indexno\n"); exit(1); } mri=MRIindexNo(mritemp,NULL); if(!mri) exit(1); } else if (strcmp(pdfname,"crs")==0) { printf("CRS \n"); if(mritemp == NULL){ printf("ERROR: need --temp with crs\n"); exit(1); } mri=MRIcrs(mritemp,NULL); if(!mri) exit(1); } else { printf("ERROR: pdf %s unrecognized, must be gaussian, uniform,\n" "const, delta, checker\n", pdfname); exit(1); } if (tempid != NULL) { MRIcopyHeader(mritemp,mri); mri->type = MRI_FLOAT; // Override if(nframes > 0) mri->nframes = nframes; if(TR > 0) mri->tr = TR; } else { if(mri == NULL) { usage_exit(); } mri->xsize = res[0]; mri->ysize = res[1]; mri->zsize = res[2]; mri->tr = res[3]; mri->x_r = cdircos[0]; mri->x_a = cdircos[1]; mri->x_s = cdircos[2]; mri->y_r = rdircos[0]; mri->y_a = rdircos[1]; mri->y_s = rdircos[2]; mri->z_r = sdircos[0]; mri->z_a = sdircos[1]; mri->z_s = sdircos[2]; if(!usep0){ mri->c_r = cras[0]; mri->c_a = cras[1]; mri->c_s = cras[2]; } else MRIp0ToCRAS(mri, p0[0], p0[1], p0[2]); } if (gstd > 0) { if(!UseFFT){ printf("Smoothing\n"); MRIgaussianSmooth(mri, gstd, gmnnorm, mri); /* gmnnorm = 1 = normalize */ } else { printf("Smoothing with FFT \n"); mri2 = MRIcopy(mri,NULL); mri = MRI_fft_gaussian(mri2, mri, gstd, gmnnorm); /* gmnnorm = 1 = normalize */ } if (rescale) { printf("Rescaling\n"); if (strcmp(pdfname,"z")==0) RFrescale(mri,rfs,NULL,mri); if (strcmp(pdfname,"chi2")==0) RFrescale(mri,rfs,NULL,mri); if (strcmp(pdfname,"t")==0) RFrescale(mri,rfs,NULL,mri); if (strcmp(pdfname,"tr")==0) RFrescale(mri,rfs,NULL,mri); if (strcmp(pdfname,"F")==0) RFrescale(mri,rfs,NULL,mri); if (strcmp(pdfname,"Fr")==0) RFrescale(mri,rfs,NULL,mri); } } if(DoHSC){ // This multiplies each frame by a random number // between HSCMin HSCMax to simulate heteroscedastisity printf("Applying HSC %lf %lf\n",HSCMin,HSCMax); for(f=0; f < mri->nframes; f++){ rval = (HSCMax-HSCMin)*drand48() + HSCMin; if(debug) printf("%3d %lf\n",f,rval); for(c=0; c < mri->width; c ++){ for(r=0; r < mri->height; r ++){ for(s=0; s < mri->depth; s ++){ val = MRIgetVoxVal(mri,c,r,s,f); MRIsetVoxVal(mri,c,r,s,f,rval*val); } } } } } if(AddOffset) { printf("Adding offset\n"); offset = MRIread(tempid); if(offset == NULL) exit(1); if(OffsetFrame == -1) OffsetFrame = nint(offset->nframes/2); printf("Offset frame %d\n",OffsetFrame); mritmp = fMRIframe(offset, OffsetFrame, NULL); if(mritmp == NULL) exit(1); MRIfree(&offset); offset = mritmp; fMRIaddOffset(mri, offset, NULL, mri); } if(SpikeTP > 0){ printf("Spiking time point %d\n",SpikeTP); for(c=0; c < mri->width; c ++){ for(r=0; r < mri->height; r ++){ for(s=0; s < mri->depth; s ++){ MRIsetVoxVal(mri,c,r,s,SpikeTP,1e9); } } } } if(DoAbs){ printf("Computing absolute value\n"); MRIabs(mri,mri); } if(!NoOutput){ printf("Saving\n"); if(!DoCurv) MRIwriteAnyFormat(mri,volid,volfmt,-1,NULL); else { printf("Saving in curv format\n"); MRIScopyMRI(surf, mri, 0, "curv"); MRISwriteCurvature(surf,volid); } } if(sum2file){ val = MRIsum2All(mri); fp = fopen(sum2file,"w"); if(fp == NULL){ printf("ERROR: opening %s\n",sum2file); exit(1); } printf("sum2all: %20.10lf\n",val); printf("vrf: %20.10lf\n",1/val); fprintf(fp,"%20.10lf\n",val); } return(0); }
/*---------------------------------------------------------*/ int main(int argc, char **argv) { int n,err, f, nhits, r,c,s; float ipr, bpr, intensity; float *framepower=NULL, val; LTA *lta; int nargs; //int endian,roitype; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mri_vol2roi.c,v 1.32 2011/03/02 00:04:25 nicks Exp $", "$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(); parse_commandline(argc, argv); check_options(); printf("--------------------------------------------------------\n"); getcwd(tmpstr,2000); printf("%s\n",tmpstr); printf("%s\n",Progname); for (n=0;n<argc;n++) printf(" %s",argv[n]); printf("\n"); printf("version %s\n",vcid); printf("--------------------------------------------------------\n"); dump_options(stdout); /* --------- load in the (possibly 4-D) source volume --------------*/ printf("Loading volume %s ...",srcvolid); mSrcVol = MRIread(srcvolid); if(mSrcVol == NULL) exit(1); printf("done\n"); /* Dsrc: read the source registration file */ if (srcregfile != NULL) { err = regio_read_register(srcregfile, &srcsubject, &ipr, &bpr, &intensity, &Dsrc, &float2int_src); if (err) exit(1); printf("srcreg Dsrc -------------\n"); MatrixPrint(stdout,Dsrc); printf("----------------------------------\n"); } else Dsrc = NULL; /* Wsrc: Get the source warping Transform */ Wsrc = NULL; /* Fsrc: Get the source FOV registration matrix */ Fsrc = NULL; /* Qsrc: Compute the quantization matrix for src volume */ Qsrc = FOVQuantMatrix(mSrcVol->width, mSrcVol->height, mSrcVol->depth, mSrcVol->xsize, mSrcVol->ysize, mSrcVol->zsize); printf("ras2vox src (tkreg) Qsrc -------------\n"); MatrixPrint(stdout,Qsrc); printf("----------------------------------\n"); /* ----------- load in the label ----------------- */ if (labelfile != NULL) { Label = LabelReadFile(labelfile); if (Label == NULL) exit(1); /* load in the source-to-label registration */ if (src2lblregfile != NULL) { //err = regio_read_xfm(src2lblregfile, &Msrc2lbl); //if(err) exit(1); lta = LTAread(src2lblregfile); if (lta->type == LINEAR_VOX_TO_VOX) { printf("INFO: converting LTA to RAS\n"); LTAvoxelTransformToCoronalRasTransform(lta); } Msrc2lbl = MatrixCopy(lta->xforms[0].m_L,NULL); } else if (labeltal) { /* Load the talairach.xfm and make it approp for reg.dat*/ Msrc2lbl = DevolveXFM(srcsubject,NULL,talxfm); if (Msrc2lbl==NULL) exit(1); } else Msrc2lbl = NULL; if (Msrc2lbl != NULL) { printf("-- Source2Label %s ---- \n",src2lblregfile); MatrixPrint(stdout,Msrc2lbl); printf("-------------------------------\n"); } } else { Label = NULL; Msrc2lbl = NULL; } /* -------------- load mask volume stuff -----------------------------*/ if (mskvolid != NULL) { /* load the mask volume (single frame) */ printf("Reading %s\n",mskvolid); mMskVol = MRIread(mskvolid); if(mMskVol == NULL) exit(1); if(mskframe > 0){ mritmp = fMRIframe(mMskVol, mskframe, NULL); if(mritmp == NULL) exit(1); MRIfree(&mMskVol); mMskVol = mritmp; } /* Qmsk: Compute the quantization matrix for msk volume */ /* crsFOV = Qmsk*xyzFOV */ Qmsk = FOVQuantMatrix(mMskVol->width, mMskVol->height, mMskVol->depth, mMskVol->xsize, mMskVol->ysize, mMskVol->zsize); /* get the mask2source registration information */ /* xyzSrc = Mmsk2src * xyzMsk */ if (msk2srcregfile != NULL) { err = regio_read_mincxfm(msk2srcregfile, &Mmsk2src, NULL); if (err) exit(1); } else Mmsk2src = NULL; /* convert from Mask Anatomical to Src FOV */ if (!msksamesrc) { mSrcMskVol = vol2vol_linear(mMskVol, Qmsk, NULL, NULL, Dmsk, Qsrc, Fsrc, Wsrc, Dsrc, mSrcVol->height, mSrcVol->width, mSrcVol->depth, Mmsk2src, INTERP_NEAREST, float2int_msk); if (mSrcMskVol == NULL) exit(1); } else mSrcMskVol = mMskVol; /* binarize the mask volume */ mri_binarize(mSrcMskVol, mskthresh, msktail, mskinvert, mSrcMskVol, &nmskhits); } else { mSrcMskVol = NULL; nmskhits = 0; } /*-------------- Done loading mask stuff -------------------------*/ /* If this is a statistical volume, raise each frame to it's appropriate power (eg, stddev needs to be squared)*/ if (is_sxa_volume(srcvolid)) { printf("INFO: Source volume detected as selxavg format\n"); sxa = ld_sxadat_from_stem(srcvolid); if (sxa == NULL) exit(1); framepower = sxa_framepower(sxa,&f); if (f != mSrcVol->nframes) { fprintf(stderr," number of frames is incorrect (%d,%d)\n", f,mSrcVol->nframes); exit(1); } printf("INFO: Adjusting Frame Power\n"); fflush(stdout); mri_framepower(mSrcVol,framepower); } /*--------- Prepare the final mask ------------------------*/ if (Label != NULL) { mFinalMskVol = label2mask_linear(mSrcVol, Qsrc, Fsrc, Wsrc, Dsrc, mSrcMskVol, Msrc2lbl, Label, labelfillthresh, float2int_src, &nlabelhits, &nfinalhits); if (mFinalMskVol == NULL) exit(1); } else { mFinalMskVol = mSrcMskVol; nfinalhits = nmskhits; } if (!oldtxtstyle) { /* count the number of functional voxels = 1 in the mask */ nfinalhits = 0; for (r=0;r<mFinalMskVol->height;r++) { for (c=0;c<mFinalMskVol->width;c++) { for (s=0;s<mFinalMskVol->depth;s++) { val = MRIgetVoxVal(mFinalMskVol,c,r,s,0); if (val > 0.5) nfinalhits ++; } } } if (Label != NULL) nlabelhits = CountLabelHits(mSrcVol, Qsrc, Fsrc, Wsrc, Dsrc, Msrc2lbl, Label, labelfillthresh,float2int_src); else nlabelhits = 0; } /*-------------------------------------------------------*/ /*--------- Map the volume into the ROI -----------------*/ printf("Averging over ROI\n"); fflush(stdout); mROI = vol2maskavg(mSrcVol, mFinalMskVol,&nhits); if (mROI == NULL) exit(1); printf("Done averging over ROI (nhits = %d)\n",nhits); /*-------------------------------------------------------*/ /* ------- Save the final mask ------------------ */ if (finalmskvolid != 0) { //mri_save_as_bvolume(mFinalMskVol,finalmskvolid,endian,BF_FLOAT); //MRIwriteAnyFormat(mFinalMskVol,finalmskvolid,"bfloat",-1,NULL); sprintf(tmpstr,"%s.%s",finalmskvolid,outext); MRIwrite(mFinalMskVol,tmpstr); } /* ------- Save CRS of the the final mask ------------------ */ if (finalmskcrs != NULL) { fp = fopen(finalmskcrs,"w"); if (fp==NULL) { fprintf(stderr,"ERROR: cannot open %s\n",finalmskcrs); exit(1); } for (r=0;r<mFinalMskVol->height;r++) { for (c=0;c<mFinalMskVol->width;c++) { for (s=0;s<mFinalMskVol->depth;s++) { val = MRIgetVoxVal(mFinalMskVol,c,r,s,0); if (val > 0.5) { fprintf(fp,"%d %d %d\n",c,r,s); } } } } fclose(fp); } /* If this is a statistical volume, lower each frame to it's appropriate power (eg, variance needs to be sqrt'ed) */ if (is_sxa_volume(srcvolid)) { printf("INFO: Readjusting Frame Power\n"); fflush(stdout); for (f=0; f < mROI->nframes; f++) framepower[f] = 1.0/framepower[f]; mri_framepower(mROI,framepower); } /* save the target volume in an appropriate format */ if(roifile != NULL){ sprintf(tmpstr,"%s.%s",roifile,outext); MRIwrite(mROI,tmpstr); /* for a stat volume, save the .dat file */ if (is_sxa_volume(srcvolid)) { sxa->nrows = 1; sxa->ncols = 1; sv_sxadat_by_stem(sxa,roifile); } } /* save as text */ if(roitxtfile != NULL) { fp = fopen(roitxtfile,"w"); if (fp==NULL) { fprintf(stderr,"ERROR: cannot open %s\n",roitxtfile); exit(1); } if (oldtxtstyle) { printf("INFO: saving as old style txt\n"); fprintf(fp,"%d \n",nmskhits); } if (! plaintxtstyle ) { fprintf(fp,"%d \n",nlabelhits); fprintf(fp,"%d \n",nfinalhits); } for (f=0; f < mROI->nframes; f++) fprintf(fp,"%f\n",MRIgetVoxVal(mROI,0,0,0,f)); fclose(fp); } /* ------- Mask the source and save it ------------------ */ if (srcmskvolid != 0) { for (r=0;r<mFinalMskVol->height;r++) { for (c=0;c<mFinalMskVol->width;c++) { for (s=0;s<mFinalMskVol->depth;s++) { val = MRIgetVoxVal(mFinalMskVol,c,r,s,0); if (val < 0.5) { for (f=0; f < mROI->nframes; f++) MRIFseq_vox(mSrcVol,c,r,s,f) = 0.0; } } } } MRIwrite(mSrcVol,srcmskvolid); } /* ------- Save as a text list ------------------ */ if (ListFile != 0) { fp = fopen(ListFile,"w"); for (c=0;c<mFinalMskVol->width;c++) { for (r=0;r<mFinalMskVol->height;r++) { for (s=0;s<mFinalMskVol->depth;s++) { val = MRIFseq_vox(mFinalMskVol,c,r,s,0); if(val < 0.5) continue; fprintf(fp,"%3d %3d %3d ",c,r,s); for (f=0; f < mROI->nframes; f++){ val = MRIgetVoxVal(mSrcVol,c,r,s,f); fprintf(fp,"%f ",val); } fprintf(fp,"\n"); } } } fclose(fp); } return(0); }