int main(int argc, char **argv) { static VString filename = ""; static VLong neighb = 2; static VLong iter = 1; static VOptionDescRec options[] = { { "n", VLongRepn, 1, &neighb, VOptionalOpt, TYPDict, "type of neighbourhood used for smoothing" }, { "iter", VLongRepn, 1, &iter, VOptionalOpt, NULL, "number of iterations" }, { "image", VStringRepn, 1, &filename, VOptionalOpt, NULL, "grey value image" }, }; FILE *in_file, *out_file, *fp; VAttrList list, list1; VImage src = NULL, dest, image = NULL; VAttrListPosn posn; /* Parse command line arguments: */ VParseFilterCmd(VNumber(options), options, argc, argv, &in_file, &out_file); /* Read transformation image: */ fp = VOpenInputFile(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, & image); if(VPixelRepn(image) != VUByteRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & image); break; } if(image == NULL) VError(" image not found"); /* Read source image(s): */ if(!(list = VReadFile(in_file, NULL))) exit(1); for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); dest = VTopSmoothImage3d(src, image, NULL, neighb, iter); if(dest == NULL) exit(1); VSetAttrValue(& posn, NULL, VImageRepn, dest); VDestroyImage(src); } /* Write the results to the output file: */ if(VWriteFile(out_file, list)) fprintf(stderr, "%s: done.\n", argv[0]); return 0; }
int main (int argc,char *argv[]) { static VFloat sigma = 1.5; static VOptionDescRec options[] = { {"sigma",VFloatRepn,1,(VPointer) &sigma,VOptionalOpt,NULL,"std dev"} }; FILE *in_file,*out_file; VAttrList list=NULL; VAttrListPosn posn; VImage src=NULL,dest=NULL; char prg[50]; sprintf(prg,"vgauss2d V%s", getVersion()); fprintf (stderr, "%s\n", prg); VParseFilterCmd (VNumber (options),options,argc,argv,&in_file,&out_file); if (! (list = VReadFile (in_file, NULL))) exit (1); fclose(in_file); for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & src); dest = VFilterGauss2d (src,NULL,(double)sigma); VSetAttrValue (& posn, NULL,VImageRepn,dest); } if (src == NULL) VError(" no input image found"); VHistory(VNumber(options),options,prg,&list,&list); if (! VWriteFile (out_file, list)) exit (1); fprintf (stderr, "%s: done.\n", argv[0]); return 0; }
VBoolean VLookupAttr (VAttrList list, VStringConst name, VAttrListPosn *posn) { for (VFirstAttr (list, posn); VAttrExists (posn); VNextAttr (posn)) if (strcmp (VGetAttrName (posn), name) == 0) return TRUE; return FALSE; }
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; }
VBoolean WriteImages (VString Name, VAttrList Images, VAttrList& history_list) { FILE* file; /* output file */ VAttrList list; /* attribute list */ VAttrListPosn pos; /* position in list */ VImage image; /* image in list */ VBoolean success; /* success flag */ char history[]="history"; /* open file */ file = fopen (Name, "w"); if (!file) { VError ("Failed to open output file '%s'", Name); return FALSE; } /* create list */ list = VCreateAttrList(); /* insert images */ for (VFirstAttr (Images, &pos); VAttrExists (&pos); VNextAttr (&pos)) { VGetAttrValue (&pos, NULL, VImageRepn, &image); VAppendAttr (list, VGetAttrName (&pos), NULL, VImageRepn, image); } /* Prepend history */ VPrependAttr(list, history ,NULL,VAttrListRepn,history_list); /* write file */ success = VWriteFile (file, list); if (!success) VError ("Failed to write output file '%s'", Name); /* remove images */ for (VFirstAttr (list, &pos); VAttrExists (&pos); VNextAttr (&pos)) if (VGetAttrRepn (&pos) == VImageRepn) VSetAttrValue (&pos, NULL, VImageRepn, NULL); /* clean-up*/ VDestroyAttrList (list); fclose (file); return success; } /* WriteImages */
int main(int argc, char *argv[]) { static VString filename = ""; static VBoolean fisher = FALSE; static VShort minval = 0; static VOptionDescRec options[] = { {"mask", VStringRepn, 1, (VPointer) &filename, VRequiredOpt, NULL, "mask"}, {"fisher", VBooleanRepn, 1, (VPointer) &fisher, VOptionalOpt, NULL, "Whether to do fisher transform"}, {"minval", VShortRepn, 1, (VPointer) &minval, VOptionalOpt, NULL, "signal threshold"} }; FILE *in_file, *out_file, *fp; VAttrList list = NULL, list1 = NULL, list2 = NULL; VAttrListPosn posn; VImage dest = NULL, mask = NULL; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vsimmat V%s", ver); fprintf(stderr, "%s\n", prg_name); VParseFilterCmd(VNumber(options), options, argc, argv, &in_file, &out_file); /* ** read mask */ fp = VOpenInputFile(filename, TRUE); list1 = VReadFile(fp, NULL); if(! list1) VError("Error reading mask file"); fclose(fp); for(VFirstAttr(list1, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & mask); if(VPixelRepn(mask) != VBitRepn) { mask = NULL; continue; } } if(mask == NULL) VError(" no mask found"); /* ** read functional data */ if(!(list = VReadFile(in_file, NULL))) exit(1); fclose(in_file); /* ** process */ dest = SimilarityMatrix(list, mask, minval, fisher); list2 = VCreateAttrList(); VAppendAttr(list2, "matrix", NULL, VImageRepn, dest); VHistory(VNumber(options), options, prg_name, &list, &list2); if(! VWriteFile(out_file, list2)) exit(1); fprintf(stderr, "%s: done.\n", argv[0]); return 0; }
CVImage::CVImage( VAttrListPosn &posn ) { VImage image; for ( ; VAttrExists ( & posn ) && ( VGetAttrRepn ( & posn ) == VImageRepn ); VNextAttr ( & posn ) ) { const VBoolean result = VGetAttrValue ( &posn, NULL, VImageRepn, &image ); assert( result ); if( !images.empty() && !fit( image ) )break; else addImage( image ); } }
void VReleaseStorage(VAttrList list) { VImage src; VAttrListPosn posn; for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); VDestroyImage(src); src = NULL; } }
int VReadObjects (FILE *file, VRepnKind repn, VAttrList *attributes, VPointer **objects) { VAttrList list; VAttrListPosn posn; int i, nobjects = 0; VPointer *vector; /* Read the file's contents: */ list = VReadFile (file, NULL); if (! list) return FALSE; /* Count the objects found: */ for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) nobjects += (VGetAttrRepn (& posn) == repn); if (nobjects == 0) { VWarning ("VReadObjects: No %s objects present in stream", VRepnName (repn)); VDestroyAttrList (list); return FALSE; } /* Allocate a vector of that many object pointers: */ vector = VMalloc (nobjects * sizeof (VPointer)); /* Extract the objects from the attribute list and place them in the vector: */ for (VFirstAttr (list, & posn), i = 0; VAttrExists (& posn); ) if (VGetAttrRepn (& posn) == repn) { VGetAttrValue (& posn, NULL, repn, vector + i); VDeleteAttr (& posn); i++; } else VNextAttr (& posn); /* Return the objects and the remaining attributes: */ *attributes = list; *objects = vector; return nobjects; }
void ReadImages (VString Name, VAttrList& Images, VAttrList& history_list) { FILE* file; /* input file */ VAttrList list, list1; /* attribute list */ VAttrListPosn pos; /* position in list */ VImage image; /* image in list */ /* initialize results */ Images = NULL; /* open file */ file = fopen (Name, "r"); if (!file) { VError ("Failed to open input file '%s'", Name); return; } /* read file */ list = VReadFile (file, NULL); if (!list) { VError ("Failed to read input file '%s'", Name); fclose (file); return; } /* Read History */ list1 = VReadHistory(&list); if (list1==NULL) list1=VCreateAttrList(); history_list = VCopyAttrList(list1); /* extract images */ for (VFirstAttr (list, &pos); VAttrExists (&pos); VNextAttr (&pos)) { if (VGetAttrRepn (&pos) != VImageRepn) continue; if (!Images) Images = VCreateAttrList (); VGetAttrValue (&pos, NULL, VImageRepn, &image); VSetAttrValue (&pos, NULL, VImageRepn, NULL); VAppendAttr (Images, "image", NULL, VImageRepn, image); } if (!Images) VError ("Input file '%s' does not contain an image", Name); /* clean-up*/ VDestroyAttrList (list); fclose (file); } /* ReadImages */
int main (int argc,char *argv[]) { static VShort aneighb = 1; static VShort arepn = 1; static VOptionDescRec options[] = { {"n",VShortRepn,1,(VPointer) &aneighb, VOptionalOpt,ADJDict,"neighbourhood type"}, {"repn",VShortRepn,1,(VPointer) &arepn, VOptionalOpt,TYPEDict,"output representation type (ubyte or short)"} }; FILE *in_file,*out_file; VAttrList list=NULL; VAttrListPosn posn; VImage src=NULL,dest=NULL; int nl=0,neighb; VRepnKind repn; char prg[50]; sprintf(prg,"vlabel2d V%s", getVersion()); fprintf (stderr, "%s\n", prg); VParseFilterCmd (VNumber (options),options,argc,argv,&in_file,&out_file); if (arepn == 0) repn = VUByteRepn; else repn = VShortRepn; if (aneighb == 0) neighb = 4; else neighb = 8; if (! (list = VReadFile (in_file, NULL))) exit (1); fclose(in_file); for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & src); dest = VLabelImage2d(src,NULL, (int) neighb,repn,&nl); fprintf(stderr," numlabels= %d\n",nl); VSetAttrValue (& posn, NULL,VImageRepn,dest); } if (src == NULL) VError(" no input image found"); VHistory(VNumber(options),options,prg,&list,&list); if (! VWriteFile (out_file, list)) exit (1); fprintf (stderr, "%s: done.\n", argv[0]); return 0; }
int main (int argc,char *argv[]) { static VShort first = 0; static VShort last = -1; static VOptionDescRec options[] = { { "first", VShortRepn, 1, (VPointer) & first, VOptionalOpt, NULL, "First slice" }, { "last", VShortRepn, 1, (VPointer) & last, VOptionalOpt, NULL, "Last slice " } }; FILE *in_file, *out_file; VAttrList list; VAttrListPosn posn; VImage src=NULL, result=NULL; char prg[50]; sprintf(prg,"vselbands V%s", getVersion()); fprintf (stderr, "%s\n", prg); /* Parse command line arguments and identify files: */ VParseFilterCmd (VNumber (options), options, argc, argv,& in_file, & out_file); /* Read the input file: */ list = VReadFile (in_file, NULL); if (! list) exit (1); /* process */ for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL, VImageRepn, & src); result = VSelSlices (src, NULL,first,last); if (! result) exit (1); VSetAttrValue (& posn, NULL, VImageRepn, result); VDestroyImage (src); } /* Write out the results: */ VHistory(VNumber(options),options,prg,&list,&list); if (! VWriteFile (out_file, list)) exit (1); fprintf (stderr, "%s: done.\n", argv[0]); return 0; }
int main (int argc,char *argv[]) { static VLong hemi = 0; static VOptionDescRec options[] = { { "hemi", VLongRepn, 1, & hemi, VOptionalOpt, HEMIDict, "Hemisphere to retain" } }; FILE *in_file, *out_file; VAttrList list; VAttrListPosn posn; VImage isrc=NULL; VGraph gsrc=NULL,dest=NULL; char prg[50]; sprintf(prg,"vhemi V%s", getVersion()); fprintf (stderr, "%s\n", prg); /* Parse command line arguments and identify files: */ VParseFilterCmd (VNumber (options), options, argc, argv, & in_file, & out_file); /* Read the grey input file: */ list = VReadFile (in_file, NULL); if (! list) VError("Error reading input image"); fclose(in_file); for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) == VImageRepn) { VGetAttrValue (& posn, NULL, VImageRepn, & isrc); VImageHemi(&isrc,hemi); VSetAttrValue (& posn, NULL,VImageRepn,isrc); } else if (VGetAttrRepn (& posn) == VGraphRepn) { VGetAttrValue (& posn, NULL, VGraphRepn, & gsrc); dest = VGraphHemi(gsrc,hemi); VSetAttrValue (& posn, NULL,VGraphRepn,dest); } } /* Write out the results: */ VHistory(VNumber(options),options,prg,&list,&list); if (! VWriteFile (out_file,list)) exit (1); fprintf (stderr, "%s: done.\n", argv[0]); return 0; }
int main(int argc, char *argv[]) { static VDouble d1 = 0; static VDouble d2 = 3.0; static VShort threshold = 155; static VShort background = 30; static VFloat edge = 20; static VOptionDescRec options[] = { { "d1", VDoubleRepn, 1, &d1, VOptionalOpt, 0, "erosion" }, { "d2", VDoubleRepn, 1, &d2, VOptionalOpt, 0, "dilation" }, { "t", VShortRepn, 1, &threshold, VOptionalOpt, 0, "threshold" }, { "background", VShortRepn, 1, &background, VOptionalOpt, 0, " image background" }, { "edge", VFloatRepn, 1, &edge, VOptionalOpt, 0, "edge strength" } }; VAttrList list; VAttrListPosn posn; VImage src = NULL, dst = NULL; FILE *in_file, *out_file; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vbrainpeel V%s", ver); fprintf(stderr, "%s\n", prg_name); VParseFilterCmd(VNumber(options), options, argc, argv, &in_file, &out_file); /* Read source image(s): */ if(!(list = VReadFile(in_file, NULL))) return 1; fclose(in_file); /* Operate on each source image: */ for(VFirstAttr(list, &posn); VAttrExists(&posn); VNextAttr(&posn)) { if(VGetAttrRepn(&posn) == VImageRepn) { VGetAttrValue(&posn, NULL, VImageRepn, &src); dst = VPeel(src, NULL, d1, d2, edge, threshold, background); if(dst) VSetAttrValue(&posn, NULL, VImageRepn, dst); else VError(" error in vbrainpeel"); } } /* Write the results to the output file: */ VHistory(VNumber(options), options, prg_name, &list, &list); if(!VWriteFile(out_file, list)) return 1; fprintf(stderr, " %s: done.\n", argv[0]); return 0; }
int main(int argc, char *argv[]) { static VBoolean x_axis = FALSE; static VBoolean y_axis = FALSE; static VBoolean z_axis = FALSE; static VOptionDescRec options[] = { { "x", VBooleanRepn, 1, (VPointer) &x_axis, VOptionalOpt, NULL, "Flip x-axis" }, { "y", VBooleanRepn, 1, (VPointer) &y_axis, VOptionalOpt, NULL, "Flip y-axis" }, { "z", VBooleanRepn, 1, (VPointer) &z_axis, VOptionalOpt, NULL, "Flip z-axis" }, }; FILE *in_file, *out_file; VAttrList list; VAttrListPosn posn; VImage src = NULL, result = NULL; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vflip3d V%s", ver); fprintf(stderr, "%s\n", prg_name); VWarning("It is recommended to use the program vswapdim since vflip3d does not support extended header informations"); /* Parse command line arguments and identify files: */ VParseFilterCmd(VNumber(options), options, argc, argv, &in_file, &out_file); /* Read the input file: */ list = VReadFile(in_file, NULL); if(! list) exit(1); /* For each attribute read... */ for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); result = Flip3dImage(src, NULL, VAllBands, x_axis, y_axis, z_axis); if(! result) exit(1); VSetAttrValue(& posn, NULL, VImageRepn, result); VDestroyImage(src); } /* Write out the results: */ if(! VWriteFile(out_file, list)) exit(1); fprintf(stderr, "%s: done.\n", argv[0]); return 0; }
VImage read_attribute_template(VString filename) { VImage attribute_image; FILE *attribute_input_file = VOpenInputFile(filename, TRUE); VAttrList attribute_list = VReadFile(attribute_input_file, NULL); fclose(attribute_input_file); if(!attribute_list) VError("Error reading image"); VAttrListPosn position; for (VFirstAttr(attribute_list, &position); VAttrExists(&position); VNextAttr(&position)) { if (VGetAttrRepn(&position) != VImageRepn) continue; VGetAttrValue(&position,NULL,VImageRepn,&attribute_image); break; } return attribute_image; }
int main (int argc,char *argv[]) { FILE *in_file, *out_file; VAttrList list; VAttrListPosn posn; VImage src=NULL, dest=NULL; char prg[50]; sprintf(prg,"vthin3d V%s", getVersion()); fprintf (stderr, "%s\n", prg); /* Parse command line arguments and identify files: */ VParseFilterCmd (0,NULL, argc, argv, & in_file, & out_file); /* Read the input file: */ list = VReadFile (in_file, NULL); if (! list) exit (1); fclose(in_file); /* For each attribute read... */ for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL, VImageRepn, & src); if (VPixelRepn(src) != VBitRepn) continue; dest = VThin3d(src, NULL,(int)26); if (! dest) exit (1); VSetAttrValue (& posn, NULL, VImageRepn, dest); VDestroyImage (src); } /* output */ VHistory(0,NULL,prg,&list,&list); if (! VWriteFile (out_file, list)) exit (1); fprintf (stderr, "%s: done.\n", argv[0]); return 0; }
int main (int argc,char *argv[]) { static VDouble xmin = 0; static VDouble xmax = 255; static VOptionDescRec options[] = { {"min",VDoubleRepn,1,(VPointer) &xmin,VOptionalOpt,NULL,"lower threshold"}, {"max",VDoubleRepn,1,(VPointer) &xmax,VOptionalOpt,NULL,"upper threshold"}, }; FILE *in_file,*out_file; VAttrList list=NULL; VAttrListPosn posn; VImage src=NULL,dest=NULL; char prg[50]; sprintf(prg,"vbinarize V%s", getVersion()); fprintf (stderr, "%s\n", prg); VParseFilterCmd (VNumber (options),options,argc,argv,&in_file,&out_file); if (! (list = VReadFile (in_file, NULL))) exit (1); fclose(in_file); for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & src); dest = VBinarizeImage (src,NULL,xmin,xmax); VSetAttrValue (& posn, NULL,VImageRepn,dest); } if (src == NULL) VError(" no input image found"); VHistory(VNumber(options),options,prg,&list,&list); if (! VWriteFile (out_file, list)) exit (1); fprintf (stderr, "%s: done.\n", argv[0]); return 0; }
static VBoolean WriteAttrList (FILE *f, VAttrList list, int indent, VList *data_list, long *offset) { VAttrListPosn posn; int i; /* Write the { marking the beginning of the attribute list: */ FailTest (fputs ("{\n", f)); /* Write each attribute in the list: */ for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) if (! WriteAttr (f, & posn, indent, data_list, offset)) return FALSE; /* Write the } marking the end of the attribute list: */ for (i = indent - 1; i > 0; i--) FailTest (fputc ('\t', f)); FailTest (fputc ('}', f)); return TRUE; Fail: VWarning ("VWriteFile: Write to stream failed"); return FALSE; }
int main(int argc, char *argv[]) { static VString cfile = ""; static VOptionDescRec options[] = { {"file", VStringRepn, 1, (VPointer) &cfile, VOptionalOpt, NULL, "file"} }; FILE *in_file, *out_file; VAttrList list = NULL; VAttrListPosn posn; VImage design = NULL, dest = NULL; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vaddcovariates V%s", ver); fprintf(stderr, "%s\n", prg_name); VParseFilterCmd(VNumber(options), options, argc, argv, &in_file, &out_file); if(!(list = VReadFile(in_file, NULL))) exit(1); fclose(in_file); for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & design); if(VPixelRepn(design) != VFloatRepn && VPixelRepn(design) != VDoubleRepn) continue; dest = VAddCovariates(design, cfile); VSetAttrValue(& posn, NULL, VImageRepn, dest); break; } if(design == NULL) VError(" design matrix not found "); /* Output: */ if(! VWriteFile(out_file, list)) exit(1); fprintf(stderr, " %s: done.\n", argv[0]); return 0; }
int main (int argc,char *argv[]) { static VString filename = ""; static VString filename1 = ""; static VString filename2 = ""; static VShort first = 2; static VShort length = 0; static VShort type = 0; static VShort minval = 0; static VShort nproc = 4; static VOptionDescRec options[] = { {"in1",VStringRepn,1,(VPointer) &filename1,VRequiredOpt,NULL,"first input file"}, {"in2",VStringRepn,1,(VPointer) &filename2,VRequiredOpt,NULL,"second input file"}, {"mask",VStringRepn,1,(VPointer) &filename,VRequiredOpt,NULL,"mask"}, {"type",VShortRepn,1,(VPointer) &type,VOptionalOpt,TYPDict,"type of concordance measure"}, {"minval",VShortRepn,1,(VPointer) &minval,VOptionalOpt,NULL,"signal threshold"}, {"first",VShortRepn,1,(VPointer) &first,VOptionalOpt,NULL,"first timestep to use"}, {"length",VShortRepn,1,(VPointer) &length,VOptionalOpt,NULL, "length of time series to use, '0' to use full length"}, {"j",VShortRepn,1,(VPointer) &nproc,VOptionalOpt,NULL,"number of processors to use, '0' to use all"} }; FILE *out_file,*fp; VAttrList list=NULL,list1=NULL,list2=NULL,out_list=NULL; VAttrListPosn posn; VImage mask=NULL,map=NULL,dest=NULL,disc=NULL; float *A1=NULL,*A2=NULL,u=0,tiny=1.0e-6; int b,r,c; char *prg = "vccm2"; VParseFilterCmd (VNumber (options),options,argc,argv,NULL,&out_file); if (type > 3) VError("illegal type"); /* ** read mask */ fp = VOpenInputFile (filename, TRUE); list = VReadFile (fp, NULL); if (! list) VError("Error reading mask file"); fclose(fp); for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & mask); if (VPixelRepn(mask) == VFloatRepn || VPixelRepn(mask) == VDoubleRepn) { mask = NULL; continue; } } if (mask == NULL) VError(" no mask found"); /* read files */ fp = VOpenInputFile (filename1, TRUE); list1 = VReadFile (fp, NULL); if (! list1) VError("Error reading 1st input file"); fclose(fp); fp = VOpenInputFile (filename2, TRUE); list2 = VReadFile (fp, NULL); if (! list2) VError("Error reading 2nd input file"); fclose(fp); /* get voxel map */ map = GetMap(list1,list2,mask,minval); /* omp-stuff */ #ifdef _OPENMP int num_procs=omp_get_num_procs(); if (nproc > 0 && nproc < num_procs) num_procs = nproc; printf("using %d cores\n",(int)num_procs); omp_set_num_threads(num_procs); #endif /* _OPENMP */ /* compute corr matrices */ A1 = VCorrMatrix(list1,map,first,length); A2 = VCorrMatrix(list2,map,first,length); /* get concordance between two corr matrices */ dest = VCCM2(A1,A2,map,(int)type); /* invert to get discordant map */ disc = VCreateImageLike(dest); VFillImage(disc,VAllBands,0); if (type < 2) { for (b=0; b<VImageNBands(dest); b++) { for (r=0; r<VImageNRows(dest); r++) { for (c=0; c<VImageNColumns(dest); c++) { if (VGetPixel(mask,b,r,c) < 1) continue; u = VPixel(dest,b,r,c,VFloat); if (u < tiny) continue; VPixel(disc,b,r,c,VFloat) = 1-u; } } } } /* output */ out_list = VCreateAttrList(); VHistory(VNumber(options),options,prg,&list,&out_list); VAppendAttr(out_list,"concordant",NULL,VImageRepn,dest); if (type < 2) VAppendAttr(out_list,"discordant",NULL,VImageRepn,disc); if (! VWriteFile (out_file, out_list)) exit (1); fprintf (stderr, "%s: done.\n", argv[0]); return 0; }
float * VCorrMatrix(VAttrList list,VImage map,VShort first,VShort length) { VAttrListPosn posn; VImage src[NSLICES]; size_t b,r,c,i,j,i1,n,m,nt,last,ntimesteps,nrows,ncols,nslices; gsl_matrix_float *mat=NULL; float *A=NULL; /* ** get image dimensions */ i = ntimesteps = nrows = ncols = 0; for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & src[i]); if (VPixelRepn(src[i]) != VShortRepn) continue; if (VImageNBands(src[i]) > ntimesteps) ntimesteps = VImageNBands(src[i]); if (VImageNRows(src[i]) > nrows) nrows = VImageNRows(src[i]); if (VImageNColumns(src[i]) > ncols) ncols = VImageNColumns(src[i]); i++; if (i >= NSLICES) VError(" too many slices"); } nslices = i; /* get time steps to include */ if (length < 1) length = ntimesteps-2; last = first + length -1; if (last >= ntimesteps) last = ntimesteps-1; if (first < 0) first = 1; nt = last - first + 1; i1 = first+1; if (nt < 2) VError(" not enough timesteps, nt= %d",nt); /* number of voxels */ n = VImageNColumns(map); fprintf(stderr," ntimesteps: %d, first= %d, last= %d, nt= %d, nvoxels: %ld\n", (int)ntimesteps,(int)first,(int)last,(int)nt,(long)n); /* ** avoid casting to float, copy data to matrix */ mat = gsl_matrix_float_calloc(n,nt); if (!mat) VError(" err allocating mat"); for (i=0; i<n; i++) { b = VPixel(map,0,0,i,VShort); r = VPixel(map,0,1,i,VShort); c = VPixel(map,0,2,i,VShort); float *ptr = gsl_matrix_float_ptr(mat,i,0); int k; j = 0; for (k=first; k<=last; k++) { if (j >= mat->size2) VError(" j= %d %d",j,mat->size2); if (k >= VImageNBands(src[b])) VError(" k= %d %d",k, VImageNBands(src[b])); *ptr++ = (float) VPixel(src[b],k,r,c,VShort); j++; } } /* ** compute similarity matrix */ m = (n*(n+1))/2; fprintf(stderr," compute correlation matrix...\n"); A = (float *) VCalloc(m,sizeof(float)); if (!A) VError(" err allocating correlation matrix"); memset(A,0,m*sizeof(float)); size_t progress=0; #pragma omp parallel for shared(progress) private(j) schedule(guided) firstprivate(mat,A) for (i=0; i<n; i++) { if (i%100 == 0) fprintf(stderr," %d00\r",(int)(++progress)); const float *arr1 = gsl_matrix_float_const_ptr(mat,i,0); for (j=0; j<i; j++) { const float *arr2 = gsl_matrix_float_const_ptr(mat,j,0); const double v = Correlation(arr1,arr2,nt); const size_t k=j+i*(i+1)/2; if (k >= m) VError(" illegal addr k= %d, m= %d",k,m); A[k] = v; } } fprintf(stderr," matrix done.\n"); gsl_matrix_float_free(mat); return A; }
VImage GetMap(VAttrList list1,VAttrList list2,VImage mask,VShort minval) { VAttrListPosn posn; VImage src1[NSLICES],src2[NSLICES],map=NULL; size_t b,r,c,i,n; int nrows,ncols,nslices,nrows2,ncols2,nslices2; /* ** get image dimensions */ i = nrows = ncols = 0; for (VFirstAttr (list1, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & src1[i]); if (VPixelRepn(src1[i]) != VShortRepn) continue; if (VImageNRows(src1[i]) > nrows) nrows = VImageNRows(src1[i]); if (VImageNColumns(src1[i]) > ncols) ncols = VImageNColumns(src1[i]); i++; if (i >= NSLICES) VError(" too many slices"); } nslices = i; i = nrows2 = ncols2 = 0; for (VFirstAttr (list2, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & src2[i]); if (VPixelRepn(src2[i]) != VShortRepn) continue; if (VImageNRows(src2[i]) > nrows2) nrows2 = VImageNRows(src2[i]); if (VImageNColumns(src2[i]) > ncols2) ncols2 = VImageNColumns(src2[i]); i++; if (i >= NSLICES) VError(" too many slices"); } nslices2 = i; if (nslices2 != nslices) VError(" inconsistent number of slices: %d %d",nslices,nslices2); if (nrows2 != nrows) VError(" inconsistent number of rows: %d %d",nrows,nrows2); if (ncols2 != ncols) VError(" inconsistent number of cols: %d %d",ncols,ncols2); /* count number of voxels */ n = 0; for (b=0; b<nslices; b++) { if (VImageNRows(src1[b]) < 2) continue; if (VImageNRows(src2[b]) < 2) continue; for (r=0; r<nrows; r++) { for (c=0; c<ncols; c++) { if (VPixel(src1[b],0,r,c,VShort) < minval) continue; if (VPixel(src2[b],0,r,c,VShort) < minval) continue; if (VGetPixel(mask,b,r,c) < 0.5) continue; n++; } } } fprintf(stderr," nvoxels: %ld\n",(long)n); /* ** voxel addresses */ map = VCreateImage(1,5,n,VShortRepn); if (map == NULL) VError(" error allocating addr map"); VFillImage(map,VAllBands,0); VCopyImageAttrs (src1[0],map); VPixel(map,0,3,0,VShort) = nslices; VPixel(map,0,3,1,VShort) = nrows; VPixel(map,0,3,2,VShort) = ncols; i = 0; for (b=0; b<nslices; b++) { if (VImageNRows(src1[b]) < 2) continue; if (VImageNRows(src2[b]) < 2) continue; for (r=0; r<nrows; r++) { for (c=0; c<ncols; c++) { if (VPixel(src1[b],0,r,c,VShort) < minval) continue; if (VPixel(src2[b],0,r,c,VShort) < minval) continue; if (VGetPixel(mask,b,r,c) < 0.5) continue; VPixel(map,0,0,i,VShort) = b; VPixel(map,0,1,i,VShort) = r; VPixel(map,0,2,i,VShort) = c; i++; } } } return map; }
int main(int argc, char *argv[]) { static VArgVector in_files; static VString out_filename; static VBoolean in_found, out_found; static VOptionDescRec options[] = { {"in", VStringRepn, 0, & in_files, & in_found, NULL, "Input files" }, {"out", VStringRepn, 1, & out_filename, & out_found, NULL, "Output file" } }; FILE *fp = NULL; VStringConst in_filename; VAttrList list = NULL, out_list = NULL; VImage *design = NULL, tmp = NULL, dest = NULL; VAttrListPosn posn; int i, nimages; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vcatdesign 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); } /* Read each input file */ nimages = in_files.number; design = (VImage *) VMalloc(sizeof(VImage) * nimages); for(i = 0; i < nimages; i++) { in_filename = ((VStringConst *) in_files.vector)[i]; fprintf(stderr, " reading: %s\n", in_filename); fp = VOpenInputFile(in_filename, TRUE); list = VReadFile(fp, NULL); if(! list) VError("Error reading file %s", in_filename); fclose(fp); for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & tmp); if(VPixelRepn(tmp) == VFloatRepn || VPixelRepn(tmp) == VDoubleRepn) { design[i] = tmp; break; } } } /* process */ dest = ConcatDesigns(design, nimages); out_list = VCreateAttrList(); VAppendAttr(out_list, "image", NULL, VImageRepn, dest); /* Output: */ VHistory(VNumber(options), options, prg_name, &list, &out_list); fp = VOpenOutputFile(out_filename, TRUE); if(!fp) VError(" error opening outout file %s", out_filename); if(! VWriteFile(fp, out_list)) exit(1); fprintf(stderr, "%s: done.\n", argv[0]); return 0; }
VAttrList VNCM(VAttrList list,VImage mask,VShort minval,VShort first,VShort length,VFloat threshold) { VAttrList out_list=NULL; VAttrListPosn posn; VImage src[NSLICES],map=NULL; VImage dest=NULL; size_t b,r,c,i,j,i1,n,m,nt,last,ntimesteps,nrows,ncols,nslices; gsl_matrix_float *mat=NULL; float *A=NULL,*ev=NULL; /* ** get image dimensions */ i = ntimesteps = nrows = ncols = 0; for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & src[i]); if (VPixelRepn(src[i]) != VShortRepn) continue; if (VImageNBands(src[i]) > ntimesteps) ntimesteps = VImageNBands(src[i]); if (VImageNRows(src[i]) > nrows) nrows = VImageNRows(src[i]); if (VImageNColumns(src[i]) > ncols) ncols = VImageNColumns(src[i]); i++; if (i >= NSLICES) VError(" too many slices"); } nslices = i; /* get time steps to include */ if (length < 1) length = ntimesteps-2; last = first + length -1; if (last >= ntimesteps) last = ntimesteps-1; if (first < 0) first = 1; nt = last - first + 1; i1 = first+1; if (nt < 2) VError(" not enough timesteps, nt= %d",nt); fprintf(stderr,"# ntimesteps: %d, first= %d, last= %d, nt= %d\n", (int)ntimesteps,(int)first,(int)last,(int)nt); /* count number of voxels */ n = 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++) { if (VPixel(src[b],i1,r,c,VShort) < minval) continue; if (VGetPixel(mask,b,r,c) < 0.5) continue; n++; } } } fprintf(stderr," nvoxels: %ld\n",(long)n); /* ** voxel addresses */ map = VCreateImage(1,5,n,VFloatRepn); if (map == NULL) VError(" error allocating addr map"); VFillImage(map,VAllBands,0); VPixel(map,0,3,0,VFloat) = nslices; VPixel(map,0,3,1,VFloat) = nrows; VPixel(map,0,3,2,VFloat) = ncols; i = 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++) { if (VPixel(src[b],i1,r,c,VShort) < minval) continue; if (VGetPixel(mask,b,r,c) < 0.5) continue; VPixel(map,0,0,i,VFloat) = b; VPixel(map,0,1,i,VFloat) = r; VPixel(map,0,2,i,VFloat) = c; i++; } } } /* ** avoid casting to float, copy data to matrix */ mat = gsl_matrix_float_calloc(n,nt); for (i=0; i<n; i++) { b = VPixel(map,0,0,i,VFloat); r = VPixel(map,0,1,i,VFloat); c = VPixel(map,0,2,i,VFloat); float *ptr = gsl_matrix_float_ptr(mat,i,0); int k; j = 0; for (k=first; k<=last; k++) { if (j >= mat->size2) VError(" j= %d %d",j,mat->size2); if (k >= VImageNBands(src[b])) VError(" k= %d %d",k, VImageNBands(src[b])); *ptr++ = (float) VPixel(src[b],k,r,c,VShort); j++; } } /* ** compute similarity matrix */ m = (n*(n+1))/2; fprintf(stderr," matrix computation, n= %ld...\n",(long)n); A = (float *) calloc(m,sizeof(float)); if (!A) VError(" err allocating correlation matrix"); memset(A,0,m*sizeof(float)); size_t progress=0; #pragma omp parallel for shared(progress) private(j) schedule(guided) firstprivate(mat,A) for (i=0; i<n; i++) { if (i%100 == 0) fprintf(stderr," %d00\r",(int)(++progress)); const float *arr1 = gsl_matrix_float_const_ptr(mat,i,0); for (j=0; j<i; j++) { const float *arr2 = gsl_matrix_float_const_ptr(mat,j,0); const double v = Correlation(arr1,arr2,nt); const size_t k=j+i*(i+1)/2; if (k >= m) VError(" illegal addr k= %d, m= %d",k,m); A[k] = (float)v; } } fprintf(stderr," matrix done.\n"); gsl_matrix_float_free(mat); /* ** eigenvector centrality */ ev = (float *) VCalloc(n,sizeof(float)); DegreeCentrality(A,ev,n,threshold); dest = WriteOutput(src[0],map,nslices,nrows,ncols,ev,n); VSetAttr(VImageAttrList(dest),"name",NULL,VStringRepn,"DegreeCM"); out_list = VCreateAttrList(); VAppendAttr(out_list,"image",NULL,VImageRepn,dest); return out_list; }
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 ) ); }
VImage SimilarityMatrix(VAttrList list, VImage mask, VShort minval, VShort fisher) { VAttrListPosn posn; VImage src[NSLICES], dest = NULL, map = NULL; int b, r, c, i, j, n, ntimesteps, nrows, ncols, nslices; double u, x, y, smin, smax, tiny = 1.0e-6; gsl_matrix_float *mat = NULL; /* ** get image dimensions */ i = ntimesteps = nrows = ncols = 0; for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src[i]); if(VPixelRepn(src[i]) != VShortRepn) continue; if(VImageNBands(src[i]) > ntimesteps) ntimesteps = VImageNBands(src[i]); if(VImageNRows(src[i]) > nrows) nrows = VImageNRows(src[i]); if(VImageNColumns(src[i]) > ncols) ncols = VImageNColumns(src[i]); i++; if(i >= NSLICES) VError(" too many slices"); } nslices = i; n = 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++) { if(VPixel(src[b], 0, r, c, VShort) < minval) continue; if(VGetPixel(mask, b, r, c) < 0.1) continue; n++; } } } fprintf(stderr, " n: %d\n", n); /* ** voxel addresses */ map = VCreateImage(1, 5, n, VFloatRepn); VFillImage(map, VAllBands, 0); VPixel(map, 0, 3, 0, VFloat) = nslices; VPixel(map, 0, 3, 1, VFloat) = nrows; VPixel(map, 0, 3, 2, VFloat) = ncols; i = 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++) { if(VPixel(src[b], 0, r, c, VShort) < minval) continue; if(VGetPixel(mask, b, r, c) < 0.1) continue; VPixel(map, 0, 0, i, VFloat) = b; VPixel(map, 0, 1, i, VFloat) = r; VPixel(map, 0, 2, i, VFloat) = c; i++; } } } /* ini output image */ if(n < 8000) dest = VCreateImage(1, n, n, VFloatRepn); else if(n >= 8000 && n < 10000) dest = VCreateImage(1, n, n, VShortRepn); else dest = VCreateImage(1, n, n, VSByteRepn); VFillImage(dest, VAllBands, 0); smin = VPixelMinValue(dest); smax = VPixelMaxValue(dest); /* ** avoid casting to float, copy data to matrix */ mat = gsl_matrix_float_calloc(n, ntimesteps); for(i = 0; i < n; i++) { b = VPixel(map, 0, 0, i, VFloat); r = VPixel(map, 0, 1, i, VFloat); c = VPixel(map, 0, 2, i, VFloat); float *ptr = gsl_matrix_float_ptr(mat, i, 0); int k; j = 0; for(k = 0; k < ntimesteps; k++) { if(j >= mat->size2) VError(" j= %d %d", j, mat->size2); if(k >= VImageNBands(src[b])) VError(" k= %d %d", k, VImageNBands(src[b])); *ptr++ = (float) VPixel(src[b], k, r, c, VShort); j++; } } /* ** main process loop */ for(i = 0; i < n; i++) { if(i % 50 == 0) fprintf(stderr, " i: %4d\r", i); const float *arr1 = gsl_matrix_float_const_ptr(mat, i, 0); for(j = 0; j <= i; j++) { const float *arr2 = gsl_matrix_float_const_ptr(mat, j, 0); u = Correlation(arr1, arr2, ntimesteps); /* Fisher r-to-z transform */ if(fisher) { x = 1 + u; y = 1 - u; if(x < tiny) x = tiny; if(y < tiny) y = tiny; u = 0.5 * log(x / y); } if(VPixelRepn(dest) != VFloatRepn) { u *= smax; if(u < smin) u = smin; if(u > smax) u = smax; } VSetPixel(dest, 0, i, j, u); VSetPixel(dest, 0, j, i, u); } } VCopyImageAttrs(src[0], dest); VSetAttr(VImageAttrList(dest), "similarity_metric", NULL, VStringRepn, "corr_pearson"); VSetAttr(VImageAttrList(dest), "name", NULL, VStringRepn, "similarity_matrix"); VSetAttr(VImageAttrList(dest), "map", NULL, VImageRepn, map); return dest; }
VAttrList GetListInfo(VString in_filename, ListInfo *linfo) { VAttrList list = NULL; VAttrListPosn posn; FILE *in_file = NULL; VString str, voxel = NULL; VRepnKind repn = VShortRepn; int ntimesteps, nrows, ncols; int id, j, itr, found, nobject, nbands; VImageInfo *imageInfo = NULL; in_file = VOpenInputFile(in_filename, TRUE); if(!in_file) VError("error opening file %s", in_filename); 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)); itr = ntimesteps = nrows = ncols = 0; nobject = nbands = found = id = 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"); linfo->ntimesteps = linfo->nrows = linfo->ncols = 0; if(imageInfo[nbands].repn == VShortRepn) { found = 1; repn = imageInfo[nbands].repn; if(imageInfo[nbands].nbands > ntimesteps) ntimesteps = imageInfo[nbands].nbands; if(imageInfo[nbands].nrows > nrows) nrows = imageInfo[nbands].nrows; if(imageInfo[nbands].ncolumns > ncols) ncols = imageInfo[nbands].ncolumns; if(voxel == NULL) voxel = imageInfo[nbands].voxel; /* check if slice contains non-zero data */ linfo->zero[nbands] = 1; if(imageInfo[nbands].nrows < 2) linfo->zero[nbands] = 0; linfo->info[id] = imageInfo[nbands]; itr = imageInfo[nbands].repetition_time; id++; nbands++; } nobject++; } fclose(in_file); if(!found) VError(" couldn't find functional data"); linfo->ntimesteps = ntimesteps; linfo->nrows = nrows; linfo->ncols = ncols; linfo->nslices = id; linfo->itr = itr; linfo->repn = repn; linfo->voxel = voxel; linfo->filename = VNewString(in_filename); return list; }
/* ** 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"); }
int main (int argc,char *argv[]) { static VString filename = ""; static VShort first = 2; static VShort length = 0; static VFloat threshold = 0.5; static VShort minval = 0; static VShort nproc = 4; static VOptionDescRec options[] = { {"mask",VStringRepn,1,(VPointer) &filename,VOptionalOpt,NULL,"mask"}, {"minval",VShortRepn,1,(VPointer) &minval,VOptionalOpt,NULL,"signal threshold"}, {"first",VShortRepn,1,(VPointer) &first,VOptionalOpt,NULL,"first timestep to use"}, {"length",VShortRepn,1,(VPointer) &length,VOptionalOpt,NULL,"length of time series to use, '0' to use full length"}, {"threshold",VFloatRepn,1,(VPointer) &threshold,VOptionalOpt,NULL,"threshold"}, {"j",VShortRepn,1,(VPointer) &nproc,VOptionalOpt,NULL,"number of processors to use, '0' to use all"} }; FILE *in_file,*out_file,*fp; VAttrList list=NULL,list1=NULL,out_list=NULL; VAttrListPosn posn; VImage mask=NULL; char *prg = "vncm"; VParseFilterCmd (VNumber (options),options,argc,argv,&in_file,&out_file); /* ** read mask */ fp = VOpenInputFile (filename, TRUE); list1 = VReadFile (fp, NULL); if (! list1) VError("Error reading mask file"); fclose(fp); for (VFirstAttr (list1, & posn); VAttrExists (& posn); VNextAttr (& posn)) { if (VGetAttrRepn (& posn) != VImageRepn) continue; VGetAttrValue (& posn, NULL,VImageRepn, & mask); if (VPixelRepn(mask) != VBitRepn && VPixelRepn(mask) != VUByteRepn && VPixelRepn(mask) != VShortRepn) { mask = NULL; continue; } } if (mask == NULL) VError(" no mask found"); /* ** read functional data */ if (! (list = VReadFile (in_file, NULL))) exit (1); fclose(in_file); /* omp-stuff */ #ifdef _OPENMP int num_procs=omp_get_num_procs(); if (nproc > 0 && nproc < num_procs) num_procs = nproc; printf("using %d cores\n",(int)num_procs); omp_set_num_threads(num_procs); #endif /* _OPENMP */ /* ** process */ out_list = VNCM(list,mask,minval,first,length,threshold); VHistory(VNumber(options),options,prg,&list,&out_list); if (! VWriteFile (out_file, out_list)) exit (1); fprintf (stderr, "%s: done.\n", argv[0]); return 0; }