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
#pragma warning ( disable : 4786 )
23
#include "SimpleITK.h"
27
#include "itkCurvatureFlowImageFilter.h"
29
// create convenient namespace alias
30
namespace sitk = itk::simple;
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
42
int main( int argc, char *argv[])
46
// Check command line parameters
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;
61
sitk::ImageFileReader reader;
62
reader.SetFileName( std::string( argv[1] ) );
63
sitk::Image image = reader.Execute();
69
sitk::ImageFileWriter writer;
70
writer.SetFileName( std::string( argv[2] ) );
73
// Blur using CurvatureFlowImageFilter
75
// Here we demonstrate the use of the ITK version of CurvatureFlowImageFilter
76
// instead of the SimpleITK version.
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.
83
const unsigned int Dimension = 2;
84
typedef float InternalPixelType;
85
typedef itk::Image< InternalPixelType, Dimension > InternalImageType;
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
91
if ( image.GetDimension() != Dimension )
93
std::cerr << "Input image is not a " << Dimension << " dimensional image as expected!" << std::endl;
98
// The read sitk::Image could be any pixel type. Cast the image, to
99
// float so we know what type we have.
101
sitk::CastImageFilter caster;
102
caster.SetOutputPixelType( sitk::sitkFloat32 );
103
image = caster.Execute( image );
106
// Extract the itk image from the SimpleITK image
108
InternalImageType::Pointer itkImage =
109
dynamic_cast <InternalImageType*>( image.GetITKBase() );
112
// Always check the results of dynamic_casts
114
if ( itkImage.IsNull() )
116
std::cerr << "Unexpected error converting SimpleITK image to ITK image!" << std::endl;
121
// Set up the blur filter and attach it to the pipeline.
123
typedef itk::CurvatureFlowImageFilter< InternalImageType, InternalImageType >
125
BlurFilterType::Pointer blurFilter = BlurFilterType::New();
126
blurFilter->SetInput( itkImage );
127
blurFilter->SetNumberOfIterations( 5 );
128
blurFilter->SetTimeStep( 0.125 );
133
// Execute the Blur pipeline by calling Update() on the blur filter.
135
blurFilter->Update();
139
// Return to the simpleITK setting by making a SimpleITK image using the
140
// output of the blur filter.
142
sitk::Image blurredImage = sitk::Image( blurFilter->GetOutput() );
146
// Now that we have finished the ITK section, we return to the SimpleITK API
151
// Set up ConnectedThresholdImageFilter for segmentation
153
sitk::ConnectedThresholdImageFilter segmentationFilter;
154
segmentationFilter.SetLower( atof( argv[3] ) );
155
segmentationFilter.SetUpper( atof( argv[4] ) );
156
segmentationFilter.SetReplaceValue( 255 );
158
for (int i = 5; i+1 < argc; i+=2)
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 )
167
std::cout << seed[j] << " ";
169
std::cout << std::endl;
172
sitk::Image outImage = segmentationFilter.Execute(blurredImage);
176
// Write out the resulting file
178
writer.Execute(outImage);