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 #include "itkTobogganImageFilter.h"
20 #include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"
21 #include "itkCastImageFilter.h"
22 #include "itkImageFileReader.h"
23 #include "itkImageFileWriter.h"
24 #include "itkTextOutput.h"
25 #include "itkTestingMacros.h"
26
27
itkTobogganImageFilterTest(int argc,char * argv[])28 int itkTobogganImageFilterTest( int argc, char* argv[] )
29 {
30 if( argc < 3 )
31 {
32 std::cerr << "Missing parameters" << std::endl;
33 std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv) << " InputImage OutputImage" << std::endl;
34 return EXIT_FAILURE;
35 }
36
37 constexpr unsigned int Dimension = 2;
38
39 using PixelType = unsigned char;
40 using FloatPixelType = float;
41 using InputImageType = itk::Image< PixelType, Dimension >;
42 using FloatImageType = itk::Image< FloatPixelType, Dimension >;
43 using OutputImageType = itk::Image< PixelType, Dimension >;
44 using LongImageType = itk::Image< itk::IdentifierType, Dimension >;
45
46
47 // Create a pipeline
48 using InputCastFilterType =
49 itk::CastImageFilter< InputImageType, FloatImageType >;
50 using FilterType = itk::TobogganImageFilter<FloatImageType>;
51 using OutputCastFilterType =
52 itk::CastImageFilter< LongImageType, OutputImageType >;
53 using GMGaussianType =
54 itk::GradientMagnitudeRecursiveGaussianImageFilter< FloatImageType, FloatImageType >;
55
56
57 itk::ImageFileReader< InputImageType >::Pointer reader =
58 itk::ImageFileReader< InputImageType >::New();
59
60 reader->SetFileName( argv[1] );
61
62 TRY_EXPECT_NO_EXCEPTION( reader->Update() );
63
64
65 FilterType::Pointer toboggan = FilterType::New();
66
67 EXERCISE_BASIC_OBJECT_METHODS( toboggan, TobogganImageFilter,
68 ImageToImageFilter );
69
70 InputCastFilterType::Pointer inputCaster = InputCastFilterType::New();
71 GMGaussianType::Pointer gmgaussian = GMGaussianType::New();
72
73 inputCaster->SetInput( reader->GetOutput() );
74 gmgaussian->SetInput( inputCaster->GetOutput() );
75 gmgaussian->SetSigma( 15.0 );
76
77 toboggan->SetInput( gmgaussian->GetOutput() );
78
79
80 TRY_EXPECT_NO_EXCEPTION( toboggan->Update() );
81
82
83 // Cast the output of the Toboggan filter
84 OutputCastFilterType::Pointer outputCaster = OutputCastFilterType::New();
85 outputCaster->SetInput( toboggan->GetOutput() );
86
87 TRY_EXPECT_NO_EXCEPTION( outputCaster->Update() );
88
89 // Write the output
90 using WriterType = itk::ImageFileWriter< OutputImageType >;
91 WriterType::Pointer writer = WriterType::New();
92 writer->SetInput( outputCaster->GetOutput() );
93 writer->SetFileName( argv[2] );
94
95 TRY_EXPECT_NO_EXCEPTION( writer->Update() );
96
97
98 std::cout << "Test finished." << std::endl;
99 return EXIT_SUCCESS;
100 }
101