forked from InsightSoftwareConsortium/ITK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itkBlockMatchingImageFilter.hxx
327 lines (277 loc) · 13.7 KB
/
itkBlockMatchingImageFilter.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
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* 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
*
* http://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 itkBlockMatchingImageFilter_hxx
#define itkBlockMatchingImageFilter_hxx
#include "itkBlockMatchingImageFilter.h"
#include "itkImageRegionConstIterator.h"
#include "itkConstNeighborhoodIterator.h"
#include <limits>
namespace itk
{
template< typename TFixedImage, typename TMovingImage, typename TFeatures, typename TDisplacements, typename TSimilarities >
BlockMatchingImageFilter< TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities >
::BlockMatchingImageFilter()
{
// defaults
this->m_BlockRadius.Fill( 2 );
this->m_SearchRadius.Fill( 3 );
// make the outputs
this->ProcessObject::SetNumberOfRequiredOutputs( 2 );
typename DisplacementsType::Pointer displacements = static_cast< DisplacementsType * >( this->MakeOutput( 0 ).GetPointer() );
this->SetNthOutput( 0, displacements.GetPointer() );
typename SimilaritiesType::Pointer similarities = static_cast< SimilaritiesType * >( this->MakeOutput( 1 ).GetPointer() );
this->SetNthOutput( 1, similarities.GetPointer() );
// all inputs are required
this->AddRequiredInputName( "FeaturePoints" );
this->SetPrimaryInputName( "FeaturePoints" );
this->AddRequiredInputName( "FixedImage" );
this->AddRequiredInputName( "MovingImage" );
}
template< typename TFixedImage, typename TMovingImage, typename TFeatures, typename TDisplacements, typename TSimilarities >
BlockMatchingImageFilter< TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities >
::~BlockMatchingImageFilter() = default;
template< typename TFixedImage, typename TMovingImage, typename TFeatures, typename TDisplacements, typename TSimilarities >
void
BlockMatchingImageFilter< TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities >
::PrintSelf( std::ostream & os, Indent indent ) const
{
Superclass::PrintSelf( os, indent );
os << indent << "Number of threads: " << this->GetNumberOfWorkUnits() << std::endl
<< indent << "m_BlockRadius: " << m_BlockRadius << std::endl
<< indent << "m_SearchRadius: " << m_SearchRadius << std::endl;
}
template< typename TFixedImage, typename TMovingImage, typename TFeatures, typename TDisplacements, typename TSimilarities >
void
BlockMatchingImageFilter< TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities >
::GenerateOutputInformation()
{
// We use the constructor defaults for all regions.
}
template< typename TFixedImage, typename TMovingImage, typename TFeatures, typename TDisplacements, typename TSimilarities >
void
BlockMatchingImageFilter< TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities >
::EnlargeOutputRequestedRegion(DataObject * output)
{
output->SetRequestedRegionToLargestPossibleRegion();
}
template< typename TFixedImage, typename TMovingImage, typename TFeatures, typename TDisplacements, typename TSimilarities >
void
BlockMatchingImageFilter< TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities >
::GenerateData()
{
// Call a method that can be overridden by a subclass to perform
// some calculations prior to splitting the main computations into
// separate threads
this->BeforeThreadedGenerateData();
// Set up the multithreaded processing
ThreadStruct str;
str.Filter = this;
this->GetMultiThreader()->SetNumberOfWorkUnits( this->GetNumberOfWorkUnits() );
this->GetMultiThreader()->SetSingleMethod(this->ThreaderCallback, &str);
// multithread the execution
this->GetMultiThreader()->SingleMethodExecute();
// Call a method that can be overridden by a subclass to perform
// some calculations after all the threads have completed
this->AfterThreadedGenerateData();
}
template< typename TFixedImage, typename TMovingImage, typename TFeatures, typename TDisplacements, typename TSimilarities >
DataObject::Pointer
BlockMatchingImageFilter< TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities >
::MakeOutput( ProcessObject::DataObjectPointerArraySizeType idx )
{
switch ( idx )
{
case 0:
{
return DisplacementsType::New().GetPointer();
}
break;
case 1:
{
return SimilaritiesType::New().GetPointer();
}
break;
}
itkExceptionMacro(<< "Bad output index " << idx );
}
template< typename TFixedImage, typename TMovingImage, typename TFeatures, typename TDisplacements, typename TSimilarities >
void
BlockMatchingImageFilter< TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities >
::BeforeThreadedGenerateData()
{
this->m_PointsCount = itk::NumericTraits< SizeValueType >::ZeroValue();
FeaturePointsConstPointer featurePoints = this->GetFeaturePoints();
if ( featurePoints )
{
this->m_PointsCount = featurePoints->GetNumberOfPoints();
}
if ( this->m_PointsCount < 1 )
{
itkExceptionMacro( "Invalid number of feature points: " << this->m_PointsCount << "." );
}
this->m_DisplacementsVectorsArray = new DisplacementsVector[ this->m_PointsCount ];
this->m_SimilaritiesValuesArray = new SimilaritiesValue[ this->m_PointsCount ];
}
template< typename TFixedImage, typename TMovingImage, typename TFeatures, typename TDisplacements, typename TSimilarities >
void
BlockMatchingImageFilter< TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities >
::AfterThreadedGenerateData()
{
FeaturePointsConstPointer featurePoints = this->GetFeaturePoints();
const typename FeaturePointsType::PointsContainer *points;
if ( featurePoints )
{
points = featurePoints->GetPoints();
DisplacementsPointer displacements = this->GetDisplacements();
using DisplacementsPointsContainerPointerType = typename DisplacementsType::PointsContainerPointer;
using DisplacementsPointsContainerType = typename DisplacementsType::PointsContainer;
DisplacementsPointsContainerPointerType displacementsPoints = DisplacementsPointsContainerType::New();
using DisplacementsPointDataContainerPointerType = typename DisplacementsType::PointDataContainerPointer;
using DisplacementsPointDataContainerType = typename DisplacementsType::PointDataContainer;
DisplacementsPointDataContainerPointerType displacementsData = DisplacementsPointDataContainerType::New();
SimilaritiesPointer similarities = this->GetSimilarities();
using SimilaritiesPointsContainerPointerType = typename SimilaritiesType::PointsContainerPointer;
using SimilaritiesPointsContainerType = typename SimilaritiesType::PointsContainer;
SimilaritiesPointsContainerPointerType similaritiesPoints = SimilaritiesPointsContainerType::New();
using SimilaritiesPointDataContainerPointerType = typename SimilaritiesType::PointDataContainerPointer;
using SimilaritiesPointDataContainerType = typename SimilaritiesType::PointDataContainer;
SimilaritiesPointDataContainerPointerType similaritiesData = SimilaritiesPointDataContainerType::New();
// insert displacements and similarities
for ( SizeValueType i = 0; i < this->m_PointsCount; i++ )
{
displacementsPoints->InsertElement( i, points->GetElement( i ) );
similaritiesPoints->InsertElement( i, points->GetElement( i ) );
displacementsData->InsertElement( i, this->m_DisplacementsVectorsArray[ i ] );
similaritiesData->InsertElement( i, this->m_SimilaritiesValuesArray[ i ] );
}
displacements->SetPoints( displacementsPoints );
displacements->SetPointData( displacementsData );
similarities->SetPoints( similaritiesPoints );
similarities->SetPointData( similaritiesData );
}
// clean up
delete[] m_DisplacementsVectorsArray;
delete[] m_SimilaritiesValuesArray;
}
// Callback routine used by the threading library. This routine just calls
// the ThreadedGenerateData method after setting the correct region for this
// thread.
template< typename TFixedImage, typename TMovingImage, typename TFeatures, typename TDisplacements, typename TSimilarities >
ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
BlockMatchingImageFilter< TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities >
::ThreaderCallback(void *arg)
{
auto * str = (ThreadStruct *)( ( (MultiThreaderBase::WorkUnitInfo *)( arg ) )->UserData );
ThreadIdType threadId = ( (MultiThreaderBase::WorkUnitInfo *)( arg ) )->WorkUnitID;
str->Filter->ThreadedGenerateData( threadId );
return ITK_THREAD_RETURN_DEFAULT_VALUE;
}
template< typename TFixedImage, typename TMovingImage, typename TFeatures, typename TDisplacements, typename TSimilarities >
void
BlockMatchingImageFilter< TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities >
::ThreadedGenerateData( ThreadIdType threadId )
{
FixedImageConstPointer fixedImage = this->GetFixedImage();
MovingImageConstPointer movingImage = this->GetMovingImage();
FeaturePointsConstPointer featurePoints = this->GetFeaturePoints();
SizeValueType threadCount = this->GetNumberOfWorkUnits();
// compute first point and number of points (count) for this thread
SizeValueType count = m_PointsCount / threadCount;
SizeValueType first = threadId * count;
if ( threadId + 1 == threadCount ) // last thread
{
count += this->m_PointsCount % threadCount;
}
// start constructing window region and center region (single voxel)
ImageRegionType window;
ImageRegionType center;
ImageSizeType windowSize;
windowSize.Fill( 1 );
center.SetSize( windowSize ); // size of center region is 1
windowSize += m_SearchRadius + m_SearchRadius;
window.SetSize( windowSize ); // size of window region is 1+2*m_BlockHalfWindow
// start constructing block iterator
SizeValueType numberOfVoxelInBlock = 1;
for ( unsigned i = 0; i < ImageSizeType::Dimension; i++ )
{
numberOfVoxelInBlock *= m_BlockRadius[ i ] + 1 + m_BlockRadius[ i ];
}
// loop thru feature points
for ( SizeValueType idx = first, last = first + count; idx < last; idx++ )
{
FeaturePointsPhysicalCoordinates originalLocation = featurePoints->GetPoint( idx );
ImageIndexType fixedIndex;
fixedImage->TransformPhysicalPointToIndex( originalLocation, fixedIndex );
ImageIndexType movingIndex;
movingImage->TransformPhysicalPointToIndex( originalLocation, movingIndex );
// the block is selected for a minimum similarity metric
SimilaritiesValue similarity = NumericTraits< SimilaritiesValue >::ZeroValue();
// New point location
DisplacementsVector displacement;
// set centers of window and center regions to current location
ImageIndexType start = fixedIndex - this->m_SearchRadius;
window.SetIndex( start );
center.SetIndex( movingIndex );
// iterate over neighborhoods in region window, for each neighborhood: iterate over voxels in blockRadius
ConstNeighborhoodIterator< FixedImageType > windowIterator( m_BlockRadius, fixedImage, window );
// iterate over voxels in neighborhood of current feature point
ConstNeighborhoodIterator< MovingImageType > centerIterator( m_BlockRadius, movingImage, center );
centerIterator.GoToBegin();
// iterate over neighborhoods in region window
for ( windowIterator.GoToBegin(); !windowIterator.IsAtEnd(); ++windowIterator )
{
SimilaritiesValue fixedSum = NumericTraits< SimilaritiesValue >::ZeroValue();
SimilaritiesValue fixedSumOfSquares = NumericTraits< SimilaritiesValue >::ZeroValue();
SimilaritiesValue movingSum = NumericTraits< SimilaritiesValue >::ZeroValue();
SimilaritiesValue movingSumOfSquares = NumericTraits< SimilaritiesValue >::ZeroValue();
SimilaritiesValue covariance = NumericTraits< SimilaritiesValue >::ZeroValue();
// iterate over voxels in blockRadius
for ( SizeValueType i = 0; i < numberOfVoxelInBlock; i++ ) // windowIterator.Size() == numberOfVoxelInBlock
{
const SimilaritiesValue fixedValue = windowIterator.GetPixel( i );
const SimilaritiesValue movingValue = centerIterator.GetPixel( i );
movingSum += movingValue;
fixedSum += fixedValue;
movingSumOfSquares += movingValue * movingValue;
fixedSumOfSquares += fixedValue * fixedValue;
covariance += fixedValue * movingValue;
}
const SimilaritiesValue fixedMean = fixedSum / numberOfVoxelInBlock;
const SimilaritiesValue movingMean = movingSum / numberOfVoxelInBlock;
const SimilaritiesValue fixedVariance = fixedSumOfSquares - numberOfVoxelInBlock * fixedMean * fixedMean;
const SimilaritiesValue movingVariance = movingSumOfSquares - numberOfVoxelInBlock * movingMean * movingMean;
covariance -= numberOfVoxelInBlock * fixedMean * movingMean;
SimilaritiesValue sim = NumericTraits< SimilaritiesValue >::ZeroValue();
if ( (fixedVariance * movingVariance) != 0.0 )
{
sim = ( covariance * covariance ) / ( fixedVariance * movingVariance );
}
if ( sim >= similarity )
{
FeaturePointsPhysicalCoordinates newLocation;
fixedImage->TransformIndexToPhysicalPoint( windowIterator.GetIndex(), newLocation );
displacement = newLocation - originalLocation;
similarity = sim;
}
}
this->m_DisplacementsVectorsArray[ idx ] = displacement;
this->m_SimilaritiesValuesArray[ idx ] = similarity;
}
}
} // end namespace itk
#endif