/usr/include/ITK-4.5/itkMutualInformationImageToImageMetric.hxx is in libinsighttoolkit4-dev 4.5.0-3.
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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | /*=========================================================================
*
* 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 __itkMutualInformationImageToImageMetric_hxx
#define __itkMutualInformationImageToImageMetric_hxx
#include "itkMutualInformationImageToImageMetric.h"
#include "itkImageRandomConstIteratorWithIndex.h"
#include "vnl/vnl_math.h"
#include "itkGaussianKernelFunction.h"
#include "itkCompensatedSummation.h"
namespace itk
{
/**
* Constructor
*/
template <typename TFixedImage, typename TMovingImage>
MutualInformationImageToImageMetric<TFixedImage, TMovingImage>
::MutualInformationImageToImageMetric()
{
m_NumberOfSpatialSamples = 0;
this->SetNumberOfSpatialSamples(50);
m_KernelFunction = dynamic_cast<KernelFunctionType *>(
GaussianKernelFunction<double>::New().GetPointer() );
m_FixedImageStandardDeviation = 0.4;
m_MovingImageStandardDeviation = 0.4;
m_MinProbability = 0.0001;
//
// Following initialization is related to
// calculating image derivatives
this->SetComputeGradient(false); // don't use the default gradient for now
m_DerivativeCalculator = DerivativeFunctionType::New();
m_DerivativeCalculator->UseImageDirectionOn();
}
template <typename TFixedImage, typename TMovingImage>
void
MutualInformationImageToImageMetric<TFixedImage, TMovingImage>
::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "NumberOfSpatialSamples: ";
os << m_NumberOfSpatialSamples << std::endl;
os << indent << "FixedImageStandardDeviation: ";
os << m_FixedImageStandardDeviation << std::endl;
os << indent << "MovingImageStandardDeviation: ";
os << m_MovingImageStandardDeviation << std::endl;
os << indent << "KernelFunction: ";
os << m_KernelFunction.GetPointer() << std::endl;
}
/*
* Set the number of spatial samples
*/
template <typename TFixedImage, typename TMovingImage>
void
MutualInformationImageToImageMetric<TFixedImage, TMovingImage>
::SetNumberOfSpatialSamples(
unsigned int num)
{
if( num == m_NumberOfSpatialSamples )
{
return;
}
this->Modified();
// clamp to minimum of 1
m_NumberOfSpatialSamples = ( ( num > 1 ) ? num : 1 );
// resize the storage vectors
m_SampleA.resize(m_NumberOfSpatialSamples);
m_SampleB.resize(m_NumberOfSpatialSamples);
}
/*
* Uniformly sample the fixed image domain. Each sample consists of:
* - the fixed image value
* - the corresponding moving image value
*
* \warning Note that this method has a different signature than the one in
* the base OptImageToImageMetric and therefore they are not intended to
* provide polymorphism. That is, this function is not overriding the one in
* the base class.
*
*/
template <typename TFixedImage, typename TMovingImage>
void
MutualInformationImageToImageMetric<TFixedImage, TMovingImage>
::SampleFixedImageDomain(
SpatialSampleContainer & samples) const
{
typedef ImageRandomConstIteratorWithIndex<FixedImageType> RandomIterator;
RandomIterator randIter( this->m_FixedImage, this->GetFixedImageRegion() );
randIter.SetNumberOfSamples(m_NumberOfSpatialSamples);
randIter.GoToBegin();
typename SpatialSampleContainer::iterator iter;
typename SpatialSampleContainer::const_iterator end = samples.end();
bool allOutside = true;
this->m_NumberOfPixelsCounted = 0; // Number of pixels that map into the
// fixed and moving image mask, if
// specified
// and the resampled fixed grid after
// transformation.
// Number of random picks made from the portion of fixed image within the
// fixed mask
SizeValueType numberOfFixedImagePixelsVisited = 0;
SizeValueType dryRunTolerance = this->GetFixedImageRegion().GetNumberOfPixels();
for( iter = samples.begin(); iter != end; ++iter )
{
// Get sampled index
FixedImageIndexType index = randIter.GetIndex();
// Get sampled fixed image value
( *iter ).FixedImageValue = randIter.Get();
// Translate index to point
this->m_FixedImage->TransformIndexToPhysicalPoint(index,
( *iter ).FixedImagePointValue);
// If not inside the fixed mask, ignore the point
if( this->m_FixedImageMask
&& !this->m_FixedImageMask->IsInside( ( *iter ).FixedImagePointValue ) )
{
++randIter; // jump to another random position
continue;
}
if( allOutside )
{
++numberOfFixedImagePixelsVisited;
if( numberOfFixedImagePixelsVisited > dryRunTolerance )
{
// We randomly visited as many points as is the size of the fixed image
// region.. Too may samples mapped ouside.. go change your transform
itkExceptionMacro(<< "Too many samples mapped outside the moving buffer");
}
}
MovingImagePointType mappedPoint =
this->m_Transform->TransformPoint( ( *iter ).FixedImagePointValue );
// If the transformed point after transformation does not lie within the
// MovingImageMask, skip it.
if( this->m_MovingImageMask
&& !this->m_MovingImageMask->IsInside(mappedPoint) )
{
++randIter;
continue;
}
// The interpolator does not need to do bounds checking if we have masks,
// since we know that the point is within the fixed and moving masks. But
// a crazy user can specify masks that are bigger than the image. Then we
// will need bounds checking.. So keep this anyway.
if( this->m_Interpolator->IsInsideBuffer(mappedPoint) )
{
( *iter ).MovingImageValue = this->m_Interpolator->Evaluate(mappedPoint);
this->m_NumberOfPixelsCounted++;
allOutside = false;
}
else
{
( *iter ).MovingImageValue = 0;
}
// Jump to random position
++randIter;
}
if( allOutside )
{
// if all the samples mapped to the outside throw an exception
itkExceptionMacro(<< "All the sampled point mapped to outside of the moving image");
}
}
/*
* Get the match Measure
*/
template <typename TFixedImage, typename TMovingImage>
typename MutualInformationImageToImageMetric<TFixedImage, TMovingImage>
::MeasureType
MutualInformationImageToImageMetric<TFixedImage, TMovingImage>
::GetValue(const ParametersType & parameters) const
{
// make sure the transform has the current parameters
this->m_Transform->SetParameters(parameters);
// collect sample set A
this->SampleFixedImageDomain(m_SampleA);
// collect sample set B
this->SampleFixedImageDomain(m_SampleB);
// calculate the mutual information
typedef CompensatedSummation< double > SumType;
SumType dLogSumFixed;
SumType dLogSumMoving;
SumType dLogSumJoint;
SumType dSumFixed;
SumType dSumMoving;
SumType dSumJoint;
typename SpatialSampleContainer::const_iterator aiter;
typename SpatialSampleContainer::const_iterator aend = m_SampleA.end();
typename SpatialSampleContainer::const_iterator biter;
typename SpatialSampleContainer::const_iterator bend = m_SampleB.end();
for( biter = m_SampleB.begin(); biter != bend; ++biter )
{
dSumFixed.ResetToZero();
dSumMoving.ResetToZero();
dSumJoint.ResetToZero();
dSumFixed += m_MinProbability;
dSumMoving += m_MinProbability;
dSumJoint += m_MinProbability;
for( aiter = m_SampleA.begin(); aiter != aend; ++aiter )
{
double valueFixed;
double valueMoving;
valueFixed = ( ( *biter ).FixedImageValue - ( *aiter ).FixedImageValue )
/ m_FixedImageStandardDeviation;
valueFixed = m_KernelFunction->Evaluate(valueFixed);
valueMoving = ( ( *biter ).MovingImageValue - ( *aiter ).MovingImageValue )
/ m_MovingImageStandardDeviation;
valueMoving = m_KernelFunction->Evaluate(valueMoving);
dSumFixed += valueFixed;
dSumMoving += valueMoving;
dSumJoint += valueFixed * valueMoving;
} // end of sample A loop
if( dSumFixed.GetSum() > 0.0 )
{
dLogSumFixed -= vcl_log( dSumFixed.GetSum() );
}
if( dSumMoving.GetSum() > 0.0 )
{
dLogSumMoving -= vcl_log( dSumMoving.GetSum() );
}
if( dSumJoint.GetSum() > 0.0 )
{
dLogSumJoint -= vcl_log( dSumJoint.GetSum() );
}
} // end of sample B loop
double nsamp = double(m_NumberOfSpatialSamples);
double threshold = -0.5 *nsamp *vcl_log(m_MinProbability);
if( dLogSumMoving.GetSum() > threshold || dLogSumFixed.GetSum() > threshold
|| dLogSumJoint.GetSum() > threshold )
{
// at least half the samples in B did not occur within
// the Parzen window width of samples in A
itkExceptionMacro(<< "Standard deviation is too small");
}
MeasureType measure = dLogSumFixed.GetSum() + dLogSumMoving.GetSum() - dLogSumJoint.GetSum();
measure /= nsamp;
measure += vcl_log(nsamp);
return measure;
}
/*
* Get the both Value and Derivative Measure
*/
template <typename TFixedImage, typename TMovingImage>
void
MutualInformationImageToImageMetric<TFixedImage, TMovingImage>
::GetValueAndDerivative(
const ParametersType & parameters,
MeasureType & value,
DerivativeType & derivative) const
{
value = NumericTraits<MeasureType>::Zero;
unsigned int numberOfParameters = this->m_Transform->GetNumberOfParameters();
DerivativeType temp(numberOfParameters);
temp.Fill(0);
derivative = temp;
// make sure the transform has the current parameters
this->m_Transform->SetParameters(parameters);
// set the DerivativeCalculator
m_DerivativeCalculator->SetInputImage(this->m_MovingImage);
// collect sample set A
this->SampleFixedImageDomain(m_SampleA);
// collect sample set B
this->SampleFixedImageDomain(m_SampleB);
// calculate the mutual information
typedef CompensatedSummation< double > SumType;
SumType dLogSumFixed;
SumType dLogSumMoving;
SumType dLogSumJoint;
SumType dSumFixed;
SumType dDenominatorMoving;
SumType dDenominatorJoint;
typename SpatialSampleContainer::iterator aiter;
typename SpatialSampleContainer::const_iterator aend = m_SampleA.end();
typename SpatialSampleContainer::iterator biter;
typename SpatialSampleContainer::const_iterator bend = m_SampleB.end();
// precalculate all the image derivatives for sample A
typedef std::vector<DerivativeType> DerivativeContainer;
DerivativeContainer sampleADerivatives;
sampleADerivatives.resize(m_NumberOfSpatialSamples);
typename DerivativeContainer::iterator aditer;
DerivativeType tempDeriv(numberOfParameters);
TransformJacobianType jacobian(numberOfParameters, numberOfParameters);
for( aiter = m_SampleA.begin(), aditer = sampleADerivatives.begin();
aiter != aend; ++aiter, ++aditer )
{
/** FIXME: is there a way to avoid the extra copying step? */
this->CalculateDerivatives( ( *aiter ).FixedImagePointValue, tempDeriv, jacobian );
( *aditer ) = tempDeriv;
}
DerivativeType derivB(numberOfParameters);
for( biter = m_SampleB.begin(); biter != bend; ++biter )
{
dDenominatorMoving.ResetToZero();
dDenominatorMoving += m_MinProbability;
dDenominatorJoint.ResetToZero();
dDenominatorJoint += m_MinProbability;
dSumFixed.ResetToZero();
dSumFixed += m_MinProbability;
for( aiter = m_SampleA.begin(); aiter != aend; ++aiter )
{
double valueFixed;
double valueMoving;
valueFixed = ( ( *biter ).FixedImageValue - ( *aiter ).FixedImageValue )
/ m_FixedImageStandardDeviation;
valueFixed = m_KernelFunction->Evaluate(valueFixed);
valueMoving = ( ( *biter ).MovingImageValue - ( *aiter ).MovingImageValue )
/ m_MovingImageStandardDeviation;
valueMoving = m_KernelFunction->Evaluate(valueMoving);
dDenominatorMoving += valueMoving;
dDenominatorJoint += valueMoving * valueFixed;
dSumFixed += valueFixed;
} // end of sample A loop
if( dSumFixed.GetSum() > 0.0 )
{
dLogSumFixed -= vcl_log( dSumFixed.GetSum() );
}
if( dDenominatorMoving.GetSum() > 0.0 )
{
dLogSumMoving -= vcl_log( dDenominatorMoving.GetSum() );
}
if( dDenominatorJoint.GetSum() > 0.0 )
{
dLogSumJoint -= vcl_log( dDenominatorJoint.GetSum() );
}
/** get the image derivative for this B sample */
this->CalculateDerivatives( ( *biter ).FixedImagePointValue, derivB, jacobian );
SumType totalWeight;
for( aiter = m_SampleA.begin(), aditer = sampleADerivatives.begin();
aiter != aend; ++aiter, ++aditer )
{
double valueFixed;
double valueMoving;
double weightMoving;
double weightJoint;
double weight;
valueFixed = ( ( *biter ).FixedImageValue - ( *aiter ).FixedImageValue )
/ m_FixedImageStandardDeviation;
valueFixed = m_KernelFunction->Evaluate(valueFixed);
valueMoving = ( ( *biter ).MovingImageValue - ( *aiter ).MovingImageValue )
/ m_MovingImageStandardDeviation;
valueMoving = m_KernelFunction->Evaluate(valueMoving);
weightMoving = valueMoving / dDenominatorMoving.GetSum();
weightJoint = valueMoving * valueFixed / dDenominatorJoint.GetSum();
weight = ( weightMoving - weightJoint );
weight *= ( *biter ).MovingImageValue - ( *aiter ).MovingImageValue;
totalWeight += weight;
derivative -= ( *aditer ) * weight;
} // end of sample A loop
derivative += derivB * totalWeight.GetSum();
} // end of sample B loop
double nsamp = double(m_NumberOfSpatialSamples);
double threshold = -0.5 *nsamp *vcl_log(m_MinProbability);
if( dLogSumMoving.GetSum() > threshold || dLogSumFixed.GetSum() > threshold
|| dLogSumJoint.GetSum() > threshold )
{
// at least half the samples in B did not occur within
// the Parzen window width of samples in A
itkExceptionMacro(<< "Standard deviation is too small");
}
value = dLogSumFixed.GetSum() + dLogSumMoving.GetSum() - dLogSumJoint.GetSum();
value /= nsamp;
value += vcl_log(nsamp);
derivative /= nsamp;
derivative /= vnl_math_sqr(m_MovingImageStandardDeviation);
}
/*
* Get the match measure derivative
*/
template <typename TFixedImage, typename TMovingImage>
void
MutualInformationImageToImageMetric<TFixedImage, TMovingImage>
::GetDerivative(const ParametersType & parameters, DerivativeType & derivative) const
{
MeasureType value;
// call the combined version
this->GetValueAndDerivative(parameters, value, derivative);
}
/*
* Calculate derivatives of the image intensity with respect
* to the transform parmeters.
*
* This should really be done by the mapper.
*
* This is a temporary solution until this feature is implemented
* in the mapper. This solution only works for any transform
* that support ComputeJacobianWithRespectToParameters()
*/
template <typename TFixedImage, typename TMovingImage>
void
MutualInformationImageToImageMetric<TFixedImage, TMovingImage>
::CalculateDerivatives(
const FixedImagePointType & point,
DerivativeType & derivatives,
TransformJacobianType &jacobian) const
{
MovingImagePointType mappedPoint = this->m_Transform->TransformPoint(point);
CovariantVector<double, MovingImageDimension> imageDerivatives;
if( m_DerivativeCalculator->IsInsideBuffer(mappedPoint) )
{
imageDerivatives = m_DerivativeCalculator->Evaluate(mappedPoint);
}
else
{
derivatives.Fill(0.0);
return;
}
typedef typename TransformType::JacobianType JacobianType;
this->m_Transform->ComputeJacobianWithRespectToParameters(point, jacobian);
unsigned int numberOfParameters = this->m_Transform->GetNumberOfParameters();
for( unsigned int k = 0; k < numberOfParameters; k++ )
{
derivatives[k] = 0.0;
for( unsigned int j = 0; j < MovingImageDimension; j++ )
{
derivatives[k] += jacobian[j][k] * imageDerivatives[j];
}
}
}
/*
* Reinitialize the seed of the random number generator
*/
template <typename TFixedImage, typename TMovingImage>
void
MutualInformationImageToImageMetric<TFixedImage, TMovingImage>
::ReinitializeSeed()
{
Statistics::MersenneTwisterRandomVariateGenerator::GetInstance()->SetSeed();
}
/*
* Reinitialize the seed of the random number generator
*/
template <typename TFixedImage, typename TMovingImage>
void
MutualInformationImageToImageMetric<TFixedImage, TMovingImage>
::ReinitializeSeed(int seed)
{
Statistics::MersenneTwisterRandomVariateGenerator::GetInstance()->SetSeed(seed);
}
} // end namespace itk
#endif
|