Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            try {
                if (args.Length < 1)
                {
                    Console.WriteLine("Usage: ImageIOSelection image_input_file");
                    return;
                }

                // Find out which image IOs are supported
                ImageFileReader         reader    = new ImageFileReader();
                itk.simple.VectorString image_ios = reader.GetRegisteredImageIOs();
                Console.Write("The supported image IOs are: ");
                for (int i = 0; i < image_ios.Count; i++)
                {
                    Console.Write(image_ios[i] + " ");
                }
                Console.WriteLine("\n--------------------");

                // Another option is to just print the reader and see which
                // IOs are supported
                Console.WriteLine(reader.ToString());
                Console.WriteLine("--------------------");

                // Force the use of a specific IO. If the IO doesn't support
                // reading the image type it will throw an exception.
                reader.SetImageIO("PNGImageIO");
                reader.SetFileName(args[0]);
                Image image = reader.Execute();
                Console.WriteLine("Read image: " + args[0]);
            } catch (Exception ex) {
                Console.WriteLine("Read failed: " + ex);
            }
        }
Ejemplo n.º 2
0
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (filename == null)
            {
                string[] parameters = richTextBox1.Text.Split('\n');

                foreach (var param in parameters)
                {
                    string[]          splitted = param.Split(' ');
                    sitk.VectorString vec      = GetVectorFromString(param);
                    if (vec != null)
                    {
                        if (Parametermap.ContainsKey(splitted[0]))
                        {
                            Parametermap[splitted[0]] = vec;
                        }
                        else
                        {
                            Parametermap.Add(splitted[0], vec);
                        }
                    }
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                sitk.ElastixImageFilter elx = new sitk.ElastixImageFilter();
                elx.WriteParameterFile(Parametermap, filename);
                elx.Dispose();
            }
        }
 /// <summary>
 /// Convert an arbitrary number of string values to a sitk.VectorString.
 /// </summary>
 /// <param name="values">string values</param>
 /// <returns>simple itk vector of strings</returns>
 public static sitk.VectorString GetVectorString(params string[] values)
 {
     sitk.VectorString vec = new sitk.VectorString();
     foreach (string value in values)
     {
         vec.Add(value);
     }
     return(vec);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Add a parameter to the parameter map.
        /// </summary>
        /// <param name="parameterName">parameter key</param>
        /// <param name="value">parameter value</param>
        protected void AddParameter(string parameterName, sitk.VectorString value)
        {
            if (parameterMap.ContainsKey(parameterName))
            {
                parameterMap.Remove(parameterName);
            }

            parameterMap.Add(parameterName, value);
        }
 /// <summary>
 /// Change or add a parameter (if param does not exist) to parameter map.
 /// </summary>
 /// <param name="paramMap">reference to parameter map</param>
 /// <param name="key">parameter key</param>
 /// <param name="value">values as a vector of strings</param>
 public static void ChangeOrAddParamIfNotExist(ref sitk.ParameterMap paramMap, string key, sitk.VectorString value)
 {
     if (paramMap.ContainsKey(key))
     {
         paramMap[key] = value;
     }
     else
     {
         paramMap.Add(key, value);
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Add generic parameter value to map.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameterName">parameter key</param>
        /// <param name="values">parameter value</param>
        protected void AddParameter <T>(string parameterName, params T[] values)
        {
            if (parameterMap.ContainsKey(parameterName))
            {
                parameterMap.Remove(parameterName);
            }

            sitk.VectorString vec = new sitk.VectorString();
            foreach (T value in values)
            {
                vec.Add(value.ToString());
            }
            parameterMap.Add(parameterName, vec);
        }
        /// <summary>
        /// Merge transform parameters
        /// </summary>
        /// <param name="parammap"></param>
        /// <returns></returns>
        private List <sitk.VectorOfParameterMap> MergeTransformParameters(List <sitk.VectorOfParameterMap> parammap)
        {
            List <sitk.VectorOfParameterMap> resultMap = new List <sitk.VectorOfParameterMap>();

            resultMap.Add(parammap.First());

            List <sitk.VectorString> tParams = new List <sitk.VectorString>();

            foreach (sitk.VectorOfParameterMap vec in parammap)
            {
                if (vec[0].ContainsKey("TransformParameters"))
                {
                    tParams.Add(vec[0]["TransformParameters"]);
                }
            }

            double[] avgParams = new double[tParams.First().Count];
            for (int i = 0; i < tParams.First().Count; i++)
            {
                double sum = 0;
                for (int j = 0; j < tParams.Count; j++)
                {
                    string sParam = tParams[j][i];
                    double parsed = Double.Parse(sParam.Replace(',', '.'), CultureInfo.InvariantCulture);
                    sum += parsed;
                }
                avgParams[i] = sum / tParams.Count;
            }

            sitk.VectorString resultVec = new sitk.VectorString();
            foreach (double avgParam in avgParams)
            {
                resultVec.Add(avgParam.ToString());
            }

            if (resultMap.First()[0].ContainsKey("TransformParameters"))
            {
                resultMap.First()[0]["TransformParameters"] = resultVec;
            }
            else
            {
                resultMap.First()[0].Add(new KeyValuePair <string, sitk.VectorString>("TransformParameters", resultVec));
            }

            return(resultMap);
        }
Ejemplo n.º 8
0
        private sitk.VectorString GetVectorFromString(string parameter)
        {
            try
            {
                string[] values = parameter.Split('(')[1].Split(')')[0].Split(';');

                sitk.VectorString vec = new sitk.VectorString();
                foreach (string value in values)
                {
                    string valueC = value.Replace(" ", "");
                    vec.Add(valueC);
                }
                return(vec);
            } catch (Exception)
            {
                return(null);
            }
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            string catalog = @"C:\Users\vroob\Downloads\projektKWDM\serie";

            string[] pathToSeries = Directory.GetFiles(catalog);

            sitk.VectorString fileNames = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(catalog);

            sitk.ImageSeriesReader imageSeriesReader = new sitk.ImageSeriesReader();
            imageSeriesReader.SetFileNames(fileNames);
            imageSeriesReader.SetOutputPixelType(sitk.PixelIDValueEnum.sitkInt16);
            sitk.Image imageDicomOrg = imageSeriesReader.Execute();

            // binaryzacja
            sitk.BinaryThresholdImageFilter binthr = new sitk.BinaryThresholdImageFilter();
            binthr.SetLowerThreshold(-950);
            binthr.SetUpperThreshold(-720);
            binthr.SetOutsideValue(0);
            binthr.SetInsideValue(1);
            sitk.Image imageDicom = binthr.Execute(imageDicomOrg);

            sitk.VotingBinaryIterativeHoleFillingImageFilter holeFiller = new sitk.VotingBinaryIterativeHoleFillingImageFilter();
            holeFiller.SetRadius(2);
            holeFiller.SetForegroundValue(1);
            holeFiller.SetBackgroundValue(0);
            imageDicom = holeFiller.Execute(imageDicom);

            sitk.ConnectedComponentImageFilter labeler = new sitk.ConnectedComponentImageFilter();
            labeler.SetFullyConnected(true);
            sitk.Image labelImage = labeler.Execute(imageDicom);

            sitk.RelabelComponentImageFilter relabeler = new sitk.RelabelComponentImageFilter();
            relabeler.SetMinimumObjectSize(700);
            labelImage = relabeler.Execute(labelImage);

            sitk.ThresholdImageFilter thresholder = new sitk.ThresholdImageFilter();
            thresholder.SetLower(2);
            thresholder.SetUpper(2);
            thresholder.SetOutsideValue(0);
            labelImage = thresholder.Execute(labelImage);

            SaveImage(imageDicom, "labelImage.vtk");
        }
        /// <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;
        }