SimpleITK  
Python/BoarderSegmentation.py
1 #!/usr/bin/env python
2 # =========================================================================
3 #
4 # Copyright NumFOCUS
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0.txt
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # =========================================================================
19 
20 
21 import sys
22 import SimpleITK as sitk
23 import os
24 
25 # verify that we have the correct number of arguments
26 if len(sys.argv) != 5:
27  sys.stderr.write("Usage: prog inputFile outputFile replaceValue upperThreshold\n")
28  exit(1)
29 
30 # copy the arguments in to variables
31 inputFileName = sys.argv[1]
32 outputFileName = sys.argv[2]
33 replaceValue = int(sys.argv[3])
34 upperThreshold = float(sys.argv[4])
35 
36 # Read the file into an sitkImage
37 image = sitk.ReadImage(inputFileName)
38 
39 # Threshold the value [0,2), results in values inside the range 1, 0 otherwise
40 boundary = sitk.BinaryThreshold(image, 0, upperThreshold, 1, 0)
41 
42 boundary = sitk.BinaryMorphologicalClosing(boundary, [1] * image.GetDimension())
43 
44 # Remove any label pixel not connected to the boarder
45 boundary = sitk.BinaryGrindPeak(boundary)
46 
47 boundary = sitk.Cast(boundary, image.GetPixelID())
48 
49 # Multiply, the input image by not the boarder.
50 # This will multiply the image by 0 or 1, where 0 is the
51 # boarder. Making the board 0
52 image = image * ~boundary
53 
54 # add the replace value to the pixel on the board
55 image = image + (boundary * replaceValue)
56 
57 if "SITK_NOSHOW" not in os.environ:
58  sitk.Show(image, "Boarder Segmentation")
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::BinaryGrindPeak
Image BinaryGrindPeak(const Image &image1, bool fullyConnected=false, double foregroundValue=1.0, double backgroundValue=0)
Remove the objects not connected to the boundary of the image.
itk::simple::BinaryMorphologicalClosing
Image BinaryMorphologicalClosing(const Image &image1, std::vector< unsigned int > kernelRadius=std::vector< uint32_t >(3, 1), KernelEnum kernelType=itk::simple::sitkBall, double foregroundValue=1.0, bool safeBorder=true)
binary morphological closing of an image.
itk::simple::Cast
Image Cast(const Image &image, PixelIDValueEnum pixelID)
itk::simple::ReadImage
SITKIO_EXPORT Image ReadImage(const std::vector< PathType > &fileNames, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageSeriesReader class which is convenient for most image...