1717#include " itkRegularStepGradientDescentOptimizer.h"
1818#include " itkIterateNeighborhoodOptimizer.h"
1919
20- int example_gradientdescent ( int argc, char * argv[] )
20+ int
21+ example_gradientdescent (int argc, char * argv[])
2122{
2223 // Typedefs
2324 constexpr unsigned int Dimension = 2 ;
2425 using PixelType = float ;
2526 using OutputPixelType = unsigned char ;
26- using ImageType = itk::Image< PixelType, Dimension >;
27- using OutputImageType = itk::Image< OutputPixelType, Dimension >;
28- using ReaderType = itk::ImageFileReader< ImageType >;
29- using WriterType = itk::ImageFileWriter< OutputImageType >;
30- using PathType = itk::PolyLineParametricPath< Dimension >;
31- using PathFilterType = itk::SpeedFunctionToPathFilter< ImageType, PathType >;
27+ using ImageType = itk::Image<PixelType, Dimension>;
28+ using OutputImageType = itk::Image<OutputPixelType, Dimension>;
29+ using ReaderType = itk::ImageFileReader<ImageType>;
30+ using WriterType = itk::ImageFileWriter<OutputImageType>;
31+ using PathType = itk::PolyLineParametricPath<Dimension>;
32+ using PathFilterType = itk::SpeedFunctionToPathFilter<ImageType, PathType>;
3233 using CoordRepType = PathFilterType::CostFunctionType::CoordRepType;
33- using PathIteratorType = itk::PathIterator< OutputImageType, PathType >;
34+ using PathIteratorType = itk::PathIterator<OutputImageType, PathType>;
3435
3536 // Get filename arguments
3637 unsigned int argi = 1 ;
37- const char * outputFileName = argv[argi++];
38- const char * speedFileName = argv[argi++];
38+ const char * outputFileName = argv[argi++];
39+ const char * speedFileName = argv[argi++];
3940
4041 // Read speed function
4142 ReaderType::Pointer reader = ReaderType::New ();
42- reader->SetFileName ( speedFileName );
43+ reader->SetFileName (speedFileName);
4344 reader->Update ();
4445 ImageType::Pointer speed = reader->GetOutput ();
4546 speed->DisconnectPipeline ();
@@ -49,101 +50,104 @@ int example_gradientdescent( int argc, char* argv[] )
4950 InterpolatorType::Pointer interp = InterpolatorType::New ();
5051
5152 // Create cost function
52- PathFilterType::CostFunctionType::Pointer cost =
53- PathFilterType::CostFunctionType::New ();
54- cost->SetInterpolator ( interp );
53+ PathFilterType::CostFunctionType::Pointer cost = PathFilterType::CostFunctionType::New ();
54+ cost->SetInterpolator (interp);
5555
5656 // Create optimizer
5757 using OptimizerType = itk::GradientDescentOptimizer;
5858 OptimizerType::Pointer optimizer = OptimizerType::New ();
59- optimizer->SetNumberOfIterations ( 1000 );
59+ optimizer->SetNumberOfIterations (1000 );
6060
6161 // Create path filter
6262 PathFilterType::Pointer pathFilter = PathFilterType::New ();
63- pathFilter->SetInput ( speed );
64- pathFilter->SetCostFunction ( cost );
65- pathFilter->SetOptimizer ( optimizer );
66- pathFilter->SetTerminationValue ( 2.0 );
63+ pathFilter->SetInput (speed);
64+ pathFilter->SetCostFunction (cost);
65+ pathFilter->SetOptimizer (optimizer);
66+ pathFilter->SetTerminationValue (2.0 );
6767
6868 // Setup path points
6969 PathFilterType::PointType start, end, way1;
70- start[0 ] = 10 ; start[1 ] = 100 ;
71- end[0 ] = 100 ; end[1 ] = 10 ;
72- way1[0 ] = 10 ; way1[1 ] = 10 ;
70+ start[0 ] = 10 ;
71+ start[1 ] = 100 ;
72+ end[0 ] = 100 ;
73+ end[1 ] = 10 ;
74+ way1[0 ] = 10 ;
75+ way1[1 ] = 10 ;
7376
7477 // Add path information
7578 using PathInformationType = PathFilterType::PathInformationType;
7679 PathInformationType::Pointer info = PathInformationType::New ();
77- info->SetStartPoint ( start );
78- info->SetEndPoint ( end );
79- info->AddWayPoint ( way1 );
80- pathFilter->AddPathInformation ( info );
80+ info->SetStartPoint (start);
81+ info->SetEndPoint (end);
82+ info->AddWayPoint (way1);
83+ pathFilter->AddPathInformation (info);
8184
8285 // Compute the path
83- pathFilter->Update ( );
86+ pathFilter->Update ();
8487
8588 // Allocate output image
8689 OutputImageType::Pointer output = OutputImageType::New ();
87- output->SetRegions ( speed->GetLargestPossibleRegion () );
88- output->SetSpacing ( speed->GetSpacing () );
89- output->SetOrigin ( speed->GetOrigin () );
90- output->Allocate ( );
91- output->FillBuffer ( itk::NumericTraits<OutputPixelType>::Zero );
90+ output->SetRegions (speed->GetLargestPossibleRegion ());
91+ output->SetSpacing (speed->GetSpacing ());
92+ output->SetOrigin (speed->GetOrigin ());
93+ output->Allocate ();
94+ output->FillBuffer (itk::NumericTraits<OutputPixelType>::Zero);
9295
9396 // Rasterize path
94- for (unsigned int i= 0 ; i< pathFilter->GetNumberOfOutputs (); i++)
95- {
96- // Get the path
97- PathType::Pointer path = pathFilter->GetOutput ( i );
98-
99- // Check path is valid
100- if ( path->GetVertexList ()->Size () == 0 )
101- {
102- std::cout << " WARNING: Path " << (i+ 1 ) << " contains no points!" << std::endl;
103- continue ;
104- }
105-
106- // Iterate path and convert to image
107- PathIteratorType it ( output, path );
108- for (it.GoToBegin (); !it.IsAtEnd (); ++it)
109- {
110- it.Set ( itk::NumericTraits<OutputPixelType>::max () );
111- }
112- }
97+ for (unsigned int i = 0 ; i < pathFilter->GetNumberOfOutputs (); i++)
98+ {
99+ // Get the path
100+ PathType::Pointer path = pathFilter->GetOutput (i );
101+
102+ // Check path is valid
103+ if (path->GetVertexList ()->Size () == 0 )
104+ {
105+ std::cout << " WARNING: Path " << (i + 1 ) << " contains no points!" << std::endl;
106+ continue ;
107+ }
108+
109+ // Iterate path and convert to image
110+ PathIteratorType it (output, path);
111+ for (it.GoToBegin (); !it.IsAtEnd (); ++it)
112+ {
113+ it.Set (itk::NumericTraits<OutputPixelType>::max ());
114+ }
115+ }
113116
114117 // Write output
115118 WriterType::Pointer writer = WriterType::New ();
116- writer->SetFileName ( outputFileName );
117- writer->SetInput ( output );
119+ writer->SetFileName (outputFileName);
120+ writer->SetInput (output);
118121 writer->Update ();
119122
120- // Return
123+ // Return
121124 return EXIT_SUCCESS;
122125}
123126
124- int example_regularstepgradientdescent ( int argc, char * argv[] )
127+ int
128+ example_regularstepgradientdescent (int argc, char * argv[])
125129{
126130 // Typedefs
127131 constexpr unsigned int Dimension = 2 ;
128132 using PixelType = float ;
129133 using OutputPixelType = unsigned char ;
130- using ImageType = itk::Image< PixelType, Dimension >;
131- using OutputImageType = itk::Image< OutputPixelType, Dimension >;
132- using ReaderType = itk::ImageFileReader< ImageType >;
133- using WriterType = itk::ImageFileWriter< OutputImageType >;
134- using PathType = itk::PolyLineParametricPath< Dimension >;
135- using PathFilterType = itk::SpeedFunctionToPathFilter< ImageType, PathType >;
134+ using ImageType = itk::Image<PixelType, Dimension>;
135+ using OutputImageType = itk::Image<OutputPixelType, Dimension>;
136+ using ReaderType = itk::ImageFileReader<ImageType>;
137+ using WriterType = itk::ImageFileWriter<OutputImageType>;
138+ using PathType = itk::PolyLineParametricPath<Dimension>;
139+ using PathFilterType = itk::SpeedFunctionToPathFilter<ImageType, PathType>;
136140 using CoordRepType = PathFilterType::CostFunctionType::CoordRepType;
137- using PathIteratorType = itk::PathIterator< OutputImageType, PathType >;
141+ using PathIteratorType = itk::PathIterator<OutputImageType, PathType>;
138142
139143 // Get filename arguments
140144 unsigned int argi = 1 ;
141- const char * outputFileName = argv[argi++];
142- const char * speedFileName = argv[argi++];
145+ const char * outputFileName = argv[argi++];
146+ const char * speedFileName = argv[argi++];
143147
144148 // Read speed function
145149 ReaderType::Pointer reader = ReaderType::New ();
146- reader->SetFileName ( speedFileName );
150+ reader->SetFileName (speedFileName);
147151 reader->Update ();
148152 ImageType::Pointer speed = reader->GetOutput ();
149153 speed->DisconnectPipeline ();
@@ -153,98 +157,102 @@ int example_regularstepgradientdescent( int argc, char* argv[] )
153157 InterpolatorType::Pointer interp = InterpolatorType::New ();
154158
155159 // Create cost function
156- PathFilterType::CostFunctionType::Pointer cost =
157- PathFilterType::CostFunctionType::New ();
158- cost->SetInterpolator ( interp );
160+ PathFilterType::CostFunctionType::Pointer cost = PathFilterType::CostFunctionType::New ();
161+ cost->SetInterpolator (interp);
159162
160163 // Create optimizer
161164 using OptimizerType = itk::RegularStepGradientDescentOptimizer;
162165 OptimizerType::Pointer optimizer = OptimizerType::New ();
163- optimizer->SetNumberOfIterations ( 1000 );
164- optimizer->SetMaximumStepLength ( 0.5 );
165- optimizer->SetMinimumStepLength ( 0.1 );
166- optimizer->SetRelaxationFactor ( 0.5 );
166+ optimizer->SetNumberOfIterations (1000 );
167+ optimizer->SetMaximumStepLength (0.5 );
168+ optimizer->SetMinimumStepLength (0.1 );
169+ optimizer->SetRelaxationFactor (0.5 );
167170
168171 // Create path filter
169172 PathFilterType::Pointer pathFilter = PathFilterType::New ();
170- pathFilter->SetInput ( speed );
171- pathFilter->SetCostFunction ( cost );
172- pathFilter->SetOptimizer ( optimizer );
173- pathFilter->SetTerminationValue ( 2.0 );
173+ pathFilter->SetInput (speed);
174+ pathFilter->SetCostFunction (cost);
175+ pathFilter->SetOptimizer (optimizer);
176+ pathFilter->SetTerminationValue (2.0 );
174177
175178 // Setup path points
176179 PathFilterType::PointType start, end, way1;
177- start[0 ] = 10 ; start[1 ] = 100 ;
178- end[0 ] = 100 ; end[1 ] = 10 ;
179- way1[0 ] = 10 ; way1[1 ] = 10 ;
180+ start[0 ] = 10 ;
181+ start[1 ] = 100 ;
182+ end[0 ] = 100 ;
183+ end[1 ] = 10 ;
184+ way1[0 ] = 10 ;
185+ way1[1 ] = 10 ;
180186
181187 // Add path information
182188 using PathInformationType = PathFilterType::PathInformationType;
183189 PathInformationType::Pointer info = PathInformationType::New ();
184- info->SetStartPoint ( start );
185- info->SetEndPoint ( end );
186- info->AddWayPoint ( way1 );
187- pathFilter->AddPathInformation ( info );
190+ info->SetStartPoint (start);
191+ info->SetEndPoint (end);
192+ info->AddWayPoint (way1);
193+ pathFilter->AddPathInformation (info);
188194
189195 // Compute the path
190- pathFilter->Update ( );
196+ pathFilter->Update ();
191197
192198 // Allocate output image
193199 OutputImageType::Pointer output = OutputImageType::New ();
194- output->SetRegions ( speed->GetLargestPossibleRegion () );
195- output->SetSpacing ( speed->GetSpacing () );
196- output->SetOrigin ( speed->GetOrigin () );
197- output->Allocate ( );
198- output->FillBuffer ( itk::NumericTraits<OutputPixelType>::Zero );
200+ output->SetRegions (speed->GetLargestPossibleRegion ());
201+ output->SetSpacing (speed->GetSpacing ());
202+ output->SetOrigin (speed->GetOrigin ());
203+ output->Allocate ();
204+ output->FillBuffer (itk::NumericTraits<OutputPixelType>::Zero);
199205
200206 // Rasterize path
201- for (unsigned int i= 0 ; i< pathFilter->GetNumberOfOutputs (); i++)
202- {
203- // Get the path
204- PathType::Pointer path = pathFilter->GetOutput ( i );
205-
206- // Check path is valid
207- if ( path->GetVertexList ()->Size () == 0 )
208- {
209- std::cout << " WARNING: Path " << (i+ 1 ) << " contains no points!" << std::endl;
210- continue ;
211- }
212-
213- // Iterate path and convert to image
214- PathIteratorType it ( output, path );
215- for (it.GoToBegin (); !it.IsAtEnd (); ++it)
216- {
217- it.Set ( itk::NumericTraits<OutputPixelType>::max () );
218- }
219- }
207+ for (unsigned int i = 0 ; i < pathFilter->GetNumberOfOutputs (); i++)
208+ {
209+ // Get the path
210+ PathType::Pointer path = pathFilter->GetOutput (i );
211+
212+ // Check path is valid
213+ if (path->GetVertexList ()->Size () == 0 )
214+ {
215+ std::cout << " WARNING: Path " << (i + 1 ) << " contains no points!" << std::endl;
216+ continue ;
217+ }
218+
219+ // Iterate path and convert to image
220+ PathIteratorType it (output, path);
221+ for (it.GoToBegin (); !it.IsAtEnd (); ++it)
222+ {
223+ it.Set (itk::NumericTraits<OutputPixelType>::max ());
224+ }
225+ }
220226
221227 // Write output
222228 WriterType::Pointer writer = WriterType::New ();
223- writer->SetFileName ( outputFileName );
224- writer->SetInput ( output );
229+ writer->SetFileName (outputFileName);
230+ writer->SetInput (output);
225231 writer->Update ();
226232
227- // Return
233+ // Return
228234 return EXIT_SUCCESS;
229235}
230236
231- int main ( int argc, char * argv[] )
237+ int
238+ main (int argc, char * argv[])
232239{
233- if ( argc < 3 )
234- {
235- std::cerr << " Usage: MinimalPathExamples <SpeedImage> <OutputImage> [UseRegularStepGradientDescentOptimizer]" << std::endl;
240+ if (argc < 3 )
241+ {
242+ std::cerr << " Usage: MinimalPathExamples <SpeedImage> <OutputImage> [UseRegularStepGradientDescentOptimizer]"
243+ << std::endl;
236244 return EXIT_FAILURE;
237- }
245+ }
238246
239247 int useRegularStepGradientDescentOptimizer = 0 ;
240- if ( argc > 3 )
241- {
242- useRegularStepGradientDescentOptimizer = std::atoi ( argv[3 ] );
243- }
244-
245- if ( useRegularStepGradientDescentOptimizer )
246- {
247- return example_regularstepgradientdescent ( argc, argv );
248- }
249- return example_gradientdescent ( argc, argv );
248+ if (argc > 3 )
249+ {
250+ useRegularStepGradientDescentOptimizer = std::atoi (argv[3 ]);
251+ }
252+
253+ if (useRegularStepGradientDescentOptimizer)
254+ {
255+ return example_regularstepgradientdescent (argc, argv);
256+ }
257+ return example_gradientdescent (argc, argv);
250258}
0 commit comments