static void Main(string[] args)
        {
            if (args.Length < 8)
            {
                Console.WriteLine("Missing Parameters ");
                Console.WriteLine("Usage: " + System.AppDomain.CurrentDomain.FriendlyName +
                                  "inputImage initialModel outputImage cannyThreshold " +
                                  "cannyVariance advectionWeight initialModelIsovalue maximumIterations ");
                return;
            }

            string inputFilename        = args[0];
            string initialModelFilename = args[1];

            string outputFilename = args[2];

            double cannyThreshold      = double.Parse(args[3], CultureInfo.InvariantCulture);
            double cannyVariance       = double.Parse(args[4], CultureInfo.InvariantCulture);
            double advectionWeight     = double.Parse(args[5], CultureInfo.InvariantCulture);
            double intialModelIsovalue = double.Parse(args[6], CultureInfo.InvariantCulture);
            uint   maxIterations       = uint.Parse(args[7], CultureInfo.InvariantCulture);

            // Read input image

            SitkImage inputImage   = SimpleITK.ReadImage(inputFilename, PixelId.sitkFloat32);
            SitkImage initialModel = SimpleITK.ReadImage(initialModelFilename, PixelId.sitkFloat32);

            //  The input image will be processed with a few iterations of
            //  feature-preserving diffusion.  We create a filter and set the
            //  appropriate parameters.

            GradientAnisotropicDiffusionImageFilter diffusion = new GradientAnisotropicDiffusionImageFilter();

            diffusion.SetConductanceParameter(1.0);
            diffusion.SetTimeStep(0.125);
            diffusion.SetNumberOfIterations(5);
            SitkImage diffusedImage = diffusion.Execute(inputImage);

            // As with the other ITK level set segmentation filters, the terms of the
            // CannySegmentationLevelSetImageFilter level set equation can be
            // weighted by scalars.  For this application we will modify the relative
            // weight of the advection term.  The propagation and curvature term weights
            // are set to their defaults of 0 and 1, respectively.

            CannySegmentationLevelSetImageFilter cannySegmentation = new CannySegmentationLevelSetImageFilter();

            cannySegmentation.SetAdvectionScaling(advectionWeight);
            cannySegmentation.SetCurvatureScaling(1.0);
            cannySegmentation.SetPropagationScaling(0.0);

            //  The maximum number of iterations is specified from the command line.
            //  It may not be desirable in some applications to run the filter to
            //  convergence.  Only a few iterations may be required.

            cannySegmentation.SetMaximumRMSError(0.01);
            cannySegmentation.SetNumberOfIterations(maxIterations);

            //  There are two important parameters in the
            //  CannySegmentationLevelSetImageFilter to control the behavior of the
            //  Canny edge detection.  The variance parameter controls the
            //  amount of Gaussian smoothing on the input image.  The threshold
            //  parameter indicates the lowest allowed value in the output image.
            //  Thresholding is used to suppress Canny edges whose gradient magnitudes
            //  fall below a certain value.

            cannySegmentation.SetThreshold(cannyThreshold);
            cannySegmentation.SetVariance(cannyVariance);

            // Finally, it is very important to specify the isovalue of the surface in
            // the initial model input image. In a binary image, for example, the
            // isosurface is found midway between the foreground and background values.

            cannySegmentation.SetIsoSurfaceValue(intialModelIsovalue);

            SitkImage output = cannySegmentation.Execute(initialModel, diffusedImage);

            BinaryThresholdImageFilter thresholder = new BinaryThresholdImageFilter();

            thresholder.SetUpperThreshold(10.0);
            thresholder.SetLowerThreshold(0.0);

            thresholder.SetOutsideValue(0);
            thresholder.SetInsideValue(255);
            output = thresholder.Execute(output);


            output = SimpleITK.Cast(output, PixelIDValueEnum.sitkUInt8);

            SimpleITK.WriteImage(output, outputFilename);

            // Print out some useful information
            Console.WriteLine("");
            Console.WriteLine("Max. no. iterations: {0}", cannySegmentation.GetNumberOfIterations());
            Console.WriteLine("Max. RMS error: {0}", cannySegmentation.GetMaximumRMSError());
            Console.WriteLine("");
            Console.WriteLine("No. elpased iterations: {0}", cannySegmentation.GetElapsedIterations());
            Console.WriteLine("RMS change: {0}", cannySegmentation.GetRMSChange());
        }
