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
23import os
24import sys
25
26import SimpleITK as sitk
27
28if len(sys.argv) < 4:
29 print("Usage: " + sys.argv[0] + " <input> <variance> <output>")
30 sys.exit(1)
31
32
33
34class 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
52reader = sitk.ImageFileReader()
53reader.SetFileName(sys.argv[1])
54image = reader.Execute()
55
56pixelID = image.GetPixelID()
57
59gaussian.SetVariance(float(sys.argv[2]))
60
61
62gaussian.AddCommand(sitk.sitkStartEvent, lambda: print("StartEvent"))
63gaussian.AddCommand(sitk.sitkEndEvent, lambda: print("EndEvent"))
64
65
66cmd = MyCommand(gaussian)
67gaussian.AddCommand(sitk.sitkProgressEvent, cmd)
68
69image = gaussian.Execute(image)
70
71caster = sitk.CastImageFilter()
72caster.SetOutputPixelType(pixelID)
73image = caster.Execute(image)
74
75writer = sitk.ImageFileWriter()
76writer.SetFileName(sys.argv[3])
77writer.Execute(image)
78
79if "SITK_NOSHOW" not in os.environ:
80 sitk.Show(image, "Simple Gaussian")
A hybrid cast image filter to convert images to other types of images.
An implementation of the Command design pattern for callback.
Definition sitkCommand.h:45
Blurs an image by separable convolution with discrete gaussian kernels. This filter performs Gaussian...
Read an image file and return a SimpleITK Image.
Write out a SimpleITK image to the specified file location.
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())