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
21import os
22import sys
23
24import SimpleITK as sitk
25
26if 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
35inputImage = sys.argv[1]
36outputImage = sys.argv[2]
37sigma = float(sys.argv[3])
38initialDistance = float(sys.argv[4])
39propagationScaling = float(sys.argv[5])
40seed = [float(sys.argv[6]), float(sys.argv[7])]
41
42if len(sys.argv) > 8:
43 seed.append(float(sys.argv[8]))
44
45reader = sitk.ImageFileReader()
46reader.SetFileName(inputImage)
47image = reader.Execute()
48
50gradientMagnitude.SetSigma(sigma)
51
52featureImage = sitk.BoundedReciprocal(gradientMagnitude.Execute(image))
53
54seedImage = sitk.Image(image.GetSize()[0], image.GetSize()[1], sitk.sitkUInt8)
55seedImage.SetSpacing(image.GetSpacing())
56seedImage.SetOrigin(image.GetOrigin())
57seedImage.SetDirection(image.GetDirection())
58seedImage[seedImage.TransformPhysicalPointToIndex(seed)] = 1
59
61distance.InsideIsPositiveOff()
62distance.UseImageSpacingOn()
63
64initialImage = sitk.BinaryThreshold(distance.Execute(seedImage), -1000, 10)
65initialImage = sitk.Cast(initialImage, featureImage.GetPixelID()) * -1 + 0.5
66
68geodesicActiveContour.SetPropagationScaling(propagationScaling)
69geodesicActiveContour.SetCurvatureScaling(0.5)
70geodesicActiveContour.SetAdvectionScaling(1.0)
71geodesicActiveContour.SetMaximumRMSError(0.01)
72geodesicActiveContour.SetNumberOfIterations(1000)
73
74levelset = geodesicActiveContour.Execute(initialImage, featureImage)
75
76print("RMS Change: ", geodesicActiveContour.GetRMSChange())
77print("Elapsed Iterations: ", geodesicActiveContour.GetElapsedIterations())
78
79contour = sitk.BinaryContour(sitk.BinaryThreshold(levelset, -1000, 0))
80
81if "SITK_NOSHOW" not in os.environ:
82 sitk.Show(sitk.LabelOverlay(image, contour), "Levelset Countour")
Segments structures in images based on a user supplied edge potential map.
Computes the Magnitude of the Gradient of an image by convolution with the first derivative of a Gaus...
Read an image file and return a SimpleITK Image.
The Image class for SimpleITK.
Definition sitkImage.h:77
This filter calculates the Euclidean distance transform of a binary image in linear time for arbitrar...
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())
Image BoundedReciprocal(Image &&image1)
Computes 1/(1+x) for each pixel in the image.
Image BinaryContour(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.
Image LabelOverlay(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.
Image BinaryThreshold(Image &&image1, double lowerThreshold=0.0, double upperThreshold=255.0, uint8_t insideValue=1u, uint8_t outsideValue=0u)
Binarize an input image by thresholding.
Image Cast(const Image &image, PixelIDValueEnum pixelID)