Exemplo n.º 1
0
        private Image ApplyCastImageFilter(Image image)
        {
            Ensure.ArgumentNotNull(image, nameof(image));

            // TODO: find mapping between itk.simple.PixelIDValueEnum and System.Drawing.Imaging.PixelFormat
            // see https://github.com/SimpleITK/SimpleITK/issues/582

            // Execute cast filter
            CastImageFilter  filter    = new CastImageFilter();
            PixelIDValueEnum imageType = PixelIDValueEnum.swigToEnum(image.GetPixelIDValue());

            if (imageType == PixelIDValueEnum.sitkVectorUInt8 || imageType == PixelIDValueEnum.sitkUInt8)
            {
                return(image);
            }
            else
            {
                if (imageType.ToString().ToLowerInvariant().Contains("vector"))
                {
                    filter.SetOutputPixelType(PixelIDValueEnum.sitkVectorUInt8);
                }
                else
                {
                    filter.SetOutputPixelType(PixelIDValueEnum.sitkUInt8);
                }

                Image output = filter.Execute(image);
                filter.Dispose();
                image.Dispose();
                return(output);
            }
        }
Exemplo n.º 2
0
        private IntPtr GetBuffer(Image image)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            PixelIDValueEnum imageType = PixelIDValueEnum.swigToEnum(image.GetPixelIDValue());

            if (imageType.swigValue == PixelIDValueEnum.sitkUInt8.swigValue)
            {
                return(image.GetBufferAsUInt8());
            }
            else if (imageType.swigValue == PixelIDValueEnum.sitkUInt16.swigValue)
            {
                return(image.GetBufferAsUInt16());
            }
            else if (imageType.swigValue == PixelIDValueEnum.sitkUInt32.swigValue)
            {
                return(image.GetBufferAsUInt32());
            }
            else if (imageType.swigValue == PixelIDValueEnum.sitkUInt64.swigValue)
            {
                throw new NotSupportedException($"The image type {imageType} is not supported.");
            }
            else if (imageType.swigValue == PixelIDValueEnum.sitkInt8.swigValue)
            {
                return(image.GetBufferAsInt8());
            }
            else if (imageType.swigValue == PixelIDValueEnum.sitkInt16.swigValue)
            {
                return(image.GetBufferAsInt16());
            }
            else if (imageType.swigValue == PixelIDValueEnum.sitkInt32.swigValue)
            {
                return(image.GetBufferAsInt32());
            }
            else if (imageType.swigValue == PixelIDValueEnum.sitkInt64.swigValue)
            {
                throw new NotSupportedException($"The image type {imageType} is not supported.");
            }
            else if (imageType.swigValue == PixelIDValueEnum.sitkFloat32.swigValue ||
                     imageType.swigValue == PixelIDValueEnum.sitkComplexFloat32.swigValue)
            {
                return(image.GetBufferAsFloat());
            }
            else if (imageType.swigValue == PixelIDValueEnum.sitkFloat64.swigValue ||
                     imageType.swigValue == PixelIDValueEnum.sitkComplexFloat64.swigValue)
            {
                return(image.GetBufferAsDouble());
            }
            else
            {
                throw new NotSupportedException($"The image type {imageType} is not supported.");
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            try {
                // Create an image
                PixelIDValueEnum pixelType = PixelIDValueEnum.sitkUInt8;
                VectorUInt32     imageSize = new VectorUInt32(new uint[] { 128, 128 });
                Image            image     = new Image(imageSize, pixelType);

                // Create a face image
                VectorDouble faceSize   = new VectorDouble(new double[] { 64, 64 });
                VectorDouble faceCenter = new VectorDouble(new double[] { 64, 64 });
                Image        face       = SimpleITK.GaussianSource(pixelType, imageSize, faceSize, faceCenter);

                // Create eye images
                VectorDouble eyeSize    = new VectorDouble(new double[] { 5, 5 });
                VectorDouble eye1Center = new VectorDouble(new double[] { 48, 48 });
                VectorDouble eye2Center = new VectorDouble(new double[] { 80, 48 });
                Image        eye1       = SimpleITK.GaussianSource(pixelType, imageSize, eyeSize, eye1Center, 150);
                Image        eye2       = SimpleITK.GaussianSource(pixelType, imageSize, eyeSize, eye2Center, 150);

                // Apply the eyes to the face
                face = SimpleITK.Subtract(face, eye1);
                face = SimpleITK.Subtract(face, eye2);
                face = SimpleITK.BinaryThreshold(face, 200, 255, 255);


                // Create the mouth
                VectorDouble mouthRadii  = new VectorDouble(new double[] { 30, 20 });
                VectorDouble mouthCenter = new VectorDouble(new double[] { 64, 76 });
                Image        mouth       = SimpleITK.GaussianSource(pixelType, imageSize, mouthRadii, mouthCenter);
                mouth = SimpleITK.BinaryThreshold(mouth, 200, 255, 255);
                mouth = SimpleITK.Subtract(255, mouth);

                // Paste the mouth onto the face
                VectorUInt32 mouthSize = new VectorUInt32(new uint[] { 64, 18 });
                VectorInt32  mouthLoc  = new VectorInt32(new int[] { 32, 76 });
                face = SimpleITK.Paste(face, mouth, mouthSize, mouthLoc, mouthLoc);

                // Apply the face to the original image
                image = SimpleITK.Add(image, face);

                // Display the results
                if (Environment.GetEnvironmentVariable("SITK_NOSHOW") == null)
                {
                    SimpleITK.Show(image, "Hello World: CSharp", true);
                }
            } catch (Exception ex) {
                Console.WriteLine(ex);
            }
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the number of labels in the ITK image.
        /// </summary>
        /// <param name="image">The ITK image.</param>
        /// <returns>The number of labels in the ITK image.</returns>
        /// <exception cref="ArgumentNullException">image</exception>
        public ulong GetLabelCount(Image image)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            PixelIDValueEnum imageType  = PixelIDValueEnum.swigToEnum(image.GetPixelIDValue());
            Image            labelImage = new Image(image.GetWidth(), image.GetHeight(), image.GetDepth(), imageType);

            labelImage.SetOrigin(image.GetOrigin());
            labelImage.SetDirection(image.GetDirection());
            labelImage.SetSpacing(image.GetSpacing());

            LabelStatisticsImageFilter filter = new LabelStatisticsImageFilter();

            filter.Execute(labelImage, image);
            return(filter.GetNumberOfLabels());
        }
Exemplo n.º 6
0
        /// <inheritdoc/>
        public ulong GetLabelCount(Image image)
        {
            Ensure.ArgumentNotNull(image, nameof(image));

            ulong labelCount = 0;

            PixelIDValueEnum imageType = PixelIDValueEnum.swigToEnum(image.GetPixelIDValue());

            using (Image labelImage = new Image(image.GetWidth(), image.GetHeight(), image.GetDepth(), imageType))
            {
                labelImage.SetOrigin(image.GetOrigin());
                labelImage.SetDirection(image.GetDirection());
                labelImage.SetSpacing(image.GetSpacing());

                LabelStatisticsImageFilter filter = new LabelStatisticsImageFilter();
                filter.Execute(labelImage, image);
                labelCount = filter.GetNumberOfLabels();
                filter.Dispose();
            }

            return(labelCount);
        }