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 import os
21 import sys
22 
23 import SimpleITK as sitk
24 
25 if len(sys.argv) < 4:
26  print("Usage: " + sys.argv[0] + " <input> <variance> <output>")
27  sys.exit(1)
28 
29 
30 
31 class MyCommand(sitk.Command):
32  def __init__(self, po):
33  # required
34  super(MyCommand, self).__init__()
35  self.processObject = po
36 
37  def Execute(self):
38  print(
39  f"{self.processObject.GetName()}"
40  + f" Progress: {self.processObject.GetProgress():1.2f}"
41  )
42 
43 
44 
45 
46 reader = sitk.ImageFileReader()
47 reader.SetFileName(sys.argv[1])
48 image = reader.Execute()
49 
50 pixelID = image.GetPixelID()
51 
53 gaussian.SetVariance(float(sys.argv[2]))
54 
55 
56 gaussian.AddCommand(sitk.sitkStartEvent, lambda: print("StartEvent"))
57 gaussian.AddCommand(sitk.sitkEndEvent, lambda: print("EndEvent"))
58 
59 
60 cmd = MyCommand(gaussian)
61 gaussian.AddCommand(sitk.sitkProgressEvent, cmd)
62 
63 image = gaussian.Execute(image)
64 
65 caster = sitk.CastImageFilter()
66 caster.SetOutputPixelType(pixelID)
67 image = caster.Execute(image)
68 
69 writer = sitk.ImageFileWriter()
70 writer.SetFileName(sys.argv[3])
71 writer.Execute(image)
72 
73 if "SITK_NOSHOW" not in os.environ:
74  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:60
itk::simple::Command
An implementation of the Command design pattern for callback.
Definition: sitkCommand.h:43
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:48