-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitkMultiScaleGaussianEnhancementImageFilter.hxx
491 lines (393 loc) · 16.4 KB
/
itkMultiScaleGaussianEnhancementImageFilter.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*=========================================================================
*
* Copyright Marius Staring, Stefan Klein, David Doria. 2011.
*
* 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 __itkMultiScaleGaussianEnhancementImageFilter_txx
#define __itkMultiScaleGaussianEnhancementImageFilter_txx
#include "itkMultiScaleGaussianEnhancementImageFilter.h"
// ITK include files
#include "itkImageRegionIterator.h"
#include "itkMaximumImageFilter.h"
namespace itk
{
/**
* ********************* Constructor ****************************
*/
template< typename TInputImage, typename TOutputImage >
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::MultiScaleGaussianEnhancementImageFilter()
{
this->m_NonNegativeHessianBasedMeasure = true;
this->m_SigmaMinimum = 1.0;
this->m_SigmaMaximum = 4.0;
this->m_NumberOfSigmaSteps = 4;
this->m_SigmaStepMethod = Self::LogarithmicSigmaSteps;
this->m_GenerateScalesOutput = false;
this->m_Rescale = true;
this->ProcessObject::SetNumberOfRequiredOutputs( 5 );
typename ScalesImageType::Pointer scalesImage = ScalesImageType::New();
this->ProcessObject::SetNthOutput( 1, scalesImage.GetPointer() );
typename GaussianImageType::Pointer gaussianImage = GaussianImageType::New();
this->ProcessObject::SetNthOutput( 2, gaussianImage.GetPointer() );
typename GradientImageType::Pointer gradientImage = GradientImageType::New();
this->ProcessObject::SetNthOutput( 3, gradientImage.GetPointer() );
typename HessianTensorImageType::Pointer hessianImage = HessianTensorImageType::New();
this->ProcessObject::SetNthOutput( 4, hessianImage.GetPointer() );
// Construct GaussianEnhancementImageFilter
this->m_GaussianEnhancementFilter = SingleScaleFilterType::New();
} // end Constructor
/**
* ********************* SetUnaryFunctor ****************************
*/
template< typename TInputImage, typename TOutputImage >
void
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::SetUnaryFunctor( UnaryFunctorBaseType * _arg )
{
if ( this->m_GaussianEnhancementFilter->GetUnaryFunctor() != _arg )
{
this->m_GaussianEnhancementFilter->SetUnaryFunctor( _arg );
this->Modified();
}
} // end SetUnaryFunctor()
/**
* ********************* SetBinaryFunctor ****************************
*/
template< typename TInputImage, typename TOutputImage >
void
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::SetBinaryFunctor( BinaryFunctorBaseType * _arg )
{
if ( this->m_GaussianEnhancementFilter->GetBinaryFunctor() != _arg )
{
this->m_GaussianEnhancementFilter->SetBinaryFunctor( _arg );
this->Modified();
}
} // end SetBinaryFunctor()
/**
* ********************* SetNormalizeAcrossScale ****************************
*/
template< typename TInputImage, typename TOutputImage >
void
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::SetNormalizeAcrossScale( bool normalize )
{
if ( this->m_GaussianEnhancementFilter->GetNormalizeAcrossScale() != normalize )
{
this->m_GaussianEnhancementFilter->SetNormalizeAcrossScale( normalize );
this->Modified();
}
} // end SetNormalizeAcrossScale()
/**
* ********************* MakeOutput ****************************
*/
template< typename TInputImage, typename TOutputImage >
typename MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >::DataObjectPointer
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::MakeOutput( unsigned int idx )
{
if ( idx == 1 )
{
return static_cast<DataObject*>( ScalesImageType::New().GetPointer() );
}
return Superclass::MakeOutput( idx );
} // end MakeOutput()
/**
* ********************* EnlargeOutputRequestedRegion ****************************
*/
template< typename TInputImage, typename TOutputImage >
void
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::EnlargeOutputRequestedRegion( DataObject *output )
{
// currently this filter can not stream so we must set the outputs
// requested region to the largest
output->SetRequestedRegionToLargestPossibleRegion();
} // end EnlargeOutputRequestedRegion()
/**
* ********************* SetNumberOfThreads ****************************
*/
template< typename TInputImage, typename TOutputImage >
void
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::SetNumberOfThreads( ThreadIdType nt )
{
Superclass::SetNumberOfThreads( nt );
this->m_GaussianEnhancementFilter->SetNumberOfThreads( nt );
this->Modified();
} // end SetNumberOfThreads()
/**
* ********************* GenerateData ****************************
*/
template< typename TInputImage, typename TOutputImage >
void
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::GenerateData( void )
{
// TODO: Move the allocation to a derived AllocateOutputs method
// Allocate the output
this->GetOutput()->SetBufferedRegion( this->GetOutput()->GetRequestedRegion() );
this->GetOutput()->Allocate();
if ( this->m_NonNegativeHessianBasedMeasure )
{
this->GetOutput()->FillBuffer( itk::NumericTraits<OutputPixelType>::Zero );
}
else
{
this->GetOutput()->FillBuffer( itk::NumericTraits<OutputPixelType>::NonpositiveMin() );
}
if ( this->m_GenerateScalesOutput )
{
typename ScalesImageType::Pointer scalesImage
= dynamic_cast<ScalesImageType*>( this->ProcessObject::GetOutput( 1 ) );
scalesImage->SetBufferedRegion( scalesImage->GetRequestedRegion() );
scalesImage->Allocate();
scalesImage->FillBuffer( itk::NumericTraits<ScalesPixelType>::Zero );
typename GaussianImageType::Pointer gaussianImage
= dynamic_cast<GaussianImageType*>( this->ProcessObject::GetOutput( 2 ) );
gaussianImage->SetBufferedRegion( gaussianImage->GetRequestedRegion() );
gaussianImage->Allocate();
gaussianImage->FillBuffer( itk::NumericTraits<GaussianPixelType>::Zero );
typename GradientImageType::Pointer gradientImage
= dynamic_cast<GradientImageType*>( this->ProcessObject::GetOutput( 3 ) );
gradientImage->SetBufferedRegion( gradientImage->GetRequestedRegion() );
gradientImage->Allocate();
gradientImage->FillBuffer( itk::NumericTraits<GradientPixelType>::Zero );
typename HessianTensorImageType::Pointer hessianImage
= dynamic_cast<HessianTensorImageType*>( this->ProcessObject::GetOutput( 4 ) );
hessianImage->SetBufferedRegion( hessianImage->GetRequestedRegion() );
hessianImage->Allocate();
hessianImage->FillBuffer( itk::NumericTraits<HessianPixelType>::Zero );
}
// Check stuff here before starting
if ( this->m_SigmaMinimum > this->m_SigmaMaximum )
{
itkExceptionMacro( << "ERROR: SigmaMinimum: " << this->m_SigmaMinimum
<< " cannot be greater than SigmaMaximum: " << this->m_SigmaMaximum );
}
typename InputImageType::ConstPointer input = this->GetInput();
// Set filter input
this->m_GaussianEnhancementFilter->SetInput( input );
this->m_GaussianEnhancementFilter->SetRescale( this->m_Rescale );
unsigned int scaleLevel = 0;
while ( scaleLevel < this->m_NumberOfSigmaSteps )
{
// Determine sigma for this level
double sigma = this->ComputeSigmaValue( scaleLevel );
// Compute vesselness for this level.
this->m_GaussianEnhancementFilter->SetSigma( sigma );
this->m_GaussianEnhancementFilter->Update();
/*if( sigma == m_SigmaMinimum )
{
this->GraftNthOutput(2, const_cast<GaussianImageType*>( this->m_GaussianEnhancementFilter->GetGaussianImage() ) );
}*/
//this->m_GaussianEnhancementFilter->GetGaussianImage(),
// Get the maximum so far.
this->UpdateMaximumResponse( this->m_GaussianEnhancementFilter->GetOutput(), scaleLevel,
this->m_GaussianEnhancementFilter->GetGaussianImage(),
this->m_GaussianEnhancementFilter->GetGradientImage(),
this->m_GaussianEnhancementFilter->GetHessianImage() ); //
scaleLevel++;
}
} // end GenerateData()
/**
* ********************* UpdateMaximumResponse ****************************
*/
template< typename TInputImage, typename TOutputImage >
void
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::UpdateMaximumResponse(
const OutputImageType *seOutput,
const unsigned int &scaleLevel,
const GaussianImageType *seGaussian,
const GradientImageType *seGradient,
const HessianTensorImageType *seHessian ) //
{
// Generate the scales output.
if ( this->m_GenerateScalesOutput )
{
double sigma = this->ComputeSigmaValue( scaleLevel );
OutputRegionType outputRegion = this->GetOutput()->GetBufferedRegion();
//Scale
typename ScalesImageType::Pointer scalesImage
= static_cast<ScalesImageType*>( this->ProcessObject::GetOutput( 1 ) );
ImageRegionIterator<ScalesImageType> scalesIter( scalesImage, outputRegion );
// Gaussian
typename GaussianImageType::Pointer gaussianImage
= static_cast<GaussianImageType*>( this->ProcessObject::GetOutput( 2 ) );
ImageRegionIterator<GaussianImageType> gaussianIter( gaussianImage, outputRegion );
// Gradient
typename GradientImageType::Pointer gradientImage
= static_cast<GradientImageType*>( this->ProcessObject::GetOutput( 3 ) );
ImageRegionIterator<GradientImageType> gradientIter( gradientImage, outputRegion );
//Hessian
typename HessianTensorImageType::Pointer hessianImage
= static_cast<HessianTensorImageType*>( this->ProcessObject::GetOutput( 4 ) );
ImageRegionIterator<HessianTensorImageType> hessianIter( hessianImage, outputRegion );
ImageRegionConstIterator<OutputImageType> prevMaxResponseIter( this->GetOutput(), outputRegion );
ImageRegionConstIterator<OutputImageType> currentResponseIter( seOutput, outputRegion );
ImageRegionConstIterator<GaussianImageType> gaussianCurrentIter( seGaussian, outputRegion );
ImageRegionConstIterator<GradientImageType> gradientCurrentIter( seGradient, outputRegion );
ImageRegionConstIterator<HessianTensorImageType> hessianCurrentIter( seHessian, outputRegion );
scalesIter.GoToBegin();
gaussianIter.GoToBegin();
gradientIter.GoToBegin();
hessianIter.GoToBegin();
prevMaxResponseIter.GoToBegin();
currentResponseIter.GoToBegin();
gaussianCurrentIter.GoToBegin();
gradientCurrentIter.GoToBegin();
hessianCurrentIter.GoToBegin();
while ( !scalesIter.IsAtEnd() )
{
if ( prevMaxResponseIter.Value() < currentResponseIter.Value() )
{
scalesIter.Set( static_cast<ScalesPixelType>( sigma ) );
gaussianIter.Set( gaussianCurrentIter.Value() );
gradientIter.Set( gradientCurrentIter.Value() );
hessianIter.Set( hessianCurrentIter.Value() );
}
++scalesIter; ++prevMaxResponseIter; ++currentResponseIter;
++gaussianIter; ++gaussianCurrentIter;
++gradientIter; ++gradientCurrentIter;
++hessianIter;++hessianCurrentIter;
}
} // end if scales image
// Generate the current maximum response.
typedef MaximumImageFilter< OutputImageType,
OutputImageType, OutputImageType> MaxFilterType;
typename MaxFilterType::Pointer maxFilter = MaxFilterType::New();
maxFilter->SetInput1( this->GetOutput() );
maxFilter->SetInput2( seOutput );
maxFilter->SetNumberOfThreads(this->GetNumberOfThreads());
maxFilter->InPlaceOn();
maxFilter->Update();
this->GraftOutput( maxFilter->GetOutput() );
} // end UpdateMaximumResponse()
/**
* ********************* ComputeSigmaValue ****************************
*/
template< typename TInputImage, typename TOutputImage >
double
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::ComputeSigmaValue( const unsigned int & scaleLevel )
{
double sigmaValue = this->m_SigmaMinimum;
switch ( this->m_SigmaStepMethod )
{
case Self::EquispacedSigmaSteps:
{
const double stepSize = vnl_math_max( 1e-10,
( this->m_SigmaMaximum - this->m_SigmaMinimum ) / ( this->m_NumberOfSigmaSteps - 1 ) );
sigmaValue = this->m_SigmaMinimum + stepSize * scaleLevel;
break;
}
case Self::LogarithmicSigmaSteps:
{
const double stepSize = vnl_math_max( 1e-10,
( vcl_log( this->m_SigmaMaximum ) - vcl_log( this->m_SigmaMinimum ) )
/ ( this->m_NumberOfSigmaSteps - 1 ) );
sigmaValue = vcl_exp( vcl_log ( this->m_SigmaMinimum ) + stepSize * scaleLevel );
break;
}
default:
itkExceptionMacro( << "ERROR: Invalid SigmaStepMethod" );
break;
}
return sigmaValue;
} // end ComputeSigmaValue()
/**
* ********************* SetSigmaStepMethodToEquispaced ****************************
*/
template< typename TInputImage, typename TOutputImage >
void
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::SetSigmaStepMethodToEquispaced( void )
{
this->SetSigmaStepMethod( Self::EquispacedSigmaSteps );
} // end SetSigmaStepMethodToEquispaced()
/**
* ********************* SetSigmaStepMethodToLogarithmic ****************************
*/
template< typename TInputImage, typename TOutputImage >
void
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::SetSigmaStepMethodToLogarithmic( void )
{
this->SetSigmaStepMethod(Self::LogarithmicSigmaSteps);
} // end SetSigmaStepMethodToLogarithmic()
/**
* ********************* GetScalesOutput ****************************
*/
template< typename TInputImage, typename TOutputImage >
const typename MultiScaleGaussianEnhancementImageFilter<TInputImage, TOutputImage >::ScalesImageType *
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::GetScalesOutput( void ) const
{
return static_cast<const ScalesImageType*>(this->ProcessObject::GetOutput(1));
} // end GetScalesOutput()
/**
* ********************* GetGaussianOutput ****************************
*/
template< typename TInputImage, typename TOutputImage >
const typename MultiScaleGaussianEnhancementImageFilter<TInputImage, TOutputImage >::GaussianImageType *
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::GetGaussianOutput( void ) const
{
return static_cast<const GaussianImageType*>(this->ProcessObject::GetOutput(2));
}
/**
* ********************* GetGradientOutput ****************************
*/
template< typename TInputImage, typename TOutputImage >
const typename MultiScaleGaussianEnhancementImageFilter<TInputImage, TOutputImage >::GradientImageType *
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::GetGradientOutput( void ) const
{
return static_cast<const GradientImageType*>(this->ProcessObject::GetOutput(3));
}
/**
* ********************* GetHessianOutput ****************************
*/
template< typename TInputImage, typename TOutputImage >
const typename MultiScaleGaussianEnhancementImageFilter<TInputImage, TOutputImage >::HessianTensorImageType *
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::GetHessianOutput( void ) const
{
return static_cast<const HessianTensorImageType*>(this->ProcessObject::GetOutput(4));
}
/**
* ********************* PrintSelf ****************************
*/
template< typename TInputImage, typename TOutputImage >
void
MultiScaleGaussianEnhancementImageFilter< TInputImage, TOutputImage >
::PrintSelf( std::ostream & os, Indent indent ) const
{
Superclass::PrintSelf( os, indent );
os << indent << "SigmaMinimum: " << this->m_SigmaMinimum << std::endl;
os << indent << "SigmaMaximum: " << this->m_SigmaMaximum << std::endl;
os << indent << "NumberOfSigmaSteps: " << this->m_NumberOfSigmaSteps << std::endl;
os << indent << "SigmaStepMethod: " << this->m_SigmaStepMethod << std::endl;
os << indent << "NonNegativeHessianBasedMeasure: "
<< this->m_NonNegativeHessianBasedMeasure << std::endl;
os << indent << "GenerateScalesOutput: " << this->m_GenerateScalesOutput << std::endl;
os << indent << "Rescale: " << this->m_Rescale << std::endl;
os << indent << "NormalizeAcrossScale: "
<< this->m_GaussianEnhancementFilter->GetNormalizeAcrossScale() << std::endl;
} // end PrintSelf()
} // end namespace itk
#endif