示例#1
0
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;
}
示例#2
0
/*
** read several rows of data from a file
*/
void
VReadObjectData (FILE *fp,int object_id,VImage *image)
{
  static VImageInfo imageInfo;
  static VAttrList list=NULL;

  fseek(fp,0L,SEEK_SET);
  if (! ReadHeader (fp)) VError("error reading header"); 


  if (! (list = ReadAttrList (fp)))  
    VError("error reading attr list"); 
  
  if (! VGetImageInfo(fp,list,object_id,&imageInfo))
    VError(" error reading image info");

  if (imageInfo.nbands != VImageNBands((*image)))
    VError("incorrect number of bands");
  if (imageInfo.nrows != VImageNRows((*image))) 
    VError("incorrect number of rows");
  if (imageInfo.ncolumns != VImageNColumns((*image)))
    VError("incorrect number of columns");
  if (imageInfo.repn != VPixelRepn((*image))) 
    VError("incorrect pixel representation");
  
  if (! VReadBlockData (fp,&imageInfo,0,imageInfo.nrows,image))
    VError(" error reading data");
}
示例#3
0
VBoolean VImageStats(VImage src, VBand band, VDouble *pmin, VDouble *pmax,
		      VDouble *pmean, VDouble *pvar)
{
    int npixels, i;
    VPointer first_pixel;
    VDouble pixel, tmin, tmax, tmean, tvar;

    /* Prepare to iterate over the specified band(s) of source pixels: */
    if (! VSelectBand ("VImageStats", src, band, & npixels, & first_pixel))
	return FALSE;

    /* Initialize accumulators: */
    tmin = tmax = tmean = tvar = 0.0;

    /* Tally pixels: */
    switch (VPixelRepn (src)) {
    case VBitRepn:	TallyStats (VBit); break;
    case VUByteRepn:	TallyStats (VUByte); break;
    case VSByteRepn:	TallyStats (VSByte); break;
    case VShortRepn:	TallyStats (VShort); break;
    case VLongRepn:	TallyStats (VLong); break;
    case VFloatRepn:	TallyStats (VFloat); break;
    case VDoubleRepn:	TallyStats (VDouble); break;
    default:		break;
    }

    /* Compute and return the final results: */
    tmean /= npixels;
    tvar = tvar / npixels - (tmean * tmean);
    if (pmin) *pmin = tmin;
    if (pmax) *pmax = tmax;
    if (pmean) *pmean = tmean;
    if (pvar) *pvar = tvar;
    return TRUE;
}
示例#4
0
文件: QuickMorph.c 项目: Rollmops/via
/*!
  \fn VImage VDTDilate(VImage src,VImage dest,VDouble radius)
  \brief 3D morphological dilation
  \param src   input image (bit repn)
  \param dest  output image (bit repn)
  \param radius radius of the spherical structural element
*/
VImage
VDTDilate(VImage src,VImage dest,VDouble radius)
{
  VImage float_image;
  VBit *bin_pp;
  VFloat *float_pp;
  int i,nbands,nrows,ncols,npixels;

  if (VPixelRepn(src) != VBitRepn) 
    VError("Input image must be of type VBit");

  nbands  = VImageNBands(src);
  nrows   = VImageNRows(src);
  ncols   = VImageNColumns(src);
  npixels = nbands * nrows * ncols;

  float_image = VChamferDist3d(src,NULL,VFloatRepn);
  if (! float_image) 
    VError("VDTDilate failed.\n");

  dest = VSelectDestImage("VDTDilate",dest,nbands,nrows,ncols,VBitRepn);
  if (! dest) return NULL;

  float_pp  = (VFloat *) VPixelPtr(float_image,0,0,0);
  bin_pp    = (VBit *) VPixelPtr(dest,0,0,0);
  for (i=0; i<npixels; i++)
    *bin_pp++ = ((*float_pp++ > radius) ? 0 : 1);

  VDestroyImage(float_image);

  VCopyImageAttrs (src,dest);
  return dest;
}
示例#5
0
VImage VCombineBands (int nels, VImage src_images[], VBand src_bands[],
		      VImage dest)
{
  int n, i;
  VImage result, src = src_images[0];

  /* Count the number of bands needed in the destination image: */
  for (i = n = 0; i < nels; i++)
    n += (src_bands[i] == VAllBands) ? VImageNBands (src_images[i]) : 1;

  /* Check or allocate the destination image: */
  result = VSelectDestImage ("VCombineBands", dest, n,
			     VImageNRows (src), VImageNColumns (src), 
			     VPixelRepn (src));
  if (! result)
    return NULL;

  /* Copy each source band into the destination image: */
  for (i = n = 0; i < nels; i++) {
    if (! VCopyBand (src_images[i], src_bands[i], result, n)) {
      if (result != dest)
	VDestroyImage (result);
      return NULL;
    }
    n += (src_bands[i] == VAllBands) ? VImageNBands (src_images[i]) : 1;
  }
  return result;
}
示例#6
0
VImage VSelectDestImage (VStringConst routine, VImage dest,
			 int nbands, int nrows, int ncolumns,
			 VRepnKind pixel_repn)
{
  /* If no destination image was specified, allocate one: */
  if (! dest)
    return VCreateImage (nbands, nrows, ncolumns, pixel_repn);

    /* Otherwise check that the destination provided has the appropriate
       characteristics: */
  if (VImageNBands (dest) != nbands) {
    VWarning ("%s: Destination image has %d bands; %d expected",
	      routine, VImageNBands (dest), nbands);
    return NULL;
  }
  if (VImageNRows (dest) != nrows) {
    VWarning ("%s: Destination image has %d rows; %d expected",
	      routine, VImageNRows (dest), nrows);
    return NULL;
  }
  if (VImageNColumns (dest) != ncolumns) {
    VWarning ("%s: Destination image has %d columns; %d expected",
	      routine, VImageNColumns (dest), ncolumns);
    return NULL;
  }
  if (VPixelRepn (dest) != pixel_repn) {
    VWarning ("%s: Destination image has %s pixels; %s expected", routine, 
	      VPixelRepnName (dest), VRepnName (pixel_repn));
    return NULL;
  }
  return dest;
}
示例#7
0
VBoolean VFillImage (VImage image, VBand band, VDoublePromoted value)
{
  int i, npixels;
  VPointer first_pixel;

#define Fill(type)					\
	{						\
	    type d = value, *pp = first_pixel;		\
	    for (i = npixels; i > 0; i--)		\
		*pp++ = d;				\
	}

  /* Locate the specified band(s) in the image: */
  if (! VSelectBand ("VFillImage", image, band, & npixels, & first_pixel))
    return FALSE;

  /* To avoid surprises when filling integer pixels, round the fill
     value to the nearest integer: */
  if (VIsIntegerRepn (VPixelRepn (image)))
    value += (value > 0.0) ? 0.5 : -0.5;

  /* The memset() routine is probably the fastest way of filling memory, but
     it can only be used here in certain situations. We only check for
     these, the most straightforward of the situations:
     (a) pixel values occupy a byte
     (b) the value to be filled is all 0's
     It is when the value to be filled is all 0's and the pixel
     representation is floating point that we take advantage of the
     assumption that 0.0 is represented as all-bits-zero. */
  if (VPixelSize (image) == 1 || value == 0.0)
    memset (first_pixel, (int) value, npixels * VPixelSize (image));

  /* Otherwise, fill by looping over all pixels: */
  else
    switch (VPixelRepn (image)) {
    case VBitRepn:	Fill (VBit);	break;
    case VUByteRepn:	Fill (VUByte);  break;
    case VSByteRepn:	Fill (VSByte);  break;
    case VShortRepn:	Fill (VShort);  break;
    case VLongRepn:	Fill (VLong);   break;
    case VFloatRepn:	Fill (VFloat);  break;
    case VDoubleRepn:	Fill (VDouble); break;
    default: break;
    }

  return TRUE;
}
示例#8
0
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;
}
示例#9
0
文件: vsimmat.c 项目: Rollmops/lipsia
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;
}
示例#10
0
文件: SelSlices.c 项目: Rollmops/via
VImage 
VSelSlices (VImage src,VImage dest,VShort first,VShort last)
{
  int nbands,nrows,ncols,nbands_neu,npixels;
  int i,b,bn;
  VRepnKind repn;

  nbands = VImageNBands(src);
  nrows  = VImageNRows(src);
  ncols  = VImageNColumns(src);
  repn   = VPixelRepn(src);

  if (first < 0) VError("VSelSlices: parameter <first> must be positive");
  if (last < 0) last = nbands-1;
  if (last >= nbands) last = nbands-1;


  if (first > last)
    VError("VSelSlices: <first> must be less than <last>.");

  nbands_neu = last - first + 1;
  if (dest == NULL)
    dest = VCreateImage(nbands_neu,nrows,ncols,repn);

  npixels = nrows * ncols;

#define SelSlices(type) \
{ \
  type *src_pp, *dest_pp; \
  bn = 0; \
  for (b=first; b<=last; b++) { \
    src_pp  = VPixelPtr(src,b,0,0); \
    dest_pp = VPixelPtr(dest,bn,0,0); \
    for (i=0; i<npixels; i++) *dest_pp++ = *src_pp++; \
    bn++; \
  } \
}

      
  /* Instantiate the macro once for each source pixel type: */
  switch (repn) {
  case VBitRepn:	SelSlices (VBit);	break;
  case VUByteRepn:	SelSlices (VUByte);	break;
  case VSByteRepn:	SelSlices (VSByte);	break;
  case VShortRepn:	SelSlices (VShort);	break;
  case VLongRepn:	SelSlices (VLong);	break;
  case VFloatRepn:	SelSlices (VFloat);	break;
  case VDoubleRepn:	SelSlices (VDouble);	break;
  default: ;
  }


  /* Let the destination inherit any attributes of the source image: */
  VCopyImageAttrs (src, dest);
  return dest;
}
示例#11
0
void VSetPixel(VImage image, int band, int row, int column, VDoublePromoted value)
{
  VPointer p = VPixelPtr (image, band, row, column);
  switch (VPixelRepn (image)) {
  case VBitRepn:	*(VBit *) p = value; break;
  case VUByteRepn:	*(VUByte *) p = value; break;
  case VSByteRepn:	*(VSByte *) p = value; break;
  case VShortRepn:	*(VShort *) p = value; break;
  case VLongRepn:	*(VLong *) p = value; break;
  case VFloatRepn:	*(VFloat *) p = value; break;
  case VDoubleRepn:	*(VDouble *) p = value; break;
  default:		VError("VSetPixel: %s images not supported", VPixelRepnName (image));
  }
}
示例#12
0
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;
}
示例#13
0
VDouble VGetPixel (VImage image, int band, int row, int column)
{
  VPointer p = VPixelPtr (image, band, row, column);
  switch (VPixelRepn (image)) {
  case VBitRepn:	return (VDouble) *(VBit *) p;
  case VUByteRepn:	return (VDouble) *(VUByte *) p;
  case VSByteRepn:	return (VDouble) *(VSByte *) p;
  case VShortRepn:	return (VDouble) *(VShort *) p;
  case VLongRepn:	return (VDouble) *(VLong *) p;
  case VFloatRepn:	return (VDouble) *(VFloat *) p;
  case VDoubleRepn:	return (VDouble) *(VDouble *) p;
  default:		VError("VGetPixel: %s images not supported", VPixelRepnName (image));
  }
  return 0.0;		/* to make lint happy */
}
示例#14
0
static VAttrList VImageEncodeAttrMethod (VPointer value, size_t *lengthp)
{
  VImage image = value;
  VAttrList list;
  size_t length;

#define OptionallyPrepend(value, name)				\
	if (value != 1)							\
	    VPrependAttr (list, name, NULL, VLongRepn, (VLong) value)

  /* Temporarily prepend several attributes to the image's attribute list: */
  if ((list = VImageAttrList (image)) == NULL)
    list = VImageAttrList (image) = VCreateAttrList ();
  VPrependAttr (list, VRepnAttr, VNumericRepnDict,
		VLongRepn, (VLong) VPixelRepn (image));
  VPrependAttr (list, VNColumnsAttr, NULL,
		VLongRepn, (VLong) VImageNColumns (image));
  VPrependAttr (list, VNRowsAttr, NULL,
		VLongRepn, (VLong) VImageNRows (image));
  OptionallyPrepend (VImageNComponents (image), VNComponentsAttr);
  OptionallyPrepend (VImageNColors (image), VNColorsAttr);
  OptionallyPrepend (VImageNViewpoints (image), VNViewpointsAttr);
  OptionallyPrepend (VImageNFrames (image), VNFramesAttr);
  OptionallyPrepend (VImageNBands (image), VNBandsAttr);

  /* Compute the file space needed for the image's binary data: */
  length = VImageNPixels (image);
  if (VPixelRepn (image) == VBitRepn)
    length = (length + 7) / 8;
  else length *= VPixelPrecision (image) / 8;
  *lengthp = length;

  return list;

#undef OptionallyPrepend
}
示例#15
0
VImage VCopyImageAttrs (VImage src, VImage dest)
{
  VAttrList list;

  if (src == dest)
    return dest;
  if (! dest) {
    dest = VCreateImage (VImageNBands (src), VImageNRows (src),
			 VImageNColumns (src), VPixelRepn (src));
    if (! dest)
      return NULL;
  }

  /* Clone the source image's attribute list if it isn't empty: */
  if (! VAttrListEmpty (VImageAttrList (src))) {
    list = VImageAttrList (dest);
    VImageAttrList (dest) = VCopyAttrList (VImageAttrList (src));
  } else if (! VAttrListEmpty (VImageAttrList (dest))) {
    list = VImageAttrList (dest);
    VImageAttrList (dest) = VCreateAttrList ();
  } else list = NULL;
  if (list)
    VDestroyAttrList (list);

  /* Preserve band interpretation attributes only if the source and
     destination images have the same number of bands: */
  if (VImageNBands (src) > 1 && VImageNBands (dest) == VImageNBands (src)) {
    VImageNFrames (dest) = VImageNFrames (src);
    VImageNViewpoints (dest) = VImageNViewpoints (src);
    VImageNColors (dest) = VImageNColors (src);
    VImageNComponents (dest) = VImageNComponents (src);
  } else {
    VExtractAttr (VImageAttrList (dest), VFrameInterpAttr, NULL,
		  VBitRepn, NULL, FALSE);
    VExtractAttr (VImageAttrList (dest), VViewpointInterpAttr, NULL,
		  VBitRepn, NULL, FALSE);
    VExtractAttr (VImageAttrList (dest), VColorInterpAttr, NULL,
		  VBitRepn, NULL, FALSE);
    VExtractAttr (VImageAttrList (dest), VComponentInterpAttr, NULL,
		  VBitRepn, NULL, FALSE);
    VImageNComponents (dest) = VImageNColors (dest) = 
      VImageNViewpoints (dest) = 1;
    VImageNFrames (dest) = VImageNBands (dest);
  }
  return dest;
}
示例#16
0
文件: vtopgm.c 项目: Rollmops/via
int main (int argc, char *argv[])
{
    static VShort band;
    static VOptionDescRec options[] = {
	{ "band", VShortRepn, 1, & band, VOptionalOpt, NULL,"Image band to convert" }
    };
    FILE *infile, *outfile;
    int nimages;
    VAttrList attributes;
    VImage *images, image;
    char prg[50];	
    sprintf(prg,"vtopgm V%s", getVersion());
    fprintf (stderr, "%s\n", prg);


    /* Parse command line arguments and identify the input and output files */
    VParseFilterCmd (VNumber (options), options, argc, argv,
		     & infile, & outfile);

    /* Read all images from the input file */
    if ((nimages = VReadImages (infile, & attributes, & images)) == 0)
	exit (EXIT_FAILURE);	/* no images found */
    fclose (infile);

    /* Ensure that the specified band exists: */
    if (nimages != 1)
	VWarning ("Only the first of %d images will be converted", nimages);
    if (band < 0 || band >= VImageNBands (images[0]))
	VError ("Image of %d band(s) has no band %d",
		VImageNBands (images[0]), band);

    /* Convert the image to UByte: */
    if (VPixelRepn(images[0]) != VUByteRepn) {
	image = VConvertImageRange (images[0], NULL, (VBand) band, VUByteRepn);
	if (! image)
	    exit (EXIT_FAILURE);
	band = 0;
    } else image = images[0];

    /* Output the first image as a pgm file. */
    WritePgmFile (outfile, image, (VBand) band);

    return EXIT_SUCCESS;
}
示例#17
0
VBoolean VCopyBand (VImage src, VBand src_band, VImage dest, VBand dest_band)
{
  int nbands, src_npixels, dest_npixels;
  VPointer src_pixels, dest_pixels;

  /* The destination image must exist: */
  if (! dest) {
    VWarning ("VCopyBand: No destination specified");
    return FALSE;
  }

  /* VAllBands not accepted for destination band: */
  if (dest_band < 0 || dest_band >= VImageNBands (dest)) {
    VWarning ("VCopyBand: Band %d referenced in image of %d bands",
	      (int) dest_band, (int) VImageNBands (dest));
    return FALSE;
  }

  /* Ensure that the destination image has the appropriate dimensions
     and pixel representation: */
  nbands = dest_band;
  if (src_band == VAllBands)
    nbands += VImageNBands (src) - 1;
  if (nbands < VImageNBands (dest))
    nbands = VImageNBands (dest);
  if (! VSelectDestImage ("VCopyBand", dest, nbands, VImageNRows (src),
			  VImageNColumns (src), VPixelRepn (src)))
    return FALSE;

  /* Locate the specified source and destination bands: */
  if (! VSelectBand ("VCopyBand", src, src_band,
		     & src_npixels, & src_pixels))
    return FALSE;
  if (! VSelectBand ("VCopyBand", dest, dest_band,
		     & dest_npixels, & dest_pixels))
    return FALSE;

  /* Copy from the source band to the destination band: */
  memcpy (dest_pixels, src_pixels, src_npixels * VPixelSize (src));

  return TRUE;
}
示例#18
0
VImage VCopyImagePixels (VImage src, VImage dest, VBand band)
{
  int npixels;
  VPointer src_pixels;
  VImage result;

  /* Locate the source and destination of the copy: */
  if (! VSelectBand ("VCopyImagePixels", src, band, & npixels, & src_pixels))
    return NULL;
  result = VSelectDestImage ("VCopyImagePixels", dest,
			     band == VAllBands ? VImageNBands (src) : 1,
			     VImageNRows (src), VImageNColumns (src),
			     VPixelRepn (src));
  if (! result)
    return NULL;

  /* Copy pixel values from src to dest: */
  memcpy (VImageData (result), src_pixels, npixels * VPixelSize (src));

  return result;
}
示例#19
0
文件: vthin3d.c 项目: Rollmops/via
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;
}
示例#20
0
static VPointer VImageEncodeDataMethod (VPointer value, VAttrList list,
					size_t length, VBoolean *free_itp)
{
  VImage image = value;
  VAttrListPosn posn;
  size_t len;
  VPointer ptr;

  /* Remove the attributes prepended by the VImageEncodeAttrsMethod: */
  for (VFirstAttr (list, & posn);
       strcmp (VGetAttrName (& posn), VRepnAttr) != 0;
       VDeleteAttr (& posn)) ;
  VDeleteAttr (& posn);

  /* Pack and return pixel data: */
  if (! VPackData (VPixelRepn (image), VImageNPixels (image),
		   VImageData (image), VMsbFirst, & len, & ptr, free_itp))
    return NULL;
  if (len != length)
    VError ("VImageEncodeDataMethod: Encoded data has unexpected length");
  return ptr;
}
示例#21
0
VImage VCombineBandsVa (VImage dest, ...)
{
  va_list args;
  VImage src, result;
  int nbands;
  VBand src_band, dest_band;

  /* Count the number of bands to be combined: */
  va_start (args, dest);
  for (nbands = 0; (src = va_arg (args, VImage)) != 0; nbands +=
	 (va_arg (args, VBand) == VAllBands) ? VImageNBands (src) : 1) ;
  va_end (args);

  /* Check or allocate the destination image: */
  va_start (args, dest);
  src = va_arg (args, VImage);
  va_end (args);
  result = VSelectDestImage ("VCombineBandsVa", dest, nbands,
			     VImageNRows (src), VImageNColumns (src),
			     VPixelRepn (src));
  if (! result)
    return NULL;

  /* Copy each source band into the destination image: */
  va_start (args, dest);
  for (dest_band = 0; (src = va_arg (args, VImage)) != 0; ) {
    src_band = va_arg (args, VBand);
    if (! VCopyBand (src, src_band, result, dest_band)) {
      if (result != dest)
	VDestroyImage (result);
      return NULL;
    }
    dest_band += (src_band == VAllBands) ? VImageNBands (src) : 1;
  }
  va_end (args);
  return result;
}
示例#22
0
文件: vflip3d.c 项目: Rollmops/lipsia
/*
 *  Flip3dImage
 *
 */
