33#include " itkGradientRecursiveGaussianImageFilter.h"
44#include " itkGradientMagnitudeImageFilter.h"
55#include " itkImageFileReader.h"
6-
6+
77#include " vnl/vnl_math.h"
88#include < vnl/vnl_matrix.h>
99#include " vnl/algo/vnl_determinant.h"
1010#include " vnl/algo/vnl_matrix_inverse.h"
1111#include < vnl/vnl_vector.h>
12-
12+
1313#include < iostream>
14-
14+
1515namespace
1616{
1717using ImageType = itk::Image< unsigned char , 2 >;
@@ -25,7 +25,7 @@ using GradMagfilterType = itk::GradientMagnitudeImageFilter<
2525 ImageType, FloatImageType >;
2626using ReaderType = itk::ImageFileReader< ImageType >;
2727}
28-
28+
2929vnl_vector<double > generateCircle (double cx, double cy,
3030 double rx, double ry, int n);
3131void createImage (ImageType::Pointer image,
@@ -35,27 +35,27 @@ vnl_matrix<double> computeP(double alpha, double beta, double gamma,
3535vnl_vector<double > sampleImage (vnl_vector<double > x, vnl_vector<double > y,
3636 OutputImageType::Pointer gradient,
3737 int position);
38-
38+
3939int main ( int argc, char * argv[] )
4040{
41- // Image dimensions
41+ // Image dimensions
4242 int w = 300 ;
4343 int h = 300 ;
4444 ImageType::Pointer image;
45- if ( argc < 7 )
45+ if ( argc < 7 )
4646 {
4747 std::cout << " Usage " << argv[0 ]
4848 << " points alpha beta gamma sigma iterations [image]"
4949 << std::endl;
50- return EXIT_SUCCESS;;
50+ return EXIT_SUCCESS;
5151 }
52- else if ( argc < 8 )
52+ else if ( argc < 8 )
5353 {
5454 // Synthesize the image
5555 image = ImageType::New ();
5656 createImage (image, w, h, 150 , 150 , 50 , 50 );
5757 }
58- else if ( argc == 8 )
58+ else if ( argc == 8 )
5959 {
6060 // Open the image
6161 ReaderType::Pointer reader = ReaderType::New ();
@@ -73,200 +73,200 @@ int main( int argc, char* argv[] )
7373 return EXIT_FAILURE;
7474 }
7575 }
76-
77- // Snake parameters
76+
77+ // Snake parameters
7878 double alpha = 0.001 ;
7979 double beta = 0.4 ;
8080 double gamma = 100 ;
8181 double iterations = 1 ;
8282 int nPoints = 20 ;
8383 double sigma;
84-
84+
8585 nPoints = atoi (argv[1 ]);
8686 alpha = atof (argv[2 ]);
8787 beta = atof (argv[3 ]);
8888 gamma = atof (argv[4 ]);
8989 sigma = atof (argv[5 ]);
9090 iterations = atoi (argv[6 ]);
91-
92- // Temporal variables
91+
92+ // Temporal variables
9393 vnl_matrix<double > P;
9494 vnl_vector<double > v;
9595 double N;
96-
97- // Generate initial snake circle
96+
97+ // Generate initial snake circle
9898 v = generateCircle (130 , 130 , 50 , 50 , nPoints);
9999
100- // Computes P matrix.
100+ // Compute P matrix.
101101 N = v.size ()/2 ;
102102 try
103103 {
104104 P = computeP (alpha, beta, gamma, N);
105105 }
106- catch ( int n )
106+ catch (... )
107107 {
108- return EXIT_FAILURE;;
108+ return EXIT_FAILURE;
109109 }
110-
111- // Computes the magnitude gradient
110+
111+ // Compute the magnitude gradient
112112 GradMagfilterType::Pointer gradientMagnitudeFilter =
113113 GradMagfilterType::New ();
114114 gradientMagnitudeFilter->SetInput ( image );
115115 gradientMagnitudeFilter->Update ();
116-
117- // Computes the gradient of the gradient magnitude
116+
117+ // Compute the gradient of the gradient magnitude
118118 FilterType::Pointer gradientFilter = FilterType::New ();
119119 gradientFilter->SetInput ( gradientMagnitudeFilter->GetOutput () );
120120 gradientFilter->SetSigma ( sigma );
121121 gradientFilter->Update ();
122-
123- // Loop
122+
123+ // Loop
124124 vnl_vector<double > x (N);
125125 vnl_vector<double > y (N);
126-
126+
127127 std::cout << " Initial snake" << std::endl;
128- for (int i = 0 ; i < N; i++ )
128+ for (int i = 0 ; i < N; ++i )
129129 {
130130 x[i] = v[2 *i];
131- y[i] = v[2 *i+ 1 ];
131+ y[i] = v[2 *i + 1 ];
132132 std::cout << " (" << x[i] << " , " << y[i] << " )" << std::endl;
133133 }
134-
135- for ( int i = 0 ; i < iterations; i++ )
134+
135+ for ( int i = 0 ; i < iterations; ++i )
136136 {
137137 vnl_vector<double > fex;
138138 vnl_vector<double > fey;
139139 fex = sampleImage (x, y, gradientFilter->GetOutput (), 0 );
140140 fey = sampleImage (x, y, gradientFilter->GetOutput (), 1 );
141-
141+
142142 x = (x+gamma*fex).post_multiply (P);
143143 y = (y+gamma*fey).post_multiply (P);
144144 }
145-
146- // Display the answer
145+
146+ // Display the answer
147147 std::cout << " Final snake after " << iterations << " iterations" << std::endl;
148148 vnl_vector<double > v2 (2 *N);
149- for ( int i= 0 ; i< N; i++ )
149+ for ( int i = 0 ; i < N; ++i )
150150 {
151151 v2[2 *i] = x[i];
152- v2[2 *i+ 1 ] = y[i];
152+ v2[2 *i + 1 ] = y[i];
153153 std::cout << " (" << x[i] << " , " << y[i] << " )" << std::endl;
154154 }
155-
156- return EXIT_SUCCESS;;
155+
156+ return EXIT_SUCCESS;
157157}
158-
158+
159159vnl_vector<double > generateCircle (
160160 double cx, double cy, double rx, double ry, int n)
161161{
162162 vnl_vector<double > v (2 *(n+1 ));
163-
164- for ( int i= 0 ; i< n; i++ )
163+
164+ for ( int i = 0 ; i < n; ++i )
165165 {
166166 v[2 *i] = cx + rx*cos (2 *vnl_math::pi*i/n);
167- v[2 *i+ 1 ] = cy + ry*sin (2 *vnl_math::pi*i/n);
167+ v[2 *i + 1 ] = cy + ry*sin (2 *vnl_math::pi*i/n);
168168 }
169- v[2 *n]= v[0 ];
170- v[2 *n+ 1 ]= v[1 ];
169+ v[2 *n] = v[0 ];
170+ v[2 *n + 1 ] = v[1 ];
171171 return v;
172172}
173173
174174void createImage (ImageType::Pointer image,
175175 int w, int h, double cx, double cy, double rx, double ry)
176176{
177-
178177 itk::Size<2 > size;
179178 size[0 ] = w;
180179 size[1 ] = h;
181-
180+
182181 itk::RandomImageSource<ImageType>::Pointer randomImageSource = itk::RandomImageSource<ImageType>::New ();
183182 randomImageSource->SetNumberOfThreads (1 ); // to produce non-random results
184183 randomImageSource->SetSize (size);
185184 randomImageSource->SetMin (200 );
186185 randomImageSource->SetMax (255 );
187186 randomImageSource->Update ();
188-
187+
189188 image->SetRegions (randomImageSource->GetOutput ()->GetLargestPossibleRegion ());
190189 image->Allocate ();
191-
190+
192191 IndexType index;
193-
194- // Draw oval.
195- for ( int i= 0 ; i< w; i++ )
192+
193+ // Draw oval
194+ for ( int i = 0 ; i < w; ++i )
196195 {
197- for (int j= 0 ; j< h; j++ )
196+ for (int j = 0 ; j < h; ++j )
198197 {
199- index[0 ] = i; index[1 ] = j;
200- if ( ((i-cx)*(i-cx)/(rx*rx) + (j-cy)*(j-cy)/(ry*ry) ) < 1 )
198+ index[0 ] = i;
199+ index[1 ] = j;
200+ if ( ((i - cx)*(i - cx)/(rx*rx) + (j - cy)*(j - cy)/(ry*ry) ) < 1 )
201201 {
202202 image->SetPixel (index, randomImageSource->GetOutput ()->GetPixel (index)-100 );
203203 }
204204 else
205205 {
206206 image->SetPixel (index, randomImageSource->GetOutput ()->GetPixel (index));
207207 }
208-
208+
209209 }
210210 }
211211}
212-
212+
213213vnl_matrix<double > computeP (double alpha, double beta, double gamma, double N) throw (int )
214214{
215-
216- double a = gamma*(2 *alpha+ 6 *beta)+1 ;
217- double b = gamma*(-alpha- 4 *beta);
215+
216+ double a = gamma*(2 *alpha + 6 *beta)+1 ;
217+ double b = gamma*(-alpha - 4 *beta);
218218 double c = gamma*beta;
219-
220- vnl_matrix<double > P (N,N);
221-
219+
220+ vnl_matrix<double > P (N, N);
221+
222222 P.fill (0 );
223-
224- // fill diagonal
223+
224+ // Fill diagonal
225225 P.fill_diagonal (a);
226-
227- // fill next two diagonals
228- for ( int i= 0 ; i<(N- 1 ); i++ )
226+
227+ // Fill next two diagonals
228+ for ( int i = 0 ; i < N - 1 ; ++i )
229229 {
230- P (i+ 1 , i) = b;
231- P (i,i+ 1 ) = b;
230+ P (i + 1 , i) = b;
231+ P (i, i + 1 ) = b;
232232 }
233- // Moreover
234- P (0 , N-1 )= b;
235- P (N-1 , 0 )= b;
236-
237- // fill next two diagonals
238- for ( int i= 0 ; i<(N- 2 ); i++ )
233+ // Moreover
234+ P (0 , N-1 ) = b;
235+ P (N-1 , 0 ) = b;
236+
237+ // Fill next two diagonals
238+ for ( int i = 0 ; i < N- 2 ; ++i )
239239 {
240- P (i+ 2 , i) = c;
241- P (i,i+ 2 ) = c;
240+ P (i + 2 , i) = c;
241+ P (i, i + 2 ) = c;
242242 }
243- // Moreover
244- P (0 , N- 2 )= c;
245- P (1 , N- 1 )= c;
246- P (N- 2 , 0 )= c;
247- P (N- 1 , 1 )= c;
248-
249- if ( vnl_determinant (P) == 0.0 )
243+ // Moreover
244+ P (0 , N - 2 ) = c;
245+ P (1 , N - 1 ) = c;
246+ P (N - 2 , 0 ) = c;
247+ P (N - 1 , 1 ) = c;
248+
249+ if ( vnl_determinant (P) == 0.0 )
250250 {
251251 std::cerr << " Singular matrix. Determinant is 0." << std::endl;
252252 throw 2 ;
253253 }
254-
255- // Compute the inverse of the matrix P
254+
255+ // Compute the inverse of the matrix P
256256 vnl_matrix< double > Pinv;
257257 Pinv = vnl_matrix_inverse< double >(P);
258-
258+
259259 return Pinv.transpose ();
260260}
261-
261+
262262vnl_vector<double > sampleImage (vnl_vector<double > x, vnl_vector<double > y, OutputImageType::Pointer gradient, int position)
263263{
264264 int size;
265265 size = x.size ();
266266 vnl_vector<double > ans (size);
267-
267+
268268 IndexType index;
269- for ( int i= 0 ; i< size; i++ )
269+ for ( int i = 0 ; i < size; ++i )
270270 {
271271 index[0 ] = x[i];
272272 index[1 ] = y[i];
0 commit comments