Exemple #1
0
 /// <summary>
 /// Split color channels of a rgb image and return a list with channels as sitk.Image.
 /// list[0] = red channel
 /// list[1] = blue channel
 /// list[2] = green channel
 /// </summary>
 /// <param name="img">color image (rgb)</param>
 /// <returns>list with three color channels</returns>
 public static List <sitk.Image> SplitColorChannels(sitk.Image img)
 {
     if (img.GetNumberOfComponentsPerPixel() >= 3)
     {
         List <sitk.Image> result = new List <sitk.Image>();
         sitk.VectorIndexSelectionCastImageFilter rgbVector = new sitk.VectorIndexSelectionCastImageFilter();
         result.Add(rgbVector.Execute(img, 0, sitk.PixelIDValueEnum.sitkFloat32));
         result.Add(rgbVector.Execute(img, 1, sitk.PixelIDValueEnum.sitkFloat32));
         result.Add(rgbVector.Execute(img, 2, sitk.PixelIDValueEnum.sitkFloat32));
         return(result);
     }
     else
     {
         return(null);
     }
 }
Exemple #2
0
        /// <summary>
        /// Get given color channel as a grayscale image.
        /// </summary>
        /// <param name="img">image</param>
        /// <param name="channel">color channel (r, g, b)</param>
        /// <returns>grayscale image</returns>
        public static sitk.Image GetColorChannelAsImage(sitk.Image img, ColorChannel channel)
        {
            sitk.Image result = null;
            sitk.VectorIndexSelectionCastImageFilter rgbVector = new sitk.VectorIndexSelectionCastImageFilter();

            switch (channel)
            {
            case ColorChannel.R:
                result = rgbVector.Execute(img, 0, sitk.PixelIDValueEnum.sitkFloat32); break;

            case ColorChannel.G:
                result = rgbVector.Execute(img, 1, sitk.PixelIDValueEnum.sitkFloat32); break;

            case ColorChannel.B:
                result = rgbVector.Execute(img, 2, sitk.PixelIDValueEnum.sitkFloat32); break;
            }

            return(result == null? img : result);
        }
        /// <summary>
        /// Reads an ITK image as a grayscale image.
        /// </summary>
        /// <param name="file">filename</param>
        /// <param name="outputType">output datatype</param>
        /// <param name="channel">channel to extract</param>
        /// <returns>grayscale ITK image</returns>
        public static sitk.Image ReadITKImageAsGrayscaleFromFile(string file, sitk.PixelIDValueEnum outputType, ColorChannel channel)
        {
            sitk.ImageFileReader reader = new sitk.ImageFileReader();
            reader.SetFileName(file);
            reader.SetOutputPixelType(outputType);
            sitk.Image temp = reader.Execute();

            sitk.Image result = null;
            sitk.VectorIndexSelectionCastImageFilter rgbVector = new sitk.VectorIndexSelectionCastImageFilter();
            switch (channel)
            {
            case ColorChannel.R: result = rgbVector.Execute(temp, 0, sitk.PixelIDValueEnum.sitkFloat32); break;

            case ColorChannel.G: result = rgbVector.Execute(temp, 1, sitk.PixelIDValueEnum.sitkFloat32); break;

            case ColorChannel.B: result = rgbVector.Execute(temp, 2, sitk.PixelIDValueEnum.sitkFloat32); break;
            }

            return(result);
        }
 /// <summary>
 /// Create a difference image of two given images for a specific color channel.
 /// </summary>
 /// <param name="img01">image 1</param>
 /// <param name="img02">image 2</param>
 /// <param name="channel">color channel</param>
 /// <returns>difference image</returns>
 public static sitk.Image GetTotalDifferenceImage(sitk.Image img01, sitk.Image img02, uint channel = 0)
 {
     sitk.VectorIndexSelectionCastImageFilter channelFilter = new sitk.VectorIndexSelectionCastImageFilter();
     channelFilter.SetIndex(channel);
     sitk.Image ch01 = channelFilter.Execute(img01);
     sitk.VectorIndexSelectionCastImageFilter channelFilter2 = new sitk.VectorIndexSelectionCastImageFilter();
     channelFilter2.SetIndex(channel);
     sitk.Image ch02 = channelFilter2.Execute(img02);
     sitk.SubtractImageFilter subtractImageFilter = new sitk.SubtractImageFilter();
     sitk.Image imgResult = subtractImageFilter.Execute(ch01, ch02);
     return(imgResult);
 }
        /// <summary>
        /// Cast image to float 32.
        /// </summary>
        /// <param name="img">image</param>
        /// <returns>casted image</returns>
        public static sitk.Image CastImageToFloat32(sitk.Image img)
        {
            sitk.CastImageFilter castImageFilter = new sitk.CastImageFilter();
            castImageFilter.SetOutputPixelType(sitk.PixelIDValueEnum.sitkVectorFloat32);
            sitk.Image vector = castImageFilter.Execute(img);

            sitk.VectorIndexSelectionCastImageFilter vectorFilter = new sitk.VectorIndexSelectionCastImageFilter();
            sitk.Image tempImage = vectorFilter.Execute(vector, 0, sitk.PixelIDValueEnum.sitkFloat32);

            castImageFilter.Dispose();
            vector.Dispose();

            return(tempImage);
        }
