forked from luisibanez/ImageReconstruction
-
Notifications
You must be signed in to change notification settings - Fork 17
/
itkMontagePCMTestSynthetic.cxx
376 lines (331 loc) · 13.3 KB
/
itkMontagePCMTestSynthetic.cxx
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkArray.h"
#include "itkImageFileWriter.h"
#include "itkPhaseCorrelationOptimizer.h"
#include "itkMontageTestHelper.hxx" //for WriteTransform
#include "itkNumericTraits.h"
#include "itkPhaseCorrelationImageRegistrationMethod.h"
#include <array>
namespace itk
{
template <typename TPixel, unsigned int VDimension>
class HyperSphereImageSource : public itk::Object
{
public:
using Self = HyperSphereImageSource;
using Superclass = Object;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;
using ParametersType = Array<double>;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(Image, Object);
using ImageType = itk::Image<TPixel, VDimension>;
ImageType *
GenerateImage()
{
m_Image = ImageType::New();
typename ImageType::IndexType index;
index.Fill(0);
typename ImageType::RegionType region;
region.SetSize(m_ImageSize);
region.SetIndex(index);
m_Image->SetLargestPossibleRegion(region);
m_Image->SetBufferedRegion(region);
m_Image->SetRequestedRegion(region);
m_Image->Allocate();
m_Image->SetSpacing(m_ImageSpacing);
m_Image->SetOrigin(m_ImageOrigin);
m_Image->SetDirection(m_ImageDirection);
multiThreader->ParallelizeImageRegion<VDimension>(
region,
[&](const typename ImageType::RegionType & fragment) {
itk::Point<double, VDimension> p;
TPixel value;
itk::ImageRegionIteratorWithIndex<ImageType> it(m_Image, fragment);
while (!it.IsAtEnd())
{
m_Image->TransformIndexToPhysicalPoint(it.GetIndex(), p);
if (m_SphereCenter.EuclideanDistanceTo(p) > m_SphereRadius)
{
value = itk::NumericTraits<TPixel>::ZeroValue();
}
else
{
value = itk::NumericTraits<TPixel>::OneValue();
}
it.Set(value);
++it;
}
},
nullptr);
return m_Image.GetPointer();
}
protected:
HyperSphereImageSource()
{
m_SphereRadius = 50.0;
m_SphereCenter.Fill(50.0);
m_ImageOrigin.Fill(0.0);
m_ImageSize.Fill(100);
m_ImageSpacing.Fill(1.0);
m_ImageDirection.SetIdentity();
}
private:
typename ImageType::Pointer m_Image;
typename MultiThreaderBase::Pointer multiThreader = MultiThreaderBase::New();
public:
double m_SphereRadius;
typename ImageType::PointType m_SphereCenter;
typename ImageType::PointType m_ImageOrigin;
typename ImageType::SizeType m_ImageSize;
typename ImageType::SpacingType m_ImageSpacing;
typename ImageType::DirectionType m_ImageDirection;
};
} // namespace itk
template <unsigned int VDimension, typename TFixedImagePixel, typename TMovingImagePixel>
int
PhaseCorrelationRegistration(int argc, char * argv[])
{
if (argc < 6)
{
std::cerr << "Usage: " << argv[0] << " <<dimension><fixedTypeChar><movingTypeChar>>";
std::cerr << " <phaseCorrelationFile> <transformFile> <startSize> <endSize> [movingImageSpacings]" << std::endl;
std::cerr << "e.g.\n\t" << argv[0] << " 2cf phase.nrrd transform.tfm 17 31 1.0 1.0" << std::endl;
return EXIT_FAILURE;
}
const char * phaseCorrelationFile = argv[2];
unsigned startSize = std::stoul(argv[4]);
unsigned endSize = std::stoul(argv[5]);
using FixedImageType = itk::Image<TFixedImagePixel, VDimension>;
using MovingImageType = itk::Image<TMovingImagePixel, VDimension>;
using SizeType = typename MovingImageType::SizeType;
using FixedImageSourceType = itk::HyperSphereImageSource<typename FixedImageType::PixelType, VDimension>;
using MovingImageSourceType = itk::HyperSphereImageSource<typename MovingImageType::PixelType, VDimension>;
using PCMType = itk::PhaseCorrelationImageRegistrationMethod<FixedImageType, MovingImageType>;
using OperatorType = itk::PhaseCorrelationOperator<typename PCMType::InternalPixelType, VDimension>;
using OptimizerType = itk::PhaseCorrelationOptimizer<typename PCMType::InternalPixelType, VDimension>;
using TransformType = typename PCMType::TransformType;
using ParametersType = typename TransformType::ParametersType;
typename OperatorType::Pointer pcmOperator = OperatorType::New();
typename OptimizerType::Pointer pcmOptimizer = OptimizerType::New();
typename PCMType::Pointer pcm = PCMType::New();
pcm->SetOperator(pcmOperator);
pcm->SetOptimizer(pcmOptimizer);
pcm->DebugOn();
typename FixedImageSourceType::Pointer fixedImageSource = FixedImageSourceType::New();
typename MovingImageSourceType::Pointer movingImageSource = MovingImageSourceType::New();
bool pass = true;
itk::ObjectFactoryBase::RegisterFactory(itk::TxtTransformIOFactory::New());
using TestCoefficientsType = std::array<double, 5>;
std::vector<TestCoefficientsType> testCoefficients = { { 2.0, -0.1, 0.05, 0.1, -2.1 },
{ 2.5, -0.3, 0.05, 0.15, 2.15 } };
for (unsigned size1 = startSize; size1 <= endSize; size1++)
{
std::cout << "\nSize " << size1 << std::endl;
for (const auto & coef : testCoefficients)
{
std::cout << "Coefficient set " << (&coef - &testCoefficients[0]) << std::endl;
fixedImageSource->m_SphereRadius = size1 / coef[0];
fixedImageSource->m_SphereCenter.Fill(size1 / 2.0);
movingImageSource->m_SphereRadius = size1 / coef[0];
movingImageSource->m_SphereCenter.Fill(size1 / 2.0);
SizeType size;
size.Fill(size1);
fixedImageSource->m_ImageSize = size;
typename FixedImageType::ConstPointer fixedImage = fixedImageSource->GenerateImage();
ParametersType actualParameters(VDimension);
typename MovingImageType::SizeType movingSize;
typename MovingImageType::PointType movingOrigin;
typename MovingImageType::SpacingType movingSpacing;
movingSpacing.Fill(1.0);
for (unsigned int i = 0; i < VDimension; i++)
{
actualParameters[i] = coef[1] + i * (1 + size1 * coef[2]);
movingImageSource->m_SphereCenter[i] += actualParameters[i];
movingOrigin[i] = size1 * coef[3] + i * coef[4];
// movingSpacing[i] = 1.0 / (0.8 + i); //test different spacing (unsupported)
if (argc > 6 + int(i))
{
movingSpacing[i] = std::stod(argv[6 + i]);
}
movingSize[i] = (unsigned long)(size[i] / movingSpacing[i] + 3 * std::pow(-1, i));
movingSize[i] = std::max<itk::SizeValueType>(movingSize[i], 7u);
}
// modify the size of the moving image
// this tests the ability of PCM to padd the images to the same real size
movingImageSource->m_ImageSize = movingSize;
movingImageSource->m_ImageSpacing = movingSpacing;
// shift the origin of the moving image
// this tests the ability of PCM to introduce between-image origin offset
// into the transformation (so the final parameters can be directly used to
// resample the two images into the same coordinate system)
movingImageSource->m_ImageOrigin = movingOrigin;
typename MovingImageType::ConstPointer movingImage = movingImageSource->GenerateImage();
pcm->SetFixedImage(fixedImage);
pcm->SetMovingImage(movingImage);
using PadMethod = typename PCMType::PaddingMethodEnum;
for (auto padMethod : { PadMethod::Zero, PadMethod::Mirror, PadMethod::MirrorWithExponentialDecay })
{
pcm->SetPaddingMethod(padMethod);
std::cout << "Padding method " << static_cast<int>(padMethod) << std::endl;
using PeakMethod = typename OptimizerType::PeakInterpolationMethodEnum;
for (auto peakMethod : { PeakMethod::None, PeakMethod::Parabolic, PeakMethod::Cosine })
{
pcmOptimizer->SetPeakInterpolationMethod(peakMethod);
pcm->Modified(); // optimizer is not an "input" to PCM
// so its modification does not cause a pipeline update automatically
std::cout << "Peak interpolation method " << static_cast<int>(peakMethod) << std::endl;
try
{
pcm->Update();
if (pcm->GetFixedImageFFT()->GetLargestPossibleRegion().GetSize(0) == 0)
{
std::cout << "Fixed FFT cache's size[0] must be positive!" << std::endl;
pass = false;
}
if (pcm->GetMovingImageFFT()->GetLargestPossibleRegion().GetSize(0) == 0)
{
std::cout << "Moving FFT cache's size[0] must be positive!" << std::endl;
pass = false;
}
}
catch (itk::ExceptionObject & e)
{
std::cerr << e << std::endl;
pass = false;
}
// Get registration result and validate it.
ParametersType finalParameters = pcm->GetTransformParameters();
ParametersType transformParameters = pcm->GetOutput()->Get()->GetParameters();
const unsigned int numberOfParameters = actualParameters.Size();
const double tolerance = 1.0 + 1e-6;
// Validate the translation parameters
for (unsigned int i = 0; i < numberOfParameters; i++)
{
std::cout << finalParameters[i] << " == " << actualParameters[i] << " == " << transformParameters[i];
if ((itk::Math::abs(finalParameters[i] - actualParameters[i]) > tolerance) ||
(itk::Math::abs(transformParameters[i] - actualParameters[i]) > tolerance))
{
std::cout << " Tolerance exceeded at component " << i << std::endl;
pass = false;
}
else
{
std::cout << std::endl;
}
}
// All other parameters must be 0
for (unsigned int i = numberOfParameters; i < VDimension; i++)
{
if ((itk::Math::abs(finalParameters[i]) > tolerance) ||
(itk::Math::abs(transformParameters[i]) > tolerance))
{
std::cout << "Tolerance exceeded at component " << i << std::endl;
pass = false;
}
}
using WriterType = itk::ImageFileWriter<typename PCMType::RealImageType>;
typename WriterType::Pointer writer = WriterType::New();
writer->SetFileName(phaseCorrelationFile);
writer->SetInput(pcm->GetPhaseCorrelationImage());
try
{
writer->Update();
}
catch (itk::ExceptionObject & e)
{
std::cerr << e << std::endl;
pass = false;
}
try
{
WriteTransform(pcm->GetOutput()->Get(), argv[3]);
}
catch (itk::ExceptionObject & e)
{
std::cerr << e << std::endl;
pass = false;
}
} // for peakMethod
} // for padMethod
} // for testCoefficients
} // for size1
std::cout << *pcm;
if (!pass)
{
std::cout << "Test FAILED." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Test PASSED." << std::endl;
return EXIT_SUCCESS;
}
int
itkMontagePCMTestSynthetic(int argc, char * argv[])
{
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " <<Dimension><FixedPixelTypeCharacter><MovingImageTypeCharacter>>"
<< std::endl;
return EXIT_FAILURE;
}
if (!strcmp(argv[1], "2cc"))
{
return PhaseCorrelationRegistration<2, signed char, signed char>(argc, argv);
}
else if (!strcmp(argv[1], "2ff"))
{
return PhaseCorrelationRegistration<2, float, float>(argc, argv);
}
else if (!strcmp(argv[1], "2dd"))
{
return PhaseCorrelationRegistration<2, double, double>(argc, argv);
}
else if (!strcmp(argv[1], "2cf"))
{
return PhaseCorrelationRegistration<2, signed char, float>(argc, argv);
}
else if (!strcmp(argv[1], "2fd"))
{
return PhaseCorrelationRegistration<2, float, double>(argc, argv);
}
else if (!strcmp(argv[1], "3cc"))
{
return PhaseCorrelationRegistration<3, signed char, signed char>(argc, argv);
}
else if (!strcmp(argv[1], "3ff"))
{
return PhaseCorrelationRegistration<3, float, float>(argc, argv);
}
else if (!strcmp(argv[1], "3dd"))
{
return PhaseCorrelationRegistration<3, double, double>(argc, argv);
}
else if (!strcmp(argv[1], "3cf"))
{
return PhaseCorrelationRegistration<3, signed char, float>(argc, argv);
}
else if (!strcmp(argv[1], "3fd"))
{
return PhaseCorrelationRegistration<3, float, double>(argc, argv);
}
std::cerr << "Unexpected Dimension/FixedPixelType/MovingPixelType!" << std::endl;
return EXIT_FAILURE;
}