/// <summary>
        /// Transform a pointset for given transform parameters.
        /// </summary>
        /// <param name="transformParameters">transform params</param>
        /// <param name="parameters">registration params</param>
        /// <returns>filename of transformed point set</returns>
        public static string TransfromPointSet(List <sitk.VectorOfParameterMap> transformParameters, RegistrationParameters parameters, string movingImageName = null)
        {
            sitk.TransformixImageFilter transformix = null;
            try
            {
                transformix = new sitk.TransformixImageFilter();
                transformix.SetTransformParameterMap(transformParameters.First());
                transformix.LogToConsoleOn();
                transformix.LogToFileOn();
                transformix.SetLogFileName("transformix.log");
                //transformix.SetLogToFile(true);

                if (transformParameters.Count > 1)
                {
                    for (int i = 1; i < transformParameters.Count; i++)
                    {
                        var vectorParameterMap = transformParameters[i];
                        foreach (var paramMap in vectorParameterMap.AsEnumerable())
                        {
                            transformix.AddTransformParameterMap(paramMap);
                        }
                    }
                }

                if (parameters.Type == RegistrationType.Rigid)
                {
                    transformix.SetFixedPointSetFileName(parameters.FixedImagePointSetFilename);
                }
                else
                {
                    transformix.SetFixedPointSetFileName(parameters.MovingImagePointSetFilename);
                }
                transformix.SetOutputDirectory(ReadWriteUtils.GetOutputDirectory(parameters));
                //transformix.SetTransformParameter(0, "UseBinaryFormatForTransformationParameters", "true" );
                var par = transformix.GetTransformParameter(0, "TransformParameters");

                if (movingImageName != null)
                {
                    sitk.Image movImg = ReadWriteUtils.ReadITKImageFromFile(movingImageName, sitk.PixelIDValueEnum.sitkFloat32);
                    transformix.SetMovingImage(movImg);
                }

                sitk.Image image  = transformix.Execute();
                string     output = ReadWriteUtils.GetOutputDirectory(parameters) + "\\outputpoints.txt";
                image.Dispose();
                return(output);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(null);
            } finally
            {
                transformix.Dispose();
            }
        }
示例#2
0
 /// <summary>
 /// Execute actual transformation.
 /// </summary>
 /// <param name="parameterMaps">parameter maps</param>
 /// <param name="channel">image channel as grayscale image</param>
 /// <returns></returns>
 private sitk.Image ExecuteTransform(sitk.VectorOfParameterMap parameterMaps, sitk.Image channel)
 {
     using (sitk.TransformixImageFilter transformix = new sitk.TransformixImageFilter())
     {
         transformix.SetTransformParameterMap(parameterMaps);
         transformix.SetMovingImage(channel);
         transformix.SetLogToConsole(true);
         transformix.SetOutputDirectory(ApplicationContext.OutputPath);
         return(transformix.Execute());
     }
 }
        /// <summary>
        /// Transform a pointset for given transform parameters.
        /// </summary>
        /// <param name="transformParameters"></param>
        /// <param name="pointSetFilename"></param>
        /// <param name="outputDir"></param>
        /// <param name="movingImageName"></param>
        public static void TransfromPointSet(List <sitk.VectorOfParameterMap> transformParameters, string pointSetFilename, string outputDir, string movingImageName = null)
        {
            sitk.TransformixImageFilter transformix = null;
            try
            {
                transformix = new sitk.TransformixImageFilter();
                transformix.SetTransformParameterMap(transformParameters.First());
                transformix.LogToConsoleOn();
                transformix.LogToFileOn();
                transformix.SetLogFileName("transformix.log");

                if (transformParameters.Count > 1)
                {
                    for (int i = 1; i < transformParameters.Count; i++)
                    {
                        var vectorParameterMap = transformParameters[i];
                        foreach (var paramMap in vectorParameterMap.AsEnumerable())
                        {
                            transformix.AddTransformParameterMap(paramMap);
                        }
                    }
                }

                transformix.SetFixedPointSetFileName(pointSetFilename);
                transformix.SetOutputDirectory(Path.GetDirectoryName(pointSetFilename));

                if (movingImageName != null)
                {
                    sitk.Image movImg = ReadWriteUtils.ReadITKImageFromFile(movingImageName, sitk.PixelIDValueEnum.sitkFloat32);
                    transformix.SetMovingImage(movImg);
                }

                sitk.Image image = transformix.Execute();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                transformix.Dispose();
            }
        }
        /// <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;
        }