コード例 #1
0
ファイル: VImageManager.cpp プロジェクト: hahtse/Lipsia
VImage VImageManager::scaleImage( VImage src, VImage dest, double factor )
{
	int nPixel = VImageSize( src ) / VPixelSize( src );

	if ( dest == NULL ) {
		dest = VCreateImage(
				   VImageNBands( src ),
				   VImageNRows( src ),
				   VImageNColumns( src ),
				   VUByteRepn );
	}


	VUByte *destPtr = ( VUByte * ) VImageData( dest );
	VShort *srcPtr = ( VShort * ) VImageData( src );

	srcPtr = ( VShort * ) VImageData( src );

	for ( int i = 0; i < nPixel; i++ ) {
		if ( *srcPtr <= m_xmin )
			*destPtr = 0;
		else if ( *srcPtr >= m_xmax )
			*destPtr = 255;
		else {
			int val = *srcPtr - m_xmin;
			*destPtr = ( VUByte ) ( val * factor );
		}

		srcPtr++;
		destPtr++;
	}

	return dest;
}
コード例 #2
0
ファイル: FIL_Vista_Image.c プロジェクト: KoraST/ft_BIU-1
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;
}
コード例 #3
0
ファイル: FIL_Vista_Image.c プロジェクト: KoraST/ft_BIU-1
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;
}
コード例 #4
0
ファイル: FIL_Vista_Image.c プロジェクト: KoraST/ft_BIU-1
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;
}
コード例 #5
0
ファイル: SpatialFilter.c プロジェクト: Rollmops/lipsia
VImage
VSConvolveCol (VImage src,VImage dest,VImage kernel)
{
  int b,r,c,nbands,nrows,ncols;
  int c0,c1,cc;
  float sum,x;
  VFloat *float_pp;
  int dim,d;

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

  dest = VSelectDestImage("VConvolveCol",dest,nbands,nrows,ncols,VFloatRepn);
  VFillImage(dest,VAllBands,0);
  dim  = VImageNColumns(kernel);
  d    = dim/2;

  for (b=0; b<nbands; b++) {
    for (r=0; r<nrows; r++) {
      for (c=d; c<ncols-d; c++) {

	float_pp = (VFloat *) VImageData(kernel);
	sum = 0;
	c0 = c-d;
	c1 = c+d;
	if (c0 < 0) c0 = 0;
	if (c1 >= ncols) c1 = ncols-1;
	for (cc=c0; cc<=c1; cc++) {
	  x = VPixel(src,b,r,cc,VFloat);
	  sum += x * (*float_pp++);
	}
	VPixel(dest,b,r,c,VFloat) = sum;
      }
    }
  }
  return dest;
}
コード例 #6
0
ファイル: SpatialFilter.c プロジェクト: Rollmops/lipsia
VImage
VSConvolveRow (VImage src,VImage dest,VImage kernel)
{
  int b,r,c,nbands,nrows,ncols;
  int r0,r1,rr;
  float sum,x;
  VFloat *float_pp;
  int d,dim;

  dim = VImageNColumns(kernel);
  d = dim/2;

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

  dest = VSelectDestImage("VConvolveRow",dest,nbands,nrows,ncols,VFloatRepn);

  for (b=0; b<nbands; b++) {
    for (r=d; r<nrows-d; r++) {
      for (c=0; c<ncols; c++) {

	float_pp = (VFloat *) VImageData(kernel);
	sum = 0;
	r0 = r-d;
	r1 = r+d;
	if (r0 < 0) r0 = 0;
	if (r1 >= nrows) r1 = nrows-1;
	for (rr=r0; rr<=r1; rr++) {
	  x = VPixel(src,b,rr,c,VFloat);
	  sum += x * (*float_pp++);
	}
	VPixel(dest,b,r,c,VFloat) = sum;
      }
    }
  }
  return dest;
}
コード例 #7
0
ファイル: SpatialFilter.c プロジェクト: Rollmops/lipsia
VImage
VSConvolveBand (VImage src,VImage dest,VImage kernel)
{
  int b,r,c,nbands,nrows,ncols;
  int b0,b1,bb;
  float sum,x;
  VFloat *float_pp;
  int d,dim;

  dim = VImageNColumns(kernel);
  d = dim/2;

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

  dest = VSelectDestImage("VConvolveBand",dest,nbands,nrows,ncols,VFloatRepn);

  for (b=d; b<nbands-d; b++) {
    for (r=0; r<nrows; r++) {
      for (c=0; c<ncols; c++) {

	float_pp = (VFloat *) VImageData(kernel);
	sum = 0;
	b0 = b-d;
	b1 = b+d;
	if (b0 < 0) b0 = 0;
	if (b1 >= nbands) b1 = nbands-1;
	for (bb=b0; bb<=b1; bb++) {
	  x = VPixel(src,bb,r,c,VFloat);
	  sum += x * (*float_pp++);
	}
	VPixel(dest,b,r,c,VFloat) = sum;
      }
    }
  }
  return dest;
}
コード例 #8
0
ファイル: Contrast.c プロジェクト: Rollmops/via
/*!
\fn VImage VContrastShort(VImage src,VImage dest,VFloat percent,VFloat background)
\param src   input image  (short repn)
\param dest  output image  (ubyte repn)
*/
VImage
VContrastShort(VImage src,VImage dest,VFloat low,VFloat high)
{
  int nbands,nrows,ncols,npixels;
  float u,v,xmin,xmax,slope,sum;
  float *histo;
  int i,j,dim;
  VShort *src_pp;
  VUByte *dest_pp;
  double smin,smax;
  double percent1,percent2;

  if (VPixelRepn(src) != VShortRepn) VError(" input pixel repn must be short");

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

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


  smin = VRepnMinValue(VShortRepn);
  smax = VRepnMaxValue(VShortRepn);

  percent1 = low;    /* unten  */
  percent2 = high;   /* oben   */


  dim = 2.0 * smax + 1.0;
  histo = (float *) VCalloc(dim,sizeof(float));
  for (j=0; j<dim; j++) histo[j] = 0;


  src_pp = (VShort *) VImageData(src);
  for (i=0; i<npixels; i++) {
    j = *src_pp++;
    j -= smin;
    histo[j]++;
  }
 
  sum = 0;
  for (j=0; j<dim; j++) sum += histo[j];
  for (j=0; j<dim; j++) histo[j] /= sum;

  xmin = 0;
  sum  = 0;
  for (j=0; j<dim; j++) {
    sum += histo[j];
    if (sum > percent1) break;
  }
  xmin = j+smin;

  xmax = dim;
  sum = 0;
  for (j=dim; j>0; j--) {
    sum += histo[j];
    if (sum > percent2) break;
  }
  xmax = j+smin;


  slope = 255.0f / (xmax - xmin);
  
  src_pp  = (VShort *) VImageData(src);
  dest_pp = (VUByte *) VImageData(dest);
  for (i=0; i<npixels; i++) {
    u = *src_pp++;
    v = (int) (slope * (u - xmin) + 0.5);
    if (v < 0) v = 0;
    if (v > 255) v = 255;
    *dest_pp++ = (VUByte) v;
  }

  VFree(histo);
  VCopyImageAttrs (src, dest);
  return dest;
}
コード例 #9
0
ファイル: VImageManager.cpp プロジェクト: hahtse/Lipsia
void VImageManager::prepareScaleValue()
{
	QPtrList<V_ImageRec>::iterator it = m_imageList.begin();

	unsigned int lastIndex = 90;

	if ( lastIndex >= m_imageList.count() )
		lastIndex = m_imageList.count();

	for ( unsigned int i = 0; i < lastIndex; i++ ) {

		VImage src = m_imageList.at( i );
		int ncols = VImageNColumns( src );
		int nrows = VImageNRows( src );
		int nbands = VImageNFrames( src );

		int npixels = nbands * nrows * ncols;
		int smin = ( int )VRepnMinValue( VShortRepn );
		int smax = ( int )VRepnMaxValue( VShortRepn );
		int i = 0, j = 0;
		int dim = 2 * smax + 1;

		float *histo = new float[dim];

		for ( j = 0; j < dim; j++ ) histo[j] = 0;

		VShort *src_pp = ( VShort * ) VImageData( src );

		for ( i = 0; i < ( int )( npixels ); i++ ) {
			j = *src_pp;
			src_pp ++;
			j -= smin;
			histo[j]++;
		}

		float sum = 0;

		for ( j = 0; j < dim; j++ ) sum += histo[j];

		for ( j = 0; j < dim; j++ ) histo[j] /= sum;

		sum  = 0;

		for ( j = 0; j < dim; j++ ) {
			sum += histo[j];

			if ( sum > m_black ) break;
		}

		int xmin = j + smin;

		sum = 0;

		for ( j = dim; j > 0; j-- ) {
			sum += histo[j];

			if ( sum > m_white ) break;
		}

		int xmax = j + smin;


		if ( xmin < m_xmin ) m_xmin = xmin;

		if ( xmax > m_xmax ) m_xmax = xmax;
	}
}
コード例 #10
0
ファイル: TopSmooth.c プロジェクト: Rollmops/lipsia
VImage
VTopSmoothImage3d(VImage bit_image, VImage grey_image, VImage result, VLong neighb, VLong numiter) {
    long nbands, nrows, ncols, npixels;
    VRepnKind repn;
    long i, i0, i1, n, iter, npts, u;
    long b0, b1, r0, r1, c0, c1, b, r, c;
    long n1, n6, n18, n26;
    int isum;
    double sum, norm = 0;
    VBit *dest_pp, *bit_pp;
    VUByte *ubyte_pp;
    int background = 0;
    VPoint *array = NULL;
    repn   = VPixelRepn(bit_image);
    if(repn != VBitRepn)
        VError("Smooth3d: repn must be bit");
    nbands = VImageNBands(bit_image);
    nrows  = VImageNRows(bit_image);
    ncols  = VImageNColumns(bit_image);
    npixels = nbands * nrows * ncols;
    if(result == NULL)
        result = VCreateImage(nbands, nrows, ncols, repn);
    if(! result)
        return NULL;
    bit_pp = VPixelPtr(bit_image, 0, 0, 0);
    dest_pp = VPixelPtr(result, 0, 0, 0);
    for(i = 0; i < npixels; i++)
        *dest_pp++ = *bit_pp++;
    n1  = 8;
    n6  = 4;
    n18 = 2;
    n26 = 1;
    switch(neighb) {
    case 0:
        norm = n1 + 6 * n6;
        break;
    case 1:
        norm = n1 + 6 * n6 + 12 * n18;
        break;
    case 2:
        norm = n1 + 6 * n6 + 12 * n18 + 8 * n26;
        break;
    default:
        ;
    }
    n = 1;
    ubyte_pp = VPixelPtr(grey_image, 0, 0, 0);
    for(i = 0; i < npixels; i++)
        if(*ubyte_pp++ > background)
            n++;
    array = (VPoint *) VMalloc(sizeof(VPoint) * (n + 2));
    for(i = 0; i < n + 2; i++)
        array[i].val = array[i].b = array[i].r = array[i].c = 0;
    n = 1;
    for(b = 0; b < nbands; b++) {
        for(r = 0; r < nrows; r++) {
            for(c = 0; c < ncols; c++) {
                u = VPixel(grey_image, b, r, c, VUByte);
                if(u > background) {
                    array[n].b = b;
                    array[n].r = r;
                    array[n].c = c;
                    array[n].val = (float)u;
                    n++;
                }
            }
        }
    }
    npts = n;
    VPoint_hpsort(n - 1, array);
    iter = 0;
    n = 100;
    while(n > 1 && iter < numiter) {
        iter++;
        n = 0;
        for(i = 1; i < npts; i++) {
            b = array[i].b;
            r = array[i].r;
            c = array[i].c;
            b0 = (b < 1) ? 0 : b - 1;
            b1 = (b > nbands - 2) ?  nbands - 1 : b + 1;
            r0 = (r < 1) ? 0 : r - 1;
            r1 = (r > nrows - 2) ?  nrows - 1 : r + 1;
            c0 = (c < 1) ? 0 : c - 1;
            c1 = (c > ncols - 2) ?  ncols - 1 : c + 1;
            isum = 0;
            i1 = VPixel(bit_image, b, r, c, VBit);
            isum += (int) i1 * n1;
            isum += (int) VPixel(bit_image, b0, r, c, VBit) * n6;
            isum += (int) VPixel(bit_image, b, r0, c, VBit) * n6;
            isum += (int) VPixel(bit_image, b, r, c0, VBit) * n6;
            isum += (int) VPixel(bit_image, b1, r, c, VBit) * n6;
            isum += (int) VPixel(bit_image, b, r1, c, VBit) * n6;
            isum += (int) VPixel(bit_image, b, r, c1, VBit) * n6;
            if(neighb == 1) {
                isum += VPixel(bit_image, b0, r0, c, VBit) * n18;
                isum += (int) VPixel(bit_image, b, r0, c0, VBit) * n18;
                isum += (int) VPixel(bit_image, b0, r, c0, VBit) * n18;
                isum += (int) VPixel(bit_image, b1, r1, c, VBit) * n18;
                isum += (int) VPixel(bit_image, b, r1, c1, VBit) * n18;
                isum += (int) VPixel(bit_image, b1, r, c1, VBit) * n18;
                isum += (int) VPixel(bit_image, b1, r0, c, VBit) * n18;
                isum += (int) VPixel(bit_image, b, r1, c0, VBit) * n18;
                isum += (int) VPixel(bit_image, b1, r, c0, VBit) * n18;
                isum += (int) VPixel(bit_image, b0, r1, c, VBit) * n18;
                isum += (int) VPixel(bit_image, b, r0, c1, VBit) * n18;
                isum += (int) VPixel(bit_image, b0, r, c1, VBit) * n18;
            }
            if(neighb == 2) {
                isum += (int) VPixel(bit_image, b0, r0, c0, VBit) * n26;
                isum += (int) VPixel(bit_image, b1, r0, c0, VBit) * n26;
                isum += (int) VPixel(bit_image, b0, r1, c0, VBit) * n26;
                isum += (int) VPixel(bit_image, b0, r0, c1, VBit) * n26;
                isum += (int) VPixel(bit_image, b1, r1, c0, VBit) * n26;
                isum += (int) VPixel(bit_image, b1, r0, c1, VBit) * n26;
                isum += (int) VPixel(bit_image, b0, r1, c1, VBit) * n26;
                isum += (int) VPixel(bit_image, b1, r1, c1, VBit) * n26;
            }
            sum = (double) isum / (double) norm;
            i0 = 0;
            if(sum >= 0.5)
                i0 = 1;
            if(i1 == 1 && i0 == 0) {
                if(VSimplePoint(result, b, r, c, 26) == 0)
                    i0 = 1;
            } else if(i1 == 0 && i0 == 1) {
                VPixel(result, b, r, c, VBit) = 1;
                if(VSimplePoint(result, b, r, c, 26) == 0)
                    i0 = 0;
            }
            VPixel(result, b, r, c, VBit) = i0;
            if(i0 != i1)
                n++;
        }
        if(numiter > 1) {
            bit_pp  = (VBit *) VImageData(bit_image);
            dest_pp = (VBit *) VImageData(result);
            for(i = 0; i < npixels; i++)
                *bit_pp++ = *dest_pp++;
        }
    }
    /* Successful completion: */
    VCopyImageAttrs(bit_image, result);
    return result;
}
コード例 #11
0
ファイル: vcolorglm.c プロジェクト: karda/lipsia
/*
** general linear regression
*/
VAttrList
VRegression(ListInfo *linfo, int nlists, VShort minval, VImage design, VFloat sigma, VLong itr) {
    VAttrList out_list;
    VImageInfo *xinfo;
    int nbands = 0, nslices = 0, nrows = 0, ncols = 0, slice, row, col, nr, nc;
    VImage src[NSLICES], res_image = NULL;
    VImage beta_image[MBETA], BCOV = NULL, KX_image = NULL;
    VImage res_map[ETMP];
    float  smooth_fwhm = 0, vx = 0, vy = 0, vz = 0;
    VFloat *float_pp, df;
    VRepnKind repn;
    float d, err;
    int   i, k, l, n, m = 0, nt, fd = 0, npix = 0;
    int   i0 = 0, i1 = 0;
    double u, sig, trace = 0, trace2 = 0, var = 0, sum = 0, nx = 0, mean = 0, sum2;
    float *ptr1, *ptr2;
    double x;
    gsl_matrix_float *X = NULL, *XInv = NULL, *SX = NULL;
    gsl_vector_float *y, *z, *beta, *ys;
    gsl_vector *kernel;
    gsl_matrix_float *S = NULL, *Vc = NULL, *F = NULL, *P = NULL, *Q = NULL;
    gsl_matrix_float *R = NULL, *RV = NULL;
    VBoolean smooth = TRUE;  /* no smoothness estimation */
    gsl_set_error_handler_off();
    /*
    ** read input data
    */
    nslices = nbands = nrows = ncols = 0;
    for(k = 0; k < nlists; k++) {
        n  = linfo[k].nslices;
        nr = linfo[k].nrows;
        nc = linfo[k].ncols;
        nt = linfo[k].ntimesteps;
        nbands += nt;
        if(nslices == 0)
            nslices = n;
        else if(nslices != n)
            VError(" inconsistent image dimensions, slices: %d %d", n, nslices);
        if(nrows == 0)
            nrows = nr;
        else if(nrows != nr)
            VError(" inconsistent image dimensions, rows: %d %d", nr, nrows);
        if(ncols == 0)
            ncols = nc;
        else if(ncols != nc)
            VError(" inconsistent image dimensions, cols: %d %d", nc, ncols);
    }
    fprintf(stderr, " num images: %d,  image dimensions: %d x %d x %d\n",
            nlists, nslices, nrows, ncols);
    /*
    ** get design dimensions
    */
    m = VImageNRows(design);      /* number of timesteps   */
    n = VImageNColumns(design);   /* number of covariates */
    fprintf(stderr, " ntimesteps=%d,   num covariates=%d\n", m, n);
    if(n >= MBETA)
        VError(" too many covariates (%d), max is %d", n, MBETA);
    if(m != nbands)
        VError(" design dimension inconsistency: %d %d", m, nbands);
    fprintf(stderr, " working...\n");
    /*
    ** read design matrix
    */
    X = gsl_matrix_float_alloc(m, n);
    for(k = 0; k < m; k++) {
        for(l = 0; l < n; l++) {
            x = VGetPixel(design, 0, k, l);
            fmset(X, k, l, (float)x);
        }
    }
    /*
    ** pre-coloring, set up K-matrix, S=K, V = K*K^T with K=S
    */
    S  = gsl_matrix_float_alloc(m, m);
    GaussMatrix((double)sigma, S);
    Vc = fmat_x_matT(S, S, NULL);
    /*
    ** compute pseudoinverse
    */
    SX = fmat_x_mat(S, X, NULL);
    XInv = fmat_PseudoInv(SX, NULL);
    /*
    ** get variance estimate
    */
    Q = fmat_x_mat(XInv, Vc, Q);
    F = fmat_x_matT(Q, XInv, F);
    BCOV = VCreateImage(1, n, n, VFloatRepn);
    float_pp = VImageData(BCOV);
    ptr1 = F->data;
    for(i = 0; i < n * n; i++)
        *float_pp++ = *ptr1++;
    gsl_matrix_float_free(Q);
    gsl_matrix_float_free(F);
    /*
    ** get effective degrees of freedom
    */
    R  = gsl_matrix_float_alloc(m, m);
    P = fmat_x_mat(SX, XInv, P);
    gsl_matrix_float_set_identity(R);
    gsl_matrix_float_sub(R, P);
    RV = fmat_x_mat(R, Vc, NULL);
    trace = 0;
    for(i = 0; i < m; i++)
        trace += fmget(RV, i, i);
    P = fmat_x_mat(RV, RV, P);
    trace2 = 0;
    for(i = 0; i < m; i++)
        trace2 += fmget(P, i, i);
    df = (trace * trace) / trace2;
    fprintf(stderr, " df= %.3f\n", df);
    /*
    ** create output images
    */
    xinfo = linfo[0].info;
    out_list = VCreateAttrList();
    res_image = VCreateImage(nslices, nrows, ncols, VFloatRepn);
    VFillImage(res_image, VAllBands, 0);
    VSetAttr(VImageAttrList(res_image), "name", NULL, VStringRepn, "RES/trRV");
    VSetAttr(VImageAttrList(res_image), "modality", NULL, VStringRepn, "RES/trRV");
    VSetAttr(VImageAttrList(res_image), "df", NULL, VFloatRepn, df);
    VSetAttr(VImageAttrList(res_image), "patient", NULL, VStringRepn, xinfo->patient);
    VSetAttr(VImageAttrList(res_image), "voxel", NULL, VStringRepn, xinfo->voxel);
    VSetAttr(VImageAttrList(res_image), "repetition_time", NULL, VLongRepn, itr);
    VSetAttr(VImageAttrList(res_image), "talairach", NULL, VStringRepn, xinfo->talairach);

    /* neu */
    VSetAttr(VImageAttrList(res_image),"indexOrigin",NULL,VStringRepn,xinfo->indexOrigin);
    VSetAttr(VImageAttrList(res_image),"columnVec",NULL,VStringRepn,xinfo->columnVec);
    VSetAttr(VImageAttrList(res_image),"rowVec",NULL,VStringRepn,xinfo->rowVec);
    VSetAttr(VImageAttrList(res_image),"sliceVec",NULL,VStringRepn,xinfo->sliceVec);
    VSetAttr(VImageAttrList(res_image),"FOV",NULL,VStringRepn,xinfo->FOV);
    /*--------*/

    if(xinfo->fixpoint[0] != 'N')
        VSetAttr(VImageAttrList(res_image), "fixpoint", NULL, VStringRepn, xinfo->fixpoint);
    if(xinfo->ca[0] != 'N') {
        VSetAttr(VImageAttrList(res_image), "ca", NULL, VStringRepn, xinfo->ca);
        VSetAttr(VImageAttrList(res_image), "cp", NULL, VStringRepn, xinfo->cp);
        VSetAttr(VImageAttrList(res_image), "extent", NULL, VStringRepn, xinfo->extent);
    }
    VAppendAttr(out_list, "image", NULL, VImageRepn, res_image);
    for(i = 0; i < n; i++) {
        beta_image[i] = VCreateImage(nslices, nrows, ncols, VFloatRepn);
        VFillImage(beta_image[i], VAllBands, 0);
        VSetAttr(VImageAttrList(beta_image[i]), "patient", NULL, VStringRepn, xinfo->patient);
        VSetAttr(VImageAttrList(beta_image[i]), "voxel", NULL, VStringRepn, xinfo->voxel);
        VSetAttr(VImageAttrList(beta_image[i]), "repetition_time", NULL, VLongRepn, itr);
        VSetAttr(VImageAttrList(beta_image[i]), "talairach", NULL, VStringRepn, xinfo->talairach);

	/* neu */
	VSetAttr(VImageAttrList(beta_image[i]),"indexOrigin",NULL,VStringRepn,xinfo->indexOrigin);
	VSetAttr(VImageAttrList(beta_image[i]),"columnVec",NULL,VStringRepn,xinfo->columnVec);
	VSetAttr(VImageAttrList(beta_image[i]),"rowVec",NULL,VStringRepn,xinfo->rowVec);
	VSetAttr(VImageAttrList(beta_image[i]),"sliceVec",NULL,VStringRepn,xinfo->sliceVec);
	VSetAttr(VImageAttrList(beta_image[i]),"FOV",NULL,VStringRepn,xinfo->FOV);
	/*--------*/


        if(xinfo->fixpoint[0] != 'N')
            VSetAttr(VImageAttrList(beta_image[i]), "fixpoint", NULL, VStringRepn, xinfo->fixpoint);
        if(xinfo->ca[0] != 'N') {
            VSetAttr(VImageAttrList(beta_image[i]), "ca", NULL, VStringRepn, xinfo->ca);
            VSetAttr(VImageAttrList(beta_image[i]), "cp", NULL, VStringRepn, xinfo->cp);
            VSetAttr(VImageAttrList(beta_image[i]), "extent", NULL, VStringRepn, xinfo->extent);
        }
        VSetAttr(VImageAttrList(beta_image[i]), "name", NULL, VStringRepn, "BETA");
        VSetAttr(VImageAttrList(beta_image[i]), "modality", NULL, VStringRepn, "BETA");
        VSetAttr(VImageAttrList(beta_image[i]), "beta", NULL, VShortRepn, i + 1);
        VSetAttr(VImageAttrList(beta_image[i]), "df", NULL, VFloatRepn, df);
        VAppendAttr(out_list, "image", NULL, VImageRepn, beta_image[i]);
    }
    VSetAttr(VImageAttrList(design), "name", NULL, VStringRepn, "X");
    VSetAttr(VImageAttrList(design), "modality", NULL, VStringRepn, "X");
    VAppendAttr(out_list, "image", NULL, VImageRepn, design);
    KX_image = Mat2Vista(SX);
    VSetAttr(VImageAttrList(KX_image), "name", NULL, VStringRepn, "KX");
    VSetAttr(VImageAttrList(KX_image), "modality", NULL, VStringRepn, "KX");
    VAppendAttr(out_list, "image", NULL, VImageRepn, KX_image);
    VSetAttr(VImageAttrList(BCOV), "name", NULL, VStringRepn, "BCOV");
    VSetAttr(VImageAttrList(BCOV), "modality", NULL, VStringRepn, "BCOV");
    VAppendAttr(out_list, "image", NULL, VImageRepn, BCOV);
    /*
    ** create temporary images for smoothness estimation
    */
    /* smoothness estim only for 3D images, i.e. CA/CP known */
/*    if(xinfo->ca[0] == 'N')
        smooth = FALSE;*/
    if(smooth) {
        i0 = 20;
        i1 = i0 + 30;
        if(i1 > m)
            i1 = m;
        for(i = i0; i < i1; i++) {
            if(i - i0 >= ETMP)
                VError(" too many tmp images");
            res_map[i - i0] = VCreateImage(nslices, nrows, ncols, VFloatRepn);
            VFillImage(res_map[i - i0], VAllBands, 0);
        }
    }
    /*
    ** process
    */
    ys   = gsl_vector_float_alloc(m);
    y    = gsl_vector_float_alloc(m);
    z    = gsl_vector_float_alloc(m);
    beta = gsl_vector_float_alloc(n);
    kernel = GaussKernel((double)sigma);
    for(k = 0; k < nlists; k++) {
        src[k] = VCreateImage(linfo[k].ntimesteps, nrows, ncols, linfo[k].repn);
        VFillImage(src[k], VAllBands, 0);
    }
    npix = 0;
    for(slice = 0; slice < nslices; slice++) {
        if(slice % 5 == 0)
            fprintf(stderr, " slice: %3d\r", slice);
        for(k = 0; k < nlists; k++) {
            if(linfo[k].zero[slice] == 0)
                goto next1;
            fd = open(linfo[k].filename, O_RDONLY);
            if(fd == -1)
                VError("could not open file %s", linfo[k].filename);
            nt = linfo[k].ntimesteps;
            if(! VReadBandDataFD(fd, &linfo[k].info[slice], 0, nt, &src[k]))
                VError(" error reading data");
            close(fd);
        }
        repn = linfo[0].repn;
        for(row = 0; row < nrows; row++) {
            for(col = 0; col < ncols; col++) {
                for(k = 0; k < nlists; k++)
                    if(VPixel(src[k], 0, row, col, VShort) < minval + 1)
                        goto next;
                npix++;
                /* read time series data */
                sum = sum2 = nx = 0;
                ptr1 = y->data;
                for(k = 0; k < nlists; k++) {
                    nt  = VImageNBands(src[k]);
                    for(i = 0; i < nt; i++) {
                        u = VPixel(src[k], i, row, col, VShort);
                        (*ptr1++) = u;
                        sum  += u;
                        sum2 += u * u;
                        nx++;
                    }
                }
                mean = sum / nx;
                sig = sqrt((double)((sum2 - nx * mean * mean) / (nx - 1.0)));
                if(sig < 0.001)
                    continue;
                /* centering and scaling, Seber, p.330 */
                ptr1 = y->data;
                for(i = 0; i < m; i++) {
                    u = ((*ptr1) - mean) / sig;
                    (*ptr1++) = u + 100.0;
                }
                /* S x y */
                ys = VectorConvolve(y, ys, kernel);
                /* compute beta's */
                fmat_x_vector(XInv, ys, beta);
                /* residuals */
                fmat_x_vector(SX, beta, z);
                err = 0;
                ptr1 = ys->data;
                ptr2 = z->data;
                for(i = 0; i < m; i++) {
                    d = ((*ptr1++) - (*ptr2++));
                    err += d * d;
                }
                /* sigma^2 */
                var = err / trace;
                /* write residuals output */
                VPixel(res_image, slice, row, col, VFloat) = (VFloat)var;
                /* save residuals of several timesteps for smoothness estimation */
                if(smooth) {
                    ptr1 = ys->data;
                    ptr2 = z->data;
                    err = 0;
                    for(i = i0; i < i1; i++) {
                        d = ((*ptr1++) - (*ptr2++));
                        err += d * d;
                        VPixel(res_map[i - i0], slice, row, col, VFloat) = d;
                    }
                    if (err > 0) err = sqrt(err); 
                    for(i = i0; i < i1; i++) {
                        d = VPixel(res_map[i - i0], slice, row, col, VFloat);
						if (err > 1.0e-6) d /= err;
						else d = 0; 
                        VPixel(res_map[i - i0], slice, row, col, VFloat) = d / err;
                    }
                }
                /* write beta output */
                ptr1 = beta->data;
                for(i = 0; i < n; i++)
                    VPixel(beta_image[i], slice, row, col, VFloat) = (VFloat)(*ptr1++);
next:
                ;
            }
        }
next1:
        ;
    }
    /*
    ** Smoothness estimation based on residual images
    */
    if(smooth) {
        smooth_fwhm = VSmoothnessEstim(res_map, i1 - i0);
        sscanf(xinfo->voxel, "%f %f %f", &vx, &vy, &vz);
        vx = (vx + vy + vz) / 3.0;    /* voxels should be isotropic */
        smooth_fwhm *= vx;
        fprintf(stderr, " smoothness: %f\n", smooth_fwhm);
        VSetAttr(VImageAttrList(res_image), "smoothness", NULL, VFloatRepn, smooth_fwhm);
        for(i = 0; i < n; i++) {
            VSetAttr(VImageAttrList(beta_image[i]), "smoothness", NULL, VFloatRepn, smooth_fwhm);
        }
        for(i = 0; i < i1 - i0; i++)
            VDestroyImage(res_map[i]);
    }
ende:
    if(npix == 0)
        VError(" no voxels above threshold %d found", minval);
    return out_list;
}
コード例 #12
0
ファイル: DoMulticomp3d.c プロジェクト: Rollmops/lipsia
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;
}
コード例 #13
0
ファイル: vdelcereb.c プロジェクト: tibu-mpg/Lipsia
/*
** identify conponents belonging to the cerebellum
*/
void
VDeleteCereb(VImage label_image, VImage *wm, int ta, int tb) {
    VUByte *ubyte_pp;
    VBit *bit_pp;
    double *npix;
    int i, j, maxlabel = 0;
    Point *mean;
    int b, r, c, nbands, nrows, ncols, npixels;
    nbands  = VImageNBands((*wm));
    nrows   = VImageNRows((*wm));
    ncols   = VImageNColumns((*wm));
    npixels = nbands * nrows * ncols;
    maxlabel = 0;
    ubyte_pp = (VUByte *) VImageData(label_image);
    for(i = 0; i < npixels; i++) {
        if(*ubyte_pp > maxlabel)
            maxlabel = *ubyte_pp;
        ubyte_pp++;
    }
    maxlabel++;
    mean = (Point *) VMalloc(sizeof(Point) * maxlabel);
    npix = (double *) VMalloc(sizeof(double) * maxlabel);
    for(i = 0; i < maxlabel; i++) {
        mean[i].b = 0;
        mean[i].r = 0;
        mean[i].c = 0;
        npix[i]   = 0;
    }
    /* get centroids */
    ubyte_pp = (VUByte *) VImageData(label_image);
    for(b = 0; b < nbands; b++) {
        for(r = 0; r < nrows; r++) {
            for(c = 0; c < ncols; c++) {
                i = (int) * ubyte_pp++;
                if(i != 0) {
                    npix[i]++;
                    mean[i].b += b;
                    mean[i].r += r;
                    mean[i].c += c;
                }
            }
        }
    }
    for(i = 0; i < maxlabel; i++) {
        if(npix[i] > 1) {
            mean[i].b /= npix[i];
            mean[i].r /= npix[i];
            mean[i].c /= npix[i];
        }
    }
    /* del comp */
    bit_pp = (VBit *) VImageData((*wm));
    ubyte_pp = (VUByte *) VImageData(label_image);
    for(j = 0; j < npixels; j++) {
        i = (int) * ubyte_pp++;
        if(i > 0) {
            if(ta == 0) {       /* remove center blobs  (axial) */
                if(npix[i] < 1000
                        && ABS(mean[i].r - (nrows / 2)) < 10
                        && ABS(mean[i].c - (ncols / 2)) < 10)
                    *bit_pp = 0;
            } else {
                if(mean[i].r >= 100 && tb == 0
                        && ABS(mean[i].c - (ncols / 2)) < 10)      /* coronal only */
                    *bit_pp = 0;
                else if(mean[i].r >= ta && mean[i].c >= tb)   /* sagittal, coronal */
                    *bit_pp = 0;
            }
        }
        bit_pp++;
    }
}
コード例 #14
0
ファイル: vdelcereb.c プロジェクト: tibu-mpg/Lipsia
VImage
VCerebellum(VImage src) {
    VImage coronal, label_coronal;
    VImage sagital, label_sagital;
    VImage axial, label_axial;
    VImage bin0_image, bin1_image, label_image;
    VBit *bin0_pp, *src_pp;
    VUByte *ubyte_pp;
    int i, nbands, nrows, ncols, npixels, nl = 0;
    float x0, x1, x2;
    VString str;
    int b, r, c, cp, rp, bp, slice_extent, slice_origin, b0;
    nbands  = VImageNBands(src);
    nrows   = VImageNRows(src);
    ncols   = VImageNColumns(src);
    npixels = nbands * nrows * ncols;
    str = VMalloc(80);
    if(VGetAttr(VImageAttrList(src), "cp", NULL, VStringRepn, (VPointer) &str) != VAttrFound)
        VError(" attribute 'cp' not found");
    sscanf(str, "%f %f %f", &x0, &x1, &x2);
    bp = (int) VRint((double) x2);
    rp = (int) VRint((double) x1);
    cp = (int) VRint((double) x0);
    if(VGetAttr(VImageAttrList(src), "extent", NULL, VStringRepn, (VPointer) &str) != VAttrFound)
        VError(" attribute 'extent' not found");
    sscanf(str, "%f %f %f", &x0, &x1, &x2);
    slice_extent = (int) VRint((double) x2);
    if(VGetAttr(VImageAttrList(src), "origin", NULL, VStringRepn, (VPointer) &str) != VAttrFound)
        VError(" attribute 'origin' not found");
    sscanf(str, "%f %f %f", &x0, &x1, &x2);
    slice_origin = (int) VRint((double) x2);
    /* erode */
    bin1_image = VDTErode(src, NULL, (VDouble) 2.0);
    /* readdress to coronal slices and remove small 2D components */
    coronal = VCreateImage(1, nbands, ncols, VBitRepn);
    label_coronal = VCreateImage(1, nbands, ncols, VUByteRepn);
    ubyte_pp = (VUByte *) VImageData(label_coronal);
    for(i = 0; i < (nbands * ncols); i++)
        *ubyte_pp++ = 0;
    for(r = rp; r < nrows; r++) {
        bin0_pp = (VBit *) VImageData(coronal);
        for(i = 0; i < (nbands * ncols); i++)
            *bin0_pp++ = 0;
        for(b = 0; b < nbands; b++) {
            for(c = 0; c < ncols; c++) {
                VPixel(coronal, 0, b, c, VBit) = VPixel(bin1_image, b, r, c, VBit);
            }
        }
        label_coronal = VLabelImage2d(coronal, label_coronal, 8, VUByteRepn, &nl);
        VDeleteCereb(label_coronal, &coronal, 110, (int) 0);
        for(b = 0; b < nbands; b++) {
            for(c = 0; c < ncols; c++) {
                if(VPixel(coronal, 0, b, c, VBit) == 0)
                    VPixel(bin1_image, b, r, c, VBit) = 0;
            }
        }
    }
    /* readdress to sagital slices and remove small 2D components */
    sagital = VCreateImage(1, nbands, nrows, VBitRepn);
    label_sagital = VCreateImage(1, nbands, nrows, VUByteRepn);
    ubyte_pp = (VUByte *) VImageData(label_sagital);
    for(i = 0; i < (nbands * nrows); i++)
        *ubyte_pp++ = 0;
    for(c = 0; c < ncols; c++) {
        if(ABS(c - 80) < 10)
            continue;
        bin0_pp = (VBit *) VImageData(sagital);
        for(i = 0; i < (nbands * nrows); i++)
            *bin0_pp++ = 0;
        for(b = 0; b < nbands; b++) {
            for(r = 0; r < nrows; r++) {
                VPixel(sagital, 0, b, r, VBit) = VPixel(bin1_image, b, r, c, VBit);
            }
        }
        label_sagital = VLabelImage2d(sagital, label_sagital, (int) 8, VUByteRepn, &nl);
        VDeleteCereb(label_sagital, &sagital, 115, cp);
        for(b = 0; b < nbands; b++) {
            for(r = 0; r < nrows; r++) {
                if(VPixel(sagital, 0, b, r, VBit) == 0)
                    VPixel(bin1_image, b, r, c, VBit) = 0;
            }
        }
    }
    /* readdress to axial slices and remove small 2D components */
    axial = VCreateImage(1, nrows, ncols, VBitRepn);
    label_axial = VCreateImage(1, nrows, ncols, VUByteRepn);
    ubyte_pp = (VUByte *) VImageData(label_axial);
    for(i = 0; i < (nrows * ncols); i++)
        *ubyte_pp++ = 0;
    /*  for (b=bp; b<nbands; b++) { */
    for(b = 105; b < nbands; b++) {
        bin0_pp = (VBit *) VImageData(axial);
        for(i = 0; i < (nrows * ncols); i++)
            *bin0_pp++ = 0;
        for(r = 0; r < nrows; r++) {
            for(c = 0; c < ncols; c++) {
                VPixel(axial, 0, r, c, VBit) = VPixel(bin1_image, b, r, c, VBit);
            }
        }
        label_sagital = VLabelImage2d(axial, label_axial, (int) 8, VUByteRepn, &nl);
        VDeleteCereb(label_axial, &axial, 0, 0);
        for(r = 0; r < nrows; r++) {
            for(c = 0; c < ncols; c++) {
                if(VPixel(axial, 0, r, c, VBit) == 0)
                    VPixel(bin1_image, b, r, c, VBit) = 0;
            }
        }
    }
    /* remove everything below slice extent */
    b0 = slice_extent + slice_origin;
    npixels = nrows * ncols;
    for(b = b0; b < nbands; b++) {
        bin0_pp = (VBit *) VPixelPtr(bin1_image, b, 0, 0);
        for(i = 0; i < npixels; i++)
            *bin0_pp++ = 0;
    }
    /* dilate */
    bin0_image = VDTDilate(bin1_image, NULL, (VDouble) 3.0);
    npixels = nrows * ncols;
    for(b = 0; b < bp; b++) {
        bin0_pp = (VBit *) VPixelPtr(bin0_image, b, 0, 0);
        src_pp  = (VBit *) VPixelPtr(src, b, 0, 0);
        for(i = 0; i < npixels; i++)
            *bin0_pp++ = *src_pp++;
    }
    for(b = bp; b < nbands; b++) {
        bin0_pp = (VBit *) VPixelPtr(bin0_image, b, 0, 0);
        src_pp  = (VBit *) VPixelPtr(src, b, 0, 0);
        for(i = 0; i < npixels; i++) {
            if(*src_pp == 0)
                *bin0_pp = 0;
            src_pp++;
            bin0_pp++;
        }
    }
    /*
    ** remove small remaining components
    */
    label_image = VLabelImage3d(bin0_image, NULL, (int) 26, VShortRepn, &nl);
    bin0_image = VSelectBig(label_image, bin0_image);
    VImageAttrList(bin0_image) = VCopyAttrList(VImageAttrList(src));
    return bin0_image;
}
コード例 #15
0
ファイル: Contrast.c プロジェクト: Rollmops/via
VImage
VContrastAny(VImage src,VImage dest,VFloat low,VFloat high)
{
  int nbands,nrows,ncols;
  double xmin,xmax,slope,sum,a;
  float *histo;
  int b,r,c,j,dim;
  VUByte *dest_pp;
  double smin,smax,u,v,tiny;

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

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

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

  for (b=0; b<nbands; b++) {
    for (r=0; r<nrows; r++) {
      for (c=0; c<ncols; c++) {
	u = VGetPixel(src,b,r,c);
	if (u < smin) smin = u;
	if (u > smax) smax = u;
      }
    }
  }
  
  dim = 10000;
  if (VPixelRepn(src) == VUByteRepn) dim = 256;
  histo = (float *) VCalloc(dim,sizeof(float));
  for (j=0; j<dim; j++) histo[j] = 0;
  tiny = 2.0/(double)dim;

  a = ((double) dim) / (smax - smin);

  for (b=0; b<nbands; b++) {
    for (r=0; r<nrows; r++) {
      for (c=0; c<ncols; c++) {
	u = VGetPixel(src,b,r,c);
	if (ABS(u) < tiny) continue;
	j = (int) (a * (u - smin) + 0.5);
	if (j < 0) j = 0;
	if (j >= dim) j = dim-1;
	histo[j]++;
      }
    }
  }

 
  sum = 0;
  for (j=0; j<dim; j++) sum += histo[j];
  for (j=0; j<dim; j++) histo[j] /= sum;

  xmin = 0;
  sum  = 0;
  for (j=0; j<dim; j++) {
    sum += histo[j];
    if (sum > low) break;
  }
  xmin = ((double)j)/a + smin;
  
  xmax = dim;
  sum = 0;
  for (j=dim; j>0; j--) {
    sum += histo[j];
    if (sum > high) break;
  }
  xmax = ((double)j)/a + smin;

  slope = 255.0 / (xmax - xmin);

  dest_pp = (VUByte *) VImageData(dest);
  for (b=0; b<nbands; b++) {
    for (r=0; r<nrows; r++) {
      for (c=0; c<ncols; c++) {
	u = VGetPixel(src,b,r,c);
	v = (int) (slope * (u - xmin) + 0.5);
	if (ABS(u) < tiny) v = 0;
	if (v < 0) v = 0;
	if (v > 255) v = 255;
	*dest_pp++ = (VUByte) v;
      }
    }
  }

  VCopyImageAttrs (src, dest);
  return dest;
}
コード例 #16
0
ファイル: Contrast.c プロジェクト: Rollmops/via
VImage
VHistoEqualize(VImage src,VImage dest,VFloat exponent)
{
  int nbands,nrows,ncols,npixels;
  float u,v,sum;
  float *histo,*p;
  int i,j,dim;
  VShort *src_pp;
  VUByte *dest_pp;
  double smin,smax,x,y;


  if (VPixelRepn(src) != VShortRepn) VError(" input pixel repn must be short");
  if (exponent < 0.5) VError("parameter '-exponent' should be >= 0.5"); 
  if (exponent > 10) VWarning("parameter '-exponent' should be < 10"); 

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

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


  y = (double) exponent;
  smin = VRepnMinValue(VShortRepn);
  smax = VRepnMaxValue(VShortRepn);

  dim = 2.0 * smax + 1.0;
  histo = (float *) VCalloc(dim,sizeof(float));
  for (j=0; j<dim; j++) histo[j] = 0;

  p = (float *) VCalloc(dim,sizeof(float));


  src_pp = (VShort *) VImageData(src);
  for (i=0; i<npixels; i++) {
    j = *src_pp++;
    j -= smin;
    if (j == 0) continue;
    histo[j]++;
  }
 
  sum = 0;
  for (j=0; j<dim; j++) sum += histo[j];
  for (j=0; j<dim; j++) histo[j] /= sum;


  /* cumulative hist */
  for (i=0; i<dim; i++) {
    sum = 0;
    for (j=0; j<=i; j++) sum += histo[j];
    p[i] = sum;
  }


  /* make lut */
  for (i=0; i<dim; i++) {
    x = (double)p[i];
    if (x > 0)
      p[i] = (float)(pow(x,y) * 255.0);
  }

  /* apply lut */
  src_pp  = (VShort *) VImageData(src);
  dest_pp = (VUByte *) VImageData(dest);
  for (i=0; i<npixels; i++) {
    u = *src_pp++;
    j = (int) (u-smin);
    v = (double)p[j];
    if (v < 0) v = 0;
    if (v > 255) v = 255;
    *dest_pp++ = (VUByte) v;
  }

  VFree(p);
  VFree(histo);
  VCopyImageAttrs (src, dest);
  return dest;
}
コード例 #17
0
ファイル: Contrast.c プロジェクト: Rollmops/via
/*!
\fn VImage VContrastUByte(VImage src,VImage dest,VFloat percent,VFloat background)
\param src   input image  (ubyte repn)
\param dest  output image  (ubyte repn)
\param percent percentage of pixels to ignore at either end of the histogram.
\param background input grey values with absolute values less than <background> are
assumed to be image background.
*/
VImage
VContrastUByte(VImage src,VImage dest,VFloat low,VFloat high)
{
  int nbands,nrows,ncols,npixels;
  float u,v,xmin,xmax,slope,sum,background;
  float histo[256];
  int i,j;
  VUByte *src_pp,*dest_pp;

  if (VPixelRepn(src) != VUByteRepn) VError(" input pixel repn must be ubyte");

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

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

  for (j=0; j<256; j++) histo[j] = 0;

  xmin = VPixelMaxValue(src);
  xmax = VPixelMinValue(src);
  background = xmin;
  
  src_pp = (VUByte *) VImageData(src);
  for (i=0; i<npixels; i++) {
    j = *src_pp++;
    if (j <= background) continue;
    histo[j]++;
  }
 
  sum = 0;
  for (j=0; j<256; j++) sum += histo[j];
  for (j=0; j<256; j++) histo[j] /= sum;

  xmin = 0;
  sum  = 0;
  for (j=0; j<256; j++) {
    if (j > background) sum += histo[j];
    if (sum > low) break;
  }
  xmin = j;

  xmax = 255.0;
  sum = 0;
  for (j=255; j>0; j--) {
    if (j > background) sum += histo[j];
    if (sum > high) break;
  }
  xmax = j;


  slope = (float) (255.0) / ((float) (xmax - xmin));
  
  src_pp  = (VUByte *) VImageData(src);
  dest_pp = (VUByte *) VImageData(dest);

  for (i=0; i<npixels; i++) {
    u = *src_pp;
 
    if (u <= background) {
      v = 0;
    }
    else {
      v = (int) (slope * (u - xmin) + 0.5);
      if (v <   0) v = 0;
      if (v > 255) v = 255;
    }

    *dest_pp = (VUByte) v;
    src_pp++;
    dest_pp++;
  }

  VFree(histo);
  VCopyImageAttrs (src, dest);
  return dest;
}
コード例 #18
0
ファイル: vgetcontrast.c プロジェクト: Rollmops/lipsia
VAttrList
VGetContrast(VAttrList list, gsl_vector_float *con, VShort type) {
    VAttrList out_list;
    int nbands = 0, nrows = 0, ncols = 0, band, row, col;
    VImage src = NULL, dest = NULL, std_image = NULL;
    VImage beta_images[MBETA], res_image = NULL, bcov_image = NULL;
    VString buf = NULL;
    VAttrListPosn posn;
    VString str;
    int    i, nbeta;
    float  t = 0, s = 0, tsigma = 0, z = 0, zmax = 0, zmin = 0;
    float  sigma, var, sum, df;
    float  *ptr1, *ptr2;
    char *constring = NULL;
    gsl_vector_float *beta = NULL, *tmp = NULL;
    gsl_matrix_float *bcov = NULL;
    i = 0;
    for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) {
        if(VGetAttrRepn(& posn) != VImageRepn)
            continue;
        VGetAttrValue(& posn, NULL, VImageRepn, & src);
        if(VPixelRepn(src) != VFloatRepn)
            continue;
        VGetAttr(VImageAttrList(src), "modality", NULL, VStringRepn, &str);
        if(strcmp(str, "BETA") == 0) {
            beta_images[i++] = VCopyImage(src, NULL, VAllBands);
        } else if(strcmp(str, "RES/trRV") == 0) {
            res_image = VCopyImage(src, NULL, VAllBands);
        } else if(strcmp(str, "BCOV") == 0) {
            bcov_image = VCopyImage(src, NULL, VAllBands);
        }
    }
    nbeta  = VImageNRows(bcov_image);
    nbands = VImageNBands(beta_images[0]);
    nrows  = VImageNRows(beta_images[0]);
    ncols  = VImageNColumns(beta_images[0]);
    if(VGetAttr(VImageAttrList(beta_images[0]), "df", NULL, VFloatRepn, &df) != VAttrFound)
        VError(" attribute 'df' not found");
    if(nbeta > MBETA) {
        fprintf(stderr, " number of betas: %d,  maximum number of betas: %d\n", nbeta, MBETA);
        VError(" maximum number of betas is exceeded");
    }
    /*
    ** read contrast vector
    */
    if(nbeta != con->size)
        VError("contrast vector has bad length (%d), correct length is %d", con->size, nbeta);
    fprintf(stderr, " contrast vector:\n");
    char str1[10];
    constring = (char *)VMalloc(sizeof(char) * 10 * nbeta);
    constring[0] = '\0';
    for(i = 0; i < nbeta; i++) {
        fprintf(stderr, "  %.2f", fvget(con, i));
        sprintf(str1, "%1.2f ", fvget(con, i));
        strcat((char *)constring, (const char *)str1);
    }
    fprintf(stderr, "\n");
    /* get variance estimation */
    bcov = gsl_matrix_float_alloc(nbeta, nbeta);
    ptr1 = VImageData(bcov_image);
    ptr2 = bcov->data;
    for(i = 0; i < nbeta * nbeta; i++)
        *ptr2++ = *ptr1++;
    gsl_matrix_float_transpose(bcov);
    tmp   = fmat_x_vector(bcov, con, tmp);
    var   = fskalarproduct(tmp, con);
    sigma = sqrt(var);
    /*
    ** create output data structs
    */
    out_list = VCreateAttrList();
    dest = VCreateImage(nbands, nrows, ncols, VFloatRepn);
    VFillImage(dest, VAllBands, 0);
    VCopyImageAttrs(beta_images[0], dest);
    switch(type) {
    case 0:    /* conimg  */
        buf = VNewString("conimg");
        break;
    case 1:    /* t-image */
        buf = VNewString("tmap");
        break;
    case 2:    /* zmap    */
        buf = VNewString("zmap");
        break;
    default:
        VError(" illegal type");
    }
    fprintf(stderr, " output type: %s\n", buf);
    VSetAttr(VImageAttrList(dest), "modality", NULL, VStringRepn, buf);
    VSetAttr(VImageAttrList(dest), "name", NULL, VStringRepn, buf);
    VSetAttr(VImageAttrList(dest), "contrast", NULL, VStringRepn, constring);
    VAppendAttr(out_list, "image", NULL, VImageRepn, dest);
    if(type == 0) {
        std_image = VCreateImage(nbands, nrows, ncols, VFloatRepn);
        VFillImage(std_image, VAllBands, 0);
        VCopyImageAttrs(beta_images[0], std_image);
        VSetAttr(VImageAttrList(std_image), "modality", NULL, VStringRepn, "std_dev");
        VSetAttr(VImageAttrList(std_image), "name", NULL, VStringRepn, "std_dev");
        VAppendAttr(out_list, "image", NULL, VImageRepn, std_image);
    }
    /*
    ** loop thru image
    */
    zmax = zmin = 0;
    beta = gsl_vector_float_alloc(nbeta);
    for(band = 0; band < nbands; band++) {
        for(row = 0; row < nrows; row++) {
            for(col = 0; col < ncols; col++) {
                t = z = sum = 0;
                ptr1 = beta->data;
                for(i = 0; i < nbeta; i++) {
                    *ptr1++ = VPixel(beta_images[i], band, row, col, VFloat);
                }
                sum  = fskalarproduct(beta, con);
                if(ABS(sum) < 1.0e-10)
                    continue;
                s = VPixel(res_image, band, row, col, VFloat);
                tsigma = sqrt(s) * sigma;
                if(tsigma > 0.00001)
                    t = sum / tsigma;
                else
                    t = 0;
                if(isnan(t) || isinf(t))
                    t = 0;
                switch(type) {
                case 0:    /* conimg  */
                    z = sum;
                    break;
                case 1:    /* t-image */
                    z = t;
                    break;
                case 2:    /* zmap    */
                    z = t2z_approx(t, df);
                    if(z > 30)
                        z = 30;
                    if(sum < 0)
                        z = -z;
                    break;
                default:
                    ;
                }
                if(isnan(z) || isinf(z))
                    z = 0;
                if(z > zmax)
                    zmax = z;
                if(z < zmin)
                    zmin = z;
                VPixel(dest, band, row, col, VFloat) = z;
                if(type == 0)
                    VPixel(std_image, band, row, col, VFloat) = tsigma;
            }
        }
    }
    fprintf(stderr, " min= %.3f,  max= %.3f\n", zmin, zmax);
    return out_list;
}
コード例 #19
0
ファイル: FIL_Vista_Image.c プロジェクト: KoraST/ft_BIU-1
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
}
コード例 #20
0
ファイル: vbayes.c プロジェクト: Rollmops/lipsia
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;
}