/usr/include/InsightToolkit/Review/itkESMDemonsRegistrationFunction.txx is in libinsighttoolkit3-dev 3.20.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 492 493 | /*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkESMDemonsRegistrationFunction.txx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkESMDemonsRegistrationFunction_txx
#define __itkESMDemonsRegistrationFunction_txx
#include "itkESMDemonsRegistrationFunction.h"
#include "itkExceptionObject.h"
#include "vnl/vnl_math.h"
namespace itk {
/**
* Default constructor
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
ESMDemonsRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::ESMDemonsRegistrationFunction()
{
RadiusType r;
unsigned int j;
for( j = 0; j < ImageDimension; j++ )
{
r[j] = 0;
}
this->SetRadius(r);
m_TimeStep = 1.0;
m_DenominatorThreshold = 1e-9;
m_IntensityDifferenceThreshold = 0.001;
m_MaximumUpdateStepLength = 0.5;
this->SetMovingImage(NULL);
this->SetFixedImage(NULL);
m_FixedImageSpacing.Fill( 1.0 );
m_FixedImageOrigin.Fill( 0.0 );
m_FixedImageDirection.SetIdentity();
m_Normalizer = 0.0;
m_FixedImageGradientCalculator = GradientCalculatorType::New();
// Gradient orientation will be taken care of explicitely
m_FixedImageGradientCalculator->UseImageDirectionOff();
m_MappedMovingImageGradientCalculator = MovingImageGradientCalculatorType::New();
// Gradient orientation will be taken care of explicitely
m_MappedMovingImageGradientCalculator->UseImageDirectionOff();
this->m_UseGradientType = Symmetric;
typename DefaultInterpolatorType::Pointer interp =
DefaultInterpolatorType::New();
m_MovingImageInterpolator = static_cast<InterpolatorType*>(
interp.GetPointer() );
m_MovingImageWarper = WarperType::New();
m_MovingImageWarper->SetInterpolator( m_MovingImageInterpolator );
m_MovingImageWarper->SetEdgePaddingValue( NumericTraits<MovingPixelType>::max() );
m_Metric = NumericTraits<double>::max();
m_SumOfSquaredDifference = 0.0;
m_NumberOfPixelsProcessed = 0L;
m_RMSChange = NumericTraits<double>::max();
m_SumOfSquaredChange = 0.0;
}
/*
* Standard "PrintSelf" method.
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
ESMDemonsRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "UseGradientType: ";
os << m_UseGradientType << std::endl;
os << indent << "MaximumUpdateStepLength: ";
os << m_MaximumUpdateStepLength << std::endl;
os << indent << "MovingImageIterpolator: ";
os << m_MovingImageInterpolator.GetPointer() << std::endl;
os << indent << "FixedImageGradientCalculator: ";
os << m_FixedImageGradientCalculator.GetPointer() << std::endl;
os << indent << "MappedMovingImageGradientCalculator: ";
os << m_MappedMovingImageGradientCalculator.GetPointer() << std::endl;
os << indent << "DenominatorThreshold: ";
os << m_DenominatorThreshold << std::endl;
os << indent << "IntensityDifferenceThreshold: ";
os << m_IntensityDifferenceThreshold << std::endl;
os << indent << "Metric: ";
os << m_Metric << std::endl;
os << indent << "SumOfSquaredDifference: ";
os << m_SumOfSquaredDifference << std::endl;
os << indent << "NumberOfPixelsProcessed: ";
os << m_NumberOfPixelsProcessed << std::endl;
os << indent << "RMSChange: ";
os << m_RMSChange << std::endl;
os << indent << "SumOfSquaredChange: ";
os << m_SumOfSquaredChange << std::endl;
}
/**
*
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
ESMDemonsRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::SetIntensityDifferenceThreshold(double threshold)
{
m_IntensityDifferenceThreshold = threshold;
}
/**
*
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
double
ESMDemonsRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::GetIntensityDifferenceThreshold() const
{
return m_IntensityDifferenceThreshold;
}
/**
* Set the function state values before each iteration
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
ESMDemonsRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::InitializeIteration()
{
if( !this->GetMovingImage() || !this->GetFixedImage()
|| !m_MovingImageInterpolator )
{
itkExceptionMacro(
<< "MovingImage, FixedImage and/or Interpolator not set" );
}
// cache fixed image information
m_FixedImageOrigin = this->GetFixedImage()->GetOrigin();
m_FixedImageSpacing = this->GetFixedImage()->GetSpacing();
m_FixedImageDirection = this->GetFixedImage()->GetDirection();
// compute the normalizer
if( m_MaximumUpdateStepLength > 0.0 )
{
m_Normalizer = 0.0;
for( unsigned int k = 0; k < ImageDimension; k++ )
{
m_Normalizer += m_FixedImageSpacing[k] * m_FixedImageSpacing[k];
}
m_Normalizer *= m_MaximumUpdateStepLength * m_MaximumUpdateStepLength /
static_cast<double>( ImageDimension );
}
else
{
// set it to minus one to denote a special case
// ( unrestricted update length )
m_Normalizer = -1.0;
}
// setup gradient calculator
m_FixedImageGradientCalculator->SetInputImage( this->GetFixedImage() );
m_MappedMovingImageGradientCalculator->SetInputImage( this->GetMovingImage() );
// Compute warped moving image
m_MovingImageWarper->SetOutputOrigin( this->m_FixedImageOrigin );
m_MovingImageWarper->SetOutputSpacing( this->m_FixedImageSpacing );
m_MovingImageWarper->SetOutputDirection( this->m_FixedImageDirection );
m_MovingImageWarper->SetInput( this->GetMovingImage() );
m_MovingImageWarper->SetDeformationField( this->GetDeformationField() );
m_MovingImageWarper->GetOutput()->SetRequestedRegion( this->GetDeformationField()->GetRequestedRegion() );
m_MovingImageWarper->Update();
// setup moving image interpolator for further access
m_MovingImageInterpolator->SetInputImage( this->GetMovingImage() );
// initialize metric computation variables
m_SumOfSquaredDifference = 0.0;
m_NumberOfPixelsProcessed = 0L;
m_SumOfSquaredChange = 0.0;
}
/**
* Compute update at a non boundary neighbourhood
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
typename ESMDemonsRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::PixelType
ESMDemonsRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::ComputeUpdate(const NeighborhoodType &it, void * gd,
const FloatOffsetType& itkNotUsed(offset))
{
GlobalDataStruct *globalData = (GlobalDataStruct *)gd;
PixelType update;
IndexType FirstIndex = this->GetFixedImage()->GetLargestPossibleRegion().GetIndex();
IndexType LastIndex = this->GetFixedImage()->GetLargestPossibleRegion().GetIndex() +
this->GetFixedImage()->GetLargestPossibleRegion().GetSize();
const IndexType index = it.GetIndex();
// Get fixed image related information
// Note: no need to check if the index is within
// fixed image buffer. This is done by the external filter.
const double fixedValue = static_cast<double>(
this->GetFixedImage()->GetPixel( index ) );
// Get moving image related information
// check if the point was mapped outside of the moving image using
// the "special value" NumericTraits<MovingPixelType>::max()
MovingPixelType movingPixValue
= m_MovingImageWarper->GetOutput()->GetPixel( index );
if( movingPixValue == NumericTraits <MovingPixelType>::max() )
{
update.Fill( 0.0 );
return update;
}
const double movingValue = static_cast<double>( movingPixValue );
// We compute the gradient more or less by hand.
// We first start by ignoring the image orientation and introduce it afterwards
CovariantVectorType usedOrientFreeGradientTimes2;
if( (this->m_UseGradientType==Symmetric) ||
(this->m_UseGradientType==WarpedMoving) )
{
// we don't use a CentralDifferenceImageFunction here to be able to
// check for NumericTraits<MovingPixelType>::max()
CovariantVectorType warpedMovingGradient;
IndexType tmpIndex = index;
for( unsigned int dim = 0; dim < ImageDimension; dim++ )
{
// bounds checking
if( FirstIndex[dim]==LastIndex[dim]
|| index[dim]<FirstIndex[dim]
|| index[dim]>=LastIndex[dim] )
{
warpedMovingGradient[dim] = 0.0;
continue;
}
else if ( index[dim] == FirstIndex[dim] )
{
// compute derivative
tmpIndex[dim] += 1;
movingPixValue = m_MovingImageWarper->GetOutput()->GetPixel( tmpIndex );
if( movingPixValue == NumericTraits <MovingPixelType>::max() )
{
// weird crunched border case
warpedMovingGradient[dim] = 0.0;
}
else
{
// forward difference
warpedMovingGradient[dim] = static_cast<double>( movingPixValue ) - movingValue;
warpedMovingGradient[dim] /= m_FixedImageSpacing[dim];
}
tmpIndex[dim] -= 1;
continue;
}
else if ( index[dim] == (LastIndex[dim]-1) )
{
// compute derivative
tmpIndex[dim] -= 1;
movingPixValue = m_MovingImageWarper->GetOutput()->GetPixel( tmpIndex );
if( movingPixValue == NumericTraits<MovingPixelType>::max() )
{
// weird crunched border case
warpedMovingGradient[dim] = 0.0;
}
else
{
// backward difference
warpedMovingGradient[dim] = movingValue - static_cast<double>( movingPixValue );
warpedMovingGradient[dim] /= m_FixedImageSpacing[dim];
}
tmpIndex[dim] += 1;
continue;
}
// compute derivative
tmpIndex[dim] += 1;
movingPixValue = m_MovingImageWarper->GetOutput()->GetPixel( tmpIndex );
if ( movingPixValue == NumericTraits
<MovingPixelType>::max() )
{
// backward difference
warpedMovingGradient[dim] = movingValue;
tmpIndex[dim] -= 2;
movingPixValue = m_MovingImageWarper->GetOutput()->GetPixel( tmpIndex );
if( movingPixValue == NumericTraits<MovingPixelType>::max() )
{
// weird crunched border case
warpedMovingGradient[dim] = 0.0;
}
else
{
// backward difference
warpedMovingGradient[dim] -= static_cast<double>(
m_MovingImageWarper->GetOutput()->GetPixel( tmpIndex ) );
warpedMovingGradient[dim] /= m_FixedImageSpacing[dim];
}
}
else
{
warpedMovingGradient[dim] = static_cast<double>( movingPixValue );
tmpIndex[dim] -= 2;
movingPixValue = m_MovingImageWarper->GetOutput()->GetPixel( tmpIndex );
if ( movingPixValue == NumericTraits<MovingPixelType>::max() )
{
// forward difference
warpedMovingGradient[dim] -= movingValue;
warpedMovingGradient[dim] /= m_FixedImageSpacing[dim];
}
else
{
// normal case, central difference
warpedMovingGradient[dim] -= static_cast<double>( movingPixValue );
warpedMovingGradient[dim] *= 0.5 / m_FixedImageSpacing[dim];
}
}
tmpIndex[dim] += 1;
}
if( this->m_UseGradientType == Symmetric )
{
// Compute orientation-free gradient with calculator
const CovariantVectorType fixedGradient
= m_FixedImageGradientCalculator->EvaluateAtIndex( index );
usedOrientFreeGradientTimes2 = fixedGradient + warpedMovingGradient;
}
else if (this->m_UseGradientType==WarpedMoving)
{
usedOrientFreeGradientTimes2 = warpedMovingGradient + warpedMovingGradient;
}
else
{
itkExceptionMacro(<<"Unknown gradient type");
}
}
else if (this->m_UseGradientType==Fixed)
{
// Compute orientation-free gradient with calculator
const CovariantVectorType fixedGradient
= m_FixedImageGradientCalculator->EvaluateAtIndex( index );
usedOrientFreeGradientTimes2 = fixedGradient + fixedGradient;
}
else if (this->m_UseGradientType==MappedMoving)
{
PointType mappedPoint;
this->GetFixedImage()->TransformIndexToPhysicalPoint(index, mappedPoint);
for( unsigned int j = 0; j < ImageDimension; j++ )
{
mappedPoint[j] += it.GetCenterPixel()[j];
}
const CovariantVectorType mappedMovingGradient
= m_MappedMovingImageGradientCalculator->Evaluate( mappedPoint );
usedOrientFreeGradientTimes2 = mappedMovingGradient + mappedMovingGradient;
}
else
{
itkExceptionMacro(<<"Unknown gradient type");
}
#ifdef ITK_USE_ORIENTED_IMAGE_DIRECTION
CovariantVectorType usedGradientTimes2;
this->GetFixedImage()->TransformLocalVectorToPhysicalVector(
usedOrientFreeGradientTimes2, usedGradientTimes2);
#else
CovariantVectorType usedGradientTimes2=usedOrientFreeGradientTimes2;
#endif
/**
* Compute Update.
* We avoid the mismatch in units between the two terms.
* and avoid large step using a normalization term.
*/
const double usedGradientTimes2SquaredMagnitude =
usedGradientTimes2.GetSquaredNorm();
const double speedValue = fixedValue - movingValue;
if ( vnl_math_abs(speedValue) < m_IntensityDifferenceThreshold )
{
update.Fill( 0.0 );
}
else
{
double denom;
if( m_Normalizer > 0.0 )
{
// "ITK-Thirion" normalization
denom = usedGradientTimes2SquaredMagnitude + (vnl_math_sqr(speedValue)/m_Normalizer);
}
else
{
// least square solution of the system
denom = usedGradientTimes2SquaredMagnitude;
}
if( denom < m_DenominatorThreshold )
{
update.Fill( 0.0 );
}
else
{
const double factor = 2.0 * speedValue / denom;
for( unsigned int j = 0; j < ImageDimension; j++ )
{
update[j] = factor * usedGradientTimes2[j];
}
}
}
// WARNING!! We compute the global data without taking into account the current update step.
// There are several reasons for that: If an exponential, a smoothing or any other operation
// is applied on the update field, we cannot compute the newMappedCenterPoint here; and even
// if we could, this would be an often unnecessary time-consuming task.
if ( globalData )
{
globalData->m_SumOfSquaredDifference += vnl_math_sqr( speedValue );
globalData->m_NumberOfPixelsProcessed += 1;
globalData->m_SumOfSquaredChange += update.GetSquaredNorm();
}
return update;
}
/**
* Update the metric and release the per-thread-global data.
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
ESMDemonsRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::ReleaseGlobalDataPointer( void *gd ) const
{
GlobalDataStruct * globalData = (GlobalDataStruct *) gd;
m_MetricCalculationLock.Lock();
m_SumOfSquaredDifference += globalData->m_SumOfSquaredDifference;
m_NumberOfPixelsProcessed += globalData->m_NumberOfPixelsProcessed;
m_SumOfSquaredChange += globalData->m_SumOfSquaredChange;
if( m_NumberOfPixelsProcessed )
{
m_Metric = m_SumOfSquaredDifference /
static_cast<double>( m_NumberOfPixelsProcessed );
m_RMSChange = vcl_sqrt( m_SumOfSquaredChange /
static_cast<double>( m_NumberOfPixelsProcessed ) );
}
m_MetricCalculationLock.Unlock();
delete globalData;
}
} // end namespace itk
#endif
|