コード例 #1
0
    static void Main()
    {
        try
        {
            // In native ITK, images are templated over the pixel type
            // and number of dimensions. Templates are not supported by
            // the CLR, so therefore the creation of images with
            // ManagedITK is somewhat different. The wrapping process
            // creates two types of images: a number of explicit types
            // for various template combinations, and a wrapper type.
            // In this example we use an explicit type.

            // Create an image using an explicit type
            // Note: "UC2" stands for itk::Image< unsigned char, 2 >.
            itkImageBase image = itkImage_UC2.New();

            // Create some image information
            itkSize size = new itkSize(128, 128);
            itkSpacing spacing = new itkSpacing(1.0, 1.0);
            itkIndex index = new itkIndex(0, 0);
            itkPoint origin = new itkPoint(0.0, 0.0);
            itkImageRegion region = new itkImageRegion(size, index);

            // Set the information
            // Note: we must call SetRegions() *before* calling Allocate().
            image.SetRegions(region);
            image.Allocate();
            image.Spacing = spacing;
            image.Origin = origin;

            // Fill the image with gray (ie. 128)
            image.FillBuffer(128);

            // Test a pixel value
            itkPixel pixel = image.GetPixel(index);

            // Display some image information
            Console.WriteLine(String.Format("Image{0}={1}",index, pixel));
            Console.WriteLine(String.Format("PixelType={0}",image.PixelType));
            Console.WriteLine(String.Format("Dimension={0}",image.Dimension));
            Console.WriteLine(String.Format("Size={0}",image.Size));
            Console.WriteLine(String.Format("Spacing={0}",image.Spacing));
            Console.WriteLine(String.Format("Origin={0}",image.Origin));
            Console.WriteLine("======================================");
            Console.WriteLine("Image.ToString():");
            Console.WriteLine(image);

            // Dispose of the image
            // Note: the image will be automatically disposed when it goes
            // out of scope, however it is good practice to dispose of
            // objects when they are no longer required.
            image.Dispose();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    } // end main
コード例 #2
0
ファイル: ItkHelper.cs プロジェクト: hksonngan/Xian
        /// <summary>
        /// Create a managed ITK image from a given ClearCanvas image.
        /// </summary>
        /// <param name="image">The given grayscale image.</param>
        public static itkImageBase CreateItkImage(GrayscaleImageGraphic image)
        {
            itkPixelType pixelType;

            if (image.BitsPerPixel == 16)
            {
                if (image.IsSigned)
                {
                    pixelType = itkPixelType.SS;
                }
                else
                {
                    pixelType = itkPixelType.SS;// itkPixelType.US; there is no itkImage_US<dim>
                }
            }
            else //if (image.BitsPerPixel == 8)
            {
                if (image.IsSigned)
                {
                    pixelType = itkPixelType.UC;// itkPixelType.SC; threre is no itkImage_UC<dim>
                }
                else
                {
                    pixelType = itkPixelType.UC;
                }
            }
            itkImage itkImage = itkImage.New(pixelType, 2);
            // Create some image information
            itkSize        size    = new itkSize(image.Columns, image.Rows);
            itkSpacing     spacing = new itkSpacing(1.0, 1.0);
            itkIndex       index   = new itkIndex(0, 0);
            itkPoint       origin  = new itkPoint(0.0, 0.0);
            itkImageRegion region  = new itkImageRegion(size, index);

            // Set the information
            // Note: we must call SetRegions() *before* calling Allocate().
            itkImage.SetRegions(region);
            itkImage.Allocate();
            itkImage.Spacing = spacing;
            itkImage.Origin  = origin;
            //itkImage.BufferedRegion;
            return(itkImage);
        }
コード例 #3
0
    static void Main(string[] args)
    {
        try
        {
            // Read an explicitly typed image
            itkImageBase input = itkImage_UC2.New();
            input.Read(args[0]);

            // Allocate an empty output image
            itkImageBase output = itkImage_UC2.New();
            itkImageRegion region = input.LargestPossibleRegion;
            output.SetRegions(region);
            output.Allocate();
            output.FillBuffer(0);
            output.Spacing = input.Spacing;
            output.Origin = input.Origin;

            // Create iterators to walk the input and output images
            itkImageRegionConstIterator_IUC2 inputIt;
            itkImageRegionIterator_IUC2 outputIt;
            inputIt = new itkImageRegionConstIterator_IUC2(input, region);
            outputIt = new itkImageRegionIterator_IUC2(output, region);

            // Walk the images using the iterators
            foreach (itkPixel pixel in inputIt)
            {
                outputIt.Set(pixel);
                outputIt++;
            }

            // Write the output image
            output.Write(args[1]);

            // Dispose of the images
            input.Dispose();
            output.Dispose();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    } // end main
コード例 #4
0
ファイル: ItkHelper.cs プロジェクト: nhannd/Xian
		/// <summary>
		/// Create a managed ITK image from a given ClearCanvas image.
		/// </summary>
		/// <param name="image">The given grayscale image.</param>
        public static itkImageBase CreateItkImage(GrayscaleImageGraphic image)
		{
            itkPixelType pixelType;
            if (image.BitsPerPixel == 16)
            {
                if (image.IsSigned)
                {
                    pixelType = itkPixelType.SS;
                }
                else
                {
                    pixelType = itkPixelType.SS;// itkPixelType.US; there is no itkImage_US<dim>
                }
            }
            else //if (image.BitsPerPixel == 8)
            {
                if (image.IsSigned)
                {
                    pixelType = itkPixelType.UC;// itkPixelType.SC; threre is no itkImage_UC<dim>
                }
                else
                {
                    pixelType = itkPixelType.UC;
                }
            }
            itkImage itkImage = itkImage.New(pixelType, 2);
            // Create some image information
            itkSize size = new itkSize(image.Columns, image.Rows);
            itkSpacing spacing = new itkSpacing(1.0, 1.0);
            itkIndex index = new itkIndex(0, 0);
            itkPoint origin = new itkPoint(0.0, 0.0);
            itkImageRegion region = new itkImageRegion(size, index);
            // Set the information
            // Note: we must call SetRegions() *before* calling Allocate().
            itkImage.SetRegions(region);
            itkImage.Allocate();
            itkImage.Spacing = spacing;
            itkImage.Origin = origin;
            //itkImage.BufferedRegion;
            return itkImage;
		}
コード例 #5
0
        public void Apply()
        {
            if (this.SelectedImageGraphicProvider == null)
            {
                return;
            }

            ImageGraphic image = this.SelectedImageGraphicProvider.ImageGraphic;

            if (image == null)
            {
                return;
            }

            if (!(image is GrayscaleImageGraphic))
            {
                return;
            }

            byte[] pixels = image.PixelData.Raw;

            itkImageBase   input  = ItkHelper.CreateItkImage(image as GrayscaleImageGraphic);
            itkImageRegion region = input.LargestPossibleRegion;

            itkImageBase output = itkImage.New(input);

            ItkHelper.CopyToItkImage(image as GrayscaleImageGraphic, input);

            string mangledType            = input.MangledTypeString;
            string mangledType2           = input.PixelType.MangledTypeString;
            CastImageFilterType castToIF2 = CastImageFilterType.New(mangledType + "IF2");

            castToIF2.SetInput(input);

            FilterType filter = FilterType.New("IF2IF2");

            filter.SetInput(castToIF2.GetOutput());

            intensityFilterType intensityFilter = intensityFilterType.New("IF2" + mangledType);

            intensityFilter.SetInput(filter.GetOutput());
            intensityFilter.OutputMinimum = 0;
            if (image.BitsPerPixel == 16)
            {
                intensityFilter.OutputMaximum = (image as GrayscaleImageGraphic).ModalityLut.MaxInputValue;
            }
            else
            {
                intensityFilter.OutputMaximum = 255;
            }
            intensityFilter.Update();


            intensityFilter.GetOutput(output);
            ItkHelper.CopyFromItkImage(image as GrayscaleImageGraphic, output);
            image.Draw();

            filter.Dispose();
            intensityFilter.Dispose();
            input.Dispose();
            output.Dispose();
        }
コード例 #6
0
        public void Apply()
        {
            if (this.SelectedImageGraphicProvider == null)
            {
                return;
            }

            ImageGraphic image = this.SelectedImageGraphicProvider.ImageGraphic;

            if (image == null)
            {
                return;
            }

            if (!(image is GrayscaleImageGraphic))
            {
                return;
            }

            byte[] pixels = image.PixelData.Raw;

            itkImageBase   input  = ItkHelper.CreateItkImage(image as GrayscaleImageGraphic);
            itkImageRegion region = input.LargestPossibleRegion;

            itkImageBase output = itkImage.New(input);

            ItkHelper.CopyToItkImage(image as GrayscaleImageGraphic, input);

            String mangledType            = input.MangledTypeString;
            CastImageFilterType castToIF2 = CastImageFilterType.New(mangledType + "IF2");

            castToIF2.SetInput(input);

            FilterType filter = FilterType.New("IF2IF2");

            filter.SetInput(castToIF2.GetOutput());

            // TODO: need to allow user to set parameters of filter
            filter.LowerThreshold = 90;
            filter.UpperThreshold = 127;
            //filter.OutsideValue = 0;
            // smoothing the edge
            double[] error = { 0.01, 0.01 };
            filter.MaximumError = error;
            double[] var = { 1.0, 1.0 };
            filter.Variance = var;

            intensityFilterType intensityFilter = intensityFilterType.New("IF2" + mangledType);

            intensityFilter.SetInput(filter.GetOutput());
            intensityFilter.OutputMinimum = 0;
            if (image.BitsPerPixel == 16)
            {
                intensityFilter.OutputMaximum = (image as GrayscaleImageGraphic).ModalityLut.MaxInputValue;//32767;
            }
            else
            {
                intensityFilter.OutputMaximum = 255;
            }
            intensityFilter.Update();

#if DEBUG
            bool debug = false;
            if (debug)
            {
                itkImageBase outputIF2 = itkImage.New("IF2");
                filter.GetOutput(outputIF2);
                float min = float.MaxValue, max = float.MinValue;
                unsafe
                {
                    fixed(byte *pDstByte = image.PixelData.Raw)
                    {
                        itkImageRegionConstIterator_IF2 itkIt = new itkImageRegionConstIterator_IF2(outputIF2, region);
                        byte *pDst   = (byte *)pDstByte;
                        int   height = image.Rows;
                        int   width  = image.Columns;

                        for (int y = 0; y < height; y++)
                        {
                            for (int x = 0; x < width; x++)
                            {
                                float f = itkIt.Get().ValueAsF;
                                if (f > max)
                                {
                                    max = f;
                                }
                                if (f < min)
                                {
                                    min = f;
                                }
                                pDst[0] = (byte)itkIt.Get().ValueAsF;
                                pDst++;
                                itkIt++;
                            }
                        }
                    }
                }
                Console.WriteLine("min max "); Console.Write(min); Console.Write(" "); Console.WriteLine(max);
            }
#endif

            intensityFilter.GetOutput(output);
            ItkHelper.CopyFromItkImage(image as GrayscaleImageGraphic, output);
            image.Draw();

            filter.Dispose();
            intensityFilter.Dispose();
            input.Dispose();
            output.Dispose();
        }