예제 #1
0
파일: vlabel2d.c 프로젝트: Rollmops/via
int 
main (int argc,char *argv[])
{ 
  static VShort aneighb = 1;
  static VShort arepn = 1;
  static VOptionDescRec  options[] = {
    {"n",VShortRepn,1,(VPointer) &aneighb,
       VOptionalOpt,ADJDict,"neighbourhood type"},
    {"repn",VShortRepn,1,(VPointer) &arepn,
       VOptionalOpt,TYPEDict,"output representation type (ubyte or short)"}
  };
  FILE *in_file,*out_file;
  VAttrList list=NULL;
  VAttrListPosn posn;
  VImage src=NULL,dest=NULL;
  int nl=0,neighb;
  VRepnKind repn;
  char prg[50];	
  sprintf(prg,"vlabel2d V%s", getVersion());
  fprintf (stderr, "%s\n", prg);

  VParseFilterCmd (VNumber (options),options,argc,argv,&in_file,&out_file);

  if (arepn == 0) repn = VUByteRepn;
  else repn = VShortRepn;

  if (aneighb == 0) neighb = 4;
  else neighb = 8;

  if (! (list = VReadFile (in_file, NULL))) exit (1);
  fclose(in_file);

  for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) {
    if (VGetAttrRepn (& posn) != VImageRepn) continue;
    VGetAttrValue (& posn, NULL,VImageRepn, & src);

    dest = VLabelImage2d(src,NULL, (int) neighb,repn,&nl);
    fprintf(stderr," numlabels= %d\n",nl);
    VSetAttrValue (& posn, NULL,VImageRepn,dest);
  }
  if (src == NULL) VError(" no input image found");


  VHistory(VNumber(options),options,prg,&list,&list);
  if (! VWriteFile (out_file, list)) exit (1);
  fprintf (stderr, "%s: done.\n", argv[0]);
  return 0;
}
예제 #2
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;
}