/usr/include/InsightToolkit/Algorithms/itkLevelSetMotionRegistrationFunction.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 | /*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkLevelSetMotionRegistrationFunction.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 __itkLevelSetMotionRegistrationFunction_txx
#define __itkLevelSetMotionRegistrationFunction_txx
#include "itkLevelSetMotionRegistrationFunction.h"
#include "itkExceptionObject.h"
#include "vnl/vnl_math.h"
namespace itk {
/**
* Default constructor
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::LevelSetMotionRegistrationFunction()
{
RadiusType r;
unsigned int j;
for( j = 0; j < ImageDimension; j++ )
{
r[j] = 0;
}
this->SetRadius(r);
m_Alpha = 0.1;
m_GradientMagnitudeThreshold = 1e-9;
m_IntensityDifferenceThreshold = 0.001;
m_GradientSmoothingStandardDeviations = 1.0;
this->SetMovingImage(NULL);
this->SetFixedImage(NULL);
typename DefaultInterpolatorType::Pointer interp =
DefaultInterpolatorType::New();
m_MovingImageInterpolator = static_cast<InterpolatorType*>(
interp.GetPointer() );
m_Metric = NumericTraits<double>::max();
m_SumOfSquaredDifference = 0.0;
m_NumberOfPixelsProcessed = 0L;
m_RMSChange = NumericTraits<double>::max();
m_SumOfSquaredChange = 0.0;
m_UseImageSpacing = true;
m_MovingImageSmoothingFilter = MovingImageSmoothingFilterType::New();
m_MovingImageSmoothingFilter
->SetSigma( m_GradientSmoothingStandardDeviations );
m_MovingImageSmoothingFilter->SetNormalizeAcrossScale(false);
m_SmoothMovingImageInterpolator
= static_cast<InterpolatorType *>(
DefaultInterpolatorType::New().GetPointer());
}
/*
* Standard "PrintSelf" method.
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "MovingImageIterpolator: ";
os << m_MovingImageInterpolator.GetPointer() << std::endl;
os << indent << "IntensityDifferenceThreshold: ";
os << m_IntensityDifferenceThreshold << std::endl;
os << indent << "GradientMagnitudeThreshold: ";
os << m_GradientMagnitudeThreshold << std::endl;
os << indent << "Alpha: ";
os << m_Alpha << 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
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::SetAlpha(double alpha)
{
m_Alpha = alpha;
}
/**
*
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
double
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::GetAlpha() const
{
return m_Alpha;
}
/**
*
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::SetIntensityDifferenceThreshold(double threshold)
{
m_IntensityDifferenceThreshold = threshold;
}
/**
*
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
double
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::GetIntensityDifferenceThreshold() const
{
return m_IntensityDifferenceThreshold;
}
/**
*
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::SetGradientMagnitudeThreshold(double threshold)
{
m_GradientMagnitudeThreshold = threshold;
}
/**
*
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
double
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::GetGradientMagnitudeThreshold() const
{
return m_GradientMagnitudeThreshold;
}
/**
*
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::SetGradientSmoothingStandardDeviations(double sigma)
{
m_GradientSmoothingStandardDeviations = sigma;
}
/**
*
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
double
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::GetGradientSmoothingStandardDeviations() const
{
return m_GradientSmoothingStandardDeviations;
}
/**
* Return the flag that defines whether the image spacing should be taken into
* account in computations.
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
bool
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::GetUseImageSpacing() const
{
return this->m_UseImageSpacing;
}
/**
* Set the flag that defines whether the image spacing should be taken into
* account in computations.
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::SetUseImageSpacing( bool useImageSpacing )
{
this->m_UseImageSpacing = useImageSpacing;
}
/**
* Set the function state values before each iteration
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::InitializeIteration()
{
if( !this->GetMovingImage() || !this->GetFixedImage() || !m_MovingImageInterpolator )
{
itkExceptionMacro( << "MovingImage, FixedImage and/or Interpolator not set" );
}
// create a smoothed version of the moving image for the calculation
// of gradients. due to the pipeline structure, this will only be
// calculated once. InitializeIteration() is called in a single
// threaded execution model.
m_MovingImageSmoothingFilter->SetInput( this->GetMovingImage() );
m_MovingImageSmoothingFilter
->SetSigma( m_GradientSmoothingStandardDeviations );
m_MovingImageSmoothingFilter->Update();
m_SmoothMovingImageInterpolator
->SetInputImage( m_MovingImageSmoothingFilter->GetOutput() );
// setup moving image interpolator
m_MovingImageInterpolator->SetInputImage( this->GetMovingImage() );
// initialize metric computation variables
m_SumOfSquaredDifference = 0.0;
m_NumberOfPixelsProcessed = 0L;
m_SumOfSquaredChange = 0.0;
}
/**
* Compute update at a specify neighbourhood
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
typename LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::PixelType
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::ComputeUpdate(const NeighborhoodType &it, void * gd,
const FloatOffsetType& itkNotUsed(offset))
{
const IndexType index = it.GetIndex();
// Get fixed image related information
// Note: no need to check the index is within
// fixed image buffer. This is done by the external filter.
const double fixedValue = (double) this->GetFixedImage()->GetPixel( index );
// Get moving image related information
PointType mappedPoint;
this->GetFixedImage()->TransformIndexToPhysicalPoint(index, mappedPoint);
for(unsigned int j = 0; j < ImageDimension; j++ )
{
mappedPoint[j] += it.GetCenterPixel()[j];
}
PixelType update;
double movingValue;
if( m_MovingImageInterpolator->IsInsideBuffer( mappedPoint ) )
{
movingValue = m_MovingImageInterpolator->Evaluate( mappedPoint );
}
else
{
update.Fill(0.0);
return update;
}
// Calculate the gradient using minmod finite differences
//
//
//
// first calculate the forward and backward differences on the
// smooth image. Do we need to structure the gradient calculation to
// take into account the Jacobian of the deformation field? i.e. in
// which coordinate frame do we ultimately want the gradient vector?
MovingSpacingType mSpacing = this->GetMovingImage()->GetSpacing();
if( !this->m_UseImageSpacing )
{
mSpacing.Fill( 1.0 );
}
PointType mPoint( mappedPoint );
const double centralValue = m_SmoothMovingImageInterpolator->Evaluate( mPoint );
double forwardDifferences[ImageDimension];
double backwardDifferences[ImageDimension];
for (unsigned int j=0; j < ImageDimension; j++)
{
mPoint[j] += mSpacing[j];
if( m_SmoothMovingImageInterpolator->IsInsideBuffer( mPoint ) )
{
forwardDifferences[j] = m_SmoothMovingImageInterpolator->Evaluate(mPoint)
- centralValue;
forwardDifferences[j] /= mSpacing[j];
}
else
{
forwardDifferences[j] = 0.0;
}
mPoint[j] -= (2.0 * mSpacing[j]);
if( m_SmoothMovingImageInterpolator->IsInsideBuffer( mPoint ) )
{
backwardDifferences[j] = centralValue
- m_SmoothMovingImageInterpolator->Evaluate( mPoint );
backwardDifferences[j] /= mSpacing[j];
}
else
{
backwardDifferences[j] = 0.0;
}
// std::cout << "F(" << j << ") : " << forwardDifferences[j] << std::endl;
// std::cout << "B(" << j << ") : " << backwardDifferences[j] << std::endl;
mPoint[j] += mSpacing[j];
}
// minmod finite difference
//
// m(x,y) = sign(x) min(|x|, |y|) if xy > 0
// 0 if xy <= 0
//
// gradient[j] = m(forwardDifferences[j], backwardDifferences[j])
//
CovariantVectorType gradient;
double gradientMagnitude = 0.0;
for(unsigned int j = 0; j < ImageDimension; j++ )
{
if (forwardDifferences[j] * backwardDifferences[j] > 0.0)
{
const double bvalue = vnl_math_abs(backwardDifferences[j]);
double gvalue = vnl_math_abs(forwardDifferences[j]);
if (gvalue > bvalue)
{
gvalue = bvalue;
}
gradient[j] = gvalue * vnl_math_sgn(forwardDifferences[j]);
}
else
{
gradient[j] = 0.0;
}
gradientMagnitude += vnl_math_sqr( gradient[j] );
}
gradientMagnitude = vcl_sqrt( gradientMagnitude );
/**
* Compute Update.
*/
const double speedValue = fixedValue - movingValue;
// update the metric
GlobalDataStruct *globalData = (GlobalDataStruct *)gd;
if ( globalData )
{
globalData->m_SumOfSquaredDifference += vnl_math_sqr( speedValue );
globalData->m_NumberOfPixelsProcessed += 1;
}
if ( vnl_math_abs(speedValue) < m_IntensityDifferenceThreshold
|| gradientMagnitude < m_GradientMagnitudeThreshold )
{
update.Fill(0.0);
return update;
}
double L1norm = 0.0;
for(unsigned int j = 0; j < ImageDimension; j++ )
{
update[j] = speedValue * gradient[j] / (gradientMagnitude + m_Alpha);
if ( globalData )
{
globalData->m_SumOfSquaredChange += vnl_math_sqr( update[j] );
// build up the L1norm of the update, normalized by the pixel
// spacing. we will use this to calculate a timestep which
// converts the update (measured in intensity) to a vector
// measured in physical units (mm).
L1norm += (vnl_math_abs(update[j]) / mSpacing[j]);
}
}
// Store the L1 norm of the update vector if it is the largest
// update. This is used in calculating the timestep.
if (globalData && (L1norm > globalData->m_MaxL1Norm))
{
globalData->m_MaxL1Norm = L1norm;
}
return update;
}
/**
* Compute the global time step for this iteration.
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
typename LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>::TimeStepType
LevelSetMotionRegistrationFunction<TFixedImage,TMovingImage,TDeformationField>
::ComputeGlobalTimeStep(void *GlobalData) const
{
TimeStepType dt = 1.0;
GlobalDataStruct *d = (GlobalDataStruct *)GlobalData;
if (d->m_MaxL1Norm > 0.0)
{
dt = 1.0 / d->m_MaxL1Norm;
// std::cout << "Computed timestep: " << dt << std::endl;
}
else
{
// std::cout << "Using default timestep: " << dt << std::endl;
}
return dt;
}
/*
* Update the metric and release the per-thread-global data.
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
LevelSetMotionRegistrationFunction<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
|