SimpleITK  
HelloWorld/HelloWorld.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 import os
21 import sys
22 import SimpleITK as sitk
23 
24 
25 def main(args):
26  # Create an image
27  pixelType = sitk.sitkUInt8
28  imageSize = [128, 128]
29  image = sitk.Image(imageSize, pixelType)
30 
31  # Create a face image
32  faceSize = [64, 64]
33  faceCenter = [64, 64]
34  face = sitk.GaussianSource(pixelType, imageSize, faceSize, faceCenter)
35 
36  # Create eye images
37  eyeSize = [5, 5]
38  eye1Center = [48, 48]
39  eye2Center = [80, 48]
40  eye1 = sitk.GaussianSource(pixelType, imageSize, eyeSize, eye1Center, 150)
41  eye2 = sitk.GaussianSource(pixelType, imageSize, eyeSize, eye2Center, 150)
42 
43  # Apply the eyes to the face
44  face = face - eye1 - eye2
45  face = sitk.BinaryThreshold(face, 200, 255, 255)
46 
47  # Create the mouth
48  mouthRadii = [30, 20]
49  mouthCenter = [64, 76]
50  mouth = 255 - sitk.BinaryThreshold(
51  sitk.GaussianSource(pixelType, imageSize, mouthRadii, mouthCenter),
52  200,
53  255,
54  255,
55  )
56  # Paste the mouth into the face
57  mouthSize = [64, 18]
58  mouthLoc = [32, 76]
59  face = sitk.Paste(face, mouth, mouthSize, mouthLoc, mouthLoc)
60 
61  # Apply the face to the original image
62  image = image + face
63  return {"output_image":image}
64 
65 
66 # Display the results
67 if __name__ == "__main__":
68  img = main(sys.argv)
69  if "SITK_NOSHOW" not in os.environ:
70  sitk.Show(img, title="Hello World: Python", debugOn=True)
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::Paste
Image Paste(const Image &destinationImage, const Image &sourceImage, std::vector< unsigned int > sourceSize=std::vector< unsigned int >(SITK_MAX_DIMENSION, 1), std::vector< int > sourceIndex=std::vector< int >(SITK_MAX_DIMENSION, 0), std::vector< int > destinationIndex=std::vector< int >(SITK_MAX_DIMENSION, 0), std::vector< bool > DestinationSkipAxes=std::vector< bool >())
Paste an image into another image.
itk::simple::GaussianSource
Image GaussianSource(PixelIDValueEnum outputPixelType=itk::simple::sitkFloat32, std::vector< unsigned int > size=std::vector< unsigned int >(3, 64), std::vector< double > sigma=std::vector< double >(3, 16.0), std::vector< double > mean=std::vector< double >(3, 32.0), double scale=255, std::vector< double > origin=std::vector< double >(3, 0.0), std::vector< double > spacing=std::vector< double >(3, 1.0), std::vector< double > direction=std::vector< double >(), bool normalized=false)
Generate an n-dimensional image of a Gaussian.
itk::simple::Show
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())