SimpleITK  
Python/GeodesicActiveContourSegmentation.py
1 # =========================================================================
2 #
3 # Copyright NumFOCUS
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0.txt
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 # =========================================================================
18 
19 """ An example showing how to use SimpleITK's GeodesicActiveContourLevelSetImageFilter """
20 
21 import os
22 import sys
23 
24 import SimpleITK as sitk
25 
26 if len(sys.argv) < 8:
27  print(
28  "Usage:",
29  sys.argv[0],
30  "<inputImage> <outputImage> <sigma> <InitialDistance>",
31  "<PropagationScaling> <seedX> <seedY> <seedZ>",
32  )
33  sys.exit(1)
34 
35 inputImage = sys.argv[1]
36 outputImage = sys.argv[2]
37 sigma = float(sys.argv[3])
38 initialDistance = float(sys.argv[4])
39 propagationScaling = float(sys.argv[5])
40 seed = [float(sys.argv[6]), float(sys.argv[7])]
41 
42 if len(sys.argv) > 8:
43  seed.append(float(sys.argv[8]))
44 
45 reader = sitk.ImageFileReader()
46 reader.SetFileName(inputImage)
47 image = reader.Execute()
48 
50 gradientMagnitude.SetSigma(sigma)
51 
52 featureImage = sitk.BoundedReciprocal(gradientMagnitude.Execute(image))
53 
54 seedImage = sitk.Image(image.GetSize()[0], image.GetSize()[1], sitk.sitkUInt8)
55 seedImage.SetSpacing(image.GetSpacing())
56 seedImage.SetOrigin(image.GetOrigin())
57 seedImage.SetDirection(image.GetDirection())
58 seedImage[seedImage.TransformPhysicalPointToIndex(seed)] = 1
59 
61 distance.InsideIsPositiveOff()
62 distance.UseImageSpacingOn()
63 
64 initialImage = sitk.BinaryThreshold(distance.Execute(seedImage), -1000, 10)
65 initialImage = sitk.Cast(initialImage, featureImage.GetPixelID()) * -1 + 0.5
66 
67 geodesicActiveContour = sitk.GeodesicActiveContourLevelSetImageFilter()
68 geodesicActiveContour.SetPropagationScaling(propagationScaling)
69 geodesicActiveContour.SetCurvatureScaling(0.5)
70 geodesicActiveContour.SetAdvectionScaling(1.0)
71 geodesicActiveContour.SetMaximumRMSError(0.01)
72 geodesicActiveContour.SetNumberOfIterations(1000)
73 
74 levelset = geodesicActiveContour.Execute(initialImage, featureImage)
75 
76 print("RMS Change: ", geodesicActiveContour.GetRMSChange())
77 print("Elapsed Iterations: ", geodesicActiveContour.GetElapsedIterations())
78 
79 contour = sitk.BinaryContour(sitk.BinaryThreshold(levelset, -1000, 0))
80 
81 if "SITK_NOSHOW" not in os.environ:
82  sitk.Show(sitk.LabelOverlay(image, contour), "Levelset Countour")
itk::simple::Image
The Image class for SimpleITK.
Definition: sitkImage.h:76
itk::simple::BinaryThreshold
Image BinaryThreshold(const Image &image1, double lowerThreshold=0.0, double upperThreshold=255.0, uint8_t insideValue=1u, uint8_t outsideValue=0u)
Binarize an input image by thresholding.
itk::simple::Show
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())
itk::simple::ImageFileReader
Read an image file and return a SimpleITK Image.
Definition: sitkImageFileReader.h:74
itk::simple::Cast
Image Cast(const Image &image, PixelIDValueEnum pixelID)
itk::simple::BoundedReciprocal
Image BoundedReciprocal(const Image &image1)
Computes 1/(1+x) for each pixel in the image.
itk::simple::GeodesicActiveContourLevelSetImageFilter
Segments structures in images based on a user supplied edge potential map.
Definition: sitkGeodesicActiveContourLevelSetImageFilter.h:93
itk::simple::BinaryContour
Image BinaryContour(const Image &image1, bool fullyConnected=false, double backgroundValue=0.0, double foregroundValue=1.0)
Labels the pixels on the border of the objects in a binary image.
itk::simple::GradientMagnitudeRecursiveGaussianImageFilter
Computes the Magnitude of the Gradient of an image by convolution with the first derivative of a Gaus...
Definition: sitkGradientMagnitudeRecursiveGaussianImageFilter.h:41
itk::simple::LabelOverlay
Image LabelOverlay(const Image &image, const Image &labelImage, double opacity=0.5, double backgroundValue=0.0, std::vector< uint8_t > colormap=std::vector< uint8_t >())
Apply a colormap to a label image and put it on top of the input image.
itk::simple::SignedMaurerDistanceMapImageFilter
This filter calculates the Euclidean distance transform of a binary image in linear time for arbitrar...
Definition: sitkSignedMaurerDistanceMapImageFilter.h:53