Exemple #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
Exemple #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
Exemple #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);
        }
Exemple #4
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;
		}
        public void Apply()
        {
            if (this.SelectedImageGraphicProvider == null)
            {
                return;
            }

            ImageGraphic image = this.SelectedImageGraphicProvider.ImageGraphic;

            if (image == null)
            {
                return;
            }

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

            itkImageBase input  = ItkHelper.CreateItkImage(image as GrayscaleImageGraphic);
            itkImageBase output = itkImage.New(input);

            ItkHelper.CopyToItkImage(image as GrayscaleImageGraphic, input);

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

            SmoothingFilterType smoothingFilter = SmoothingFilterType.New("IF2IF2");

            smoothingFilter.TimeStep             = 0.125;
            smoothingFilter.NumberOfIterations   = 5;
            smoothingFilter.ConductanceParameter = 9.0;

            GradientMagnitudeFilterType gradientMagnitudeFilter = GradientMagnitudeFilterType.New("IF2IF2");

            gradientMagnitudeFilter.Sigma = 1.0;

            SigmoidFilterType sigmoidFilter = SigmoidFilterType.New("IF2IF2");

            sigmoidFilter.OutputMinimum = 0.0;
            sigmoidFilter.OutputMaximum = 1.0;
            sigmoidFilter.Alpha         = -0.5; //-0.3
            sigmoidFilter.Beta          = 3.0;  //2.0

            FastMarchingFilterType fastMarchingFilter = FastMarchingFilterType.New("IF2IF2");
            double seedValue = 0.0;

            int[]    seedPosition = { 256, 256 };// user input
            itkIndex seedIndex    = new itkIndex(seedPosition);

            itkLevelSetNode[] trialPoints = { new itkLevelSetNode(seedValue, seedIndex) };
            fastMarchingFilter.TrialPoints   = trialPoints;
            fastMarchingFilter.StoppingValue = 100;

            BinaryThresholdFilterType binaryThresholdFilter = BinaryThresholdFilterType.New("IF2" + mangledType); //to UC2?

            binaryThresholdFilter.UpperThreshold = 100.0;                                                         //200
            binaryThresholdFilter.LowerThreshold = 0.0;
            binaryThresholdFilter.OutsideValue   = 0;
            if (image.BitsPerPixel == 16)
            {
                binaryThresholdFilter.InsideValue = (image as GrayscaleImageGraphic).ModalityLut.MaxInputValue;//32767;
            }
            else
            {
                binaryThresholdFilter.InsideValue = 255;
            }

            //intensityFilterType intensityFilter = intensityFilterType.New("UC2" + mangledType);
            //intensityFilter.OutputMinimum = 0;
            //if (image.BitsPerPixel == 16)
            //    intensityFilter.OutputMaximum = (image as GrayscaleImageGraphic).ModalityLut.MaxInputValue;//32767;
            //else
            //    intensityFilter.OutputMaximum = 255;

            // Make data stream connections
            castToIF2.SetInput(input);
            smoothingFilter.SetInput(castToIF2.GetOutput());
            gradientMagnitudeFilter.SetInput(smoothingFilter.GetOutput());
            sigmoidFilter.SetInput(gradientMagnitudeFilter.GetOutput());
            fastMarchingFilter.SetInput(sigmoidFilter.GetOutput());
            binaryThresholdFilter.SetInput(fastMarchingFilter.GetOutput());
            //intensityFilter.SetInput(binaryThresholdFilter.GetOutput());

            //smoothingFilter.Update();
            fastMarchingFilter.OutputSize = input.BufferedRegion.Size;//?
            binaryThresholdFilter.Update();

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

            input.Dispose();
            output.Dispose();
        }
		public void Apply()
		{
			if (this.SelectedImageGraphicProvider == null)
				return;

			ImageGraphic image = this.SelectedImageGraphicProvider.ImageGraphic;

			if (image == null)
				return;

			if (!(image is GrayscaleImageGraphic))
				return;

            itkImageBase input = ItkHelper.CreateItkImage(image as GrayscaleImageGraphic);
            itkImageBase output = itkImage.New(input);
            ItkHelper.CopyToItkImage(image as GrayscaleImageGraphic, input);

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

            SmoothingFilterType smoothingFilter = SmoothingFilterType.New("IF2IF2");
            smoothingFilter.TimeStep = 0.125;
            smoothingFilter.NumberOfIterations = 5;
            smoothingFilter.ConductanceParameter = 9.0;

            GradientMagnitudeFilterType gradientMagnitudeFilter = GradientMagnitudeFilterType.New("IF2IF2");
            gradientMagnitudeFilter.Sigma = 1.0;

            SigmoidFilterType sigmoidFilter = SigmoidFilterType.New("IF2IF2");
            sigmoidFilter.OutputMinimum = 0.0;
            sigmoidFilter.OutputMaximum = 1.0;
            sigmoidFilter.Alpha = -0.5;//-0.3
            sigmoidFilter.Beta = 3.0;//2.0

            FastMarchingFilterType fastMarchingFilter = FastMarchingFilterType.New("IF2IF2");
            double initialDistance = 5.0;
            double seedValue = -initialDistance;
            int[] seedPosition = {256, 256};// user input
            itkIndex seedIndex = new itkIndex(seedPosition);
            itkLevelSetNode[] trialPoints = { new itkLevelSetNode(seedValue, seedIndex) };
            fastMarchingFilter.TrialPoints = trialPoints;
            fastMarchingFilter.SpeedConstant = 1.0;
            fastMarchingFilter.StoppingValue = 600;

            ShapeDetectionLevelSetFilterType shapeDetectionFilter = ShapeDetectionLevelSetFilterType.New("IF2IF2F");//IF2IF2
            double curvatureScaling = 0.05;
            double propagationScaling = 1.0;
            shapeDetectionFilter.CurvatureScaling = curvatureScaling;
            shapeDetectionFilter.PropagationScaling = propagationScaling;
            shapeDetectionFilter.MaximumRMSError = 0.02;
            shapeDetectionFilter.NumberOfIterations = 800;

            BinaryThresholdFilterType binaryThresholdFilter = BinaryThresholdFilterType.New("IF2" + mangledType);//to UC2?
            binaryThresholdFilter.UpperThreshold = 0.0; // to display the zero set of the resulting level set
            binaryThresholdFilter.LowerThreshold = -1000; // large negative 
            binaryThresholdFilter.OutsideValue = 0;
            if (image.BitsPerPixel == 16)
                binaryThresholdFilter.InsideValue = (image as GrayscaleImageGraphic).ModalityLut.MaxInputValue;//32767;
            else
                binaryThresholdFilter.InsideValue = 255;

            //intensityFilterType intensityFilter = intensityFilterType.New("UC2" + mangledType);
            //intensityFilter.OutputMinimum = 0;
            //if (image.BitsPerPixel == 16)
            //    intensityFilter.OutputMaximum = (image as GrayscaleImageGraphic).ModalityLut.MaxInputValue;//32767;
            //else
            //    intensityFilter.OutputMaximum = 255;

            // Make data stream connections
            castToIF2.SetInput(input);
            smoothingFilter.SetInput(castToIF2.GetOutput());
            gradientMagnitudeFilter.SetInput(smoothingFilter.GetOutput());
            sigmoidFilter.SetInput(gradientMagnitudeFilter.GetOutput());
            shapeDetectionFilter.SetInput(fastMarchingFilter.GetOutput());
            shapeDetectionFilter.SetFeatureImage(sigmoidFilter.GetOutput());
            binaryThresholdFilter.SetInput(shapeDetectionFilter.GetOutput());
            //intensityFilter.SetInput(binaryThresholdFilter.GetOutput());

            //smoothingFilter.Update();
            fastMarchingFilter.OutputSize = input.BufferedRegion.Size;//?
            binaryThresholdFilter.Update();

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

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