Exemple #6
0
        public static sitk.Image BinaryThinning(sitk.Image inputImage)
        {
            sitk.VectorIndexSelectionCastImageFilter vectorIndexSelectionCastImageFilter = new sitk.VectorIndexSelectionCastImageFilter();
            sitk.CastImageFilter castImageFilter = new sitk.CastImageFilter();
            sitk.Image           image           = vectorIndexSelectionCastImageFilter.Execute(inputImage, 0, castImageFilter.GetOutputPixelType());

            sitk.BinaryThresholdImageFilter binaryThresholdImageFilter = new sitk.BinaryThresholdImageFilter();
            binaryThresholdImageFilter.SetInsideValue(0);
            binaryThresholdImageFilter.SetOutsideValue(255);
            binaryThresholdImageFilter.SetLowerThreshold(0);
            binaryThresholdImageFilter.SetUpperThreshold(100);
            sitk.Image binaryThresholdImage = binaryThresholdImageFilter.Execute(image);

            sitk.BinaryThinningImageFilter binaryThinningImageFilter = new sitk.BinaryThinningImageFilter();
            sitk.Image binaryThinningImage = binaryThinningImageFilter.Execute(binaryThresholdImage);

            sitk.RescaleIntensityImageFilter rescaleIntensityImageFilter = new sitk.RescaleIntensityImageFilter();
            rescaleIntensityImageFilter.SetOutputMinimum(0);
            rescaleIntensityImageFilter.SetOutputMaximum(255);
            sitk.Image rescaleIntensityImage = rescaleIntensityImageFilter.Execute(binaryThinningImage);

            return(rescaleIntensityImage);
        }
        /// <summary>
        /// Execute transformation of rgb image.
        /// </summary>
        public void Execute()
        {
            string outputDir = ReadWriteUtils.GetOutputDirectory(registrationParameters, registrationParameters.Iteration);

            if (Directory.Exists(outputDir))
            {
                Directory.CreateDirectory(outputDir);
            }

            // split rgb channels
            sitk.VectorIndexSelectionCastImageFilter rgbVector = new sitk.VectorIndexSelectionCastImageFilter();
            sitk.Image redChannel   = rgbVector.Execute(movingImage, 0, sitk.PixelIDValueEnum.sitkFloat32);
            sitk.Image greenChannel = rgbVector.Execute(movingImage, 1, sitk.PixelIDValueEnum.sitkFloat32);
            sitk.Image blueChannel  = rgbVector.Execute(movingImage, 2, sitk.PixelIDValueEnum.sitkFloat32);

            foreach (var parameter in parameterMaps[0])
            {
                parameter["DefaultPixelValue"][0]    = "255.0";
                parameter["ResultImagePixelType"][0] = "short";

                if (parameter.ContainsKey("UseBinaryFormatForTransformationParameters"))
                {
                    parameter.Remove("UseBinaryFormatForTransformationParameters");
                }

                if (interpolationOrder != -1)
                {
                    parameter["FinalBSplineInterpolationOrder"][0] = interpolationOrder.ToString();
                }
                else
                {
                    parameter["FinalBSplineInterpolationOrder"][0] = "3";
                }

                if (parameter["Transform"][0] == "SimilarityTransform" ||
                    registrationParameters.RegistrationDefaultParams == RegistrationDefaultParameters.similarity)
                {
                    sitk.VectorString vec = new sitk.VectorString();
                    vec.Add((registrationParameters.LargestImageWidth / 2).ToString());  // fixed image width / 2
                    vec.Add((registrationParameters.LargestImageHeight / 2).ToString()); // fixed image height / 2
                    parameter.Add("CenterOfRotationPoint", vec);
                }
            }

            // initialize transformix
            transformix.SetOutputDirectory(outputDir);
            transformix.SetTransformParameterMap(parameterMaps.First()[0]);
            // add further transform parameters
            if (parameterMaps[0].Count > 1)
            {
                for (int i = 1; i < parameterMaps[0].Count; i++)
                {
                    var vectorParameterMap = parameterMaps[0][i];
                    transformix.AddTransformParameterMap(vectorParameterMap);
                }
            }

            transformix.ComputeDeformationFieldOn();
            transformix.LogToFileOn();

            if (registrationParameters.ComputeJaccobian)
            {
                transformix.ComputeSpatialJacobianOn();
                transformix.ComputeDeterminantOfSpatialJacobianOn();
            }

            transformix.PrintParameterMap();

            // red
            transformix.SetMovingImage(redChannel);
            sitk.Image resultRedChannel = transformix.Execute();
            resultRedChannel = TransformationUtils.InterpolateImage(resultRedChannel, sitk.InterpolatorEnum.sitkBSplineResamplerOrder3, sitk.PixelIDValueEnum.sitkUInt8);
            //ReadWriteUtils.WriteSitkImageWithPreCast(resultRedChannel, registrationParameters.OutputDirectory + "\\red_channel.png");

            // green
            transformix.SetMovingImage(greenChannel);
            sitk.Image resultGreenChannel = transformix.Execute();
            resultGreenChannel = TransformationUtils.InterpolateImage(resultGreenChannel, sitk.InterpolatorEnum.sitkBSplineResamplerOrder3, sitk.PixelIDValueEnum.sitkUInt8);
            //ReadWriteUtils.WriteSitkImageWithPreCast(resultGreenChannel, registrationParameters.OutputDirectory + "\\green_channel.png");

            // blue
            transformix.SetMovingImage(blueChannel);
            sitk.Image resultBlueChannel = transformix.Execute();
            resultBlueChannel = TransformationUtils.InterpolateImage(resultBlueChannel, sitk.InterpolatorEnum.sitkBSplineResamplerOrder3, sitk.PixelIDValueEnum.sitkUInt8);
            //ReadWriteUtils.WriteSitkImageWithPreCast(resultBlueChannel, registrationParameters.OutputDirectory + "\\blue_channel.png");

            // compose image channels
            sitk.VectorOfImage vectorImages = new sitk.VectorOfImage();
            vectorImages.Add(resultRedChannel);
            vectorImages.Add(resultGreenChannel);
            vectorImages.Add(resultBlueChannel);
            sitk.ComposeImageFilter composeImageFilter = new sitk.ComposeImageFilter();
            sitk.Image composedImage = composeImageFilter.Execute(vectorImages);

            if (registrationParameters.Type == RegistrationType.NonRigid)
            {
                transformedImage = TransformationUtils.InterpolateImage(composedImage, sitk.InterpolatorEnum.sitkBSplineResamplerOrder5, composedImage.GetPixelID());
            }
            else
            {
                transformedImage = composedImage;
            }

            transformedImage = composedImage;

            // interpolation of output image (needs to be improved -> currently theres a little loss in quality)
            // possible solution: interpolate grayscale images and compose afterwards

            /*sitk.ExpandImageFilter expandImageFilter = new sitk.ExpandImageFilter();
             * expandImageFilter.SetInterpolator(sitk.InterpolatorEnum.sitkLinear);
             * transformedImage = expandImageFilter.Execute(composedImage);*/

            //transformedImage = composedImage;
        }