-
Notifications
You must be signed in to change notification settings - Fork 20
/
itkBlockMatchingMultiResolutionSimilarityFunctionSearchRegionImageSource.hxx
343 lines (311 loc) · 12.8 KB
/
itkBlockMatchingMultiResolutionSimilarityFunctionSearchRegionImageSource.hxx
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
/*=========================================================================
*
* 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.
*
*=========================================================================*/
#ifndef itkBlockMatchingMultiResolutionSimilarityFunctionSearchRegionImageSource_hxx
#define itkBlockMatchingMultiResolutionSimilarityFunctionSearchRegionImageSource_hxx
#include "itkBlockMatchingMaximumPixelDisplacementCalculator.h"
#include "itkImageConstIteratorWithIndex.h"
#include "itkImageRegionConstIterator.h"
#include "itkImageRegionIterator.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkNearestNeighborExtrapolateImageFunction.h"
namespace itk
{
namespace BlockMatching
{
template <class TFixedImage,
class TMovingImage,
class TMetricImage,
class TDisplacementImage,
class TFunctor,
class TInterpolatorPrecisionType>
MultiResolutionSimilarityFunctionSearchRegionImageSource<
TFixedImage,
TMovingImage,
TMetricImage,
TDisplacementImage,
TFunctor,
TInterpolatorPrecisionType>::MultiResolutionSimilarityFunctionSearchRegionImageSource()
: m_TopLevelRadiusSpecified(false)
{
this->m_CacheMetricImage = true;
this->m_SearchRegionRadiusImage = nullptr;
m_SearchRegionRadiusResampler = SearchRegionRadiusResamplerType::New();
using ExtrapolatorType = NearestNeighborExtrapolateImageFunction<DisplacementImageType, SpacePrecisionType>;
typename ExtrapolatorType::Pointer nnExtrapolator = ExtrapolatorType::New();
m_SearchRegionRadiusResampler->SetExtrapolator(nnExtrapolator);
// Sane default.
m_MinimumSearchRegionRadiusFactor.Fill(1.0);
m_DisplacementCalculator = MaximumPixelDisplacementCalculator<TMetricImage, TDisplacementImage>::New();
m_Interpolator = LinearInterpolateImageFunction<MetricImageType, TInterpolatorPrecisionType>::New();
}
template <class TFixedImage,
class TMovingImage,
class TMetricImage,
class TDisplacementImage,
class TFunctor,
class TInterpolatorPrecisionType>
void
MultiResolutionSimilarityFunctionSearchRegionImageSource<
TFixedImage,
TMovingImage,
TMetricImage,
TDisplacementImage,
TFunctor,
TInterpolatorPrecisionType>::SetDisplacementImage(DisplacementImageType * image)
{
DisplacementCalculatorSuperclass::SetDisplacementImage(image);
if (this->m_SearchRegionRadiusImage.GetPointer() == nullptr)
{
this->m_SearchRegionRadiusImage = SearchRegionRadiusImageType::New();
}
m_SearchRegionRadiusImage->CopyInformation(image);
m_SearchRegionRadiusImage->SetRegions(image->GetLargestPossibleRegion());
m_SearchRegionRadiusImage->Allocate();
m_DisplacementCalculator->SetDisplacementImage(image);
}
template <class TFixedImage,
class TMovingImage,
class TMetricImage,
class TDisplacementImage,
class TFunctor,
class TInterpolatorPrecisionType>
void
MultiResolutionSimilarityFunctionSearchRegionImageSource<
TFixedImage,
TMovingImage,
TMetricImage,
TDisplacementImage,
TFunctor,
TInterpolatorPrecisionType>::SetMetricImagePixel(const PointType & point,
const IndexType & index,
MetricImageType * metricImage)
{
DisplacementCalculatorSuperclass::SetMetricImagePixel(point, index, metricImage);
m_DisplacementCalculator->SetMetricImagePixel(point, index, metricImage);
}
template <class TFixedImage,
class TMovingImage,
class TMetricImage,
class TDisplacementImage,
class TFunctor,
class TInterpolatorPrecisionType>
void
MultiResolutionSimilarityFunctionSearchRegionImageSource<TFixedImage,
TMovingImage,
TMetricImage,
TDisplacementImage,
TFunctor,
TInterpolatorPrecisionType>::Compute()
{
m_DisplacementCalculator->Compute();
OutputImageType * outputPtr = this->GetOutput();
if (!outputPtr)
{
return;
}
PointType point;
typename MetricImageType::PixelType metric;
typename SearchRegionRadiusImageType::PixelType radius;
typename OutputRegionType::SizeType size;
// Iterate over the displacements and get the corresponding metric image value
// at the given point.
ImageRegionConstIteratorWithIndex<MetricImageImageType> metricImageImageConstIt(
this->m_MetricImageImage, this->m_MetricImageImage->GetBufferedRegion());
ImageRegionConstIterator<CenterPointsImageType> centerPointsConstIt(this->m_CenterPointsImage,
this->m_CenterPointsImage->GetBufferedRegion());
ImageRegionConstIterator<DisplacementImageType> displacementIt(this->m_DisplacementImage,
this->m_DisplacementImage->GetBufferedRegion());
ImageRegionIterator<OutputImageType> previousSearchRegionIt(outputPtr, outputPtr->GetBufferedRegion());
ImageRegionIterator<SearchRegionRadiusImageType> searchRegionRadiusIt(
this->m_SearchRegionRadiusImage, this->m_SearchRegionRadiusImage->GetBufferedRegion());
for (metricImageImageConstIt.GoToBegin(),
centerPointsConstIt.GoToBegin(),
displacementIt.GoToBegin(),
previousSearchRegionIt.GoToBegin(),
searchRegionRadiusIt.GoToBegin();
!metricImageImageConstIt.IsAtEnd();
++metricImageImageConstIt,
++centerPointsConstIt,
++displacementIt,
++previousSearchRegionIt,
++searchRegionRadiusIt)
{
point = centerPointsConstIt.Get() + displacementIt.Get();
m_Interpolator->SetInputImage(metricImageImageConstIt.Get());
metric = m_Interpolator->Evaluate(point);
size = previousSearchRegionIt.Get().GetSize();
for (unsigned int i = 0; i < ImageDimension; ++i)
{
radius[i] = (size[i] - 1) / 2 * m_Functor(metric);
}
searchRegionRadiusIt.Set(radius);
}
}
template <class TFixedImage,
class TMovingImage,
class TMetricImage,
class TDisplacementImage,
class TFunctor,
class TInterpolatorPrecisionType>
void
MultiResolutionSimilarityFunctionSearchRegionImageSource<TFixedImage,
TMovingImage,
TMetricImage,
TDisplacementImage,
TFunctor,
TInterpolatorPrecisionType>::BeforeThreadedGenerateData()
{
Superclass::BeforeThreadedGenerateData();
if (this->m_CurrentLevel != 0)
{
typename OutputImageType::Pointer outputPtr = this->GetOutput();
if (!outputPtr)
{
return;
}
m_SearchRegionRadiusResampler->SetInput(this->m_SearchRegionRadiusImage);
m_SearchRegionRadiusResampler->SetSize(outputPtr->GetRequestedRegion().GetSize());
m_SearchRegionRadiusResampler->SetOutputStartIndex(outputPtr->GetRequestedRegion().GetIndex());
m_SearchRegionRadiusResampler->SetOutputSpacing(outputPtr->GetSpacing());
m_SearchRegionRadiusResampler->SetOutputOrigin(outputPtr->GetOrigin());
m_SearchRegionRadiusResampler->SetOutputDirection(outputPtr->GetDirection());
m_SearchRegionRadiusResampler->UpdateLargestPossibleRegion();
}
if (!m_TopLevelRadiusSpecified)
{
itkExceptionMacro(<< "The top level radius has not been specified.");
}
}
template <class TFixedImage,
class TMovingImage,
class TMetricImage,
class TDisplacementImage,
class TFunctor,
class TInterpolatorPrecisionType>
void
MultiResolutionSimilarityFunctionSearchRegionImageSource<
TFixedImage,
TMovingImage,
TMetricImage,
TDisplacementImage,
TFunctor,
TInterpolatorPrecisionType>::ThreadedGenerateData(const OutputRegionType & outputRegion, ThreadIdType threadID)
{
OutputImageType * outputPtr = this->GetOutput();
if (!outputPtr)
{
return;
}
OutputRegionType region;
OutputRegionType movingLargestRegion = this->m_MovingImage->GetLargestPossibleRegion();
typename MovingImageType::PointType point;
typename MovingImageType::IndexType index;
typename MovingImageType::SizeType unitySize;
unitySize.Fill(1);
RadiusType radius;
if (this->m_CurrentLevel == 0)
{
ImageRegionIteratorWithIndex<OutputImageType> it(outputPtr, outputRegion);
for (it.GoToBegin(); !it.IsAtEnd(); ++it)
{
index = it.GetIndex();
outputPtr->TransformIndexToPhysicalPoint(index, point);
this->m_MovingImage->TransformPhysicalPointToIndex(point, index);
region.SetIndex(index);
region.SetSize(unitySize);
region.PadByRadius(m_TopLevelRadius);
if (!region.Crop(movingLargestRegion))
{
itkExceptionMacro(<< "Attempted to create a search region entirely outside the moving image.");
}
it.Set(region);
}
}
else
{
// Cached for determining the closest index when outside the input.
IndexType startIndex = this->m_MovingImage->GetLargestPossibleRegion().GetIndex();
IndexType endIndex;
IndexType closestIndex;
RadiusType minimumSearchRegionRadius;
typename MovingImageType::SizeType minimumRegionSize;
for (unsigned int i = 0; i < ImageDimension; ++i)
{
minimumSearchRegionRadius[i] = static_cast<unsigned int>(
Math::Ceil(static_cast<double>(this->m_FixedBlockRadius[i]) * this->m_MinimumSearchRegionRadiusFactor[i]));
// @todo figure out what the theoretical minimum required here without
// getting weird out-of-region bounds issues.
minimumRegionSize[i] = 2 * minimumSearchRegionRadius[i] + 1;
endIndex[i] =
startIndex[i] + this->m_MovingImage->GetLargestPossibleRegion().GetSize()[i] - 1 - minimumRegionSize[i];
}
ImageRegionIteratorWithIndex<OutputImageType> it(outputPtr, outputRegion);
ImageRegionConstIterator<DisplacementImageType> dispIt(this->m_DisplacementResampler->GetOutput(), outputRegion);
ImageRegionConstIterator<SearchRegionRadiusImageType> radiusIt(m_SearchRegionRadiusResampler->GetOutput(),
outputRegion);
for (it.GoToBegin(), dispIt.GoToBegin(), radiusIt.GoToBegin(); !it.IsAtEnd(); ++it, ++dispIt, ++radiusIt)
{
index = it.GetIndex();
outputPtr->TransformIndexToPhysicalPoint(index, point);
// resample displacement image
this->m_MovingImage->TransformPhysicalPointToIndex(point + dispIt.Get(), index);
region.SetIndex(index);
region.SetSize(unitySize);
// Expand the search region radius by the scaling that occured between
// levels.
for (unsigned int i = 0; i < ImageDimension; ++i)
{
radius[i] = std::ceil(static_cast<float>(radiusIt.Get()[i]) *
static_cast<float>(this->m_PyramidSchedule(this->m_CurrentLevel - 1, i)) /
static_cast<float>(this->m_PyramidSchedule(this->m_CurrentLevel, i)));
if (radius[i] < minimumSearchRegionRadius[i])
{
radius[i] = minimumSearchRegionRadius[i];
}
else if (radius[i] > m_TopLevelRadius[i])
{
radius[i] = m_TopLevelRadius[i] / 2;
}
}
region.PadByRadius(radius);
if (!region.Crop(movingLargestRegion))
{
// Set to the closest index and with a valid minimumsearch region
// radius. .
for (unsigned int i = 0; i < ImageDimension; ++i)
{
closestIndex[i] = index[i];
if (index[i] < startIndex[i])
{
closestIndex[i] = startIndex[i];
}
else if (index[i] > endIndex[i])
{
closestIndex[i] = endIndex[i] - minimumRegionSize[i];
}
}
region.SetIndex(closestIndex);
region.SetSize(minimumRegionSize);
}
it.Set(region);
}
}
}
} // namespace BlockMatching
} // namespace itk
#endif