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 
20 import os
21 import sys
22 
23 import SimpleITK as sitk
24 
25 if len(sys.argv) < 8:
26  print(
27  "Usage:",
28  sys.argv[0],
29  "<inputImage> <outputImage> <sigma> <InitialDistance>",
30  "<PropagationScaling> <seedX> <seedY> <seedZ>",
31  )
32  sys.exit(1)
33 
34 inputImage = sys.argv[1]
35 outputImage = sys.argv[2]
36 sigma = float(sys.argv[3])
37 initialDistance = float(sys.argv[4])
38 propagationScaling = float(sys.argv[5])
39 seed = [float(sys.argv[6]), float(sys.argv[7])]
40 
41 if len(sys.argv) > 8:
42  seed.append(float(sys.argv[8]))
43 
44 reader = sitk.ImageFileReader()
45 reader.SetFileName(inputImage)
46 image = reader.Execute()
47 
49 gradientMagnitude.SetSigma(sigma)
50 
51 featureImage = sitk.BoundedReciprocal(gradientMagnitude.Execute(image))
52 
53 seedImage = sitk.Image(image.GetSize()[0], image.GetSize()[1], sitk.sitkUInt8)
54 seedImage.SetSpacing(image.GetSpacing())
55 seedImage.SetOrigin(image.GetOrigin())
56 seedImage.SetDirection(image.GetDirection())
57 seedImage[seedImage.TransformPhysicalPointToIndex(seed)] = 1
58 
60 distance.InsideIsPositiveOff()
61 distance.UseImageSpacingOn()
62 
63 initialImage = sitk.BinaryThreshold(distance.Execute(seedImage), -1000, 10)
64 initialImage = sitk.Cast(initialImage, featureImage.GetPixelID()) * -1 + 0.5
65 
66 geodesicActiveContour = sitk.GeodesicActiveContourLevelSetImageFilter()
67 geodesicActiveContour.SetPropagationScaling(propagationScaling)
68 geodesicActiveContour.SetCurvatureScaling(0.5)
69 geodesicActiveContour.SetAdvectionScaling(1.0)
70 geodesicActiveContour.SetMaximumRMSError(0.01)
71 geodesicActiveContour.SetNumberOfIterations(1000)
72 
73 levelset = geodesicActiveContour.Execute(initialImage, featureImage)
74 
75 print("RMS Change: ", geodesicActiveContour.GetRMSChange())
76 print("Elapsed Iterations: ", geodesicActiveContour.GetElapsedIterations())
77 
78 contour = sitk.BinaryContour(sitk.BinaryThreshold(levelset, -1000, 0))
79 
80 if "SITK_NOSHOW" not in os.environ:
81  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