~ubuntu-branches/debian/sid/simpleitk/sid

« back to all changes in this revision

Viewing changes to Examples/FilterProgressReporting/FilterProgressReporting.cxx

  • Committer: Package Import Robot
  • Author(s): Ghislain Antony Vaillant
  • Date: 2017-11-02 08:49:18 UTC
  • Revision ID: package-import@ubuntu.com-20171102084918-7hs09ih668xq87ej
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=========================================================================
 
2
*
 
3
*  Copyright Insight Software Consortium
 
4
*
 
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
 
8
*
 
9
*         http://www.apache.org/licenses/LICENSE-2.0.txt
 
10
*
 
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.
 
16
*
 
17
*=========================================================================*/
 
18
 
 
19
// This one header will include all SimpleITK filters and external
 
20
// objects.
 
21
#include <SimpleITK.h>
 
22
#include <iostream>
 
23
#include <stdlib.h>
 
24
#include <iomanip>
 
25
 
 
26
// create convenient namespace alias
 
27
namespace sitk = itk::simple;
 
28
 
 
29
// Create A Command Callback to be registered with the ProcessObject
 
30
// on the ProgressEvent
 
31
//
 
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.
 
36
class ProgressUpdate
 
37
  : public sitk::Command
 
38
{
 
39
public:
 
40
  ProgressUpdate(const sitk::ProcessObject &po)
 
41
    : m_Process(po)
 
42
    {}
 
43
 
 
44
  virtual void Execute( )
 
45
    {
 
46
      // stash the stream state
 
47
      std::ios  state(NULL);
 
48
      state.copyfmt(std::cout);
 
49
      std::cout << std::fixed << std::setw( 3 ) << std::setprecision( 2 );
 
50
 
 
51
      // Print the Progress "Active Measurement"
 
52
      std::cout << m_Process.GetName()<< " Progress: " << m_Process.GetProgress() << std::endl;
 
53
 
 
54
      std::cout.copyfmt(state);
 
55
    }
 
56
private:
 
57
  const sitk::ProcessObject &m_Process;
 
58
};
 
59
 
 
60
 
 
61
int main ( int argc, char* argv[] ) {
 
62
 
 
63
  if ( argc < 4 ) {
 
64
    std::cerr << "Usage: " << argv[0] << " <input> <variance> <output>\n";
 
65
    return 1;
 
66
  }
 
67
 
 
68
  // Read the image file
 
69
  sitk::ImageFileReader reader;
 
70
  reader.SetFileName ( std::string ( argv[1] ) );
 
71
  sitk::Image image = reader.Execute();
 
72
 
 
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] ) );
 
77
 
 
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 );
 
82
 
 
83
  sitk::Image blurredImage = gaussian.Execute ( image );
 
84
 
 
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
 
87
  // pixels.
 
88
  sitk::CastImageFilter caster;
 
89
  caster.SetOutputPixelType( image.GetPixelID() );
 
90
  sitk::Image outputImage = caster.Execute( blurredImage );
 
91
 
 
92
  // write the image
 
93
  sitk::ImageFileWriter writer;
 
94
  writer.SetFileName ( std::string ( argv[3] ) );
 
95
  writer.Execute ( outputImage );
 
96
 
 
97
  return 0;
 
98
}