private sitk.VectorOfParameterMap InvertTransformParameters(string parameterFilename)
        {
            sitk.Image fixedImage           = ReadWriteUtils.ReadITKImageFromFile(registrationParameters.FixedImageFilename, sitk.PixelIDValueEnum.sitkFloat32);
            sitk.ElastixImageFilter elastix = null;
            try
            {
                // elastix manual 6.1.6: DisplacementMagnitudePenalty
                elastix = new sitk.ElastixImageFilter();
                elastix.SetInitialTransformParameterFileName(parameterFilename);
                elastix.SetParameterMap(sitk.SimpleITK.GetDefaultParameterMap("rigid"));
                elastix.SetFixedImage(fixedImage);
                elastix.SetMovingImage(fixedImage);
                elastix.SetParameter("HowToCombineTransforms", "Compose");
                elastix.SetParameter("Metric", "DisplacementMagnitudePenalty");
                elastix.SetParameter("NumberOfResolutions", "1");
                elastix.Execute();
                return(elastix.GetTransformParameterMap());

                /*sitk.TransformixImageFilter transformix = new sitk.TransformixImageFilter();
                 * transformix.SetTransformParameterMap(elastix.GetTransformParameterMap());
                 * transformix.SetTransformParameter("InitialTransformParametersFileName", "NoInitialTransform");
                 * transformix.Execute();*/
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(null);
            } finally
            {
                elastix.Dispose();
                fixedImage.Dispose();
            }
        }
        /// <summary>
        /// Write transformed image and deformation field to disk
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="transformparams"></param>
        private void WriteTransform(string filename, List <sitk.VectorOfParameterMap> transformparams)
        {
            string resultFilename = ReadWriteUtils.GetOutputDirectory(_parameters) + "\\" + Path.GetFileNameWithoutExtension(filename) + ".png";

            // add transform parameter map to registration parameters
            if (_parameters.TransformationParameterMap.ContainsKey(resultFilename))
            {
                _parameters.TransformationParameterMap.Remove(resultFilename);
            }
            _parameters.TransformationParameterMap.Add(resultFilename, transformparams);

            // read moving image from file
            sitk.Image movingImageToTransform = ReadWriteUtils.ReadITKImageFromFile(filename, sitk.PixelIDValueEnum.sitkVectorUInt8);
            // initialize transform instance
            TransformRGB trans = new TransformRGB(movingImageToTransform, transformparams, _parameters);

            if (_parameters.IsBinaryTransform)
            {
                // for binary reg set interpolation order to zero
                trans.SetInterpolationOrder(0);
            }

            trans.Execute();
            trans.WriteTransformedImage(resultFilename);

            // write deformation field
            sitk.Image deformationField         = trans.GetDeformationField();
            string     filenameDeformationField = ReadWriteUtils.GetOutputDirectory(_parameters, _parameters.Iteration) + "\\deformationField.mhd";

            ReadWriteUtils.WriteSitkImage(deformationField, filenameDeformationField);
            trans.Dispose();
            movingImageToTransform.Dispose();
        }
        /// <summary>
        /// Get the coefficient map of inner structures of tissue for the transform rigidity penalty term.
        /// </summary>
        /// <param name="filename">image filename</param>
        /// <returns>return coefficient map filename</returns>
        private string GetInnerStructureSegmentationsAsCoefficientMap(string filename)
        {
            InnerTissueSegmentation innerSegImage = GetInnerStructureSegmentation(filename);

            string filenameCoefficientMap = ReadWriteUtils.GetOutputDirectory(_parameters, _parameters.Iteration) + Constants.cCoefficientFilename;

            ReadWriteUtils.WriteUMatToFile(filenameCoefficientMap, innerSegImage.GetOutput().FirstOrDefault());
            innerSegImage.Dispose();

            // rescale image
            sitk.Image           img        = ReadWriteUtils.ReadITKImageFromFile(filenameCoefficientMap);
            sitk.CastImageFilter castFilter = new sitk.CastImageFilter();
            castFilter.SetOutputPixelType(sitk.PixelIDValueEnum.sitkFloat32);
            img = castFilter.Execute(img);
            sitk.RescaleIntensityImageFilter filter = new sitk.RescaleIntensityImageFilter();
            filter.SetOutputMinimum(0.0);
            filter.SetOutputMaximum(1.0);
            sitk.Image coefficientMap = filter.Execute(img);

            // save as mhd
            filenameCoefficientMap = ReadWriteUtils.GetOutputDirectory(_parameters, _parameters.Iteration) + Constants.cCoefficientFilename;
            ReadWriteUtils.WriteSitkImage(coefficientMap, filenameCoefficientMap);
            coefficientMap.Dispose();
            return(filenameCoefficientMap);
        }
        /// <summary>
        /// Create a checkerboard image of two corresponding images.
        /// </summary>
        /// <param name="fImg01">image 1</param>
        /// <param name="fImg02">image 2</param>
        /// <param name="size">grid size of the checker boar</param>
        /// <returns>checker board image</returns>
        public static sitk.Image GetCheckerBoardV2(string fImg01, string fImg02, uint size = 0)
        {
            sitk.Image img01 = ReadWriteUtils.ReadITKImageFromFile(fImg01);
            sitk.Image img02 = ReadWriteUtils.ReadITKImageFromFile(fImg02);

            uint width  = img01.GetWidth() > img02.GetWidth() ? img01.GetWidth() : img02.GetWidth();
            uint height = img01.GetHeight() > img02.GetHeight() ? img01.GetHeight() : img02.GetHeight();

            sitk.Image reference   = ImageUtils.ResizeImage(img01, width, height);
            sitk.Image transformed = ImageUtils.ResizeImage(img02, width, height);

            sitk.CheckerBoardImageFilter checkerBoard = new sitk.CheckerBoardImageFilter();
            uint lx = size;
            uint ly = width / (height / size);

            if (size != 0)
            {
                sitk.VectorUInt32 vec = new sitk.VectorUInt32();
                vec.Add(ly);
                vec.Add(lx);
                checkerBoard.SetCheckerPattern(vec);
            }
            sitk.Image result = checkerBoard.Execute(reference, transformed);
            img01.Dispose();
            img02.Dispose();
            reference.Dispose();
            transformed.Dispose();
            return(result);
        }
        public static sitk.Image GetCheckerBoard(sitk.Image img01, sitk.Image img02, uint size = 0)
        {
            uint width  = img01.GetWidth() > img02.GetWidth() ? img01.GetWidth() : img02.GetWidth();
            uint height = img01.GetHeight() > img02.GetHeight() ? img01.GetHeight() : img02.GetHeight();

            //Console.WriteLine(string.Format("width: Img01 [{0}] - Img02 [{1}]", img01.GetWidth(), img02.GetWidth()));
            //Console.WriteLine(string.Format("height: Img01 [{0}] - Img02 [{1}]", img01.GetHeight(), img02.GetHeight()));
            //Console.WriteLine(string.Format("pixel type: Img01 [{0}] - Img02 [{1}]", img01.GetPixelIDTypeAsString(), img02.GetPixelIDTypeAsString()));

            sitk.Image reference   = ImageUtils.ResizeImage(img01, width, height, sitk.PixelIDValueEnum.sitkFloat32);
            sitk.Image transformed = ImageUtils.ResizeImage(img02, width, height, sitk.PixelIDValueEnum.sitkFloat32);

            sitk.CheckerBoardImageFilter checkerBoard = new sitk.CheckerBoardImageFilter();

            if (size != 0)
            {
                sitk.VectorUInt32 vec = new sitk.VectorUInt32();
                vec.Add(size);
                vec.Add(size);
                checkerBoard.SetCheckerPattern(vec);
            }

            sitk.Image           temp            = checkerBoard.Execute(reference, transformed);
            sitk.CastImageFilter castImageFilter = new sitk.CastImageFilter();
            castImageFilter.SetOutputPixelType(sitk.PixelIDValueEnum.sitkUInt8);
            sitk.Image result = castImageFilter.Execute(temp);
            temp.Dispose();
            return(result);
        }
        /// <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();
            }
        }
 private static sitk.LabelOverlapMeasuresImageFilter GetOverlapImageFilter(string mask01iFn, string mask02iFn)
 {
     sitk.LabelOverlapMeasuresImageFilter overlapFilter;
     sitk.Image sImg01 = ReadWriteUtils.ReadITKImageFromFile(mask01iFn);
     sitk.Image sImg02 = ReadWriteUtils.ReadITKImageFromFile(mask02iFn);
     overlapFilter = VisualizationEvaluationUtils.GetOverlapImageFilter(sImg01, sImg02);
     sImg01.Dispose();
     sImg02.Dispose();
     return(overlapFilter);
 }
        /// <summary>
        /// Create a label map image for a given image.
        /// </summary>
        /// <param name="img">image</param>
        /// <returns>label map</returns>
        public static sitk.Image CreateLabelMapImage(sitk.Image img)
        {
            sitk.BinaryImageToLabelMapFilter binaryImageToLabel = new sitk.BinaryImageToLabelMapFilter();
            sitk.Image temp = binaryImageToLabel.Execute(img);

            sitk.LabelMapToLabelImageFilter labelMapToLabel = new sitk.LabelMapToLabelImageFilter();
            sitk.Image result = labelMapToLabel.Execute(temp);

            temp.Dispose();
            return(result);
        }
        /// <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);
        }
        /// <summary>
        /// Resize image by width and height and set output pixel type.
        /// </summary>
        /// <param name="img">input image</param>
        /// <param name="width">width</param>
        /// <param name="height">height</param>
        /// <param name="pixelType">output pixel type</param>
        /// <returns>resampled image</returns>
        public static sitk.Image ResizeImage(sitk.Image img, uint width, uint height, sitk.PixelIDValueEnum pixelType)
        {
            sitk.VectorUInt32 vec = new sitk.VectorUInt32();
            vec.Add(width);
            vec.Add(height);

            sitk.ResampleImageFilter resampleFilter = new sitk.ResampleImageFilter();
            resampleFilter.SetSize(vec);
            resampleFilter.SetOutputOrigin(img.GetOrigin());
            resampleFilter.SetOutputDirection(img.GetDirection());
            resampleFilter.SetOutputSpacing(img.GetSpacing());
            resampleFilter.SetOutputPixelType(pixelType);
            resampleFilter.SetDefaultPixelValue(255.0);
            sitk.Image resultImage = resampleFilter.Execute(img);
            img.Dispose();

            return(resultImage);
        }
 /// <summary>
 /// Dispose used instances.
 /// </summary>
 public void Dispose()
 {
     transformix.Dispose();
     movingImage.Dispose();
     transformedImage.Dispose();
 }