示例#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
    static void Main(string[] args)
    {
        try
        {
            // Create a managed array to import
            const Int32 Width = 256;
            const Int32 Height = 256;
            List<float> list = new List<float>(Width * Height);
            for (int j = 0; j < Height; j++)
            {
                for (int i = 0; i < Width; i++)
                {
                    if (i == j)
                        list.Add(Convert.ToSingle(i));
                    else
                        list.Add(0.0F);
                }
            }

            // Pin the managed array for use with unmanaged code
            float[] array = list.ToArray();
            GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);

            // Setup for import
            itkImportImageFilter_F2 importer = itkImportImageFilter_F2.New();
            itkSize size = new itkSize(Width, Height);
            itkIndex index = new itkIndex(0, 0);
            importer.Region = new itkImageRegion(size, index);
            importer.Spacing = new itkSpacing(1.0, 1.0);
            importer.Origin = new itkPoint(0.0, 0.0);
            importer.SetImportPointer(handle.AddrOfPinnedObject(), (uint)list.Count, false);

            // Perform import
            itkImageBase output = itkImage_F2.New();
            importer.UpdateLargestPossibleRegion();
            importer.GetOutput(output);
            output.Write(args[0]);

            // Display some image information
            Console.WriteLine(String.Format("{0}", output));

            // Cleanup
            output.Dispose();
            importer.Dispose();
            handle.Free();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    } // end main
示例#3
0
        /// <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);
        }
示例#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;
		}