Пример #1
0
        private static int[] AsPixelArray(Image image)
        {
            var copy          = SimpleITK.Cast(image, PixelIDValueEnum.sitkInt32);
            var length        = Convert.ToInt32(copy.GetNumberOfPixels());
            var buffer        = copy.GetConstBufferAsInt32();
            var bufferAsArray = new int[length * sizeof(int)];

            Marshal.Copy(buffer, bufferAsArray, 0, length);
            return(bufferAsArray);
        }
Пример #2
0
        public Bitmap konwertujObraz(Image obraz1, int przekroj = 0)
        {
            uint r = obraz1.GetWidth();
            //  VectorUInt32 w = new VectorUInt32(new[] { r, 512, 4 + 1 });

            VectorInt32 start = new VectorInt32(new[] { 0, 0, 0 });
            VectorInt32 size1 = new VectorInt32(new[] { 512, 512, 1 });

            obraz1 = WybierzPrzekroj(obraz1, przekroj);
            IntensityWindowingImageFilter normalize = new IntensityWindowingImageFilter();

            normalize.SetOutputMinimum(0);
            normalize.SetOutputMaximum(255);
            obraz1 = normalize.Execute(obraz1);

            PixelIDValueEnum u = PixelIDValueEnum.sitkFloat32;
            int          len   = 1;
            Image        input = SimpleITK.Cast(obraz1, u);
            VectorUInt32 size  = input.GetSize();

            for (int dim = 0; dim < input.GetDimension(); dim++)
            {
                len *= (int)size[dim];
            }
            IntPtr buffer    = input.GetBufferAsFloat();
            float  bufferPtr = (float)buffer.ToInt32();

            float[] bufferAsArray = new float[len];
            float[,] newData = new float[size[0], size[1]];
            Marshal.Copy(buffer, bufferAsArray, 0, len);
            obrazBitmap = new Bitmap(Convert.ToInt32(size[0]), Convert.ToInt32(size[1]));
            for (int j = 0; j < size[1]; j++)
            {
                for (int i = 0; i < size[0]; i++)
                {
                    var bur = bufferAsArray[j * size[1] + i];
                    System.Drawing.Color newColor = System.Drawing.Color.FromArgb((int)bur, 0, 0, 0);
                    obrazBitmap.SetPixel(j, i, newColor);
                }
            }
            Color s = obrazBitmap.GetPixel(34, 56);

            return(obrazBitmap);
        }
        static void Main(string[] args)
        {
            if (args.Length < 8)
            {
                Console.WriteLine("Missing Parameters ");
                Console.WriteLine("Usage: " + System.AppDomain.CurrentDomain.FriendlyName +
                                  "inputImage initialModel outputImage cannyThreshold " +
                                  "cannyVariance advectionWeight initialModelIsovalue maximumIterations ");
                return;
            }

            string inputFilename        = args[0];
            string initialModelFilename = args[1];

            string outputFilename = args[2];

            double cannyThreshold      = double.Parse(args[3], CultureInfo.InvariantCulture);
            double cannyVariance       = double.Parse(args[4], CultureInfo.InvariantCulture);
            double advectionWeight     = double.Parse(args[5], CultureInfo.InvariantCulture);
            double intialModelIsovalue = double.Parse(args[6], CultureInfo.InvariantCulture);
            uint   maxIterations       = uint.Parse(args[7], CultureInfo.InvariantCulture);

            // Read input image

            SitkImage inputImage   = SimpleITK.ReadImage(inputFilename, PixelId.sitkFloat32);
            SitkImage initialModel = SimpleITK.ReadImage(initialModelFilename, PixelId.sitkFloat32);

            //  The input image will be processed with a few iterations of
            //  feature-preserving diffusion.  We create a filter and set the
            //  appropriate parameters.

            GradientAnisotropicDiffusionImageFilter diffusion = new GradientAnisotropicDiffusionImageFilter();

            diffusion.SetConductanceParameter(1.0);
            diffusion.SetTimeStep(0.125);
            diffusion.SetNumberOfIterations(5);
            SitkImage diffusedImage = diffusion.Execute(inputImage);

            // As with the other ITK level set segmentation filters, the terms of the
            // CannySegmentationLevelSetImageFilter level set equation can be
            // weighted by scalars.  For this application we will modify the relative
            // weight of the advection term.  The propagation and curvature term weights
            // are set to their defaults of 0 and 1, respectively.

            CannySegmentationLevelSetImageFilter cannySegmentation = new CannySegmentationLevelSetImageFilter();

            cannySegmentation.SetAdvectionScaling(advectionWeight);
            cannySegmentation.SetCurvatureScaling(1.0);
            cannySegmentation.SetPropagationScaling(0.0);

            //  The maximum number of iterations is specified from the command line.
            //  It may not be desirable in some applications to run the filter to
            //  convergence.  Only a few iterations may be required.

            cannySegmentation.SetMaximumRMSError(0.01);
            cannySegmentation.SetNumberOfIterations(maxIterations);

            //  There are two important parameters in the
            //  CannySegmentationLevelSetImageFilter to control the behavior of the
            //  Canny edge detection.  The variance parameter controls the
            //  amount of Gaussian smoothing on the input image.  The threshold
            //  parameter indicates the lowest allowed value in the output image.
            //  Thresholding is used to suppress Canny edges whose gradient magnitudes
            //  fall below a certain value.

            cannySegmentation.SetThreshold(cannyThreshold);
            cannySegmentation.SetVariance(cannyVariance);

            // Finally, it is very important to specify the isovalue of the surface in
            // the initial model input image. In a binary image, for example, the
            // isosurface is found midway between the foreground and background values.

            cannySegmentation.SetIsoSurfaceValue(intialModelIsovalue);

            SitkImage output = cannySegmentation.Execute(initialModel, diffusedImage);

            BinaryThresholdImageFilter thresholder = new BinaryThresholdImageFilter();

            thresholder.SetUpperThreshold(10.0);
            thresholder.SetLowerThreshold(0.0);

            thresholder.SetOutsideValue(0);
            thresholder.SetInsideValue(255);
            output = thresholder.Execute(output);


            output = SimpleITK.Cast(output, PixelIDValueEnum.sitkUInt8);

            SimpleITK.WriteImage(output, outputFilename);

            // Print out some useful information
            Console.WriteLine("");
            Console.WriteLine("Max. no. iterations: {0}", cannySegmentation.GetNumberOfIterations());
            Console.WriteLine("Max. RMS error: {0}", cannySegmentation.GetMaximumRMSError());
            Console.WriteLine("");
            Console.WriteLine("No. elpased iterations: {0}", cannySegmentation.GetElapsedIterations());
            Console.WriteLine("RMS change: {0}", cannySegmentation.GetRMSChange());
        }