VImage Flip3dImage(VImage src, VImage dest, VBand band, VBoolean x_axis, VBoolean y_axis, VBoolean z_axis) {
    int nx, ny, nz;
    int i, j, k;
    nx = VImageNColumns(src);
    ny = VImageNRows(src);
    nz = VImageNBands(src);
    /* Check the destination image.
       If it is NULL, then create one of the appropriate size and type. */
    dest = VSelectDestImage("Flip3dImage", dest,
                            nz, ny, nx, VPixelRepn(src));
    if(! dest)
        return NULL;
    for(i = 0; i < nx; i++)
        for(j = 0; j < ny; j++)
            for(k = 0; k < nz; k++) {
                if(! x_axis && ! y_axis && ! z_axis)
                    VSetPixel(dest, k, j, i, VGetPixel(src, k, j, i));
                if(x_axis && ! y_axis && ! z_axis)
                    VSetPixel(dest, k, j, i, VGetPixel(src, k, j, nx - i - 1));
                if(! x_axis &&   y_axis && ! z_axis)
                    VSetPixel(dest, k, j, i, VGetPixel(src, k, ny - j - 1, i));
                if(! x_axis && ! y_axis &&   z_axis)
                    VSetPixel(dest, k, j, i, VGetPixel(src, nz - k - 1, j, i));
                if(x_axis &&   y_axis && ! z_axis)
                    VSetPixel(dest, k, j, i, VGetPixel(src, k, ny - j - 1, nx - i - 1));
                if(x_axis && ! y_axis &&   z_axis)
                    VSetPixel(dest, k, j, i, VGetPixel(src, nz - k - 1, j, nx - i - 1));
                if(! x_axis &&   y_axis &&   z_axis)
                    VSetPixel(dest, k, j, i, VGetPixel(src, nz - k - 1, ny - j - 1, i));
                if(x_axis &&   y_axis &&   z_axis)
                    VSetPixel(dest, k, j, i, VGetPixel(src, nz - k - 1, ny - j - 1, nx - i - 1));
            };
    /* Let the destination inherit any attributes of the source image: */
    VCopyImageAttrs(src, dest);
    return dest;
};
示例#23
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;
}
示例#24
0
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;
}
示例#25
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;
}
示例#26
0
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;
}
示例#27
0
文件: vncm.c 项目: Rollmops/lipsia
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;
}
示例#28
0
文件: vncm.c 项目: Rollmops/lipsia
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;
}
示例#29
0
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 ) );
}
示例#30
0
文件: vsimmat.c 项目: Rollmops/lipsia
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;
}