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""" A SimpleITK 'Hello World' example. """
21
22import os
23import sys
24import SimpleITK as sitk
25
26
27def main(_):
28 """ Generate a simple image and display it using the SimpleITK Show function. """
29 # Create an image
30 pixelType = sitk.sitkUInt8
31 imageSize = [128, 128]
32 image = sitk.Image(imageSize, pixelType)
33
34 # Create a face image
35 faceSize = [64, 64]
36 faceCenter = [64, 64]
37 face = sitk.GaussianSource(pixelType, imageSize, faceSize, faceCenter)
38
39 # Create eye images
40 eyeSize = [5, 5]
41 eye1Center = [48, 48]
42 eye2Center = [80, 48]
43 eye1 = sitk.GaussianSource(pixelType, imageSize, eyeSize, eye1Center, 150)
44 eye2 = sitk.GaussianSource(pixelType, imageSize, eyeSize, eye2Center, 150)
45
46 # Apply the eyes to the face
47 face = face - eye1 - eye2
48 face = sitk.BinaryThreshold(face, 200, 255, 255)
49
50 # Create the mouth
51 mouthRadii = [30, 20]
52 mouthCenter = [64, 76]
53 mouth = 255 - sitk.BinaryThreshold(
54 sitk.GaussianSource(pixelType, imageSize, mouthRadii, mouthCenter),
55 200,
56 255,
57 255,
58 )
59 # Paste the mouth into the face
60 mouthSize = [64, 18]
61 mouthLoc = [32, 76]
62 face = sitk.Paste(face, mouth, mouthSize, mouthLoc, mouthLoc)
63
64 # Apply the face to the original image
65 image = image + face
66 return {"output_image": image}
67
68
69# Display the results
70if __name__ == "__main__":
71 img = main(sys.argv)
72 if "SITK_NOSHOW" not in os.environ:
73 sitk.Show(img, title="Hello World: Python", debugOn=True)
The Image class for SimpleITK.
Definition sitkImage.h:77
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.
Image Paste(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.
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())
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.