1
/*=========================================================================
3
* Copyright Insight Software Consortium
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0.txt
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
*=========================================================================*/
19
// This one header will include all SimpleITK filters and external
21
#include <SimpleITK.h>
26
// create convenient namespace alias
27
namespace sitk = itk::simple;
29
// Create A Command Callback to be registered with the ProcessObject
30
// on the ProgressEvent
32
// Internally we maintain a reference to the ProcessObject passed
33
// during construction. Therefore, it would be an error to execute the
34
// Execute method after the ProcessObject is delete. But this class
35
// can be created on the stack, with out issue.
37
: public sitk::Command
40
ProgressUpdate(const sitk::ProcessObject &po)
44
virtual void Execute( )
46
// stash the stream state
48
state.copyfmt(std::cout);
49
std::cout << std::fixed << std::setw( 3 ) << std::setprecision( 2 );
51
// Print the Progress "Active Measurement"
52
std::cout << m_Process.GetName()<< " Progress: " << m_Process.GetProgress() << std::endl;
54
std::cout.copyfmt(state);
57
const sitk::ProcessObject &m_Process;
61
int main ( int argc, char* argv[] ) {
64
std::cerr << "Usage: " << argv[0] << " <input> <variance> <output>\n";
68
// Read the image file
69
sitk::ImageFileReader reader;
70
reader.SetFileName ( std::string ( argv[1] ) );
71
sitk::Image image = reader.Execute();
73
// This filters perform a gaussian bluring with sigma in physical
74
// space. The output image will be of real type.
75
sitk::DiscreteGaussianImageFilter gaussian;
76
gaussian.SetVariance ( atof ( argv[2] ) );
78
// Construct our custom command on the stack
79
ProgressUpdate cmd(gaussian);
80
// register it with the filter for the ProgressEvent
81
gaussian.AddCommand( sitk::sitkProgressEvent, cmd );
83
sitk::Image blurredImage = gaussian.Execute ( image );
85
// Covert the real output image back to the original pixel type, to
86
// make writing easier, as many file formats don't support real
88
sitk::CastImageFilter caster;
89
caster.SetOutputPixelType( image.GetPixelID() );
90
sitk::Image outputImage = caster.Execute( blurredImage );
93
sitk::ImageFileWriter writer;
94
writer.SetFileName ( std::string ( argv[3] ) );
95
writer.Execute ( outputImage );