File: itkLandmarkBasedTransformInitializerTest.cxx

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,652 kB
  • sloc: cpp: 458,133; ansic: 196,223; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 430; csh: 220; perl: 193; xml: 20
file content (329 lines) | stat: -rw-r--r-- 12,517 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "itkLandmarkBasedTransformInitializer.h"
#include "itkImage.h"
#include <math.h>
#include <iostream>


//
// The test specifies a bunch of fixed and moving landmarks and test if the 
// fixed landmarks after transform by the computed transform coincides
// with the moving landmarks....

int itkLandmarkBasedTransformInitializerTest(int, char * [])
{

  const double nPI = 4.0 * vcl_atan( 1.0 );  

  {
    // Test LandmarkBasedTransformInitializer for Rigid 3D landmark
    // based alignment
    std::cout << "Testing Landmark alignment with VersorRigid3DTransform" << std::endl;
    
    typedef  unsigned char  PixelType;
    const unsigned int Dimension = 3;
    
    typedef itk::Image< PixelType, Dimension >  FixedImageType;
    typedef itk::Image< PixelType, Dimension >  MovingImageType;

    FixedImageType::Pointer fixedImage   = FixedImageType::New();
    MovingImageType::Pointer movingImage = MovingImageType::New();

    // Create fixed and moving images of size 30 x 30 x 30
    // 
    FixedImageType::RegionType fRegion;
    FixedImageType::SizeType   fSize;
    FixedImageType::IndexType  fIndex;
    fSize.Fill(30);
    fIndex.Fill(0);
    fRegion.SetSize( fSize );
    fRegion.SetIndex( fIndex );
    MovingImageType::RegionType mRegion;
    MovingImageType::SizeType   mSize;
    MovingImageType::IndexType  mIndex;
    mSize.Fill(30);
    mIndex.Fill(0);
    mRegion.SetSize( mSize );
    mRegion.SetIndex( mIndex );
    fixedImage->SetLargestPossibleRegion( fRegion );
    fixedImage->SetBufferedRegion( fRegion );
    fixedImage->SetRequestedRegion( fRegion );
    fixedImage->Allocate();
    movingImage->SetLargestPossibleRegion( mRegion );
    movingImage->SetBufferedRegion( mRegion );
    movingImage->SetRequestedRegion( mRegion );
    movingImage->Allocate();
   
    // Set the transform type..
    typedef itk::VersorRigid3DTransform< double > TransformType;
    TransformType::Pointer transform = TransformType::New();
    typedef itk::LandmarkBasedTransformInitializer< TransformType, 
            FixedImageType, MovingImageType > TransformInitializerType;
    TransformInitializerType::Pointer initializer = TransformInitializerType::New();

    // Set fixed and moving landmarks
    TransformInitializerType::LandmarkPointContainer fixedLandmarks;
    TransformInitializerType::LandmarkPointContainer movingLandmarks;
    TransformInitializerType::LandmarkPointType point;
    TransformInitializerType::LandmarkPointType tmp;
    
    // Moving Landmarks = Fixed Landmarks rotated by 'angle' degrees and then
    //    translated by the 'translation'. Offset can be used to move the fixed 
    //    landmarks around.
    double angle = 10 * nPI / 180.0;
    TransformInitializerType::LandmarkPointType translation;
    translation[0] = 6;
    translation[1] = 10;
    translation[2] = 7;
    TransformInitializerType::LandmarkPointType offset;
    offset[0] = 10;
    offset[1] = 1;
    offset[2] = 5;
    
    point[0]=2 + offset[0];
    point[1]=2 + offset[1];
    point[2]=0 + offset[2];
    fixedLandmarks.push_back(point);
    tmp = point;
    point[0] = vcl_cos(angle)*point[0] - vcl_sin(angle)*point[1] + translation[0];
    point[1] = vcl_sin(angle)*tmp[0] + vcl_cos(angle)*point[1] + translation[1];
    point[2] = point[2] + translation[2];
    movingLandmarks.push_back(point);
    point[0]=2 + offset[0];
    point[1]=-2 + offset[1];
    point[2]=0 + offset[2];
    fixedLandmarks.push_back(point);
    tmp = point;
    point[0] = vcl_cos(angle)*point[0] - vcl_sin(angle)*point[1] + translation[0];
    point[1] = vcl_sin(angle)*tmp[0] + vcl_cos(angle)*point[1] + translation[1];
    point[2] = point[2] + translation[2];
    movingLandmarks.push_back(point);
    point[0]=-2 + offset[0];
    point[1]=2 + offset[1];
    point[2]=0 + offset[2];
    fixedLandmarks.push_back(point);
    tmp = point;
    point[0] = vcl_cos(angle)*point[0] - vcl_sin(angle)*point[1] + translation[0];
    point[1] = vcl_sin(angle)*tmp[0] + vcl_cos(angle)*point[1] + translation[1];
    point[2] = point[2] + translation[2];
    movingLandmarks.push_back(point);
    point[0]=-2 + offset[0];
    point[1]=-2 + offset[1];
    point[2]=0 + offset[2];
    fixedLandmarks.push_back(point);
    tmp = point;
    point[0] = vcl_cos(angle)*point[0] - vcl_sin(angle)*point[1] + translation[0];
    point[1] = vcl_sin(angle)*tmp[0] + vcl_cos(angle)*point[1] + translation[1];
    point[2] = point[2] + translation[2];
    movingLandmarks.push_back(point);
    
    initializer->SetFixedLandmarks(fixedLandmarks);
    initializer->SetMovingLandmarks(movingLandmarks);

    initializer->SetFixedImage( fixedImage );
    initializer->SetMovingImage( movingImage );
    initializer->SetTransform( transform );
    initializer->InitializeTransform();

    // Transform the landmarks now. For the given set of landmarks, since we computed the
    // moving landmarks explicitly from the rotation and translation specified, we should 
    // get a transform that does not give any mismatch. In other words, if the fixed
    // landmarks are transformed by the transform computed by the 
    // LandmarkBasedTransformInitializer, they should coincide exactly with the moving
    // landmarks. Note that we specified 4 landmarks, although three non-collinear
    // landmarks is sufficient to guarantee a solution.
    //
    TransformInitializerType::PointsContainerConstIterator 
      fitr = fixedLandmarks.begin();
    TransformInitializerType::PointsContainerConstIterator 
      mitr = movingLandmarks.begin();

    typedef TransformInitializerType::OutputVectorType  OutputVectorType;
    OutputVectorType error;
    OutputVectorType::RealValueType tolerance = 0.1;
    bool failed = false;
    
    while( mitr != movingLandmarks.end() )
      {
      std::cout << "  Fixed Landmark: " << *fitr << " Moving landmark " << *mitr 
        << " Transformed fixed Landmark : " << 
        transform->TransformPoint( *fitr ) << std::endl;
      
      error = *mitr - transform->TransformPoint( *fitr);
      if( error.GetNorm() > tolerance )
        {
        failed = true;
        }
      
      ++mitr;
      ++fitr;
      }
    
    if( failed )
      {
      // Hang heads in shame
      std::cout << "  Fixed landmarks transformed by the transform did not match closely "
        << " enough with the moving landmarks.  The transform computed was: ";
      transform->Print(std::cout);
      std::cout << "  [FAILED]" << std::endl;
      return EXIT_FAILURE;
      }
    else
      {
      std::cout << "  Landmark alignment using Rigid3D transform [PASSED]" << std::endl;
      } 
  }

  {
    //Test landmark alignment using Rigid 2D transform in 2 dimensions
    std::cout << "Testing Landmark alignment with Rigid2DTransform" << std::endl;
    
    typedef  unsigned char  PixelType;
    const unsigned int Dimension = 2;
    
    typedef itk::Image< PixelType, Dimension >  FixedImageType;
    typedef itk::Image< PixelType, Dimension >  MovingImageType;

    FixedImageType::Pointer fixedImage   = FixedImageType::New();
    MovingImageType::Pointer movingImage = MovingImageType::New();

    // Create fixed and moving images of size 30 x 30
    // 
    FixedImageType::RegionType fRegion;
    FixedImageType::SizeType   fSize;
    FixedImageType::IndexType  fIndex;
    fSize.Fill(30);
    fIndex.Fill(0);
    fRegion.SetSize( fSize );
    fRegion.SetIndex( fIndex );
    MovingImageType::RegionType mRegion;
    MovingImageType::SizeType   mSize;
    MovingImageType::IndexType  mIndex;
    mSize.Fill(30);
    mIndex.Fill(0);
    mRegion.SetSize( mSize );
    mRegion.SetIndex( mIndex );
    fixedImage->SetLargestPossibleRegion( fRegion );
    fixedImage->SetBufferedRegion( fRegion );
    fixedImage->SetRequestedRegion( fRegion );
    fixedImage->Allocate();
    movingImage->SetLargestPossibleRegion( mRegion );
    movingImage->SetBufferedRegion( mRegion );
    movingImage->SetRequestedRegion( mRegion );
    movingImage->Allocate();
   
    // Set the transform type..
    typedef itk::Rigid2DTransform< double > TransformType;
    TransformType::Pointer transform = TransformType::New();
    typedef itk::LandmarkBasedTransformInitializer< TransformType, 
            FixedImageType, MovingImageType > TransformInitializerType;
    TransformInitializerType::Pointer initializer = TransformInitializerType::New();
    initializer->DebugOn();

    // Set fixed and moving landmarks
    TransformInitializerType::LandmarkPointContainer fixedLandmarks;
    TransformInitializerType::LandmarkPointContainer movingLandmarks;
    TransformInitializerType::LandmarkPointType point;
    TransformInitializerType::LandmarkPointType tmp;
    
    // Moving Landmarks = Fixed Landmarks rotated by 'angle' degrees and then
    //    translated by the 'translation'. Offset can be used to move the fixed 
    //    landmarks around.
    double angle = 10 * nPI / 180.0;
    TransformInitializerType::LandmarkPointType translation;
    translation[0] = 6;
    translation[1] = 10;
    TransformInitializerType::LandmarkPointType offset;
    offset[0] = 10;
    offset[1] = 1;
    
    point[0]=2 + offset[0];
    point[1]=2 + offset[1];
    fixedLandmarks.push_back(point);
    tmp = point;
    point[0] = vcl_cos(angle)*point[0] - vcl_sin(angle)*point[1] + translation[0];
    point[1] = vcl_sin(angle)*tmp[0] + vcl_cos(angle)*point[1] + translation[1];
    movingLandmarks.push_back(point);
    point[0]=2 + offset[0];
    point[1]=-2 + offset[1];
    fixedLandmarks.push_back(point);
    tmp = point;
    point[0] = vcl_cos(angle)*point[0] - vcl_sin(angle)*point[1] + translation[0];
    point[1] = vcl_sin(angle)*tmp[0] + vcl_cos(angle)*point[1] + translation[1];
    movingLandmarks.push_back(point);
    point[0]=-2 + offset[0];
    point[1]=2 + offset[1];
    fixedLandmarks.push_back(point);
    tmp = point;
    point[0] = vcl_cos(angle)*point[0] - vcl_sin(angle)*point[1] + translation[0];
    point[1] = vcl_sin(angle)*tmp[0] + vcl_cos(angle)*point[1] + translation[1];
    movingLandmarks.push_back(point);
    point[0]=-2 + offset[0];
    point[1]=-2 + offset[1];
    fixedLandmarks.push_back(point);
    tmp = point;
    point[0] = vcl_cos(angle)*point[0] - vcl_sin(angle)*point[1] + translation[0];
    point[1] = vcl_sin(angle)*tmp[0] + vcl_cos(angle)*point[1] + translation[1];
    movingLandmarks.push_back(point);
    
    initializer->SetFixedLandmarks(fixedLandmarks);
    initializer->SetMovingLandmarks(movingLandmarks);


    initializer->SetFixedImage( fixedImage );
    initializer->SetMovingImage( movingImage );
    initializer->SetTransform( transform );
    initializer->InitializeTransform();

    // Transform the landmarks now. For the given set of landmarks, since we computed the
    // moving landmarks explicitly from the rotation and translation specified, we should 
    // get a transform that does not give any mismatch. In other words, if the fixed
    // landmarks are transformed by the transform computed by the 
    // LandmarkBasedTransformInitializer, they should coincide exactly with the moving
    // landmarks. Note that we specified 4 landmarks, although two 
    // landmarks is sufficient to guarantee a solution.
    //
    TransformInitializerType::PointsContainerConstIterator 
      fitr = fixedLandmarks.begin();
    TransformInitializerType::PointsContainerConstIterator 
      mitr = movingLandmarks.begin();

    typedef TransformInitializerType::OutputVectorType  OutputVectorType;
    OutputVectorType error;
    OutputVectorType::RealValueType tolerance = 0.1;
    bool failed = false;
    
    while( mitr != movingLandmarks.end() )
      {
      std::cout << "  Fixed Landmark: " << *fitr << " Moving landmark " << *mitr 
        << " Transformed fixed Landmark : " << 
        transform->TransformPoint( *fitr ) << std::endl;
      
      error = *mitr - transform->TransformPoint( *fitr);
      if( error.GetNorm() > tolerance )
        {
        failed = true;
        }
      
      ++mitr;
      ++fitr;
      }
    
    if( failed )
      {
      // Hang heads in shame
      std::cout << "  Fixed landmarks transformed by the transform did not match closely "
        << " enough with the moving landmarks.  The transform computed was: ";
      transform->Print(std::cout);
      std::cout << "[FAILED]" << std::endl;
      return EXIT_FAILURE;
      }
    else
      {
      std::cout << "  Landmark alignment using Rigid2D transform [PASSED]" << std::endl;
      } 
  }
    
  
  return EXIT_SUCCESS;
}