Ejemplo n.º 1
0
/*!
\fn VImage VMapImageRange(VImage src,VImage dest,VRepnKind repn)
\brief
The minimum and maximum grey values of the input images are computed,
and a linear mapping is performed mapping the input minimum(maximum) 
of the input image to the min(max) value of the output pixel repn.
E.g. if the input image min is -17 and its max is +2376, and the output repn
is VUByteRepn, then the linear mapping function maps -17 to 0 and
+2376 to 255.
\param src   input image  (any repn)
\param dest  output image
\param repn  the output pixel repn (e.g. VUByteRepn)
*/
VImage
VMapImageRange(VImage src,VImage dest,VRepnKind repn)
{
  int i,nbands,nrows,ncols,npixels;
  float u,ymin,ymax,dmin,dmax,slope;

  if (repn == VDoubleRepn || repn == VLongRepn || repn == VSByteRepn)
    VError(" VMapImageRange: double, long and sbyte are not supported");

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

  dest = VSelectDestImage("VMapImageRange",dest,nbands,nrows,ncols,repn);
  if (! dest) VError(" err creating dest image");
  VFillImage(dest,VAllBands,0);


  ymin = VPixelMaxValue(src);
  ymax = VPixelMinValue(src);

  for (i=0; i<npixels; i++) {
    u = VGetPixelValue (src,i);
    if (u < ymin) ymin = u;
    if (u > ymax) ymax = u;
  }

  dmax = VPixelMaxValue(dest);
  dmin = VPixelMinValue(dest);

  slope = (dmax - dmin) / (ymax - ymin);

  for (i=0; i<npixels; i++) {
    u = VGetPixelValue (src,i);
    u = slope * (u - ymin);
    if (u < dmin) u = dmin;
    if (u > dmax) u = dmax;
    VSetPixelValue(dest,i,(VFloat) u);
  }

  VCopyImageAttrs (src, dest);
  return dest;
}
Ejemplo n.º 2
0
VBoolean VSelectBand (VStringConst routine, VImage image, VBand band,
		      int *npixels, VPointer *first_pixel)
{
  if (band == VAllBands) {
    if (npixels)
      *npixels = VImageNPixels (image);
    if (first_pixel)
      *first_pixel = VImageData (image);
  } else if (band >= 0 && band < VImageNBands (image)) {
    if (npixels)
      *npixels = VImageNRows (image) * VImageNColumns (image);
    if (first_pixel)
      *first_pixel = image->band_index[band][0];
  } else {
    VWarning ("%s: Band %d referenced in image of %d band(s)",
	      routine, band, VImageNBands (image));
    return FALSE;
  }
  return TRUE;
}
Ejemplo n.º 3
0
template <class T> void OpticalFlow (VImage S, VImage M, VImage G, VImage G2, VImage& V)
{
   const float EPSILON = 1;   /* threshold for denominator of optical flow */

   int Pixels;   /* number of pixels */

   T      *s, *m;    /* source and model data pointer */
   VFloat *g, *g2;   /* gradient data pointer         */
   VFloat *v;        /* optical flow data pointer     */

   float nom, denom;   /* fraction */

   int n;   /* index */


   /* get image size */
   Pixels = VImageNPixels (S);

   /* create optical flow */
   V = VCreateImage (VImageNBands (S), VImageNRows (S), VImageNColumns (S), VFloatRepn);


   /* compute optical flow */
   s  = (T*)      VPixelPtr (S,  0, 0, 0);
   m  = (T*)      VPixelPtr (M,  0, 0, 0);
   g  = (VFloat*) VPixelPtr (G,  0, 0, 0);
   g2 = (VFloat*) VPixelPtr (G2, 0, 0, 0);
   v  = (VFloat*) VPixelPtr (V,  0, 0, 0);
   for (n = 0; n < Pixels; ++n)
   {
      /* compute fraction */
      nom   = (float) (*(m++) - *(s++));
      denom = (float) (*(g2++) + (nom * nom));

      /* assign optical flow */
      if (denom < EPSILON) *(v++) = 0;
      else                 *(v++) = (VFloat) ((nom / denom) * *g);
      ++g;
   }

} /* OpticalFlow */
Ejemplo n.º 4
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;
}
Ejemplo n.º 5
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
}
Ejemplo n.º 6
0
int main(int argc, char *argv[]) {
    static VArgVector in_files1;
    static VArgVector in_files2;
    static VString out_filename;
    static VShort type = 1;
    static VBoolean gauss = FALSE;
    static VOptionDescRec options[] = {
        {"in1", VStringRepn, 0, & in_files1, VRequiredOpt, NULL, "Input files 1" },
        {"in2", VStringRepn, 0, & in_files2, VRequiredOpt, NULL, "Input files 2" },
        {"type", VShortRepn, 1, (VPointer) &type, VOptionalOpt, TypeDict, "output type"},
        {"gaussianize", VBooleanRepn, 1, (VPointer) &gauss, VOptionalOpt, NULL, "Whether to Gaussianize"},
        {"out", VStringRepn, 1, & out_filename, VRequiredOpt, NULL, "Output file" }
    };
    FILE *fp = NULL;
    VStringConst in_filename, buf1, buf2;
    VAttrList list1, list2, out_list;
    VAttrListPosn posn;
    VString str;
    VImage src, *src1, *src2, dest = NULL;
    int i, nimages, npix = 0;
	char prg_name[100];
	char ver[100];
	getLipsiaVersion(ver, sizeof(ver));
	sprintf(prg_name, "vpaired_ttest V%s", ver);
    fprintf(stderr, "%s\n", prg_name);
    /*
    ** parse command line
    */
    if(! VParseCommand(VNumber(options), options, & argc, argv)) {
        VReportUsage(argv[0], VNumber(options), options, NULL);
        exit(EXIT_FAILURE);
    }
    if(argc > 1) {
        VReportBadArgs(argc, argv);
        exit(EXIT_FAILURE);
    }
    if(type < 0 || type > 1)
        VError(" illegal type");
    /* ini */
    nimages = in_files1.number;
    if(in_files2.number != nimages)
        VError(" inconsistent number of files %d %d", nimages, in_files2.number);
    for(i = 0; i < nimages; i++) {
        buf1 = ((VStringConst *) in_files1.vector)[i];
        buf2 = ((VStringConst *) in_files2.vector)[i];
        fprintf(stderr, "%3d:  %s  %s\n", i, buf1, buf2);
    }
    fprintf(stderr, "\n");
    /* images 1 */
    src1 = (VImage *) VCalloc(nimages, sizeof(VImage));
    for(i = 0; i < nimages; i++) {
        src1[i] = NULL;
        in_filename = ((VStringConst *) in_files1.vector)[i];
        fp = VOpenInputFile(in_filename, TRUE);
        list1 = VReadFile(fp, NULL);
        if(! list1)
            VError("Error reading image");
        fclose(fp);
        for(VFirstAttr(list1, & posn); VAttrExists(& posn); VNextAttr(& posn)) {
            if(VGetAttrRepn(& posn) != VImageRepn)
                continue;
            VGetAttrValue(& posn, NULL, VImageRepn, & src);
            if(VPixelRepn(src) != VFloatRepn)
                continue;
            if(VGetAttr(VImageAttrList(src), "modality", NULL, VStringRepn, &str) == VAttrFound) {
                if(strcmp(str, "conimg") != 0)
                    continue;
            }
            if(i == 0)
                npix = VImageNPixels(src);
            else if(npix != VImageNPixels(src))
                VError(" inconsistent image dimensions");
            src1[i] = src;
            break;
        }
        if(src1[i] == NULL)
            VError(" no contrast image found in %s", in_filename);
    }
    /* images 2 */
    src2 = (VImage *) VCalloc(nimages, sizeof(VImage));
    for(i = 0; i < nimages; i++) {
        src2[i] = NULL;
        in_filename = ((VStringConst *) in_files2.vector)[i];
        fp = VOpenInputFile(in_filename, TRUE);
        list2 = VReadFile(fp, NULL);
        if(! list2)
            VError("Error reading image");
        fclose(fp);
        for(VFirstAttr(list2, & posn); VAttrExists(& posn); VNextAttr(& posn)) {
            if(VGetAttrRepn(& posn) != VImageRepn)
                continue;
            VGetAttrValue(& posn, NULL, VImageRepn, & src);
            if(VPixelRepn(src) != VFloatRepn)
                continue;
            if(VGetAttr(VImageAttrList(src), "modality", NULL, VStringRepn, &str) == VAttrFound) {
                if(strcmp(str, "conimg") != 0)
                    continue;
            }
            if(npix != VImageNPixels(src))
                VError(" inconsistent image dimensions");
            src2[i] = src;
            break;
        }
        if(src2[i] == NULL)
            VError(" no contrast image found in %s", in_filename);
    }
    /* make normally distributed */
    if(gauss) {
        VGaussianize(src1, nimages);
        VGaussianize(src2, nimages);
    }
    /* paired t-test */
    dest = PairedTest(src1, src2, dest, nimages, type);
    /*
    ** output
    */
    out_list = VCreateAttrList();
    VHistory(VNumber(options), options, prg_name, &list1, &out_list);
    VAppendAttr(out_list, "image", NULL, VImageRepn, dest);
    fp = VOpenOutputFile(out_filename, TRUE);
    if(! VWriteFile(fp, out_list))
        exit(1);
    fclose(fp);
    fprintf(stderr, "%s: done.\n", argv[0]);
    exit(0);
}
Ejemplo n.º 7
0
VImage
VDoMulticomp3d(VImage src, VImage multicomp, VBoolean verbose) {
    VString str;
    Volumes volumes;
    Volume vol;
    VImage tmp = NULL, label_image = NULL, dest = NULL;
    VFloat *src_pp, *dst_pp, u, zthr = 0, zmax = 0, voxsize = 1, sym = 0;
    VBit   *bin_pp;
    int   i, nl, size, nbands, nrows, ncols, npixels;
    int   b, r, c, b0, r0, c0;
    int   nflag, pflag;
    float x0, x1, x2;
    float sign = 1;
    float voxel[3], ca[3], extent[3];
    /*
    ** read multicomp file header
    */
    voxsize = 1;
    if(VGetAttr(VImageAttrList(multicomp), "voxel_size", NULL,
                VFloatRepn, (VPointer) & voxsize) != VAttrFound)
        VError(" attribute 'voxel_size' not found");
    if(VGetAttr(VImageAttrList(multicomp), "zthr", NULL, VFloatRepn, (VPointer) &zthr) != VAttrFound)
        VError(" attribute 'zthr' not found");
    /*
    ** read src header
    */
    if(verbose) {
        if(VGetAttr(VImageAttrList(src), "voxel", NULL,
                    VStringRepn, (VPointer) & str) != VAttrFound)
            VError(" attribute 'voxel' not found");
        sscanf(str, "%f %f %f", &x0, &x1, &x2);
        if(ABS(x0 * x1 * x2 - voxsize) > 0.01)
            VError(" voxel sizes do not match %.3f %.3f %.3f", x0, x1, x2);
        voxel[0] = x0;
        voxel[1] = x1;
        voxel[2] = x2;
        if(VGetAttr(VImageAttrList(src), "ca", NULL,
                    VStringRepn, (VPointer) & str) != VAttrFound)
            VError(" attribute 'ca' not found");
        sscanf(str, "%f %f %f", &x0, &x1, &x2);
        ca[0] = x0;
        ca[1] = x1;
        ca[2] = x2;
        if(VGetAttr(VImageAttrList(src), "extent", NULL,
                    VStringRepn, (VPointer) & str) != VAttrFound)
            VError(" attribute 'extent' not found");
        sscanf(str, "%f %f %f", &x0, &x1, &x2);
        extent[0] = x0;
        extent[1] = x1;
        extent[2] = x2;
    }
    nbands  = VImageNBands(src);
    nrows   = VImageNRows(src);
    ncols   = VImageNColumns(src);
    npixels = VImageNPixels(src);
    tmp  = VCreateImage(nbands, nrows, ncols, VBitRepn);
    dest = VCopyImage(src, NULL, VAllBands);
    VFillImage(dest, VAllBands, 0);
    /*
    ** positive threshold
    */
    nflag = pflag = 0;
    src_pp = VImageData(src);
    bin_pp = VImageData(tmp);
    for(i = 0; i < npixels; i++) {
        *bin_pp = 0;
        u = *src_pp;
        if(u > 0 && u >= zthr)
            *bin_pp = 1;
        src_pp++;
        bin_pp++;
    }
    label_image = VLabelImage3d(tmp, label_image, (int)26, VShortRepn, &nl);
    if(nl < 1 && pflag == 0) {
        VWarning(" no voxels above threshold %.3f", zthr);
        goto next;
    } else
        pflag++;
    volumes = VImage2Volumes(label_image);
    for(vol = volumes->first; vol != NULL; vol = vol->next) {
        sign = 1;
        size = VolumeSize(vol);
        zmax = VolumeZMax(vol, src, (float)1, &b, &r, &c);
        sym  = VolumeSym(vol, src, sign, zthr);
        if(CheckValueSymm(multicomp, size, zmax, sym) == FALSE) {
            VolumeZero(vol, tmp);
        } else {
            if(verbose) {
                VPixel2Tal(ca, voxel, extent, b, r, c, &x0, &x1, &x2);
                c0 = VRint(x0);
                r0 = VRint(x1);
                b0 = VRint(x2);
                fprintf(stderr, " nvoxels: %5d,   zmax: %7.3f,   sym: %.3f,  addr: %3d %3d %3d\n",
                        size, zmax, sym, c0, r0, b0);
            }
        }
    }
    src_pp = VImageData(src);
    dst_pp = VImageData(dest);
    bin_pp = VImageData(tmp);
    for(i = 0; i < npixels; i++) {
        if(*bin_pp > 0)
            *dst_pp = *src_pp;
        src_pp++;
        dst_pp++;
        bin_pp++;
    }
    /*
    ** negative threshold
    */
next:
    src_pp = VImageData(src);
    bin_pp = VImageData(tmp);
    for(i = 0; i < npixels; i++) {
        *bin_pp = 0;
        u = *src_pp;
        if(u < 0 && -u >= zthr)
            *bin_pp = 1;
        src_pp++;
        bin_pp++;
    }
    label_image = VLabelImage3d(tmp, label_image, (int)26, VShortRepn, &nl);
    if(nl < 1 && nflag == 0) {
        /* VWarning(" no voxels below negative threshold %.3f",-zthr); */
        goto ende;
    } else
        nflag++;
    volumes = VImage2Volumes(label_image);
    for(vol = volumes->first; vol != NULL; vol = vol->next) {
        sign = -1;
        size = VolumeSize(vol);
        zmax = VolumeZMax(vol, src, sign, &b, &r, &c);
        sym  = VolumeSym(vol, src, sign, zthr);
        if(CheckValueSymm(multicomp, size, zmax, sym) == FALSE) {
            VolumeZero(vol, tmp);
        } else {
            if(verbose) {
                VPixel2Tal(ca, voxel, extent, b, r, c, &x0, &x1, &x2);
                c0 = VRint(x0);
                r0 = VRint(x1);
                b0 = VRint(x2);
                fprintf(stderr, " nvoxels: %5d,   zmax: %7.3f,   sym: %.3f,  addr: %3d %3d %3d\n",
                        size, zmax, sym, c0, r0, b0);
            }
        }
    }
    src_pp = VImageData(src);
    dst_pp = VImageData(dest);
    bin_pp = VImageData(tmp);
    for(i = 0; i < npixels; i++) {
        if(*bin_pp > 0)
            *dst_pp = *src_pp;
        src_pp++;
        dst_pp++;
        bin_pp++;
    }
ende:
    if(nflag == 0 && pflag == 0)
        VError(" no voxels passed threshold");
    return dest;
}
Ejemplo n.º 8
0
int main(int argc, char *argv[]) {
    static VArgVector in_files1;
    static VArgVector in_files2;
    static VString out_filename;
    static VOptionDescRec options[] = {
        { "in1", VStringRepn, 0, & in_files1, VRequiredOpt, NULL, "Input files 1" },
        { "in2", VStringRepn, 0, & in_files2, VRequiredOpt, NULL, "Input files 2" },
        { "out", VStringRepn, 1, & out_filename, VRequiredOpt, NULL, "Output file" }
    };
    FILE *fp = NULL;
    VStringConst in_filename, buf1, buf2;
    VAttrList list1, list2, out_list;
    VAttrListPosn posn;
    VImage src, *src1, *src2, dest = NULL;
    int i, nimages, npix = 0;
    char prg_name[100];
	char ver[100];
	getLipsiaVersion(ver, sizeof(ver));
	sprintf(prg_name, "vpaired_wilcoxtest V%s", ver);
    fprintf(stderr, "%s\n", prg_name);
    /*
    ** parse command line
    */
    if(! VParseCommand(VNumber(options), options, & argc, argv)) {
        VReportUsage(argv[0], VNumber(options), options, NULL);
        exit(EXIT_FAILURE);
    }
    if(argc > 1) {
        VReportBadArgs(argc, argv);
        exit(EXIT_FAILURE);
    }
    /* number of images */
    nimages = in_files1.number;
    if(in_files2.number != nimages)
        VError(" inconsistent number of files ");
    for(i = 0; i < nimages; i++) {
        buf1 = ((VStringConst *) in_files1.vector)[i];
        buf2 = ((VStringConst *) in_files2.vector)[i];
        fprintf(stderr, "%3d:  %s  %s\n", i, buf1, buf2);
    }
    fprintf(stderr, "\n");
    /*
    ** read images 1
    */
    src1 = (VImage *) VCalloc(nimages, sizeof(VImage));
    for(i = 0; i < nimages; i++) {
        src1[i] = NULL;
        in_filename = ((VStringConst *) in_files1.vector)[i];
        fp = VOpenInputFile(in_filename, TRUE);
        list1 = VReadFile(fp, NULL);
        if(! list1)
            VError("Error reading image");
        fclose(fp);
        for(VFirstAttr(list1, & posn); VAttrExists(& posn); VNextAttr(& posn)) {
            if(VGetAttrRepn(& posn) != VImageRepn)
                continue;
            VGetAttrValue(& posn, NULL, VImageRepn, & src);
            if(VPixelRepn(src) != VFloatRepn)
                continue;
            src1[i] = src;
            break;
        }
        if(i == 0)
            npix = VImageNPixels(src1[i]);
        else if(npix != VImageNPixels(src1[i]))
            VError(" inconsistent image dimensions");
        if(src1[i] == NULL)
            VError(" no image found in %s", in_filename);
    }
    /*
    ** read images 2
    */
    src2 = (VImage *) VCalloc(nimages, sizeof(VImage));
    for(i = 0; i < nimages; i++) {
        src2[i] = NULL;
        in_filename = ((VStringConst *) in_files2.vector)[i];
        fp = VOpenInputFile(in_filename, TRUE);
        list2 = VReadFile(fp, NULL);
        if(! list2)
            VError("Error reading image");
        fclose(fp);
        for(VFirstAttr(list2, & posn); VAttrExists(& posn); VNextAttr(& posn)) {
            if(VGetAttrRepn(& posn) != VImageRepn)
                continue;
            VGetAttrValue(& posn, NULL, VImageRepn, & src);
            if(VPixelRepn(src) != VFloatRepn)
                continue;
            src2[i] = src;
            break;
        }
        if(npix != VImageNPixels(src2[i]))
            VError(" inconsistent image dimensions");
        if(src2[i] == NULL)
            VError(" no image found in %s", in_filename);
    }
    /*
    ** paired wilcoxon test
    */
    dest = PairedWilcoxTest(src1, src2, dest, nimages);
    /*
    ** output
    */
    out_list = VCreateAttrList();
    VHistory(VNumber(options), options, prg_name, &list1, &out_list);
    VAppendAttr(out_list, "image", NULL, VImageRepn, dest);
    fp = VOpenOutputFile(out_filename, TRUE);
    if(! VWriteFile(fp, out_list))
        exit(1);
    fclose(fp);
    fprintf(stderr, "%s: done.\n", argv[0]);
    exit(0);
}
Ejemplo n.º 9
0
/*!
\fn VImage VContrast(VImage src,VImage dest,VRepnKind repn,VFloat alpha,VFloat background)
\param src   input image  (any repn)
\param dest  output image (any repn)
\param repn  the output pixel repn (e.g. VUByteRepn)
\param alpha contrast stretching factor. 
The function stretches grey values between mean-alpha*sigma and mean+alpha*sigma.
\param background input grey values with absolute values less than <background> are
assumed to be image background and are not used for computing the image mean and sigma.
If set to zero, it has no effect.
*/
VImage
VContrast(VImage src,VImage dest,VRepnKind repn,VFloat alpha,VFloat background)
{
  int i,nbands,nrows,ncols,npixels;
  float u,xmin,xmax,ymin,ymax,slope,smin,smax;
  float sum1,sum2,nx;
  float mean,sigma;

  if (repn == VDoubleRepn || repn == VLongRepn || repn == VSByteRepn)
    VError(" double, long and sbyte are not supported by VContrast");

  if (alpha <= 0) VError("alpha must be positive");


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

  dest = VSelectDestImage("VContrast",dest,nbands,nrows,ncols,repn);
  if (! dest) VError(" err creating dest image");
  VFillImage(dest,VAllBands,0);

  smin = VPixelMaxValue(src);
  smax = VPixelMinValue(src);

  sum1 = sum2 = nx = 0;
  for (i=0; i<npixels; i++) {
    u = VGetPixelValue (src,i);
    if (ABS(u) < background) continue;
    if (u < smin) smin = u;
    if (u > smax) smax = u;
    sum1 += u;
    sum2 += u*u;
    nx++;
  }
  if (nx < 2) VError(" no foreground pixels found");

  mean  = sum1/nx;
  sigma = sqrt((double)((sum2 - nx * mean * mean) / (nx - 1.0)));

  ymax = VPixelMaxValue(dest);
  ymin = VPixelMinValue(dest);

  xmax = mean + alpha * sigma;
  if (xmax > smax) xmax = smax;

  xmin = mean - alpha * sigma;
  if (xmin < smin) xmin = smin;

  slope = (ymax - ymin) / (xmax - xmin);

  for (i=0; i<npixels; i++) {
    u = VGetPixelValue (src,i);
    if (u != 0) u = slope * (u - xmin);
    if (u > ymax) u = ymax;
    if (u < ymin) u = ymin;
    VSetPixelValue(dest,i,(VFloat) u);
  }

  VCopyImageAttrs (src, dest);
  return dest;
}
Ejemplo n.º 10
0
static VPointer VImageDecodeMethod (VStringConst name, VBundle b)
{
  VImage image;
  VLong nbands, nrows, ncolumns, pixel_repn;
  VLong nframes, nviewpoints, ncolors, ncomponents;
  VAttrList list;
  size_t length;

#define Extract(name, dict, locn, required)	\
	VExtractAttr (b->list, name, dict, VLongRepn, & locn, required)

  /* Extract the number of bands, rows, columns, pixel repn, etc.: */
  nbands = nframes = nviewpoints = ncolors = ncomponents = 1;	/* defaults */
  if (! Extract (VNBandsAttr, NULL, nbands, FALSE) ||
      ! Extract (VNRowsAttr, NULL, nrows, TRUE) ||
      ! Extract (VNColumnsAttr, NULL, ncolumns, TRUE) ||
      ! Extract (VRepnAttr, VNumericRepnDict, pixel_repn, TRUE) ||
      ! Extract (VNFramesAttr, NULL, nframes, FALSE) ||
      ! Extract (VNViewpointsAttr, NULL, nviewpoints, FALSE) ||
      ! Extract (VNColorsAttr, NULL, ncolors, FALSE) ||
      ! Extract (VNComponentsAttr, NULL, ncomponents, FALSE))
    return NULL;

  /* Ensure that nbands == nframes * nviewpoints * ncolors * ncomponents.
     For backwards compatibility, set ncomponents to nbands if nbands != 1
     but nframes == nviewpoints == ncolors == ncomponents == 1. */
  if (nbands != nframes * nviewpoints * ncolors * ncomponents) {
    if (nbands != 1 && nframes == 1 && nviewpoints == 1 &&
	ncolors == 1 && ncomponents == 1)
      ncomponents = nbands;
    else {
      VWarning ("VImageDecodeMethod: %s image has inconsistent nbands",
		name);
      return NULL;
    }
  }

  /* Create an image with the specified properties: */
  if (! (image = VCreateImage ((int) nbands, (int) nrows, (int) ncolumns,
			       (VRepnKind) pixel_repn)))
    return NULL;
  VImageNFrames (image) = nframes;
  VImageNViewpoints (image) = nviewpoints;
  VImageNColors (image) = ncolors;
  VImageNComponents (image) = ncomponents;

  /* Give it whatever attributes remain: */
  list = VImageAttrList (image);
  VImageAttrList (image) = b->list;
  b->list = list;

  /* Check that the expected amount of binary data was read: */
  length = VImageNPixels (image);
  if (VPixelRepn (image) == VBitRepn)
    length = (length + 7) / 8;
  else length *= VPixelPrecision (image) / 8;
  if (length != b->length) {
    VWarning ("VImageDecodeMethod: %s image has wrong data length", name);
  Fail:   VDestroyImage (image);
    return NULL;
  }

  /* Unpack the binary pixel data: */
  length = VImageSize (image);
  if (! VUnpackData (VPixelRepn (image), VImageNPixels (image),
		     b->data, VMsbFirst, & length, & VImageData (image),
		     NULL))
    goto Fail;
  return image;

#undef Extract
}
Ejemplo n.º 11
0
VAttrList
VBayes(VImage cbeta_images[], VImage sd_images[], int nimages, VBoolean level, VBoolean zscore) {
    VAttrList out_list = NULL;
    VImage dest = NULL, sigma_image = NULL;
    int    i, j, k, npixels;
    VFloat *dest_pp, *beta_pp[N], *sd_pp[N], *sigma_pp = NULL;
    VFloat pmin, pmax;
    double mean = 0, sigma = 0;
    float  rx, wsum, msum, w0, wx, s, msumold;
    float  result = 0;
    float  gmean[N], gvar[N];
    float  tiny = 1.0e-12;
    /*
    ** create output image
    */
    dest = VCopyImage(cbeta_images[0], NULL, VAllBands);
    if(!dest)
        return NULL;
    VFillImage(dest, VAllBands, 0);
    VSetAttr(VImageAttrList(dest), "num_images", NULL, VShortRepn, (VShort)nimages);
    VSetAttr(VImageAttrList(dest), "modality", NULL, VStringRepn, "bayes_map");
    VSetAttr(VImageAttrList(dest), "name", NULL, VStringRepn, "bayes");
    if(level == TRUE) {
        VSetAttr(VImageAttrList(dest), "modality", NULL, VStringRepn, "mean");
        VSetAttr(VImageAttrList(dest), "name", NULL, VStringRepn, "mean");
        sigma_image = VCopyImage(dest, NULL, VAllBands);
        if(!sigma_image)
            return NULL;
        VFillImage(sigma_image, VAllBands, 0);
        VSetAttr(VImageAttrList(sigma_image), "num_images", NULL, VShortRepn, (VShort)nimages);
        VSetAttr(VImageAttrList(sigma_image), "modality", NULL, VStringRepn, "std_dev");
        VSetAttr(VImageAttrList(sigma_image), "name", NULL, VStringRepn, "std_dev");
    }
    /*
    ** for each voxel
    */
    pmax = VRepnMinValue(VFloatRepn);
    pmin = VRepnMaxValue(VFloatRepn);
    npixels = VImageNPixels(cbeta_images[0]);
    for(i = 0; i < nimages; i++) {
        beta_pp[i] = (VFloat *) VImageData(cbeta_images[i]);
        sd_pp[i]   = (VFloat *) VImageData(sd_images[i]);
    }
    dest_pp = (VFloat *) VImageData(dest);
    if(level == TRUE)
        sigma_pp = (VFloat *) VImageData(sigma_image);
    for(j = 0; j < npixels; j++) {
        if(j % 10000 == 0)
            fprintf(stderr, "...%.2f%%\r", (float)100 * j / (float)npixels);
        result = mean = sigma = 0;
        /*
        ** read data from input images
        */
        for(k = 0; k < nimages; k++) {
            gmean[k] = *beta_pp[k]++;  /* mean c*beta */
            rx       = *sd_pp[k]++;    /* standard deviation of c*beta*/
            gvar[k]  = rx * rx;        /* variation of c*beta*/
        }
        /* see Box,Tiao, pp.17-18     calculate probability distribution */
        wsum = 0;
        msum = 0;
        w0 = 0;
        for(k = 0; k < nimages; k++) {    /* for each image */
            s = gvar[k];
            if(s < tiny)
                goto next;
            s = sqrt((double)s);
            if(s < tiny)
                goto next;
            wx = 1.0 / (s * s);
            wsum = w0 + wx;
            msumold = msum;
            msum = (w0 * msum  +  wx * gmean[k]) / wsum;
            w0 = wsum;
            sigma = 1.0 / sqrt(wsum);
        }
        if(wsum < tiny)
            goto next;
        /* resulting mean and std dev */
        mean = msum;
        sigma = 1.0 / sqrt(wsum);
        if(level == TRUE) {
            result = mean;
            goto next;
        }
        /* calculate probability under distribution */
        result = CumulativeNormal(mean, sigma);
        /* output */
        if(zscore) {
            /* CHECK HERE IF "1.0-" is correct */
            result = (float)p2z((double)(1.0 - result));
            if(mean < 0)
                result = -result;
            if(result >  20)
                result =  20;
            if(result < -20)
                result = -20;
        } else {
            result *= 100.0;
            if(result >  100)
                result =  100;
            if(mean < 0)
                result = -result;
        }
        if(result < pmin)
            pmin = result;
        if(result > pmax)
            pmax = result;
next:
        *dest_pp++ = result;
        if(level)
            *sigma_pp++ = sigma;
    }
    out_list = VCreateAttrList();
    fprintf(stderr, "...100.00%%\n");
    if(level == FALSE) {
        fprintf(stderr, "\n min: %.3f, max: %.3f\n", pmin, pmax);
        VAppendAttr(out_list, "zmap", NULL, VImageRepn, dest);
        return out_list;
    } else {
        VAppendAttr(out_list, "mean", NULL, VImageRepn, dest);
        VAppendAttr(out_list, "std_dev", NULL, VImageRepn, sigma_image);
        return out_list;
    }
    return NULL;
}