SimpleITK  
FilterProgressReporting/FilterProgressReporting.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 """ A SimpleITK example demonstrating filter progress reporting. """
22 
23 import os
24 import sys
25 
26 import SimpleITK as sitk
27 
28 if len(sys.argv) < 4:
29  print("Usage: " + sys.argv[0] + " <input> <variance> <output>")
30  sys.exit(1)
31 
32 
33 
34 class MyCommand(sitk.Command):
35  """ Sample command class that prints the progress of the process object. """
36  def __init__(self, po):
37  """ Constructor, note that the base class constructor is called here. """
38  # required
39  super().__init__()
40  self.processObject = po
41 
42  def Execute(self):
43  """ Method that is called by the process object. """
44  print(
45  f"{self.processObject.GetName()}"
46  + f" Progress: {self.processObject.GetProgress():1.2f}"
47  )
48 
49 
50 
51 
52 reader = sitk.ImageFileReader()
53 reader.SetFileName(sys.argv[1])
54 image = reader.Execute()
55 
56 pixelID = image.GetPixelID()
57 
59 gaussian.SetVariance(float(sys.argv[2]))
60 
61 
62 gaussian.AddCommand(sitk.sitkStartEvent, lambda: print("StartEvent"))
63 gaussian.AddCommand(sitk.sitkEndEvent, lambda: print("EndEvent"))
64 
65 
66 cmd = MyCommand(gaussian)
67 gaussian.AddCommand(sitk.sitkProgressEvent, cmd)
68 
69 image = gaussian.Execute(image)
70 
71 caster = sitk.CastImageFilter()
72 caster.SetOutputPixelType(pixelID)
73 image = caster.Execute(image)
74 
75 writer = sitk.ImageFileWriter()
76 writer.SetFileName(sys.argv[3])
77 writer.Execute(image)
78 
79 if "SITK_NOSHOW" not in os.environ:
80  sitk.Show(image, "Simple Gaussian")
itk::simple::DiscreteGaussianImageFilter
Blurs an image by separable convolution with discrete gaussian kernels. This filter performs Gaussian...
Definition: sitkDiscreteGaussianImageFilter.h:59
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::Command
An implementation of the Command design pattern for callback.
Definition: sitkCommand.h:44
itk::simple::CastImageFilter
A hybrid cast image filter to convert images to other types of images.
Definition: sitkCastImageFilter.h:39
itk::simple::ImageFileWriter
Write out a SimpleITK image to the specified file location.
Definition: sitkImageFileWriter.h:51