Example #2
0
        static void Main(string[] args)
        {
            if (args.Length < 9)
            {
                Console.WriteLine("Missing Parameters ");
                Console.WriteLine("Usage: " + System.AppDomain.CurrentDomain.FriendlyName +
                                  " inputImage outputImage seedX seedY " +
                                  " Sigma SigmoidAlpha SigmoidBeta TimeThreshold");
                return;
            }

            string inputFilename  = args[0];
            string outputFilename = args[1];

            uint[] seedPosition = { Convert.ToUInt32(args[2]), Convert.ToUInt32(args[3]), 0 };

            double sigma         = double.Parse(args[4], CultureInfo.InvariantCulture);
            double alpha         = double.Parse(args[5], CultureInfo.InvariantCulture);;
            double beta          = double.Parse(args[6], CultureInfo.InvariantCulture);
            double timeThreshold = double.Parse(args[7], CultureInfo.InvariantCulture);
            double stoppingTime  = double.Parse(args[8], CultureInfo.InvariantCulture);

            // Read input image

            SitkImage inputImage = SimpleITK.ReadImage(inputFilename, PixelIDValueEnum.sitkFloat32);

            //  The input image will be processed with a few iterations of
            //  feature-preserving diffusion.  We create a filter and set the
            //  appropriate parameters.

            CurvatureAnisotropicDiffusionImageFilter smoothing = new CurvatureAnisotropicDiffusionImageFilter();

            smoothing.SetTimeStep(0.125);
            smoothing.SetNumberOfIterations(5);
            smoothing.SetConductanceParameter(9.0);
            SitkImage smoothingOutput = smoothing.Execute(inputImage);

            SitkImage gradientMagnitudeOutput = SimpleITK.GradientMagnitudeRecursiveGaussian(smoothingOutput, sigma);

            SitkImage sigmoidOutput = SimpleITK.Sigmoid(gradientMagnitudeOutput, alpha, beta, 1.0, 0.0);


            FastMarchingImageFilter fastMarching = new FastMarchingImageFilter();

            //VectorUIntList trialPoints; Add trialPoints into list if using multiple seeds. Here we only use one seedpoint

            VectorUInt32 trialPoint = new VectorUInt32(3);

            trialPoint.Add(seedPosition[0]);
            trialPoint.Add(seedPosition[1]);
            trialPoint.Add(seedPosition[2]);

            fastMarching.AddTrialPoint(trialPoint);

            //  Since the front representing the contour will propagate continuously
            //  over time, it is desirable to stop the process once a certain time has
            //  been reached. This allows us to save computation time under the
            //  assumption that the region of interest has already been computed. The
            //  value for stopping the process is defined with the method
            //  SetStoppingValue(). In principle, the stopping value should be a
            //  little bit higher than the threshold value.

            fastMarching.SetStoppingValue(stoppingTime);

            SitkImage fastmarchingOutput = fastMarching.Execute(sigmoidOutput);

            BinaryThresholdImageFilter thresholder = new BinaryThresholdImageFilter();

            thresholder.SetLowerThreshold(0.0);
            thresholder.SetUpperThreshold(timeThreshold);
            thresholder.SetOutsideValue(0);
            thresholder.SetInsideValue(255);
            SitkImage result = thresholder.Execute(fastmarchingOutput);

            SimpleITK.WriteImage(result, outputFilename);
        }
Example #3
0
        public itk.simple.Image SegmetacjaSledziony(itk.simple.Image image)
        {
            itk.simple.MorphologicalGradientImageFilter gradientFilter = new MorphologicalGradientImageFilter();
            gradientFilter.SetKernelType(KernelEnum.sitkCross);
            gradientFilter.SetKernelRadius(1);
            itk.simple.Image gradientImage = gradientFilter.Execute(image);
            
            itk.simple.SigmoidImageFilter sigmoidFilter = new SigmoidImageFilter();
            sigmoidFilter.SetOutputMinimum(0);
            sigmoidFilter.SetOutputMaximum(1);
            sigmoidFilter.SetAlpha(1.5);
            sigmoidFilter.SetBeta(100);
            itk.simple.Image sigmoidImage = sigmoidFilter.Execute(gradientImage);
          
            itk.simple.BinaryThresholdImageFilter threshFilter = new BinaryThresholdImageFilter();
            threshFilter.SetLowerThreshold(100);
            threshFilter.SetUpperThreshold(180);
            threshFilter.SetOutsideValue(0);
            threshFilter.SetInsideValue(1);
            itk.simple.Image threshImage = threshFilter.Execute(image);
            itk.simple.CastImageFilter castThresh = new CastImageFilter();
            castThresh.SetOutputPixelType(itk.simple.PixelIDValueEnum.sitkInt16);
            itk.simple.Image castThreshImage = castThresh.Execute(threshImage);

            itk.simple.SubtractImageFilter substractFilter = new SubtractImageFilter();
            itk.simple.Image substractImage = substractFilter.Execute(castThreshImage, sigmoidImage);

            itk.simple.ThresholdImageFilter threshFilter2 = new ThresholdImageFilter();
            threshFilter2.SetLower(0);
            threshFilter2.SetUpper(0);
            threshFilter2.SetOutsideValue(1);
            itk.simple.Image threshImage2 = threshFilter2.Execute(substractImage);

            itk.simple.BinaryErodeImageFilter erodeFilter = new BinaryErodeImageFilter();
            erodeFilter.SetForegroundValue(1);
            erodeFilter.SetBackgroundValue(0);
            erodeFilter.SetKernelType(itk.simple.KernelEnum.sitkCross);
            erodeFilter.SetKernelRadius(5);
            itk.simple.Image erodeImage = erodeFilter.Execute(threshImage2);

            itk.simple.ConnectedComponentImageFilter connFilter = new ConnectedComponentImageFilter();
            connFilter.SetFullyConnected(true);
            itk.simple.Image connImage = connFilter.Execute(erodeImage);

            itk.simple.RelabelComponentImageFilter relabelFilter = new RelabelComponentImageFilter();
            relabelFilter.SetMinimumObjectSize(5000);
            itk.simple.Image relabelImage = relabelFilter.Execute(connImage);

            itk.simple.BinaryThresholdImageFilter threshFilter3 = new BinaryThresholdImageFilter();
            threshFilter3.SetInsideValue(1);
            threshFilter3.SetOutsideValue(0);
            threshFilter3.SetLowerThreshold(1);
            threshFilter3.SetUpperThreshold(1);
            itk.simple.Image threshImage3 = threshFilter3.Execute(relabelImage);

            itk.simple.DilateObjectMorphologyImageFilter closeReconstructFilter = new DilateObjectMorphologyImageFilter();
            closeReconstructFilter.SetKernelType(itk.simple.KernelEnum.sitkCross);
            closeReconstructFilter.SetKernelRadius(5);
            itk.simple.Image closeReconstructImage = closeReconstructFilter.Execute(threshImage3);
            itk.simple.ImageFileWriter writer = new ImageFileWriter();
            writer.SetFileName("SegmentujSledzione.dcm");
            writer.Execute(closeReconstructImage);

            return closeReconstructImage;
        }
