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

« back to all changes in this revision

Viewing changes to Examples/ITKIntegration/ITKIntegration.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
#if defined(_MSC_VER)
 
19
#pragma warning ( disable : 4786 )
 
20
#endif
 
21
 
 
22
// SimpleITK includes
 
23
#include "SimpleITK.h"
 
24
 
 
25
// ITK includes
 
26
#include "itkImage.h"
 
27
#include "itkCurvatureFlowImageFilter.h"
 
28
 
 
29
// create convenient namespace alias
 
30
namespace sitk = itk::simple;
 
31
 
 
32
/**
 
33
 * This example shows how ITK and SimpleITK can be used together to work
 
34
 * on the same data. We use the same example application as the one presented
 
35
 * in the Segmentation/ConnectedThresholdImageFilter.cxx example, but we
 
36
 * replace the SimpleITK version of CurvatureFlowImageFilter with the
 
37
 * corresponding ITK version. While not terribly useful in this situation since
 
38
 * CurvatureFlowImageFilter is already available in SimpleITK this demonstrates
 
39
 * how ITK filters that have not been converted for SimpleITK can still be used
 
40
 * in a SimpleITK context
 
41
 */
 
42
int main( int argc, char *argv[])
 
43
{
 
44
 
 
45
  //
 
46
  // Check command line parameters
 
47
  //
 
48
  if( argc < 7 )
 
49
    {
 
50
    std::cerr << "Missing Parameters " << std::endl;
 
51
    std::cerr << "Usage: " << argv[0];
 
52
    std::cerr << " inputImage outputImage lowerThreshold upperThreshold "
 
53
      "seedX seedY [seed2X seed2Y ... ]" << std::endl;
 
54
    return 1;
 
55
    }
 
56
 
 
57
 
 
58
  //
 
59
  // Read the image
 
60
  //
 
61
  sitk::ImageFileReader reader;
 
62
  reader.SetFileName( std::string( argv[1] ) );
 
63
  sitk::Image image = reader.Execute();
 
64
 
 
65
 
 
66
  //
 
67
  // Set up writer
 
68
  //
 
69
  sitk::ImageFileWriter writer;
 
70
  writer.SetFileName( std::string( argv[2] ) );
 
71
 
 
72
  //////
 
73
  // Blur using CurvatureFlowImageFilter
 
74
  //
 
75
  // Here we demonstrate the use of the ITK version of CurvatureFlowImageFilter
 
76
  // instead of the SimpleITK version.
 
77
  //////
 
78
 
 
79
  //
 
80
  // First, define the typedefs that correspond to the types of the input
 
81
  // image. This requires foreknowlege of the data type of the input image.
 
82
  //
 
83
  const unsigned int                                 Dimension = 2;
 
84
  typedef float                                      InternalPixelType;
 
85
  typedef itk::Image< InternalPixelType, Dimension > InternalImageType;
 
86
 
 
87
  //
 
88
  // We must check the the image dimension and the pixel type of the
 
89
  // SimpleITK image match the ITK image we will cast to.s
 
90
  //
 
91
  if ( image.GetDimension() != Dimension )
 
92
    {
 
93
    std::cerr << "Input image is not a " << Dimension << " dimensional image as expected!" << std::endl;
 
94
    return 1;
 
95
    }
 
96
 
 
97
  //
 
98
  // The read sitk::Image could be any pixel type. Cast the image, to
 
99
  // float so we know what type we have.
 
100
  //
 
101
  sitk::CastImageFilter caster;
 
102
  caster.SetOutputPixelType( sitk::sitkFloat32 );
 
103
  image = caster.Execute( image );
 
104
 
 
105
  //
 
106
  // Extract the itk image from the SimpleITK image
 
107
  //
 
108
  InternalImageType::Pointer itkImage =
 
109
    dynamic_cast <InternalImageType*>( image.GetITKBase() );
 
110
 
 
111
  //
 
112
  // Always check the results of dynamic_casts
 
113
  //
 
114
  if ( itkImage.IsNull() )
 
115
    {
 
116
    std::cerr << "Unexpected error converting SimpleITK image to ITK image!" << std::endl;
 
117
    return 1;
 
118
    }
 
119
 
 
120
  //
 
121
  // Set up the blur filter and attach it to the pipeline.
 
122
  //
 
123
  typedef itk::CurvatureFlowImageFilter< InternalImageType, InternalImageType >
 
124
                                                     BlurFilterType;
 
125
  BlurFilterType::Pointer blurFilter = BlurFilterType::New();
 
126
  blurFilter->SetInput( itkImage );
 
127
  blurFilter->SetNumberOfIterations( 5 );
 
128
  blurFilter->SetTimeStep( 0.125 );
 
129
 
 
130
 
 
131
 
 
132
  //
 
133
  // Execute the  Blur pipeline by calling Update() on the blur filter.
 
134
  //
 
135
  blurFilter->Update();
 
136
 
 
137
 
 
138
  //
 
139
  // Return to the simpleITK setting by making a SimpleITK image using the
 
140
  // output of the blur filter.
 
141
  //
 
142
  sitk::Image blurredImage = sitk::Image( blurFilter->GetOutput() );
 
143
 
 
144
 
 
145
  //////
 
146
  // Now that we have finished the ITK section, we return to the SimpleITK API
 
147
  //////
 
148
 
 
149
 
 
150
  //
 
151
  // Set up ConnectedThresholdImageFilter for segmentation
 
152
  //
 
153
  sitk::ConnectedThresholdImageFilter segmentationFilter;
 
154
  segmentationFilter.SetLower( atof( argv[3] ) );
 
155
  segmentationFilter.SetUpper( atof( argv[4] ) );
 
156
  segmentationFilter.SetReplaceValue( 255 );
 
157
 
 
158
  for (int i = 5; i+1 < argc; i+=2)
 
159
    {
 
160
    std::vector<unsigned int> seed;
 
161
    seed.push_back(atoi(argv[i]));
 
162
    seed.push_back(atoi(argv[i+1]));
 
163
    segmentationFilter.AddSeed(seed);
 
164
    std::cout << "Adding a seed at ";
 
165
    for( unsigned int j = 0; j < seed.size(); ++i )
 
166
      {
 
167
      std::cout << seed[j] << " ";
 
168
      }
 
169
    std::cout << std::endl;
 
170
    }
 
171
 
 
172
  sitk::Image outImage = segmentationFilter.Execute(blurredImage);
 
173
 
 
174
 
 
175
  //
 
176
  // Write out the resulting file
 
177
  //
 
178
  writer.Execute(outImage);
 
179
 
 
180
  return 0;
 
181
}