Пример #4
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: SimpleGaussian <input>");
                return;
            }

            // Read input image
            itk.simple.Image input = SimpleITK.ReadImage(args[0]);

            // Cast to we know the the pixel type
            input = SimpleITK.Cast(input, PixelId.sitkFloat32);

            // calculate the number of pixels
            VectorUInt32 size = input.GetSize();
            int          len  = 1;

            for (int dim = 0; dim < input.GetDimension(); dim++)
            {
                len *= (int)size[dim];
            }
            IntPtr buffer = input.GetBufferAsFloat();

            // Note: C# also has a GetConstBufferAs... methods which do not
            // implicitly call MakeUnique.

            // There are two ways to access the buffer:

            // (1) Access the underlying buffer as a pointer in an "unsafe" block
            // (note that in C# "unsafe" simply means that the compiler can not
            // perform full type checking), and requires the -unsafe compiler flag
            // unsafe {
            //   float* bufferPtr = (float*)buffer.ToPointer();

            //   // Now the byte pointer can be accessed as per Brad's email
            //   // (of course this example is only a 2d single channel image):
            //   // This is a 1-D array but can be access as a 3-D. Given an
            //   // image of size [xS,yS,zS], you can access the image at
            //   // index [x,y,z] as you wish by image[x+y*xS+z*xS*yS],
            //   // so x is the fastest axis and z is the slowest.
            //   for (int j = 0; j < size[1]; j++) {
            //     for (int i = 0; i < size[0]; i++) {
            //       float pixel = bufferPtr[i + j*size[1]];
            //       // Do something with pixel here
            //     }
            //   }
            // }

            // (2) Copy the buffer to a "safe" array (i.e. a fully typed array)
            // (note that this means memory is duplicated)
            float[] bufferAsArray = new float[len]; // Allocates new memory the size of input
            Marshal.Copy(buffer, bufferAsArray, 0, len);
            double total = 0.0;

            for (int j = 0; j < size[1]; j++)
            {
                for (int i = 0; i < size[0]; i++)
                {
                    float pixel = bufferAsArray[i + j * size[1]];
                    total += pixel;
                }
            }
            Console.WriteLine("Pixel value total: {0}", total);
        }
