VImage VImageManager::vtimestep( VAttrList list, VImage dest, int step ) { VFillImage( dest, VAllBands, 0 ); VPointer src_pp = NULL; VPointer dest_pp = NULL; VAttrListPosn posn; VShort *ptr1 = NULL; VShort *ptr2 = NULL; VString str; VImage src; int n = 0; int npixels = 0; bool revert = false; for ( VFirstAttr ( list, & posn ); VAttrExists ( & posn ); VNextAttr ( & posn ) ) { if ( VGetAttrRepn ( & posn ) != VImageRepn ) continue; VGetAttrValue ( &posn, NULL, VImageRepn, &src ); if ( VPixelRepn( src ) != VShortRepn ) continue; if ( ( VGetAttr ( VImageAttrList ( src ), "MPIL_vista_0", NULL, VStringRepn, ( VPointer ) & str ) != VAttrFound ) && ( VGetAttr ( VImageAttrList ( src ), "repetition_time", NULL, VStringRepn, ( VPointer ) & str ) != VAttrFound ) ) continue; /* doch nicht rumdrehen! if (VGetAttr (VImageAttrList (src), "extent", NULL, VStringRepn, (VPointer) & str) != VAttrFound) revert = true; */ if ( n == 0 ) VCopyImageAttrs ( src, dest ); if ( VImageNRows( src ) > 1 ) { if ( VSelectBand ( "vtimestep", src, step, &npixels, &src_pp ) == FALSE ) VError( "err reading data" ); int destBand = ( revert ) ? VImageNBands( dest ) - n - 1 : n; dest_pp = VPixelPtr( dest, destBand, 0, 0 ); ptr1 = ( VShort * ) src_pp; ptr2 = ( VShort * ) dest_pp; memcpy ( ptr2, ptr1, npixels * sizeof( VShort ) ); } n++; } return dest; }
VBandInterp VImageColorInterp (VImage image) { VLong interp; VGetAttrResult result; if (VImageNBands (image) != (VImageNFrames (image) * VImageNViewpoints (image) * VImageNColors (image) * VImageNComponents (image))) VWarning ("VImageColorInterp: No. bands (%d) conflicts with no. " "of frames, etc. (%d %d %d %d)", VImageNBands (image), VImageNFrames (image), VImageNViewpoints (image), VImageNColors (image), VImageNComponents (image)); if (! VImageAttrList (image) || (result = VGetAttr (VImageAttrList (image), VColorInterpAttr, VBandInterpDict, VLongRepn, & interp)) == VAttrMissing) return VImageNColors (image) > 1 ? VBandInterpOther : VBandInterpNone; if (result == VAttrBadValue) return VBandInterpOther; switch (interp) { case VBandInterpRGB: if (VImageNColors (image) != 3) { VWarning ("VBandColorInterp: RGB image has %d color dimension(s)", VImageNColors (image)); return VBandInterpOther; } return VBandInterpRGB; } return VBandInterpOther; }
const char *CVImage::getStringAttrib( const char *name, const VImage image ) { char *value; VGetAttrResult vresult = VGetAttr( VImageAttrList( image ), name, NULL, VStringRepn, &value ); if( vresult != VAttrFound )return NULL; else return value; }
const float CVImage::getFloatAttrib( const char *name, VImage image ) { float value; VGetAttrResult vresult = VGetAttr( VImageAttrList( image ), name, NULL, VFloatRepn, &value ); if( vresult != VAttrFound )return 0; else return value; }
const unsigned long CVImage::getNumberAttrib( const char *name, VImage image ) { long value; VGetAttrResult vresult = VGetAttr( VImageAttrList( image ), name, NULL, VLongRepn, &value ); if( vresult != VAttrFound )return 0; else return value; }
VGraph VGraphHemi(VGraph src,VLong hemi) { VString str; VGraph dest=NULL; int i,j,n,ncols,half=0; float x,y,z,val=0; VNode node,node1; int *table; half = 80; /* standard coordinate for inter-hemisperic cleft */ if (VGetAttr (VGraphAttrList (src), "ca", NULL,VStringRepn, (VPointer) & str) == VAttrFound) { sscanf(str,"%f %f %f",&x,&y,&z); ncols = (int) x; half = ncols/2; } table = (int *) VMalloc(sizeof(int) * (src)->lastUsed + 1); for (i=0; i<= src->lastUsed; i++) table[i] = 0; n = (src)->lastUsed/2; dest = VCreateGraph(n,VGraphNFields(src),VNodeRepn(src),src->useWeights); for (i=1; i<=(src)->lastUsed; i++) { node = (VNode) VGraphGetNode (src,i); if (node == 0) continue; VReadNode((src),node,&x,&y,&z,&val); if ((hemi == 0 && x <= half) || (hemi == 1 && x > half)) { n = VGraphAddNode(dest,(VNode) node); table[i] = n; } } for (i=1; i<=(src)->lastUsed; i++) { node = (VNode) VGraphGetNode (src,i); if (node == 0) continue; for (j=1; j<=(src)->lastUsed; j++) { node1 = (VNode) VGraphGetNode (src,j); if (node1 == 0) continue; if (table[i] == 0 || table[j] == 0) continue; if (VGraphIsAdjacent(src,i,j) == TRUE) { VGraphLinkNodes(dest,table[i],table[j]); } } } VFree(table); return dest; }
void VROIpaired_ttest(VImage *src1, VImage *src2, VImage *mask, int n, int nmask, FILE *fp) { VString str; int i, j, id, b, r, c, c0, c1, nROI = 0; float ave1 = 0, ave2 = 0, var1 = 0, var2 = 0; float sum1 = 0, sum2 = 0, u, mx; float t, z, p, df, sd, cov, *data1 = NULL, *data2 = NULL; float tiny = 1.0e-8; float xa, ya, za, xx, yy, zz, voxelsize = 1; double mean[3]; Volumes *volumes; Volume vol; VTrack tc; VBoolean found = FALSE; gsl_set_error_handler_off(); /* ** get ROIs from mask */ fprintf(stderr, "\n List of ROIs:\n"); fprintf(fp, "\n List of ROIs:\n"); volumes = (Volumes *) VCalloc(nmask, sizeof(Volumes)); nROI = 0; for(i = 0; i < nmask; i++) { fprintf(stderr, "\n Mask %2d:\n", i + 1); fprintf(fp, "\n Mask %2d:\n", i + 1); volumes[i] = VImage2Volumes(mask[i]); voxelsize = 1; if(VGetAttr(VImageAttrList(mask[i]), "voxel", NULL, VStringRepn, (VPointer) & str) == VAttrFound) { sscanf(str, "%f %f %f", &xa, &ya, &za); voxelsize = xa * ya * za; } fprintf(stderr, " ROI addr size(mm^3)\n"); fprintf(stderr, "-----------------------------------------------\n"); fprintf(fp, " ROI addr size(mm^3)\n"); fprintf(fp, "-----------------------------------------------\n"); for(vol = volumes[i]->first; vol != NULL; vol = vol->next) { VolumeCentroid(vol, mean); if(nROI < vol->label) nROI = vol->label; b = mean[0]; r = mean[1]; c = mean[2]; xx = mean[2]; yy = mean[1]; zz = mean[0]; VGetTalCoord(src1[0], zz, yy, xx, &xa, &ya, &za); id = vol->label; fprintf(stderr, " %2d %7.2f %7.2f %7.2f %7.0f\n", id, xa, ya, za, voxelsize *(double)VolumeSize(vol)); fprintf(fp, " %2d %7.2f %7.2f %7.2f %7.0f\n", id, xa, ya, za, voxelsize *(double)VolumeSize(vol)); } } fprintf(stderr, "\n\n"); fprintf(fp, "\n\n"); /* check consistency */ if(nROI < 1) VError(" no ROIs found"); CheckROI(volumes, nmask, nROI); /* ** process each ROI */ fprintf(stderr, "\n"); fprintf(stderr, " ROI mean t z p \n"); fprintf(stderr, " --------------------------------------------------\n"); if(fp) { fprintf(fp, "\n"); fprintf(fp, " ROI mean t z p \n"); fprintf(fp, " --------------------------------------------------\n"); } df = n - 1; data1 = (float *) VCalloc(n, sizeof(float)); data2 = (float *) VCalloc(n, sizeof(float)); for(id = 1; id <= nROI; id++) { for(i = 0; i < n; i++) { j = 0; if(nmask > 1) j = i; found = FALSE; for(vol = volumes[j]->first; vol != NULL; vol = vol->next) { if(vol->label != id) continue; found = TRUE; sum1 = sum2 = mx = 0; for(j = 0; j < VolumeNBuckets(vol); j++) { for(tc = VFirstTrack(vol, j); VTrackExists(tc); tc = VNextTrack(tc)) { b = tc->band; r = tc->row; c0 = tc->col; c1 = c0 + tc->length; for(c = c0; c < c1; c++) { u = VPixel(src1[i], b, r, c, VFloat); if(ABS(u) < tiny) continue; sum1 += u; u = VPixel(src2[i], b, r, c, VFloat); if(ABS(u) < tiny) continue; sum2 += u; mx++; } } } if(mx < 1) { VWarning(" no voxels in ROI %d of mask %d", id, i); data1[i] = data2[i] = 0; continue; } data1[i] = sum1 / mx; data2[i] = sum2 / mx; } if(!found) goto next; } avevar(data1, n, &ave1, &var1); avevar(data2, n, &ave2, &var2); if(var1 < tiny || var2 < tiny) { VWarning(" no variance in ROI %d", id); continue; } z = t = p = 0; cov = 0; for(j = 0; j < n; j++) cov += (data1[j] - ave1) * (data2[j] - ave2); cov /= df; sd = sqrt((var1 + var2 - 2.0 * cov) / (df + 1.0)); if(sd < tiny) continue; t = (ave1 - ave2) / sd; if(ABS(t) < tiny) p = z = 0; else { p = t2p((double)t, (double)df); z = t2z((double)t, (double)df); if(t < 0) z = -z; } fprintf(stderr, " %3d %8.4f (%.3f) %7.3f %7.3f %7.4f\n", id, (ave1 - ave2), sd, t, z, p); if(fp) fprintf(fp, " %3d %8.4f (%.3f) %7.3f %7.3f %7.4f\n", id, (ave1 - ave2), sd, t, z, p); next: ; } fprintf(stderr, "\n"); if(fp) { fprintf(fp, "\n"); fclose(fp); } }
/* ** slicetime correction with constant TR */ void VSlicetime(VAttrList list, VShort minval, VFloat tdel, VBoolean slicetime_correction, float *onset_array) { VImage src; VAttrListPosn posn; VString str, buf; int b, r, c, i, nt, mt, val, del = 0; double xmin, xmax, sum, nx; double *xx = NULL, *yy = NULL, xi, yi, tr = 0, xtr = 0, slicetime = 0; gsl_interp_accel *acc = NULL; gsl_spline *spline = NULL; xmin = (VShort) VRepnMinValue(VShortRepn); xmax = (VShort) VRepnMaxValue(VShortRepn); buf = VMalloc(256); /* ** process data */ b = -1; for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); if(VPixelRepn(src) != VShortRepn) continue; b++; if(VImageNRows(src) < 2) continue; /* ** get header info */ if(VGetAttr(VImageAttrList(src), "slice_time", NULL, VDoubleRepn, (VPointer) & slicetime) != VAttrFound && slicetime_correction && onset_array == NULL) VError(" 'slice_time' info missing");; if(onset_array != NULL) slicetime = onset_array[b]; tr = 0; if(VGetAttr(VImageAttrList(src), "repetition_time", NULL, VDoubleRepn, (VPointer) & tr) != VAttrFound) { tr = 0; if(VGetAttr(VImageAttrList(src), "MPIL_vista_0", NULL, VStringRepn, (VPointer) & str) == VAttrFound) { sscanf(str, " repetition_time=%lf %s", &tr, buf); } } if(tr < 1) VError(" attribute 'repetition_time' missing"); xtr = tr / 1000.0; del = (int)(tdel / xtr + 0.5); /* num timesteps to be ignored */ nt = VImageNBands(src); if(acc == NULL) { acc = gsl_interp_accel_alloc(); spline = gsl_spline_alloc(gsl_interp_akima, nt); xx = (double *) VCalloc(nt, sizeof(double)); yy = (double *) VCalloc(nt, sizeof(double)); fprintf(stderr, " The first %.2f secs (%d timesteps) will be replaced.\n", tdel, del); } /* ** loop through all voxels in current slice */ if(slicetime_correction) fprintf(stderr, " slice: %3d, %10.3f ms, TR: %.3f\r", b, slicetime, xtr); for(r = 0; r < VImageNRows(src); r++) { for(c = 0; c < VImageNColumns(src); c++) { if(VPixel(src, 0, r, c, VShort) < minval) continue; /* replace first few time steps by average */ if(del > 0) { mt = del + 10; if(mt > nt) mt = nt; sum = nx = 0; for(i = del; i < mt; i++) { sum += VPixel(src, i, r, c, VShort); nx++; } if(nx < 1) continue; val = sum / nx; for(i = 0; i < del; i++) { VPixel(src, i, r, c, VShort) = val; } } if(!slicetime_correction) continue; /* correct for slicetime offsets using cubic spline interpolation */ for(i = 0; i < nt; i++) { xi = i; xx[i] = xi * tr; yy[i] = VPixel(src, i, r, c, VShort); } gsl_spline_init(spline, xx, yy, nt); for(i = 1; i < nt; i++) { xi = xx[i] - slicetime; yi = gsl_spline_eval(spline, xi, acc); val = (int)(yi + 0.49); if(val > xmax) val = xmax; if(val < xmin) val = xmin; VPixel(src, i, r, c, VShort) = val; } } } } fprintf(stderr, "\n"); }
/* ** slicetime correction for non-constant TR */ void VSlicetime_NC(VAttrList list, VShort minval, VFloat tdel, VBoolean slicetime_correction, float *onset_array, VString filename) { FILE *fp = NULL; VImage src; VAttrListPosn posn; VString buf, str; int b, r, c, i, j, nt = 0, mt = 0, val, del = 0; double xmin, xmax, sum, nx, estim_tr = 0; double *xx = NULL, *yy = NULL, xi, yi, slicetime = 0, u = 0; gsl_interp_accel *acc = NULL; gsl_spline *spline = NULL; xmin = (VShort) VRepnMinValue(VShortRepn); xmax = (VShort) VRepnMaxValue(VShortRepn); buf = VMalloc(LEN); /* ** read scan times from file, non-constant TR */ if(strlen(filename) > 2) { fp = fopen(filename, "r"); if(!fp) VError(" error opening file %s", filename); i = 0; while(!feof(fp)) { for(j = 0; j < LEN; j++) buf[j] = '\0'; fgets(buf, LEN, fp); if(buf[0] == '%' || buf[0] == '#') continue; if(strlen(buf) < 2) continue; i++; } rewind(fp); nt = i; fprintf(stderr, " num timesteps: %d\n", nt); xx = (double *) VCalloc(nt, sizeof(double)); yy = (double *) VCalloc(nt, sizeof(double)); i = 0; sum = 0; while(!feof(fp)) { for(j = 0; j < LEN; j++) buf[j] = '\0'; fgets(buf, LEN, fp); if(buf[0] == '%' || buf[0] == '#') continue; if(strlen(buf) < 2) continue; if(sscanf(buf, "%lf", &u) != 1) VError(" line %d: illegal input format", i + 1); xx[i] = u * 1000.0; /* convert to millisec */ if(i > 1) sum += xx[i] - xx[i - 1]; i++; } fclose(fp); estim_tr = sum / (double)(nt - 2); fprintf(stderr, " average scan interval = %.3f sec\n", estim_tr / 1000.0); } /* ** process data */ b = -1; for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); if(VPixelRepn(src) != VShortRepn) continue; VSetAttr(VImageAttrList(src), "repetition_time", NULL, VLongRepn, (VLong)estim_tr); VExtractAttr(VImageAttrList(src), "MPIL_vista_0", NULL, VStringRepn, &str, FALSE); b++; if(VImageNRows(src) < 2) continue; /* ** get header info */ if(VGetAttr(VImageAttrList(src), "slice_time", NULL, VDoubleRepn, (VPointer) & slicetime) != VAttrFound && slicetime_correction && onset_array == NULL) VError(" 'slice_time' info missing"); if(onset_array != NULL) slicetime = onset_array[b]; if(nt != VImageNBands(src)) VError(" inconsistent number of time steps, %d %d", nt, VImageNBands(src)); if(acc == NULL && slicetime_correction) { acc = gsl_interp_accel_alloc(); spline = gsl_spline_alloc(gsl_interp_akima, nt); for(i = 0; i < 5; i++) { if(xx[i] / 1000.0 > tdel) break; } del = i; fprintf(stderr, " The first %.2f secs (%d timesteps) will be replaced.\n", tdel, del); } /* ** loop through all voxels in current slice */ if(slicetime_correction) fprintf(stderr, " slice: %3d, %10.3f ms\r", b, slicetime); for(r = 0; r < VImageNRows(src); r++) { for(c = 0; c < VImageNColumns(src); c++) { if(VPixel(src, 0, r, c, VShort) < minval) continue; /* replace first few time steps by average */ if(del > 0) { mt = del + 10; if(mt > nt) mt = nt; sum = nx = 0; for(i = del; i < mt; i++) { sum += VPixel(src, i, r, c, VShort); nx++; } if(nx < 1) continue; val = sum / nx; for(i = 0; i < del; i++) { VPixel(src, i, r, c, VShort) = val; } } if(!slicetime_correction) continue; /* correct for slicetime offsets using cubic spline interpolation */ for(i = 0; i < nt; i++) { yy[i] = VPixel(src, i, r, c, VShort); } gsl_spline_init(spline, xx, yy, nt); for(i = 1; i < nt; i++) { xi = xx[i] - slicetime; yi = gsl_spline_eval(spline, xi, acc); val = (int)(yi + 0.49); if(val > xmax) val = xmax; if(val < xmin) val = xmin; VPixel(src, i, r, c, VShort) = val; } } } } fprintf(stderr, "\n"); }
VImage PairedTest(VImage *src1, VImage *src2, VImage dest, int n, VShort type) { int i, j, k, b, r, c, nslices, nrows, ncols; float ave1, ave2, var1, var2, nx, u; float sum, smooth = 0; float t, z, df, sd, cov, *data1 = NULL, *data2 = NULL; float tiny = 1.0e-10; nslices = VImageNBands(src1[0]); nrows = VImageNRows(src1[0]); ncols = VImageNColumns(src1[0]); gsl_set_error_handler_off(); dest = VCopyImage(src1[0], NULL, VAllBands); VFillImage(dest, VAllBands, 0); VSetAttr(VImageAttrList(dest), "num_images", NULL, VShortRepn, (VShort)n); VSetAttr(VImageAttrList(dest), "patient", NULL, VStringRepn, "paired_ttest"); if(type == 0) VSetAttr(VImageAttrList(dest), "modality", NULL, VStringRepn, "tmap"); else VSetAttr(VImageAttrList(dest), "modality", NULL, VStringRepn, "zmap"); VSetAttr(VImageAttrList(dest), "df", NULL, VShortRepn, (VShort)(n - 1)); /* get smoothness estimates */ sum = nx = 0; for(i = 0; i < n; i++) { if(VGetAttr(VImageAttrList(src1[i]), "smoothness", NULL, VFloatRepn, &smooth) == VAttrFound) { sum += smooth; nx++; } } for(i = 0; i < n; i++) { if(VGetAttr(VImageAttrList(src2[i]), "smoothness", NULL, VFloatRepn, &smooth) == VAttrFound) { sum += smooth; nx++; } } if(nx > 1) { VSetAttr(VImageAttrList(dest), "smoothness", NULL, VFloatRepn, sum / nx); } data1 = (float *) VMalloc(n * sizeof(float)); data2 = (float *) VMalloc(n * sizeof(float)); df = n - 1; nx = (float)n; for(b = 0; b < nslices; b++) { for(r = 0; r < nrows; r++) { for(c = 0; c < ncols; c++) { k = 0; for(i = 0; i < n; i++) { data1[i] = VPixel(src1[i], b, r, c, VFloat); data2[i] = VPixel(src2[i], b, r, c, VFloat); if(ABS(data1[i]) > tiny && ABS(data2[i]) > tiny) k++; } if(k < n - 3) continue; avevar(data1, n, &ave1, &var1); avevar(data2, n, &ave2, &var2); if(var1 < tiny || var2 < tiny) continue; z = t = 0; cov = 0; for(j = 0; j < n; j++) cov += (data1[j] - ave1) * (data2[j] - ave2); cov /= df; u = (var1 + var2 - 2.0 * cov); if(u < tiny) continue; sd = sqrt(u / nx); if(sd < tiny) continue; t = (ave1 - ave2) / sd; if(isnan(t) || isinf(t)) continue; switch(type) { case 0: VPixel(dest, b, r, c, VFloat) = t; break; case 1: /* z = t2z_approx(t,df); */ z = t2z((double)t, (double)df); if(t < 0) z = -z; VPixel(dest, b, r, c, VFloat) = z; break; default: VError(" illegal type"); } } } } return dest; }
int main(int argc, char *argv[]) { static VFloat reso = -1.0; static VLong itype = 0; static VBoolean flip = TRUE; static VBoolean reorder = TRUE; static VOptionDescRec options[] = { { "reso", VFloatRepn, 1, (VPointer) &reso, VOptionalOpt, NULL, "New voxel resolution in mm, def: -1 means min(1.0,\"best source resolution\")" }, { "flip", VBooleanRepn, 1, (VPointer) &flip, VOptionalOpt, NULL, "Whether to flip to natural convention" }, { "reorder", VBooleanRepn, 1, (VPointer) &reorder, VOptionalOpt, NULL, "Whether to reorder axial slices from axial source image" }, { "interpolation", VLongRepn, 1, & itype, VOptionalOpt, ITYPDict, "Type of interpolation (0: linear, 1: nearest neighbour, 2: cubic spline)" } }; FILE *in_file, *out_file; VAttrList list; VAttrListPosn posn; int nobjects = 0; VImage src = NULL, dest = NULL, result = NULL; int i, b, r, c, nbands, nrows, ncols; VString str, newstr, fixpointString, caString, cpString; float fix_c, fix_r, fix_b; float ca_c, ca_r, ca_b; float cp_c, cp_r, cp_b; float x, y, z, min; VDouble v, scale_band, scale_row, scale_col; float scale[3], shift[3]; /* print information */ char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "visotrop V%s", ver); fprintf(stderr, "%s\n", prg_name); fflush(stderr); /* Parse command line arguments: */ VParseFilterCmd(VNumber(options), options, argc, argv, & in_file, & out_file); /* Read source image(s): */ if(!(list = VReadFile(in_file, NULL))) exit(EXIT_FAILURE); /* Scale each object: */ for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { switch(VGetAttrRepn(& posn)) { case VImageRepn: VGetAttrValue(& posn, NULL, VImageRepn, & src); if(VGetAttr(VImageAttrList(src), "voxel", NULL, VStringRepn, (VPointer) & str) == VAttrFound) { sscanf(str, "%f %f %f", &x, &y, &z); fprintf(stderr, " voxel: %f %f %f\n", x, y, z); min = x < y ? x : y; min = z < min ? z : min; /* if resolution is not set, use default value 1 or smaler value if the image resolution is better */ if(reso < 0.0) reso = min < 1.0 ? min : 1.0; if(reso <= 0.0) exit(EXIT_FAILURE); fprintf(stderr, " new resolution: %f \n", reso); scale_col = x / reso; scale_row = y / reso; scale_band = z / reso; nbands = VImageNBands(src) * scale_band; nrows = VImageNRows(src) * scale_row; ncols = VImageNColumns(src) * scale_col; if(VImageNBands(src) == nbands && VImageNRows(src) == nrows && VImageNColumns(src) == ncols) { itype = 0; } fprintf(stderr, " interpolation type: %s\n", ITYPDict[itype].keyword); fprintf(stderr, " old dim: %3d %3d %3d\n", VImageNBands(src), VImageNRows(src), VImageNColumns(src)); for(i = 0; i < 3; i++) shift[i] = scale[i] = 0; scale[0] = scale_band; scale[1] = scale_row; scale[2] = scale_col; switch(itype) { /* trilinear interpolation resampling */ case 0: dest = VTriLinearScale3d(src, NULL, (int)nbands, (int)nrows, (int)ncols, shift, scale); break; /* nearest neightbour resampling */ case 1: dest = VNNScale3d(src, NULL, (int)nbands, (int)nrows, (int)ncols, shift, scale); break; /* cubic spline */ case 2: dest = VCubicSplineScale3d(src, NULL, (int)nbands, (int)nrows, (int)ncols, shift, scale); break; case 3: /* no interpolation, just reshuffle */ dest = VCopyImage(src, NULL, VAllBands); break; default: VError(" unknown resampling type %d", itype); } if(! dest) exit(EXIT_FAILURE); /*aa 2003/09/11 added function not to rotate siemens data*/ if(! VGetAttr(VImageAttrList(src), "orientation", NULL, VStringRepn, (VPointer) & str) == VAttrFound) VError(" attribute 'orientation' missing"); if(strcmp(str, "axial") == 0) { fprintf(stderr, " new dim: %3d %3d %3d\n", nbands, nrows, ncols); result = VCreateImage(nbands, nrows, ncols, VPixelRepn(src)); VFillImage(result, VAllBands, 0); for(b = 0; b < nbands; b++) for(r = 0; r < nrows; r++) for(c = 0; c < ncols; c++) { v = VGetPixel(dest, b, r, c); if((flip == FALSE) && (reorder == FALSE)) VSetPixel(result, b, r, ncols - c - 1, v); else if((flip == TRUE) && (reorder == FALSE)) VSetPixel(result, b, r, c, v); else if((flip == FALSE) && (reorder == TRUE)) VSetPixel(result, nbands - b - 1, r, ncols - c - 1, v); else if((flip == TRUE) && (reorder == TRUE)) VSetPixel(result, nbands - b - 1, r, c, v); } } else if(strcmp(str, "sagittal") == 0) { /* re-arrange from sagittal to axial orientation */ fprintf(stderr, " new dim: %3d %3d %3d\n", nrows, ncols, nbands); result = VCreateImage(nrows, ncols, nbands, VPixelRepn(src)); VFillImage(result, VAllBands, 0); for(b = 0; b < nbands; b++) for(r = 0; r < nrows; r++) for(c = 0; c < ncols; c++) { v = VGetPixel(dest, b, r, c); if(flip == FALSE) VSetPixel(result, r, c, nbands - b - 1, v); else VSetPixel(result, r, c, b, v); } } else if(strcmp(str, "coronal") == 0) { /* re-arrange from coronal to axial orientation */ fprintf(stderr, " new dim: %3d %3d %3d\n", nrows, nbands, ncols); result = VCreateImage(nrows, nbands, ncols, VPixelRepn(src)); VFillImage(result, VAllBands, 0); for(b = 0; b < nbands; b++) for(r = 0; r < nrows; r++) for(c = 0; c < ncols; c++) { v = VGetPixel(dest, b, r, c); if(flip == FALSE) VSetPixel(result, r, b, ncols - c - 1, v); else VSetPixel(result, r, b, c, v); } } else { VError(" unknown resampling type %d", itype); exit(EXIT_FAILURE); } /* copy attributes from source image */ VCopyImageAttrs(src, result); // [TS] 08/03/27 // correct 'fixpoint', 'ca' and 'cp' if they exist in the source image // // NOTE: // this is only done when no flipping or reordering is requested :-( // (WARNING!!!!) '-flip true' actually means that no flipping is done (WHAAAAT ????) // and therefore we test for reorder = false and flip = true fixpointString = VMalloc(80); caString = VMalloc(80); cpString = VMalloc(80); VBoolean _issueWarning = FALSE; if(VGetAttr(VImageAttrList(src), "fixpoint", NULL, VStringRepn, (VPointer)&fixpointString) == VAttrFound) { if(reorder == FALSE && flip == TRUE) { sscanf(fixpointString, "%f %f %f", &fix_c, &fix_r, &fix_b); fix_c *= scale_col; fix_r *= scale_row; fix_b *= scale_band; sprintf((char *)fixpointString, "%f %f %f", fix_c, fix_r, fix_b); VSetAttr(VImageAttrList(result), "fixpoint", NULL, VStringRepn, fixpointString); } else { _issueWarning = TRUE; } } if(VGetAttr(VImageAttrList(src), "ca", NULL, VStringRepn, (VPointer)&caString) == VAttrFound) { if(reorder == FALSE && flip == TRUE) { sscanf(caString, "%f %f %f", &ca_c, &ca_r, &ca_b); ca_c *= scale_col; ca_r *= scale_row; ca_b *= scale_band; sprintf((char *)caString, "%f %f %f", ca_c, ca_r, ca_b); VSetAttr(VImageAttrList(result), "ca", NULL, VStringRepn, caString); } else { _issueWarning = TRUE; } } if(VGetAttr(VImageAttrList(src), "cp", NULL, VStringRepn, (VPointer)&cpString) == VAttrFound) { if(reorder == FALSE && flip == TRUE) { sscanf(cpString, "%f %f %f", &cp_c, &cp_r, &cp_b); cp_c *= scale_col; cp_r *= scale_row; cp_b *= scale_band; sprintf((char *)cpString, "%f %f %f", cp_c, cp_r, cp_b); VSetAttr(VImageAttrList(result), "cp", NULL, VStringRepn, cpString); } else { _issueWarning = TRUE; } } if(_issueWarning) { VWarning("Attributes 'fixpoint', 'ca' and 'cp' exist but were not corrected and are therefore likely to be wrong"); VWarning("This was caused by setting -flip to false or -reorder to true"); VWarning("Please correct the values manually using vattredit"); } /* set the attributes to the changed values */ newstr = VMalloc(80); sprintf((char *)newstr, "%f %f %f", reso, reso, reso); VSetAttr(VImageAttrList(result), "voxel", NULL, VStringRepn, newstr); VSetAttr(VImageAttrList(result), "orientation", NULL, VStringRepn, "axial"); if(flip) VSetAttr(VImageAttrList(result), "convention", NULL, VStringRepn, "natural"); else VSetAttr(VImageAttrList(result), "convention", NULL, VStringRepn, "radiologic"); } VSetAttrValue(& posn, NULL, VImageRepn, result); VDestroyImage(src); break; default: continue; } nobjects++; } /* Make History */ VHistory(VNumber(options), options, prg_name, &list, &list); /* Write the results to the output file: */ if(! VWriteFile(out_file, list)) exit(EXIT_FAILURE); fprintf(stderr, "%s: done.\n", argv[0]); return EXIT_SUCCESS; }
VAttrList VGetContrast(VAttrList list, gsl_vector_float *con, VShort type) { VAttrList out_list; int nbands = 0, nrows = 0, ncols = 0, band, row, col; VImage src = NULL, dest = NULL, std_image = NULL; VImage beta_images[MBETA], res_image = NULL, bcov_image = NULL; VString buf = NULL; VAttrListPosn posn; VString str; int i, nbeta; float t = 0, s = 0, tsigma = 0, z = 0, zmax = 0, zmin = 0; float sigma, var, sum, df; float *ptr1, *ptr2; char *constring = NULL; gsl_vector_float *beta = NULL, *tmp = NULL; gsl_matrix_float *bcov = NULL; i = 0; for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); if(VPixelRepn(src) != VFloatRepn) continue; VGetAttr(VImageAttrList(src), "modality", NULL, VStringRepn, &str); if(strcmp(str, "BETA") == 0) { beta_images[i++] = VCopyImage(src, NULL, VAllBands); } else if(strcmp(str, "RES/trRV") == 0) { res_image = VCopyImage(src, NULL, VAllBands); } else if(strcmp(str, "BCOV") == 0) { bcov_image = VCopyImage(src, NULL, VAllBands); } } nbeta = VImageNRows(bcov_image); nbands = VImageNBands(beta_images[0]); nrows = VImageNRows(beta_images[0]); ncols = VImageNColumns(beta_images[0]); if(VGetAttr(VImageAttrList(beta_images[0]), "df", NULL, VFloatRepn, &df) != VAttrFound) VError(" attribute 'df' not found"); if(nbeta > MBETA) { fprintf(stderr, " number of betas: %d, maximum number of betas: %d\n", nbeta, MBETA); VError(" maximum number of betas is exceeded"); } /* ** read contrast vector */ if(nbeta != con->size) VError("contrast vector has bad length (%d), correct length is %d", con->size, nbeta); fprintf(stderr, " contrast vector:\n"); char str1[10]; constring = (char *)VMalloc(sizeof(char) * 10 * nbeta); constring[0] = '\0'; for(i = 0; i < nbeta; i++) { fprintf(stderr, " %.2f", fvget(con, i)); sprintf(str1, "%1.2f ", fvget(con, i)); strcat((char *)constring, (const char *)str1); } fprintf(stderr, "\n"); /* get variance estimation */ bcov = gsl_matrix_float_alloc(nbeta, nbeta); ptr1 = VImageData(bcov_image); ptr2 = bcov->data; for(i = 0; i < nbeta * nbeta; i++) *ptr2++ = *ptr1++; gsl_matrix_float_transpose(bcov); tmp = fmat_x_vector(bcov, con, tmp); var = fskalarproduct(tmp, con); sigma = sqrt(var); /* ** create output data structs */ out_list = VCreateAttrList(); dest = VCreateImage(nbands, nrows, ncols, VFloatRepn); VFillImage(dest, VAllBands, 0); VCopyImageAttrs(beta_images[0], dest); switch(type) { case 0: /* conimg */ buf = VNewString("conimg"); break; case 1: /* t-image */ buf = VNewString("tmap"); break; case 2: /* zmap */ buf = VNewString("zmap"); break; default: VError(" illegal type"); } fprintf(stderr, " output type: %s\n", buf); VSetAttr(VImageAttrList(dest), "modality", NULL, VStringRepn, buf); VSetAttr(VImageAttrList(dest), "name", NULL, VStringRepn, buf); VSetAttr(VImageAttrList(dest), "contrast", NULL, VStringRepn, constring); VAppendAttr(out_list, "image", NULL, VImageRepn, dest); if(type == 0) { std_image = VCreateImage(nbands, nrows, ncols, VFloatRepn); VFillImage(std_image, VAllBands, 0); VCopyImageAttrs(beta_images[0], std_image); VSetAttr(VImageAttrList(std_image), "modality", NULL, VStringRepn, "std_dev"); VSetAttr(VImageAttrList(std_image), "name", NULL, VStringRepn, "std_dev"); VAppendAttr(out_list, "image", NULL, VImageRepn, std_image); } /* ** loop thru image */ zmax = zmin = 0; beta = gsl_vector_float_alloc(nbeta); for(band = 0; band < nbands; band++) { for(row = 0; row < nrows; row++) { for(col = 0; col < ncols; col++) { t = z = sum = 0; ptr1 = beta->data; for(i = 0; i < nbeta; i++) { *ptr1++ = VPixel(beta_images[i], band, row, col, VFloat); } sum = fskalarproduct(beta, con); if(ABS(sum) < 1.0e-10) continue; s = VPixel(res_image, band, row, col, VFloat); tsigma = sqrt(s) * sigma; if(tsigma > 0.00001) t = sum / tsigma; else t = 0; if(isnan(t) || isinf(t)) t = 0; switch(type) { case 0: /* conimg */ z = sum; break; case 1: /* t-image */ z = t; break; case 2: /* zmap */ z = t2z_approx(t, df); if(z > 30) z = 30; if(sum < 0) z = -z; break; default: ; } if(isnan(z) || isinf(z)) z = 0; if(z > zmax) zmax = z; if(z < zmin) zmin = z; VPixel(dest, band, row, col, VFloat) = z; if(type == 0) VPixel(std_image, band, row, col, VFloat) = tsigma; } } } fprintf(stderr, " min= %.3f, max= %.3f\n", zmin, zmax); return out_list; }
int main(int argc, char *argv[]) { static VArgVector in_files1; static VArgVector in_files2; static VString out_filename; static VShort type = 1; static VBoolean gauss = FALSE; static VOptionDescRec options[] = { {"in1", VStringRepn, 0, & in_files1, VRequiredOpt, NULL, "Input files 1" }, {"in2", VStringRepn, 0, & in_files2, VRequiredOpt, NULL, "Input files 2" }, {"type", VShortRepn, 1, (VPointer) &type, VOptionalOpt, TypeDict, "output type"}, {"gaussianize", VBooleanRepn, 1, (VPointer) &gauss, VOptionalOpt, NULL, "Whether to Gaussianize"}, {"out", VStringRepn, 1, & out_filename, VRequiredOpt, NULL, "Output file" } }; FILE *fp = NULL; VStringConst in_filename, buf1, buf2; VAttrList list1, list2, out_list; VAttrListPosn posn; VString str; VImage src, *src1, *src2, dest = NULL; int i, nimages, npix = 0; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vpaired_ttest V%s", ver); fprintf(stderr, "%s\n", prg_name); /* ** parse command line */ if(! VParseCommand(VNumber(options), options, & argc, argv)) { VReportUsage(argv[0], VNumber(options), options, NULL); exit(EXIT_FAILURE); } if(argc > 1) { VReportBadArgs(argc, argv); exit(EXIT_FAILURE); } if(type < 0 || type > 1) VError(" illegal type"); /* ini */ nimages = in_files1.number; if(in_files2.number != nimages) VError(" inconsistent number of files %d %d", nimages, in_files2.number); for(i = 0; i < nimages; i++) { buf1 = ((VStringConst *) in_files1.vector)[i]; buf2 = ((VStringConst *) in_files2.vector)[i]; fprintf(stderr, "%3d: %s %s\n", i, buf1, buf2); } fprintf(stderr, "\n"); /* images 1 */ src1 = (VImage *) VCalloc(nimages, sizeof(VImage)); for(i = 0; i < nimages; i++) { src1[i] = NULL; in_filename = ((VStringConst *) in_files1.vector)[i]; fp = VOpenInputFile(in_filename, TRUE); list1 = VReadFile(fp, NULL); if(! list1) VError("Error reading image"); fclose(fp); for(VFirstAttr(list1, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); if(VPixelRepn(src) != VFloatRepn) continue; if(VGetAttr(VImageAttrList(src), "modality", NULL, VStringRepn, &str) == VAttrFound) { if(strcmp(str, "conimg") != 0) continue; } if(i == 0) npix = VImageNPixels(src); else if(npix != VImageNPixels(src)) VError(" inconsistent image dimensions"); src1[i] = src; break; } if(src1[i] == NULL) VError(" no contrast image found in %s", in_filename); } /* images 2 */ src2 = (VImage *) VCalloc(nimages, sizeof(VImage)); for(i = 0; i < nimages; i++) { src2[i] = NULL; in_filename = ((VStringConst *) in_files2.vector)[i]; fp = VOpenInputFile(in_filename, TRUE); list2 = VReadFile(fp, NULL); if(! list2) VError("Error reading image"); fclose(fp); for(VFirstAttr(list2, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); if(VPixelRepn(src) != VFloatRepn) continue; if(VGetAttr(VImageAttrList(src), "modality", NULL, VStringRepn, &str) == VAttrFound) { if(strcmp(str, "conimg") != 0) continue; } if(npix != VImageNPixels(src)) VError(" inconsistent image dimensions"); src2[i] = src; break; } if(src2[i] == NULL) VError(" no contrast image found in %s", in_filename); } /* make normally distributed */ if(gauss) { VGaussianize(src1, nimages); VGaussianize(src2, nimages); } /* paired t-test */ dest = PairedTest(src1, src2, dest, nimages, type); /* ** output */ out_list = VCreateAttrList(); VHistory(VNumber(options), options, prg_name, &list1, &out_list); VAppendAttr(out_list, "image", NULL, VImageRepn, dest); fp = VOpenOutputFile(out_filename, TRUE); if(! VWriteFile(fp, out_list)) exit(1); fclose(fp); fprintf(stderr, "%s: done.\n", argv[0]); exit(0); }
int main(int argc, char *argv[]) { /* command line arguments */ static VString out_filename; static VArgVector in_files; static VString trans_filename = ""; static VBoolean in_found, out_found; static VShort minval = 0; static VBoolean compress = TRUE; static VFloat resolution = 3; static VOptionDescRec options[] = { { "in", VStringRepn, 0, & in_files, & in_found, NULL, "Input file" }, { "out", VStringRepn, 1, & out_filename, & out_found, NULL, "Output file" }, { "trans", VStringRepn, 1, &trans_filename, VRequiredOpt, NULL, "File containing transformation matrix" }, { "resolution", VFloatRepn, 1, &resolution, VOptionalOpt, NULL, "Output voxel resolution in mm" }, { "minval", VShortRepn, 1, &minval, VOptionalOpt, NULL, "Signal threshold" }, { "compress", VBooleanRepn, 1, &compress, VOptionalOpt, NULL, "Whether to compress empty slices" } }; VStringConst in_filename; FILE *in_file, *out_file, *fp; VAttrList list, list1, out_list; VAttrListPosn posn; VImage trans = NULL; VImage *dst_image; VImageInfo *imageInfo; int nobject = 0, ntimesteps = 0, nbands = 0, nrows = 0, ncols = 0; VString ca, cp, extent, str; int found = 0; int j, dest_nbands; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vfunctrans V%s", ver); fprintf(stderr, "%s\n", prg_name); /* Parse command line arguments: */ if(! VParseCommand(VNumber(options), options, & argc, argv) || ! VIdentifyFiles(VNumber(options), options, "in", & argc, argv, 0) || ! VIdentifyFiles(VNumber(options), options, "out", & argc, argv, -1)) goto Usage; if(argc > 1) { VReportBadArgs(argc, argv); Usage: VReportUsage(argv[0], VNumber(options), options, NULL); exit(EXIT_FAILURE); } if(resolution <= 0) VError(" 'resolution' must be an integer > 0"); /* ** Read the transformation matrix: */ fp = VOpenInputFile(trans_filename, TRUE); list1 = VReadFile(fp, NULL); if(! list1) VError("Error reading image"); fclose(fp); for(VFirstAttr(list1, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; if(strncmp(VGetAttrName(&posn), "transform", 9) != 0) continue; VGetAttrValue(& posn, NULL, VImageRepn, & trans); break; } if(trans == NULL) VError("transformation matrix not found"); /* ** check attributes */ if(VGetAttr(VImageAttrList(trans), "ca", NULL, VStringRepn, (VPointer) & ca) != VAttrFound) VError(" attribute 'ca' missing in transformation matrix "); if(VGetAttr(VImageAttrList(trans), "cp", NULL, VStringRepn, (VPointer) & cp) != VAttrFound) VError(" attribute 'cp' missing in transformation matrix "); if(VGetAttr(VImageAttrList(trans), "extent", NULL, VStringRepn, (VPointer) & extent) != VAttrFound) VError(" attribute 'extent' missing in transformation matrix "); /* ** open in-file */ if(in_files.number < 1 || in_files.number > 1) VError(" incorrect number of input files: %d", in_files.number); in_filename = ((VStringConst *) in_files.vector)[0]; if(strcmp(in_filename, "-") == 0) in_file = stdin; else { in_file = fopen((char *)in_filename, "r"); if(! in_file) VError("Failed to open input file %s", in_filename); } /* ** read file info */ if(! ReadHeader(in_file)) VError("error reading header"); if(!(list = ReadAttrList(in_file))) VError("error reading attr list"); j = 0; for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { j++; } imageInfo = (VImageInfo *) VMalloc(sizeof(VImageInfo) * (j + 1)); nobject = nbands = found = 0; for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { str = VGetAttrName(&posn); if(strncmp(str, "history", 7) == 0) { nobject++; continue; } VImageInfoIni(&imageInfo[nbands]); if(! VGetImageInfo(in_file, list, nobject, &imageInfo[nbands])) VError(" error reading image info"); if(imageInfo[nbands].repn == VShortRepn) { found = 1; nrows = imageInfo[nbands].nrows; ncols = imageInfo[nbands].ncolumns; ntimesteps = imageInfo[nbands].nbands; nbands++; } nobject++; } fclose(in_file); if(!found) VError(" couldn't find functional data"); /* ** process each time step */ dst_image = VFunctrans(in_filename, imageInfo, nbands, trans, resolution, ntimesteps, minval, compress, &dest_nbands); /* ** output */ out_list = VCreateAttrList(); VHistory(VNumber(options), options, prg_name, &list, &out_list); for(j = 0; j < dest_nbands; j++) { VAppendAttr(out_list, "image", NULL, VImageRepn, dst_image[j]); } /* Open and write the output file: */ if(strcmp(out_filename, "-") == 0) out_file = stdout; else { out_file = fopen(out_filename, "w"); if(! out_file) VError("Failed to open output file %s", out_filename); } if(!VWriteFile(out_file, out_list) || fclose(out_file)) VSystemError("error writing output file"); fprintf(stderr, "\n%s: done.\n", argv[0]); return (EXIT_SUCCESS); }
int main(int argc, char *argv[]) { static VLong objnr = -1; static VString name = ""; static VString value = ""; static VOptionDescRec options[] = { { "obj", VLongRepn, 1, (VPointer) &objnr, VOptionalOpt, NULL, "object number, all objects (-1)" }, { "name", VStringRepn, 1, (VPointer) &name, VRequiredOpt, NULL, "attribute name" }, { "value", VStringRepn, 1, (VPointer) &value, VRequiredOpt, NULL, "attribute value" } }; FILE *in_file, *out_file; VAttrList list, olist; VAttrListPosn posn; VImage isrc; VGraph gsrc; Volumes vsrc; VString buf; int nobj; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vattredit V%s", ver); fprintf(stderr, "%s\n", prg_name); /* Parse command line arguments and identify files: */ VParseFilterCmd(VNumber(options), options, argc, argv, &in_file, & out_file); /* Read source image(s): */ if(!(list = VReadFile(in_file, NULL))) exit(1); /* Process each image: */ nobj = 0; for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { olist = NULL; if(nobj == objnr || objnr < 0) { if(VGetAttrRepn(& posn) == VImageRepn) { VGetAttrValue(& posn, NULL, VImageRepn, & isrc); olist = VImageAttrList(isrc); } else if(VGetAttrRepn(& posn) == VGraphRepn) { VGetAttrValue(& posn, NULL, VGraphRepn, & gsrc); olist = VGraphAttrList(gsrc); } else if(VGetAttrRepn(& posn) == VolumesRepn) { VGetAttrValue(& posn, NULL, VolumesRepn, & vsrc); olist = VolumesAttrList(vsrc); } if(olist != NULL) { if(VGetAttr(olist, name, NULL, VStringRepn, &buf) == VAttrFound) fprintf(stderr, " object %3d, old value: %s\n", nobj, buf); VSetAttr(olist, name, NULL, VStringRepn, value); fprintf(stderr, " object %3d, new value: %s\n", nobj, value); } } nobj++; } /* Make History */ VHistory(VNumber(options), options, prg_name, &list, &list); /* Write out the results: */ if(! VWriteFile(out_file, list)) exit(1); fprintf(stderr, "%s: done.\n", argv[0]); return 0; }
int main(int argc, char *argv[]) { static VArgVector in_files; static VString out_filename; static VBoolean level = FALSE; static VBoolean zscore = FALSE; static VBoolean in_found, out_found; static VOptionDescRec options[] = { { "in", VStringRepn, 0, & in_files, & in_found, NULL, "Contrast images" }, { "out", VStringRepn, 1, & out_filename, & out_found, NULL, "Output file" }, { "level", VBooleanRepn, 1, & level, VOptionalOpt, NULL, "Whether to produce output to be used for 3rd level analysis" } /* { "zscore", VBooleanRepn, 1, & zscore, VOptionalOpt, NULL, "Whether to produce z-scores as output"} */ }; FILE *f; VStringConst in_filename; VAttrList list, out_list; VAttrListPosn posn; VImage src = NULL; VImage cbeta_images[N], sd_images[N]; int i, nimages = 0; VString str = NULL; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vbayes V%s", ver); fprintf(stderr, "%s\n", prg_name); /* Parse command line arguments: */ if(! VParseCommand(VNumber(options), options, & argc, argv) || ! VIdentifyFiles(VNumber(options), options, "in", & argc, argv, 0) || ! VIdentifyFiles(VNumber(options), options, "out", & argc, argv, -1)) goto Usage; if(argc > 1) { VReportBadArgs(argc, argv); Usage: VReportUsage(argv[0], VNumber(options), options, NULL); exit(EXIT_FAILURE); } nimages = in_files.number; fprintf(stderr, "Processing %d input files\n\n", nimages); if(nimages >= N) VError("Too many input images, max: %d", N); if(nimages == 0) VError("Input images missing"); /* loop through all input files */ for(i = 0; i < nimages; i++) { in_filename = ((VStringConst *) in_files.vector)[i]; fprintf(stderr, "%s\n", in_filename); f = fopen((char *)in_filename, "r"); if(! f) VError("Failed to open input file %s", in_filename); if(!(list = VReadFile(f, NULL))) exit(EXIT_FAILURE); fclose(f); for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); if(VPixelRepn(src) != VFloatRepn) continue; VGetAttr(VImageAttrList(src), "modality", NULL, VStringRepn, &str); if(strcmp(str, "conimg") == 0) { cbeta_images[i] = VCopyImage(src, NULL, VAllBands); } else if(strcmp(str, "std_dev") == 0 || strcmp(str, "sd") == 0) { sd_images[i] = VCopyImage(src, NULL, VAllBands); } else VError(" Illegal input file! Make sure to use con-images as input"); } } /* calculate probabilities */ out_list = VBayes(cbeta_images, sd_images, nimages, level, zscore); if(out_list == NULL) VError(" no result"); /* ** output */ VHistory(VNumber(options), options, prg_name, &list, &out_list); if(strcmp(out_filename, "-") == 0) VError("Output file required"); f = fopen(out_filename, "w"); if(! f) VError("Failed to open output file %s", out_filename); if(! VWriteFile(f, out_list)) exit(EXIT_FAILURE); fprintf(stderr, "\n%s: done \n", argv[0]); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { static VArgVector in_files1; static VArgVector in_files2; static VArgVector mask_file; static VArgVector report_file; static VOptionDescRec options[] = { { "in1", VStringRepn, 0, & in_files1, VRequiredOpt, NULL, "Input files 1" }, { "in2", VStringRepn, 0, & in_files2, VRequiredOpt, NULL, "Input files 2" }, { "report", VStringRepn, 0, & report_file, VRequiredOpt, NULL, "Report file" }, { "mask", VStringRepn, 0, & mask_file, VRequiredOpt, NULL, "Mask file(s)" } }; FILE *fp = NULL; VStringConst in_filename; VAttrList list1, list2, list3; VAttrListPosn posn; VString str; VImage src, *src1, *src2, *mask; int i, nimages = 0, mimages = 0; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vROIpaired_ttest V%s", ver); fprintf(stderr, "%s\n", prg_name); if(!VParseCommand(VNumber(options), options, & argc, argv)) { VReportUsage(argv[0], VNumber(options), options, NULL); exit(0); } /* get number of input images */ nimages = in_files1.number; if(in_files2.number != nimages) VError(" inconsistent number of files: in1: %d, in2: %d ", nimages, in_files2.number); mimages = mask_file.number; if(nimages != mimages && mimages > 1) VError(" inconsistent number of files, images: %d, masks: %d", nimages, mimages); /* images 1 */ src1 = (VImage *) VMalloc(sizeof(VImage) * nimages); for(i = 0; i < nimages; i++) { src1[i] = NULL; in_filename = ((VStringConst *) in_files1.vector)[i]; fp = VOpenInputFile(in_filename, TRUE); if(!fp) VError("Error opening file %s", in_filename); list1 = VReadFile(fp, NULL); if(! list1) VError("Error reading file %s", in_filename); fclose(fp); for(VFirstAttr(list1, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); if(VPixelRepn(src) != VFloatRepn) continue; if(VGetAttr(VImageAttrList(src), "modality", NULL, VStringRepn, &str) == VAttrFound) { if(strcmp(str, "conimg") != 0) continue; } src1[i] = src; break; } if(src1[i] == NULL) VError(" no contrast image found in %s", in_filename); } /* images 2 */ src2 = (VImage *) VMalloc(sizeof(VImage) * nimages); for(i = 0; i < nimages; i++) { src2[i] = NULL; in_filename = ((VStringConst *) in_files2.vector)[i]; fp = VOpenInputFile(in_filename, TRUE); if(!fp) VError("Error opening file %s", in_filename); list2 = VReadFile(fp, NULL); if(! list2) VError("Error reading file %s", in_filename); fclose(fp); for(VFirstAttr(list2, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); if(VPixelRepn(src) != VFloatRepn) continue; if(VGetAttr(VImageAttrList(src), "modality", NULL, VStringRepn, &str) == VAttrFound) { if(strcmp(str, "conimg") != 0) continue; } src2[i] = src; break; } if(src2[i] == NULL) VError(" no contrast image found in %s", in_filename); } for(i = 0; i < nimages; i++) { fprintf(stderr, "%3d: %s %s\n", i, ((VStringConst *) in_files1.vector)[i], ((VStringConst *) in_files2.vector)[i]); } fprintf(stderr, "\n"); /* mask images */ mask = (VImage *) VMalloc(sizeof(VImage) * mimages); for(i = 0; i < mimages; i++) { mask[i] = NULL; in_filename = ((VStringConst *) mask_file.vector)[i]; fp = VOpenInputFile(in_filename, TRUE); if(!fp) VError("Error opening file %s", in_filename); list3 = VReadFile(fp, NULL); if(! list3) VError("Error reading file", in_filename); fclose(fp); for(VFirstAttr(list3, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); if(VPixelRepn(src) != VUByteRepn) mask[i] = VConvertImageCopy(src, NULL, VAllBands, VUByteRepn); else mask[i] = src; break; } if(mask[i] == NULL) VError(" mask %d not found", i); } /* open report_file */ fp = NULL; in_filename = ((VStringConst *) report_file.vector)[0]; fp = fopen(in_filename, "w"); if(!fp) VError(" error opening file %s", in_filename); /* paired t-test in ROIs */ VROIpaired_ttest(src1, src2, mask, nimages, mimages, fp); exit(0); }
VImage VDoMulticomp3d(VImage src, VImage multicomp, VBoolean verbose) { VString str; Volumes volumes; Volume vol; VImage tmp = NULL, label_image = NULL, dest = NULL; VFloat *src_pp, *dst_pp, u, zthr = 0, zmax = 0, voxsize = 1, sym = 0; VBit *bin_pp; int i, nl, size, nbands, nrows, ncols, npixels; int b, r, c, b0, r0, c0; int nflag, pflag; float x0, x1, x2; float sign = 1; float voxel[3], ca[3], extent[3]; /* ** read multicomp file header */ voxsize = 1; if(VGetAttr(VImageAttrList(multicomp), "voxel_size", NULL, VFloatRepn, (VPointer) & voxsize) != VAttrFound) VError(" attribute 'voxel_size' not found"); if(VGetAttr(VImageAttrList(multicomp), "zthr", NULL, VFloatRepn, (VPointer) &zthr) != VAttrFound) VError(" attribute 'zthr' not found"); /* ** read src header */ if(verbose) { if(VGetAttr(VImageAttrList(src), "voxel", NULL, VStringRepn, (VPointer) & str) != VAttrFound) VError(" attribute 'voxel' not found"); sscanf(str, "%f %f %f", &x0, &x1, &x2); if(ABS(x0 * x1 * x2 - voxsize) > 0.01) VError(" voxel sizes do not match %.3f %.3f %.3f", x0, x1, x2); voxel[0] = x0; voxel[1] = x1; voxel[2] = x2; if(VGetAttr(VImageAttrList(src), "ca", NULL, VStringRepn, (VPointer) & str) != VAttrFound) VError(" attribute 'ca' not found"); sscanf(str, "%f %f %f", &x0, &x1, &x2); ca[0] = x0; ca[1] = x1; ca[2] = x2; if(VGetAttr(VImageAttrList(src), "extent", NULL, VStringRepn, (VPointer) & str) != VAttrFound) VError(" attribute 'extent' not found"); sscanf(str, "%f %f %f", &x0, &x1, &x2); extent[0] = x0; extent[1] = x1; extent[2] = x2; } nbands = VImageNBands(src); nrows = VImageNRows(src); ncols = VImageNColumns(src); npixels = VImageNPixels(src); tmp = VCreateImage(nbands, nrows, ncols, VBitRepn); dest = VCopyImage(src, NULL, VAllBands); VFillImage(dest, VAllBands, 0); /* ** positive threshold */ nflag = pflag = 0; src_pp = VImageData(src); bin_pp = VImageData(tmp); for(i = 0; i < npixels; i++) { *bin_pp = 0; u = *src_pp; if(u > 0 && u >= zthr) *bin_pp = 1; src_pp++; bin_pp++; } label_image = VLabelImage3d(tmp, label_image, (int)26, VShortRepn, &nl); if(nl < 1 && pflag == 0) { VWarning(" no voxels above threshold %.3f", zthr); goto next; } else pflag++; volumes = VImage2Volumes(label_image); for(vol = volumes->first; vol != NULL; vol = vol->next) { sign = 1; size = VolumeSize(vol); zmax = VolumeZMax(vol, src, (float)1, &b, &r, &c); sym = VolumeSym(vol, src, sign, zthr); if(CheckValueSymm(multicomp, size, zmax, sym) == FALSE) { VolumeZero(vol, tmp); } else { if(verbose) { VPixel2Tal(ca, voxel, extent, b, r, c, &x0, &x1, &x2); c0 = VRint(x0); r0 = VRint(x1); b0 = VRint(x2); fprintf(stderr, " nvoxels: %5d, zmax: %7.3f, sym: %.3f, addr: %3d %3d %3d\n", size, zmax, sym, c0, r0, b0); } } } src_pp = VImageData(src); dst_pp = VImageData(dest); bin_pp = VImageData(tmp); for(i = 0; i < npixels; i++) { if(*bin_pp > 0) *dst_pp = *src_pp; src_pp++; dst_pp++; bin_pp++; } /* ** negative threshold */ next: src_pp = VImageData(src); bin_pp = VImageData(tmp); for(i = 0; i < npixels; i++) { *bin_pp = 0; u = *src_pp; if(u < 0 && -u >= zthr) *bin_pp = 1; src_pp++; bin_pp++; } label_image = VLabelImage3d(tmp, label_image, (int)26, VShortRepn, &nl); if(nl < 1 && nflag == 0) { /* VWarning(" no voxels below negative threshold %.3f",-zthr); */ goto ende; } else nflag++; volumes = VImage2Volumes(label_image); for(vol = volumes->first; vol != NULL; vol = vol->next) { sign = -1; size = VolumeSize(vol); zmax = VolumeZMax(vol, src, sign, &b, &r, &c); sym = VolumeSym(vol, src, sign, zthr); if(CheckValueSymm(multicomp, size, zmax, sym) == FALSE) { VolumeZero(vol, tmp); } else { if(verbose) { VPixel2Tal(ca, voxel, extent, b, r, c, &x0, &x1, &x2); c0 = VRint(x0); r0 = VRint(x1); b0 = VRint(x2); fprintf(stderr, " nvoxels: %5d, zmax: %7.3f, sym: %.3f, addr: %3d %3d %3d\n", size, zmax, sym, c0, r0, b0); } } } src_pp = VImageData(src); dst_pp = VImageData(dest); bin_pp = VImageData(tmp); for(i = 0; i < npixels; i++) { if(*bin_pp > 0) *dst_pp = *src_pp; src_pp++; dst_pp++; bin_pp++; } ende: if(nflag == 0 && pflag == 0) VError(" no voxels passed threshold"); return dest; }
VImage VCerebellum(VImage src) { VImage coronal, label_coronal; VImage sagital, label_sagital; VImage axial, label_axial; VImage bin0_image, bin1_image, label_image; VBit *bin0_pp, *src_pp; VUByte *ubyte_pp; int i, nbands, nrows, ncols, npixels, nl = 0; float x0, x1, x2; VString str; int b, r, c, cp, rp, bp, slice_extent, slice_origin, b0; nbands = VImageNBands(src); nrows = VImageNRows(src); ncols = VImageNColumns(src); npixels = nbands * nrows * ncols; str = VMalloc(80); if(VGetAttr(VImageAttrList(src), "cp", NULL, VStringRepn, (VPointer) &str) != VAttrFound) VError(" attribute 'cp' not found"); sscanf(str, "%f %f %f", &x0, &x1, &x2); bp = (int) VRint((double) x2); rp = (int) VRint((double) x1); cp = (int) VRint((double) x0); if(VGetAttr(VImageAttrList(src), "extent", NULL, VStringRepn, (VPointer) &str) != VAttrFound) VError(" attribute 'extent' not found"); sscanf(str, "%f %f %f", &x0, &x1, &x2); slice_extent = (int) VRint((double) x2); if(VGetAttr(VImageAttrList(src), "origin", NULL, VStringRepn, (VPointer) &str) != VAttrFound) VError(" attribute 'origin' not found"); sscanf(str, "%f %f %f", &x0, &x1, &x2); slice_origin = (int) VRint((double) x2); /* erode */ bin1_image = VDTErode(src, NULL, (VDouble) 2.0); /* readdress to coronal slices and remove small 2D components */ coronal = VCreateImage(1, nbands, ncols, VBitRepn); label_coronal = VCreateImage(1, nbands, ncols, VUByteRepn); ubyte_pp = (VUByte *) VImageData(label_coronal); for(i = 0; i < (nbands * ncols); i++) *ubyte_pp++ = 0; for(r = rp; r < nrows; r++) { bin0_pp = (VBit *) VImageData(coronal); for(i = 0; i < (nbands * ncols); i++) *bin0_pp++ = 0; for(b = 0; b < nbands; b++) { for(c = 0; c < ncols; c++) { VPixel(coronal, 0, b, c, VBit) = VPixel(bin1_image, b, r, c, VBit); } } label_coronal = VLabelImage2d(coronal, label_coronal, 8, VUByteRepn, &nl); VDeleteCereb(label_coronal, &coronal, 110, (int) 0); for(b = 0; b < nbands; b++) { for(c = 0; c < ncols; c++) { if(VPixel(coronal, 0, b, c, VBit) == 0) VPixel(bin1_image, b, r, c, VBit) = 0; } } } /* readdress to sagital slices and remove small 2D components */ sagital = VCreateImage(1, nbands, nrows, VBitRepn); label_sagital = VCreateImage(1, nbands, nrows, VUByteRepn); ubyte_pp = (VUByte *) VImageData(label_sagital); for(i = 0; i < (nbands * nrows); i++) *ubyte_pp++ = 0; for(c = 0; c < ncols; c++) { if(ABS(c - 80) < 10) continue; bin0_pp = (VBit *) VImageData(sagital); for(i = 0; i < (nbands * nrows); i++) *bin0_pp++ = 0; for(b = 0; b < nbands; b++) { for(r = 0; r < nrows; r++) { VPixel(sagital, 0, b, r, VBit) = VPixel(bin1_image, b, r, c, VBit); } } label_sagital = VLabelImage2d(sagital, label_sagital, (int) 8, VUByteRepn, &nl); VDeleteCereb(label_sagital, &sagital, 115, cp); for(b = 0; b < nbands; b++) { for(r = 0; r < nrows; r++) { if(VPixel(sagital, 0, b, r, VBit) == 0) VPixel(bin1_image, b, r, c, VBit) = 0; } } } /* readdress to axial slices and remove small 2D components */ axial = VCreateImage(1, nrows, ncols, VBitRepn); label_axial = VCreateImage(1, nrows, ncols, VUByteRepn); ubyte_pp = (VUByte *) VImageData(label_axial); for(i = 0; i < (nrows * ncols); i++) *ubyte_pp++ = 0; /* for (b=bp; b<nbands; b++) { */ for(b = 105; b < nbands; b++) { bin0_pp = (VBit *) VImageData(axial); for(i = 0; i < (nrows * ncols); i++) *bin0_pp++ = 0; for(r = 0; r < nrows; r++) { for(c = 0; c < ncols; c++) { VPixel(axial, 0, r, c, VBit) = VPixel(bin1_image, b, r, c, VBit); } } label_sagital = VLabelImage2d(axial, label_axial, (int) 8, VUByteRepn, &nl); VDeleteCereb(label_axial, &axial, 0, 0); for(r = 0; r < nrows; r++) { for(c = 0; c < ncols; c++) { if(VPixel(axial, 0, r, c, VBit) == 0) VPixel(bin1_image, b, r, c, VBit) = 0; } } } /* remove everything below slice extent */ b0 = slice_extent + slice_origin; npixels = nrows * ncols; for(b = b0; b < nbands; b++) { bin0_pp = (VBit *) VPixelPtr(bin1_image, b, 0, 0); for(i = 0; i < npixels; i++) *bin0_pp++ = 0; } /* dilate */ bin0_image = VDTDilate(bin1_image, NULL, (VDouble) 3.0); npixels = nrows * ncols; for(b = 0; b < bp; b++) { bin0_pp = (VBit *) VPixelPtr(bin0_image, b, 0, 0); src_pp = (VBit *) VPixelPtr(src, b, 0, 0); for(i = 0; i < npixels; i++) *bin0_pp++ = *src_pp++; } for(b = bp; b < nbands; b++) { bin0_pp = (VBit *) VPixelPtr(bin0_image, b, 0, 0); src_pp = (VBit *) VPixelPtr(src, b, 0, 0); for(i = 0; i < npixels; i++) { if(*src_pp == 0) *bin0_pp = 0; src_pp++; bin0_pp++; } } /* ** remove small remaining components */ label_image = VLabelImage3d(bin0_image, NULL, (int) 26, VShortRepn, &nl); bin0_image = VSelectBig(label_image, bin0_image); VImageAttrList(bin0_image) = VCopyAttrList(VImageAttrList(src)); return bin0_image; }
VImage VDoTrans(VImage src, VImage dest, VImage transform, VFloat resolution, VLong type) { VImage src1 = NULL, dest_cor = NULL, dest_sag = NULL, trans = NULL; int i, nbands1 = 0, nrows1 = 0, ncols1 = 0; int xdim, ydim, zdim; int orient = AXIAL; float b0, r0, c0; float scaleb, scaler, scalec; float scale[3], shift[3]; VString str, str1, str2, str3, str4, str5, str6; VShort buf; VDouble u; float transResX = 1.0, transResY = 1.0, transResZ = 1.0; VString _transResString; if(VGetAttr(VImageAttrList(transform), "voxel", NULL, VStringRepn, (VPointer) &_transResString) == VAttrFound) { sscanf(_transResString, "%f %f %f", &transResX, &transResY, &transResZ); } /* ** get matrix size */ xdim = 160; ydim = 200; zdim = 160; if(VGetAttr(VImageAttrList(transform), "zdim", NULL, VShortRepn, (VPointer) & buf) == VAttrFound) { zdim = buf; } if(VGetAttr(VImageAttrList(transform), "ydim", NULL, VShortRepn, (VPointer) & buf) == VAttrFound) { ydim = buf; } if(VGetAttr(VImageAttrList(transform), "xdim", NULL, VShortRepn, (VPointer) & buf) == VAttrFound) { xdim = buf; } /* ** get slice orientation */ if(VGetAttr(VImageAttrList(src), "orientation", NULL, VStringRepn, (VPointer) & str) != VAttrFound) VError(" attribute 'orientation' missing in input file."); if(strcmp(str, "coronal") == 0) { orient = CORONAL; nbands1 = ydim; nrows1 = zdim; ncols1 = xdim; } else if(strcmp(str, "axial") == 0) { orient = AXIAL; nbands1 = zdim; nrows1 = ydim; ncols1 = xdim; } else if(strcmp(str, "sagittal") == 0) { orient = SAGITTAL; nbands1 = zdim; nrows1 = xdim; ncols1 = ydim; } else VError("orientation must be axial or coronal"); nbands1 /= (resolution / transResX); nrows1 /= (resolution / transResY); ncols1 /= (resolution / transResZ); /* ** scale to size */ if(VGetAttr(VImageAttrList(src), "voxel", NULL, VStringRepn, (VPointer) & str) != VAttrFound) VError(" attribute 'voxel' missing in input file."); sscanf(str, "%f %f %f", &scalec, &scaler, &scaleb); scaleb /= (resolution / transResX); scaler /= (resolution / transResY); scalec /= (resolution / transResZ); scale[0] = scaleb; scale[1] = scaler; scale[2] = scalec; shift[0] = 0; shift[1] = (float)nrows1 * 0.5 - scaler * (float)VImageNRows(src) * 0.5; shift[2] = (float)ncols1 * 0.5 - scalec * (float)VImageNColumns(src) * 0.5; src1 = VShuffleSlices(src, NULL); switch(type) { case 0: src = VTriLinearScale3d(src1, NULL, nbands1, nrows1, ncols1, shift, scale); break; case 1: src = VNNScale3d(src1, NULL, nbands1, nrows1, ncols1, shift, scale); break; default: VError(" illegal resampling type"); } /* ** resample image */ trans = VCopyImage(transform, NULL, VAllBands); for(i = 0; i < 3; i++) { u = VPixel(trans, 0, i, 0, VDouble); u /= resolution; VPixel(trans, 0, i, 0, VDouble) = u; } b0 = (float)nbands1 * 0.5; r0 = (float)nrows1 * 0.5; c0 = (float)ncols1 * 0.5; switch(type) { case 0: dest = VTriLinearSample3d(src, dest, trans, b0, r0, c0, nbands1, nrows1, ncols1); break; case 1: dest = VNNSample3d(src, dest, trans, b0, r0, c0, nbands1, nrows1, ncols1); break; } /* ** update header */ float _cax, _cay, _caz, _cpx, _cpy, _cpz; float _fixpointx, _fixpointy, _fixpointz; str1 = str2 = str3 = str4 = NULL; str1 = (VString) VMalloc(80); sprintf(str1, "%.2f %.2f %.2f", resolution, resolution, resolution); if(VGetAttr(VImageAttrList(trans), "ca", NULL, VStringRepn, (VPointer) & str2) != VAttrFound) { str2 = NULL; } else { sscanf(str2, "%f %f %f", &_cax, &_cay, &_caz); _cax /= (resolution / transResX); _cay /= (resolution / transResY); _caz /= (resolution / transResZ); sprintf(str2, "%.2f %.2f %.2f", _cax, _cay, _caz); } if(VGetAttr(VImageAttrList(trans), "cp", NULL, VStringRepn, (VPointer) & str3) != VAttrFound) { str3 = NULL; } else { sscanf(str3, "%f %f %f", &_cpx, &_cpy, &_cpz); _cpx /= (resolution / transResX); _cpy /= (resolution / transResY); _cpz /= (resolution / transResZ); sprintf(str3, "%.2f %.2f %.2f", _cpx, _cpy, _cpz); } if(VGetAttr(VImageAttrList(trans), "extent", NULL, VStringRepn, (VPointer) & str4) != VAttrFound) str4 = NULL; if(VGetAttr(VImageAttrList(trans), "fixpoint", NULL, VStringRepn, (VPointer) & str5) != VAttrFound) { str5 = NULL; } else { sscanf(str5, "%f %f %f", &_fixpointx, &_fixpointy, &_fixpointz); _fixpointx /= (resolution / transResX); _fixpointy /= (resolution / transResY); _fixpointz /= (resolution / transResZ); sprintf(str5, "%.2f %.2f %.2f", _fixpointx, _fixpointy, _fixpointz); } if(VGetAttr(VImageAttrList(trans), "talairach", NULL, VStringRepn, (VPointer) & str6) != VAttrFound) { str6 = NULL; } if(orient == CORONAL) { dest_cor = VAxial2Coronal(dest, NULL); VDestroyImage(dest); VCopyImageAttrs(src, dest_cor); VSetAttr(VImageAttrList(dest_cor), "voxel", NULL, VStringRepn, str1); if(str2) VSetAttr(VImageAttrList(dest_cor), "ca", NULL, VStringRepn, str2); if(str3) VSetAttr(VImageAttrList(dest_cor), "cp", NULL, VStringRepn, str3); if(str4) VSetAttr(VImageAttrList(dest_cor), "extent", NULL, VStringRepn, str4); if(str5) VSetAttr(VImageAttrList(dest_cor), "fixpoint", NULL, VStringRepn, str5); if(str6) VSetAttr(VImageAttrList(dest_cor), "talairach", NULL, VStringRepn, str6); return dest_cor; } else if(orient == SAGITTAL) { dest_sag = VAxial2Sagittal(dest, NULL); VDestroyImage(dest); VCopyImageAttrs(src, dest_sag); VSetAttr(VImageAttrList(dest_sag), "voxel", NULL, VStringRepn, str1); if(str2) VSetAttr(VImageAttrList(dest_sag), "ca", NULL, VStringRepn, str2); if(str3) VSetAttr(VImageAttrList(dest_sag), "cp", NULL, VStringRepn, str3); if(str4) VSetAttr(VImageAttrList(dest_sag), "extent", NULL, VStringRepn, str4); if(str5) VSetAttr(VImageAttrList(dest_sag), "fixpoint", NULL, VStringRepn, str5); if(str6) VSetAttr(VImageAttrList(dest_sag), "talairach", NULL, VStringRepn, str6); return dest_sag; } else { VCopyImageAttrs(src, dest); VSetAttr(VImageAttrList(dest), "voxel", NULL, VStringRepn, str1); if(str2) VSetAttr(VImageAttrList(dest), "ca", NULL, VStringRepn, str2); if(str3) VSetAttr(VImageAttrList(dest), "cp", NULL, VStringRepn, str3); if(str4) VSetAttr(VImageAttrList(dest), "extent", NULL, VStringRepn, str4); if(str5) VSetAttr(VImageAttrList(dest), "fixpoint", NULL, VStringRepn, str5); if(str6) VSetAttr(VImageAttrList(dest), "talairach", NULL, VStringRepn, str6); return dest; } }
void VImageManager::init( VAttrList list ) { VAttrListPosn posn; int nbands = 0; int nrows = 0; int ncols = 0; int timeSlices = 2; VImage src; VString str; for ( VFirstAttr ( list, & posn ); VAttrExists ( & posn ); VNextAttr ( & posn ) ) { if ( VGetAttrRepn ( & posn ) != VImageRepn ) continue; VGetAttrValue ( &posn, NULL, VImageRepn, &src ); if ( VPixelRepn( src ) != VShortRepn ) continue; // check if we have a functional data image if ( ( VGetAttr ( VImageAttrList ( src ), "MPIL_vista_0", NULL, VStringRepn, ( VPointer ) & str ) == VAttrFound ) || ( VGetAttr ( VImageAttrList ( src ), "repetition_time", NULL, VStringRepn, ( VPointer ) & str ) == VAttrFound ) ) { if ( VImageNRows( src ) > nrows ) nrows = VImageNRows( src ); if ( VImageNColumns( src ) > ncols ) ncols = VImageNColumns( src ); if ( VImageNBands( src ) > timeSlices ) timeSlices = VImageNBands( src ); nbands++; } } if ( nbands == 0 ) VError( "No raw data found" ); // now loading the vimages int currentTimeSlice = 0; // curr slice for ( currentTimeSlice = 0; currentTimeSlice < timeSlices; currentTimeSlice++ ) { VImage dest = VCreateImage( nbands, nrows, ncols, VShortRepn ); vtimestep( list, dest, currentTimeSlice ); m_imageList.append( dest ); } prepareScaleValue(); // creating data arrays int size = ncols * nbands; m_coronarData = new unsigned char[size]; memset( m_coronarData, 0, size * sizeof( unsigned char ) ); size = ncols * nrows; m_axialData = new unsigned char[size]; memset( m_axialData, 0, size * sizeof( unsigned char ) ); size = nbands * nrows; m_sagittalData = new unsigned char[size]; memset( m_sagittalData, 0, size * sizeof( unsigned char ) ); }
int main(int argc, char *argv[]) { static VArgVector in_files; static VString out_filename; static VString filename; static VShort minval = 0; static VFloat fwhm = 4.0; static VOptionDescRec options[] = { {"in", VStringRepn, 0, & in_files, VRequiredOpt, NULL, "Input files" }, {"out", VStringRepn, 1, & out_filename, VRequiredOpt, NULL, "Output file" }, {"design", VStringRepn, 1, (VPointer) &filename, VRequiredOpt, NULL, "Design file"}, { "fwhm", VFloatRepn, 1, (VPointer) &fwhm, VOptionalOpt, NULL, "FWHM of temporal Gaussian filter in seconds" }, {"minval", VShortRepn, 1, (VPointer) &minval, VOptionalOpt, NULL, "Signal threshold"} }; FILE *fp = NULL, *f = NULL; VStringConst in_filename; VString ifilename; VAttrList list = NULL, list1 = NULL; VAttrList out_list = NULL, history_list = NULL; VAttrListPosn posn; VImage design = NULL; ListInfo *linfo; VLong itr = 0; VFloat sigma = 0, tr = 0; int i, n, nimages; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vcolorglm V%s", ver); fprintf(stderr, "%s\n", prg_name); /* ** parse command line */ if(! VParseCommand(VNumber(options), options, & argc, argv)) { VReportUsage(argv[0], VNumber(options), options, NULL); exit(EXIT_FAILURE); } if(argc > 1) { VReportBadArgs(argc, argv); exit(EXIT_FAILURE); } /* ** read design matrix */ fp = VOpenInputFile(filename, TRUE); list1 = VReadFile(fp, NULL); if(! list1) VError("Error reading design file"); fclose(fp); n = 0; for(VFirstAttr(list1, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & design); if(VPixelRepn(design) != VFloatRepn /* && VPixelRepn(design) != VDoubleRepn */) continue; n++; break; } if(n == 0) VError(" design matrix not found "); /* ** get pre-coloring info */ if(VGetAttr(VImageAttrList(design), "repetition_time", NULL, VLongRepn, &itr) != VAttrFound) VError(" TR info missing in header"); tr = (float) itr / 1000.0; sigma = 0; if(tr > 0.001 && fwhm > 0.001) { fprintf(stderr, " TR: %.3f seconds\n", tr); sigma = fwhm / 2.35482; sigma /= tr; if(sigma < 0.1) { VWarning(" 'fwhm/sigma' too small (%.3f / %.3f), will be set to zero", fwhm, sigma); sigma = 0; } } /* ** Read each input file */ nimages = in_files.number; linfo = (ListInfo *) VMalloc(sizeof(ListInfo) * nimages); for(i = 0; i < nimages; i++) { in_filename = ((VStringConst *) in_files.vector)[i]; ifilename = VNewString(in_filename); fprintf(stderr, " file: %s\n", ifilename); list = GetListInfo(ifilename, &linfo[i]); /* Create history */ if(i == 0) { history_list = VReadHistory(&list); if(history_list == NULL) history_list = VCreateAttrList(); VPrependHistory(VNumber(options), options, prg_name, &history_list); } } /* ** GLM */ out_list = VRegression(linfo, nimages, minval, design, sigma, itr); /* ** Output: */ VPrependAttr(out_list, "history", NULL, VAttrListRepn, history_list); f = VOpenOutputFile(out_filename, TRUE); if(!f) VError(" error opening outout file %s", out_filename); if(! VWriteFile(f, out_list)) exit(1); fprintf(stderr, "%s: done.\n", argv[0]); return 0; }
void VSpatialFilter(VAttrList list,VDouble fwhm) { VAttrListPosn posn; VImage src[NSLICES],xsrc=NULL,tmp=NULL,dest=NULL,kernel=NULL,tmp2d=NULL; VString str=NULL; float v0,v1,v2,v3; int b,r,c,i,size; int n,nslices,nrows,ncols,dim; double u, sigma=0; extern VImage VGaussianConv (VImage,VImage,VBand,double,int); /* get image dimensions */ dim = 3; v0 = v1 = v2 = v3 = 1; n = i = nrows = ncols = 0; str = VMalloc(100); for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (i >= NSLICES) VError(" too many slices"); if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & xsrc); if (VPixelRepn(xsrc) != VShortRepn) continue; if (VImageNBands(xsrc) > n) n = VImageNBands(xsrc); if (VImageNRows(xsrc) > nrows) nrows = VImageNRows(xsrc); if (VImageNColumns(xsrc) > ncols) ncols = VImageNColumns(xsrc); if (VGetAttr (VImageAttrList (xsrc), "voxel", NULL,VStringRepn, (VPointer) & str) == VAttrFound) { sscanf(str,"%f %f %f",&v1,&v2,&v3); } src[i] = xsrc; i++; } nslices = i; /* in general, apply 3D spatial filtering */ dim = 3; /* only if clearly non-isotropic apply 2D filtering */ v0 = 0.5*(v1+v2); if (ABS(v0-v3) > 0.5) dim = 2; /* ** Sigma */ sigma = fwhm/sqrt(8.0*log(2.0)); sigma /= (double)v1; /* ** 2D gauss filtering */ if (dim==2) { fprintf(stderr," 2D spatial filter: fwhm= %.3f mm sigma= %.3f vox\n",fwhm,sigma); size = (int)(6.0 * sigma + 1.5); if ((size & 1) == 0) size++; fprintf(stderr," size= %d\n",size); for (b=0; b<nslices; b++) { if (VImageNRows(src[b]) < 2) continue; tmp2d = VGaussianConv(src[b],tmp2d, VAllBands, sigma, size); src[b] = VCopyImagePixels(tmp2d,src[b],VAllBands); } VDestroyImage(tmp2d); } /* ** 3D gauss filtering */ if (dim==3) { fprintf(stderr," 3D spatial filter: fwhm= %.3f mm sigma= %.3f vox\n",fwhm,sigma); kernel = VSGaussKernel(sigma); tmp = VCreateImage(nslices,nrows,ncols,VFloatRepn); VFillImage(tmp,VAllBands,0); for (i=0; i<n; i++) { if (i%20 == 0) fprintf(stderr," i= %5d\r",i); VFillImage(tmp,VAllBands,0); for (b=0; b<nslices; b++) { if (VImageNRows(src[b]) < 2) continue; for (r=0; r<nrows; r++) { for (c=0; c<ncols; c++) { u = VPixel(src[b],i,r,c,VShort); VPixel(tmp,b,r,c,VFloat) = u; } } } dest = VGauss3d (tmp,dest,kernel); for (b=0; b<nslices; b++) { if (VImageNRows(src[b]) < 2) continue; for (r=0; r<nrows; r++) { for (c=0; c<ncols; c++) { u = VPixel(dest,b,r,c,VFloat); VPixel(src[b],i,r,c,VShort) = u; } } } } } }
VBandInterp VImageComponentInterp (VImage image) { VLong interp; VGetAttrResult result; if (VImageNBands (image) != (VImageNFrames (image) * VImageNViewpoints (image) * VImageNColors (image) * VImageNComponents (image))) VWarning ("VImageComponentInterp: No. bands (%d) conflicts with no. " "of frames, etc. (%d %d %d %d)", VImageNBands (image), VImageNFrames (image), VImageNViewpoints (image), VImageNColors (image), VImageNComponents (image)); if (! VImageAttrList (image) || (result = VGetAttr (VImageAttrList (image), VComponentInterpAttr, VBandInterpDict, VLongRepn, & interp)) == VAttrMissing) return VImageNComponents (image) > 1 ? VBandInterpOther : VBandInterpNone; if (result == VAttrBadValue) return VBandInterpOther; switch (interp) { case VBandInterpComplex: if (VImageNComponents (image) != 2) { VWarning ("VBandColorInterp: Complex image has %d component(s)", VImageNComponents (image)); return VBandInterpOther; } return VBandInterpComplex; case VBandInterpGradient: if (VImageNComponents (image) > 3) { VWarning ("VBandColorInterp: Gradient image has %d component(s)", VImageNComponents (image)); return VBandInterpOther; } return VBandInterpGradient; case VBandInterpIntensity: if (VImageNComponents (image) > 1) { VWarning ("VBandColorInterp: Intensity image has %d component(s)", VImageNComponents (image)); return VBandInterpOther; } return VBandInterpIntensity; case VBandInterpOrientation: if (VImageNComponents (image) > 1) { VWarning ("VBandColorInterp: " "Orientation image has %d component(s)", VImageNComponents (image)); return VBandInterpOther; } return VBandInterpOrientation; } return VBandInterpOther; }