Example #4
0
        public itk.simple.Image SegmetacjaSledziony(itk.simple.Image image)
        {
            itk.simple.MorphologicalGradientImageFilter gradientFilter = new MorphologicalGradientImageFilter();
            gradientFilter.SetKernelType(KernelEnum.sitkCross);
            gradientFilter.SetKernelRadius(1);
            itk.simple.Image gradientImage = gradientFilter.Execute(image);

            itk.simple.SigmoidImageFilter sigmoidFilter = new SigmoidImageFilter();
            sigmoidFilter.SetOutputMinimum(0);
            sigmoidFilter.SetOutputMaximum(1);
            sigmoidFilter.SetAlpha(1.5);
            sigmoidFilter.SetBeta(100);
            itk.simple.Image sigmoidImage = sigmoidFilter.Execute(gradientImage);

            itk.simple.BinaryThresholdImageFilter threshFilter = new BinaryThresholdImageFilter();
            threshFilter.SetLowerThreshold(100);
            threshFilter.SetUpperThreshold(180);
            threshFilter.SetOutsideValue(0);
            threshFilter.SetInsideValue(1);
            itk.simple.Image           threshImage = threshFilter.Execute(image);
            itk.simple.CastImageFilter castThresh  = new CastImageFilter();
            castThresh.SetOutputPixelType(itk.simple.PixelIDValueEnum.sitkInt16);
            itk.simple.Image castThreshImage = castThresh.Execute(threshImage);

            itk.simple.SubtractImageFilter substractFilter = new SubtractImageFilter();
            itk.simple.Image substractImage = substractFilter.Execute(castThreshImage, sigmoidImage);

            itk.simple.ThresholdImageFilter threshFilter2 = new ThresholdImageFilter();
            threshFilter2.SetLower(0);
            threshFilter2.SetUpper(0);
            threshFilter2.SetOutsideValue(1);
            itk.simple.Image threshImage2 = threshFilter2.Execute(substractImage);

            itk.simple.BinaryErodeImageFilter erodeFilter = new BinaryErodeImageFilter();
            erodeFilter.SetForegroundValue(1);
            erodeFilter.SetBackgroundValue(0);
            erodeFilter.SetKernelType(itk.simple.KernelEnum.sitkCross);
            erodeFilter.SetKernelRadius(5);
            itk.simple.Image erodeImage = erodeFilter.Execute(threshImage2);

            itk.simple.ConnectedComponentImageFilter connFilter = new ConnectedComponentImageFilter();
            connFilter.SetFullyConnected(true);
            itk.simple.Image connImage = connFilter.Execute(erodeImage);

            itk.simple.RelabelComponentImageFilter relabelFilter = new RelabelComponentImageFilter();
            relabelFilter.SetMinimumObjectSize(5000);
            itk.simple.Image relabelImage = relabelFilter.Execute(connImage);

            itk.simple.BinaryThresholdImageFilter threshFilter3 = new BinaryThresholdImageFilter();
            threshFilter3.SetInsideValue(1);
            threshFilter3.SetOutsideValue(0);
            threshFilter3.SetLowerThreshold(1);
            threshFilter3.SetUpperThreshold(1);
            itk.simple.Image threshImage3 = threshFilter3.Execute(relabelImage);

            itk.simple.DilateObjectMorphologyImageFilter closeReconstructFilter = new DilateObjectMorphologyImageFilter();
            closeReconstructFilter.SetKernelType(itk.simple.KernelEnum.sitkCross);
            closeReconstructFilter.SetKernelRadius(5);
            itk.simple.Image           closeReconstructImage = closeReconstructFilter.Execute(threshImage3);
            itk.simple.ImageFileWriter writer = new ImageFileWriter();
            writer.SetFileName("SegmentujSledzione.dcm");
            writer.Execute(closeReconstructImage);

            return(closeReconstructImage);
        }