Пример #5
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: inputImage outputImage");
                return;
            }

            string inputFilename  = args[0];
            string outputFilename = args[1];

            // Read input image
            SitkImage input = SimpleITK.ReadImage(inputFilename);

            // Cast so we know the the pixel type
            input = SimpleITK.Cast(input, PixelId.sitkFloat32);

            // calculate the number of pixels
            VectorUInt32 size = input.GetSize();
            int          len  = 1;

            for (int dim = 0; dim < input.GetDimension(); dim++)
            {
                len *= (int)size[dim];
            }
            IntPtr buffer = input.GetBufferAsFloat();

            // There are two ways to access the buffer:

            // (1) Access the underlying buffer as a pointer in an "unsafe" block
            // (note that in C# "unsafe" simply means that the compiler can not
            // perform full type checking), and requires the -unsafe compiler flag
            // unsafe {
            //   float* bufferPtr = (float*)buffer.ToPointer();

            //   // Now the byte pointer can be accessed as per Brad's email
            //   // (of course this example is only a 2d single channel image):
            //   // This is a 1-D array but can be access as a 3-D. Given an
            //   // image of size [xS,yS,zS], you can access the image at
            //   // index [x,y,z] as you wish by image[x+y*xS+z*xS*yS],
            //   // so x is the fastest axis and z is the slowest.
            //   for (int j = 0; j < size[1]; j++) {
            //     for (int i = 0; i < size[0]; i++) {
            //       float pixel = bufferPtr[i + j*size[1]];
            //       // Do something with pixel here
            //     }
            //   }
            // }

            // (2) Copy the buffer to a "safe" array (i.e. a fully typed array)
            // (note that this means memory is duplicated)
            float[] bufferAsArray = new float[len]; // Allocates new memory the size of input
            Marshal.Copy(buffer, bufferAsArray, 0, len);
            double total = 0.0;

            for (int j = 0; j < size[1]; j++)
            {
                for (int i = 0; i < size[0]; i++)
                {
                    float pixel = bufferAsArray[i + j * size[1]];
                    total += pixel;
                }
            }
            Console.WriteLine("Pixel value total: {0}", total);

            // Set buffer of new SimpleITK Image from managed array.
            // bufferAsArray could also have come from a bmp,png,etc...

            uint width  = input.GetWidth();
            uint height = input.GetHeight();

            SitkImage outImage       = new SitkImage(width, height, PixelId.sitkFloat32);
            IntPtr    outImageBuffer = outImage.GetBufferAsFloat();

            Marshal.Copy(bufferAsArray, 0, outImageBuffer, (int)(size[0] * size[1]));

            //
            // Write out the resulting file
            //
            outImage = SimpleITK.RescaleIntensity(outImage, 0, 255);
            outImage = SimpleITK.Cast(outImage, PixelId.sitkUInt8);

            SimpleITK.WriteImage(outImage, outputFilename);
        }