/usr/include/ITK-4.5/itkJointHistogramMutualInformationImageToImageMetricv4.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 | /*=========================================================================
*
* 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 __itkJointHistogramMutualInformationImageToImageMetricv4_hxx
#define __itkJointHistogramMutualInformationImageToImageMetricv4_hxx
#include "itkCompensatedSummation.h"
#include "itkJointHistogramMutualInformationImageToImageMetricv4.h"
#include "itkImageRandomConstIteratorWithIndex.h"
#include "itkImageIterator.h"
#include "itkDiscreteGaussianImageFilter.h"
namespace itk
{
template <typename TFixedImage, typename TMovingImage, typename TVirtualImage, typename TInternalComputationValueType, typename TMetricTraits>
JointHistogramMutualInformationImageToImageMetricv4<TFixedImage,TMovingImage,TVirtualImage,TInternalComputationValueType, TMetricTraits>
::JointHistogramMutualInformationImageToImageMetricv4()
{
// Initialize histogram properties
this->m_NumberOfHistogramBins = 20;
this->m_FixedImageTrueMin = NumericTraits< TInternalComputationValueType >::Zero;
this->m_FixedImageTrueMax = NumericTraits< TInternalComputationValueType >::Zero;
this->m_MovingImageTrueMin = NumericTraits< TInternalComputationValueType >::Zero;
this->m_MovingImageTrueMax = NumericTraits< TInternalComputationValueType >::Zero;
this->m_FixedImageBinSize = NumericTraits< TInternalComputationValueType >::Zero;
this->m_MovingImageBinSize = NumericTraits< TInternalComputationValueType >::Zero;
this->m_Padding = 2;
this->m_JointPDFSum = NumericTraits< TInternalComputationValueType >::Zero;
this->m_Log2 = vcl_log(2.0);
this->m_VarianceForJointPDFSmoothing = 1.5;
// We have our own GetValueAndDerivativeThreader's that we want
// ImageToImageMetricv4 to use.
this->m_DenseGetValueAndDerivativeThreader = JointHistogramMutualInformationDenseGetValueAndDerivativeThreaderType::New();
this->m_SparseGetValueAndDerivativeThreader = JointHistogramMutualInformationSparseGetValueAndDerivativeThreaderType::New();
this->m_JointHistogramMutualInformationDenseComputeJointPDFThreader = JointHistogramMutualInformationDenseComputeJointPDFThreaderType::New();
this->m_JointHistogramMutualInformationSparseComputeJointPDFThreader = JointHistogramMutualInformationSparseComputeJointPDFThreaderType::New();
this->m_JointPDF = JointPDFType::New();
}
template <typename TFixedImage, typename TMovingImage, typename TVirtualImage, typename TInternalComputationValueType, typename TMetricTraits>
JointHistogramMutualInformationImageToImageMetricv4<TFixedImage,TMovingImage,TVirtualImage,TInternalComputationValueType, TMetricTraits>
::~JointHistogramMutualInformationImageToImageMetricv4()
{
}
template <typename TFixedImage, typename TMovingImage, typename TVirtualImage, typename TInternalComputationValueType, typename TMetricTraits>
void
JointHistogramMutualInformationImageToImageMetricv4<TFixedImage,TMovingImage,TVirtualImage,TInternalComputationValueType, TMetricTraits>
::Initialize() throw (itk::ExceptionObject)
{
Superclass::Initialize();
/** Get the fixed and moving image true max's and mins.
* Initialize them to the PixelType min and max. */
this->m_FixedImageTrueMin =
vcl_numeric_limits<typename TFixedImage::PixelType>::max();
this->m_FixedImageTrueMax =
vcl_numeric_limits<typename TFixedImage::PixelType>::min();
this->m_MovingImageTrueMin =
vcl_numeric_limits<typename TMovingImage::PixelType>::max();
this->m_MovingImageTrueMax =
vcl_numeric_limits<typename TMovingImage::PixelType>::min();
/** Iterate through the fixed image and set the true
* max and min for the fixed image. */
itk::ImageRegionConstIteratorWithIndex<TFixedImage>
fi(this->m_FixedImage,this->m_FixedImage->GetRequestedRegion());
/** \todo multi-thread me */
while( !fi.IsAtEnd() )
{
typename TFixedImage::PointType fixedSpacePhysicalPoint;
this->m_FixedImage->TransformIndexToPhysicalPoint(fi.GetIndex(), fixedSpacePhysicalPoint);
if ( this->m_FixedImageMask.IsNull() /* A null mask implies entire space is to be used.*/
|| this->m_FixedImageMask->IsInside(fixedSpacePhysicalPoint) )
{
const typename TFixedImage::PixelType currentValue = fi.Get();
// update the Fixed Image true min accordingly
if ( currentValue < this->m_FixedImageTrueMin )
{
this->m_FixedImageTrueMin = currentValue;
}
// update the Fixed Image true max accordingly
if ( currentValue > this->m_FixedImageTrueMax )
{
this->m_FixedImageTrueMax = currentValue;
}
}
++fi;
}
/** Iterate through the moving image and set the true
* max and min for the moving image. */
itk::ImageRegionConstIteratorWithIndex<TMovingImage>
mi(this->m_MovingImage,this->m_MovingImage->GetBufferedRegion());
while( !mi.IsAtEnd() )
{
typename TMovingImage::PointType movingSpacePhysicalPoint;
this->m_MovingImage->TransformIndexToPhysicalPoint
(mi.GetIndex(), movingSpacePhysicalPoint);
if ( this->m_MovingImageMask.IsNull() /* A null mask implies entire space is to be used.*/
|| this->m_MovingImageMask->IsInside(movingSpacePhysicalPoint) )
{
const typename TMovingImage::PixelType currentValue=mi.Get();
// update the Moving Image true min accordingly
if ( currentValue < this->m_MovingImageTrueMin )
{
this->m_MovingImageTrueMin = currentValue;
}
// update the Moving Image true max accordingly
if ( currentValue > this->m_MovingImageTrueMax )
{
this->m_MovingImageTrueMax = currentValue;
}
}
++mi;
}
itkDebugMacro(" FixedImageMin: " << this->m_FixedImageTrueMin
<< " FixedImageMax: "
<< this->m_FixedImageTrueMax << std::endl);
itkDebugMacro(" MovingImageMin: " << this->m_MovingImageTrueMin
<< " MovingImageMax: "
<< this->m_MovingImageTrueMax << std::endl);
// Allocate memory for the joint PDF.
// Instantiate a region, index, size
JointPDFRegionType jointPDFRegion;
JointPDFIndexType jointPDFIndex;
JointPDFSizeType jointPDFSize;
// the jointPDF is of size NumberOfBins x NumberOfBins
jointPDFSize.Fill(m_NumberOfHistogramBins);
jointPDFIndex.Fill(0);
jointPDFRegion.SetIndex(jointPDFIndex);
jointPDFRegion.SetSize(jointPDFSize);
// Set the regions and allocate
this->m_JointPDF->SetRegions(jointPDFRegion);
//By setting these values, the joint histogram physical locations will correspond to intensity values.
JointPDFSpacingType spacing;
spacing[0]=1/(TInternalComputationValueType)(this->m_NumberOfHistogramBins-(TInternalComputationValueType)this->m_Padding*2-1);
spacing[1]=spacing[0];
this->m_JointPDF->SetSpacing(spacing);
this->m_JointPDFSpacing=this->m_JointPDF->GetSpacing();
JointPDFPointType origin;
origin[0]=this->m_JointPDFSpacing[0]*(TInternalComputationValueType)this->m_Padding*(-1.0);
origin[1]=origin[0];
this->m_JointPDF->SetOrigin(origin);
this->m_JointPDF->Allocate();
// do the same thing for the marginal pdfs
this->m_FixedImageMarginalPDF = MarginalPDFType::New();
this->m_MovingImageMarginalPDF = MarginalPDFType::New();
// Instantiate a region, index, size
typedef typename MarginalPDFType::RegionType MarginalPDFRegionType;
typedef typename MarginalPDFType::SizeType MarginalPDFSizeType;
MarginalPDFRegionType marginalPDFRegion;
MarginalPDFIndexType marginalPDFIndex;
MarginalPDFSizeType marginalPDFSize;
// the marginalPDF is of size NumberOfBins x NumberOfBins
marginalPDFSize.Fill(m_NumberOfHistogramBins);
marginalPDFIndex.Fill(0);
marginalPDFRegion.SetIndex(marginalPDFIndex);
marginalPDFRegion.SetSize(marginalPDFSize);
// Set the regions and allocate
this->m_FixedImageMarginalPDF->SetRegions(marginalPDFRegion);
this->m_MovingImageMarginalPDF->SetRegions(marginalPDFRegion);
//By setting these values, the marginal histogram physical locations will correspond to intensity values.
typename MarginalPDFType::PointType fixedorigin;
typename MarginalPDFType::PointType movingorigin;
fixedorigin[0]=origin[0];
movingorigin[0]=origin[1];
this->m_FixedImageMarginalPDF->SetOrigin(fixedorigin);
this->m_MovingImageMarginalPDF->SetOrigin(movingorigin);
typename MarginalPDFType::SpacingType mspacing;
mspacing[0]=spacing[0];
this->m_FixedImageMarginalPDF->SetSpacing(mspacing);
mspacing[0]=spacing[1];
this->m_MovingImageMarginalPDF->SetSpacing(mspacing);
this->m_FixedImageMarginalPDF->Allocate();
this->m_MovingImageMarginalPDF->Allocate();
}
template <typename TFixedImage, typename TMovingImage, typename TVirtualImage, typename TInternalComputationValueType, typename TMetricTraits>
void
JointHistogramMutualInformationImageToImageMetricv4<TFixedImage,TMovingImage,TVirtualImage,TInternalComputationValueType, TMetricTraits>
::InitializeForIteration() const
{
Superclass::InitializeForIteration();
/* Prepare histograms for use in GetValueAndDerivative */
// Initialize the joint pdf and the fixed and moving image marginal pdfs
PDFValueType pdfzero = NumericTraits< PDFValueType >::Zero;
this->m_JointPDF->FillBuffer(pdfzero);
this->m_FixedImageMarginalPDF->FillBuffer(pdfzero);
this->m_MovingImageMarginalPDF->FillBuffer(pdfzero);
/**
* First, we compute the joint histogram
*/
if( this->m_UseFixedSampledPointSet )
{
SizeValueType numberOfPoints = this->GetNumberOfDomainPoints();
if( numberOfPoints < 1 )
{
itkExceptionMacro("VirtualSampledPointSet must have 1 or more points.");
}
typename JointHistogramMutualInformationSparseComputeJointPDFThreaderType::DomainType sampledRange;
sampledRange[0] = 0;
sampledRange[1] = numberOfPoints - 1;
this->m_JointHistogramMutualInformationSparseComputeJointPDFThreader->Execute( const_cast<Self *>(this), sampledRange );
}
else
{
this->m_JointHistogramMutualInformationDenseComputeJointPDFThreader->Execute( const_cast<Self *>(this), this->GetVirtualRegion() );
}
// Optionally smooth the joint pdf
if (this->m_VarianceForJointPDFSmoothing > NumericTraits< JointPDFValueType >::Zero )
{
typedef DiscreteGaussianImageFilter<JointPDFType,JointPDFType> DgType;
typename DgType::Pointer dg = DgType::New();
dg->SetInput(this->m_JointPDF);
dg->SetVariance(this->m_VarianceForJointPDFSmoothing);
dg->SetUseImageSpacingOff();
dg->SetMaximumError(.01f);
dg->Update();
this->m_JointPDF = ( dg->GetOutput() );
}
// Compute moving image marginal PDF by summing over fixed image bins.
typedef ImageLinearIteratorWithIndex<JointPDFType> JointPDFLinearIterator;
JointPDFLinearIterator linearIter(m_JointPDF, m_JointPDF->GetBufferedRegion() );
linearIter.SetDirection( 0 );
linearIter.GoToBegin();
unsigned int fixedIndex = 0;
CompensatedSummation< TInternalComputationValueType > sum;
while( !linearIter.IsAtEnd() )
{
sum.ResetToZero();
while( !linearIter.IsAtEndOfLine() )
{
sum += linearIter.Get();
++linearIter;
}
MarginalPDFIndexType mind;
mind[0] = fixedIndex;
m_FixedImageMarginalPDF->SetPixel(mind,static_cast<PDFValueType>(sum.GetSum()));
linearIter.NextLine();
++fixedIndex;
}
linearIter.SetDirection( 1 );
linearIter.GoToBegin();
unsigned int movingIndex = 0;
while( !linearIter.IsAtEnd() )
{
sum.ResetToZero();
while( !linearIter.IsAtEndOfLine() )
{
sum += linearIter.Get();
++linearIter;
}
MarginalPDFIndexType mind;
mind[0] = movingIndex;
m_MovingImageMarginalPDF->SetPixel(mind,static_cast<PDFValueType>(sum.GetSum()));
linearIter.NextLine();
++movingIndex;
}
}
template <typename TFixedImage, typename TMovingImage, typename TVirtualImage, typename TInternalComputationValueType, typename TMetricTraits>
typename JointHistogramMutualInformationImageToImageMetricv4<TFixedImage,TMovingImage,TVirtualImage,TInternalComputationValueType, TMetricTraits>::MeasureType
JointHistogramMutualInformationImageToImageMetricv4<TFixedImage,TMovingImage,TVirtualImage,TInternalComputationValueType, TMetricTraits>
::GetValue() const
{
DerivativeType dummyDeriviative;
this->m_DerivativeResult = &dummyDeriviative;
this->InitializeForIteration();
this->m_NumberOfValidPoints = this->m_JointHistogramTotalCount;
MeasureType value;
/* This checks for the default minimum number of valid points.
* It returns true if minimum is met, otherwise is put default
* values into value and dummyDerivative and returns false. */
if( this->VerifyNumberOfValidPoints( value, dummyDeriviative ) )
{
this->m_Value = this->ComputeValue();
}
return this->m_Value;
}
template <typename TFixedImage, typename TMovingImage, typename TVirtualImage, typename TInternalComputationValueType, typename TMetricTraits>
typename JointHistogramMutualInformationImageToImageMetricv4<TFixedImage,TMovingImage,TVirtualImage,TInternalComputationValueType, TMetricTraits>::MeasureType
JointHistogramMutualInformationImageToImageMetricv4<TFixedImage,TMovingImage,TVirtualImage,TInternalComputationValueType, TMetricTraits>
::ComputeValue() const
{
/**
1- The padding is 2 in this implementation.
2- The MI energy is bounded in the range of [0 min(H(x),H(y))].
3- The ComputeMutualInformation() iterator range should cover the entire PDF.
4- The normalization is done based on NumberOfHistogramBins-1 instead of NumberOfHistogramBins. */
TInternalComputationValueType px,py,pxy;
CompensatedSummation< TInternalComputationValueType > total_mi;
TInternalComputationValueType local_mi;
TInternalComputationValueType eps = NumericTraits<TInternalComputationValueType>::epsilon();
typename JointPDFType::IndexType index;
for (SizeValueType ii = 0; ii<m_NumberOfHistogramBins; ii++)
{
MarginalPDFIndexType mind;
mind[0] = ii;
px = this->m_FixedImageMarginalPDF->GetPixel(mind);
for (SizeValueType jj = 0; jj<m_NumberOfHistogramBins; jj++)
{
mind[0] = jj;
py = this->m_MovingImageMarginalPDF->GetPixel(mind);
TInternalComputationValueType denom = px * py;
index[0] = ii;
index[1] = jj;
pxy = m_JointPDF->GetPixel(index);
local_mi = 0;
if ( fabs(denom) > eps )
{
if (pxy / denom > eps )
{
//the classic mi calculation
local_mi = pxy * vcl_log( pxy / denom );
}
}
total_mi += local_mi;
} // over jh bins 2
} // over jh bins 1
return ( -1.0 * total_mi.GetSum() / this->m_Log2 );
}
template <typename TFixedImage, typename TMovingImage, typename TVirtualImage, typename TInternalComputationValueType, typename TMetricTraits>
void
JointHistogramMutualInformationImageToImageMetricv4<TFixedImage,TMovingImage,TVirtualImage,TInternalComputationValueType, TMetricTraits>
::ComputeJointPDFPoint( const FixedImagePixelType fixedImageValue,
const MovingImagePixelType movingImageValue,
JointPDFPointType& jointPDFpoint ) const
{
TInternalComputationValueType a = ( fixedImageValue - this->m_FixedImageTrueMin ) / ( this->m_FixedImageTrueMax - this->m_FixedImageTrueMin );
TInternalComputationValueType b = ( movingImageValue - this->m_MovingImageTrueMin ) / ( this->m_MovingImageTrueMax - this->m_MovingImageTrueMin );
jointPDFpoint[0] = a;
jointPDFpoint[1] = b;
}
template <typename TFixedImage, typename TMovingImage, typename TVirtualImage, typename TInternalComputationValueType, typename TMetricTraits>
void
JointHistogramMutualInformationImageToImageMetricv4<TFixedImage,TMovingImage,TVirtualImage,TInternalComputationValueType, TMetricTraits>
::PrintSelf (std::ostream & os, Indent indent) const
{
// Print the superclass
Superclass::PrintSelf(os,indent);
os << indent << "NumberOfHistogramBins: ";
os << this->m_NumberOfHistogramBins << std::endl;
os << indent << "MovingImageTrueMin: ";
os << this->m_MovingImageTrueMin << std::endl;
os << indent << "MovingImageTrueMax: ";
os << this->m_MovingImageTrueMax << std::endl;
os << indent << "FixedImageBinSize: ";
os << this->m_FixedImageBinSize << std::endl;
os << indent << "MovingImageBinSize: ";
os << this->m_MovingImageBinSize << std::endl;
if( this->m_JointPDF.IsNotNull() )
{
os << indent << "JointPDF: ";
os << this->m_JointPDF << std::endl;
}
}
} // end namespace itk
#endif
|