/usr/include/ITK-4.5/itkImagePCAShapeModelEstimator.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 | /*=========================================================================
*
* 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 __itkImagePCAShapeModelEstimator_hxx
#define __itkImagePCAShapeModelEstimator_hxx
#include "itkImagePCAShapeModelEstimator.h"
namespace itk
{
template< typename TInputImage, typename TOutputImage >
ImagePCAShapeModelEstimator< TInputImage, TOutputImage >
::ImagePCAShapeModelEstimator(void):m_NumberOfPixels(0), m_NumberOfTrainingImages(0)
{
m_EigenVectors.set_size(0, 0);
m_EigenValues.set_size(0);
m_NumberOfPrincipalComponentsRequired = 0;
this->SetNumberOfPrincipalComponentsRequired(1);
}
template< typename TInputImage, typename TOutputImage >
ImagePCAShapeModelEstimator< TInputImage, TOutputImage >
::~ImagePCAShapeModelEstimator(void)
{}
/**
* PrintSelf
*/
template< typename TInputImage, typename TOutputImage >
void
ImagePCAShapeModelEstimator< TInputImage, TOutputImage >
::PrintSelf(std::ostream & os, Indent indent) const
{
os << indent << " " << std::endl;
os << indent << "Shape Models " << std::endl;
os << indent << "Results printed in the superclass " << std::endl;
os << indent << " " << std::endl;
Superclass::PrintSelf(os, indent);
itkDebugMacro(<< " ");
itkDebugMacro(<< "Results of the shape model algorithms");
itkDebugMacro(<< "====================================");
itkDebugMacro(<< "The eigen values new method are: ");
itkDebugMacro(<< m_EigenValues);
itkDebugMacro(<< m_EigenVectorNormalizedEnergy);
itkDebugMacro(<< " ");
itkDebugMacro(<< "================== ");
itkDebugMacro(<< "The eigen vectors new method are: ");
for ( unsigned int i = 0; i < m_EigenValues.size(); i++ )
{
itkDebugMacro( << m_EigenVectors.get_row(i) );
}
itkDebugMacro(<< " ");
itkDebugMacro(<< "+++++++++++++++++++++++++");
// Print out ivars
os << indent << "NumberOfPrincipalComponentsRequired: ";
os << m_NumberOfPrincipalComponentsRequired << std::endl;
os << indent << "NumberOfTrainingImages: ";
os << m_NumberOfTrainingImages << std::endl;
} // end PrintSelf
/**
* Enlarge the output requested region to the largest possible region.
*/
template< typename TInputImage, typename TOutputImage >
void
ImagePCAShapeModelEstimator< TInputImage, TOutputImage >
::EnlargeOutputRequestedRegion( DataObject *itkNotUsed(output) )
{
// this filter requires the all of the output images to be in
// the buffer
for ( unsigned int idx = 0; idx < this->GetNumberOfIndexedOutputs(); ++idx )
{
if ( this->GetOutput(idx) )
{
this->GetOutput(idx)->SetRequestedRegionToLargestPossibleRegion();
}
}
}
/**
* Requires all of the inputs to be in the buffer up to the
* LargestPossibleRegion of the first input.
*/
template< typename TInputImage, typename TOutputImage >
void
ImagePCAShapeModelEstimator< TInputImage, TOutputImage >
::GenerateInputRequestedRegion()
{
Superclass::GenerateInputRequestedRegion();
if ( this->GetInput(0) )
{
// Set the requested region of the first input to largest possible region
InputImagePointer input = const_cast< TInputImage * >( this->GetInput(0) );
input->SetRequestedRegionToLargestPossibleRegion();
// Set the requested region of the remaining input to the largest possible
// region of the first input
unsigned int idx;
for ( idx = 1; idx < this->GetNumberOfIndexedInputs(); ++idx )
{
if ( this->GetInput(idx) )
{
typename TInputImage::RegionType requestedRegion =
this->GetInput(0)->GetLargestPossibleRegion();
typename TInputImage::RegionType largestRegion =
this->GetInput(idx)->GetLargestPossibleRegion();
if ( !largestRegion.IsInside(requestedRegion) )
{
itkExceptionMacro(
"LargestPossibleRegion of input " << idx
<<
" is not a superset of the LargestPossibleRegion of input 0");
}
InputImagePointer ptr = const_cast< TInputImage * >( this->GetInput(idx) );
ptr->SetRequestedRegion(requestedRegion);
} // if ( this->GetIntput(idx))
} // for idx
} // if( this->GetInput(0) )
}
/**
* Generate data (start the model building process)
*/
template< typename TInputImage, typename TOutputImage >
void
ImagePCAShapeModelEstimator< TInputImage, TOutputImage >
::GenerateData()
{
this->EstimateShapeModels();
// Allocate memory for each output.
unsigned int numberOfOutputs =
static_cast< unsigned int >( this->GetNumberOfIndexedOutputs() );
InputImagePointer input = const_cast< TInputImage * >( this->GetInput(0) );
unsigned int j;
for ( j = 0; j < numberOfOutputs; j++ )
{
OutputImagePointer output = this->GetOutput(j);
output->SetBufferedRegion( output->GetRequestedRegion() );
output->Allocate();
}
// Fill the output images.
VectorOfDoubleType m_OneEigenVector;
typedef ImageRegionIterator< OutputImageType > OutputIterator;
//Fill the mean image first
typename OutputImageType::RegionType region = this->GetOutput(0)->GetRequestedRegion();
OutputIterator outIter(this->GetOutput(0), region);
unsigned int i = 0;
outIter.GoToBegin();
while ( !outIter.IsAtEnd() )
{
outIter.Set( static_cast< typename OutputImageType::PixelType >( m_Means[i] ) );
++outIter;
++i;
}
//Now fill the principal component outputs
unsigned int kthLargestPrincipalComp = m_NumberOfTrainingImages;
unsigned int numberOfValidOutputs =
vnl_math_min(numberOfOutputs, m_NumberOfTrainingImages + 1);
for ( j = 1; j < numberOfValidOutputs; j++ )
{
//Extract one column vector at a time
m_OneEigenVector = m_EigenVectors.get_column(kthLargestPrincipalComp - 1);
region = this->GetOutput(j)->GetRequestedRegion();
OutputIterator outIterJ(this->GetOutput(j), region);
unsigned int idx = 0;
outIterJ.GoToBegin();
while ( !outIterJ.IsAtEnd() )
{
outIterJ.Set( static_cast< typename OutputImageType::PixelType >(
m_OneEigenVector[idx] ) );
++outIterJ;
++idx;
}
//Decrement to get the next principal component
--kthLargestPrincipalComp;
}
// Fill extraneous outputs with zero
for (; j < numberOfOutputs; j++ )
{
region = this->GetOutput(j)->GetRequestedRegion();
OutputIterator outIterJ(this->GetOutput(j), region);
outIterJ.GoToBegin();
while ( !outIterJ.IsAtEnd() )
{
outIterJ.Set(0);
++outIterJ;
}
}
} // end Generate data
/**
* Set the number of required principal components
*/
template< typename TInputImage, typename TOutputImage >
void
ImagePCAShapeModelEstimator< TInputImage, TOutputImage >
::SetNumberOfPrincipalComponentsRequired(unsigned int n)
{
if ( m_NumberOfPrincipalComponentsRequired != n )
{
m_NumberOfPrincipalComponentsRequired = n;
this->Modified();
// Modify the required number of outputs ( 1 extra for the mean image )
this->SetNumberOfRequiredOutputs(m_NumberOfPrincipalComponentsRequired + 1);
unsigned int numberOfOutputs = static_cast< unsigned int >( this->GetNumberOfIndexedOutputs() );
unsigned int idx;
if ( numberOfOutputs < m_NumberOfPrincipalComponentsRequired + 1 )
{
// Make and add extra outputs
for ( idx = numberOfOutputs; idx <= m_NumberOfPrincipalComponentsRequired; idx++ )
{
typename DataObject::Pointer output = this->MakeOutput(idx);
this->SetNthOutput( idx, output.GetPointer() );
}
}
else if ( numberOfOutputs > m_NumberOfPrincipalComponentsRequired + 1 )
{
// Remove the extra outputs
for ( idx = numberOfOutputs - 1; idx >= m_NumberOfPrincipalComponentsRequired + 1; idx-- )
{
this->RemoveOutput(idx);
}
}
}
}
/**
* Set the number of training images.
*/
template< typename TInputImage, typename TOutputImage >
void
ImagePCAShapeModelEstimator< TInputImage, TOutputImage >
::SetNumberOfTrainingImages(unsigned int n)
{
if ( m_NumberOfTrainingImages != n )
{
m_NumberOfTrainingImages = n;
this->Modified();
// Modify the required number of inputs
this->SetNumberOfRequiredInputs(m_NumberOfTrainingImages);
}
}
/**-----------------------------------------------------------------
* Takes a set of training images and returns the means
* and variance of the various classes defined in the
* training set.
*/
template< typename TInputImage, typename TOutputImage >
void
ImagePCAShapeModelEstimator< TInputImage, TOutputImage >
::EstimateShapeModels()
{
this->CalculateInnerProduct();
this->EstimatePCAShapeModelParameters();
} // end EstimateShapeModels
/**
* Calculate the inner product between the input training vector
* where each image is treated as a vector of n-elements
*/
template< typename TInputImage, typename TOutputImage >
void
ImagePCAShapeModelEstimator< TInputImage, TOutputImage >
::CalculateInnerProduct()
{
// Get the pointers to the input images and initialize the iterators
// We use dynamic_cast since inputs are stored as DataObjects. The
// ImageToImageFilter::GetInput(int) always returns a pointer to a
// TInputImage1 so it cannot be used for the second input.
InputImagePointerArray inputImagePointerArray(m_NumberOfTrainingImages);
m_InputImageIteratorArray.resize(m_NumberOfTrainingImages);
for ( unsigned int i = 0; i < m_NumberOfTrainingImages; i++ )
{
InputImageConstPointer inputImagePtr =
dynamic_cast< const TInputImage * >( ProcessObject::GetInput(i) );
inputImagePointerArray[i] = inputImagePtr;
InputImageConstIterator inputImageIt( inputImagePtr, inputImagePtr->GetBufferedRegion() );
m_InputImageIteratorArray[i] = inputImageIt;
m_InputImageIteratorArray[i].GoToBegin();
}
//-------------------------------------------------------------------
// Set up the matrix to hold the inner product and the means from the
// training data
//-------------------------------------------------------------------
m_InputImageSize = ( inputImagePointerArray[0] )->GetBufferedRegion().GetSize();
m_NumberOfPixels = 1;
for ( unsigned int i = 0; i < InputImageDimension; i++ )
{
m_NumberOfPixels *= m_InputImageSize[i];
}
//-------------------------------------------------------------------------
//Calculate the Means
//-------------------------------------------------------------------------
m_Means.set_size(m_NumberOfPixels);
m_Means.fill(0);
InputImageConstIterator tempImageItA;
for ( unsigned int img_number = 0; img_number < m_NumberOfTrainingImages; img_number++ )
{
tempImageItA = m_InputImageIteratorArray[img_number];
for ( unsigned int band_x = 0; band_x < m_NumberOfPixels; band_x++ )
{
m_Means[band_x] += tempImageItA.Get();
++tempImageItA;
}
} // end: looping through the image
//-------------------------------------------------------------------------
m_Means /= m_NumberOfTrainingImages;
//-------------------------------------------------------------------------
// Calculate the inner product
//-------------------------------------------------------------------------
m_InnerProduct.set_size(m_NumberOfTrainingImages, m_NumberOfTrainingImages);
m_InnerProduct.fill(0);
InputImageConstIterator tempImageItB;
//-------------------------------------------------------------------------
for ( unsigned int band_x = 0; band_x < m_NumberOfTrainingImages; band_x++ )
{
for ( unsigned int band_y = 0; band_y <= band_x; band_y++ )
{
//Pointer to the vector (in original matrix)
tempImageItA = m_InputImageIteratorArray[band_x];
//Pointer to the vector in the transposed matrix
tempImageItB = m_InputImageIteratorArray[band_y];
for ( unsigned int pix_number = 0; pix_number < m_NumberOfPixels; pix_number++ )
{
m_InnerProduct[band_x][band_y] +=
( tempImageItA.Get() - m_Means[pix_number] )
* ( tempImageItB.Get() - m_Means[pix_number] );
++tempImageItA;
++tempImageItB;
} // end: looping through the image
} // end: band_y loop
} // end: band_x loop
//---------------------------------------------------------------------
// Fill the rest of the inner product matrix and make it symmetric
//---------------------------------------------------------------------
for ( unsigned int band_x = 0; band_x < ( m_NumberOfTrainingImages - 1 ); band_x++ )
{
for ( unsigned int band_y = band_x + 1; band_y < m_NumberOfTrainingImages; band_y++ )
{
m_InnerProduct[band_x][band_y] = m_InnerProduct[band_y][band_x];
} // end band_y loop
} // end band_x loop
if ( ( m_NumberOfTrainingImages - 1 ) != 0 )
{
m_InnerProduct /= ( m_NumberOfTrainingImages - 1 );
}
else
{
m_InnerProduct.fill(0);
}
} // end CalculateInnerProduct
/*-----------------------------------------------------------------
*Estimage shape models using PCA.
*-----------------------------------------------------------------
*/
template< typename TInputImage, typename TOutputImage >
void
ImagePCAShapeModelEstimator< TInputImage, TOutputImage >
::EstimatePCAShapeModelParameters()
{
MatrixOfDoubleType identityMatrix(m_NumberOfTrainingImages, m_NumberOfTrainingImages);
identityMatrix.set_identity();
vnl_generalized_eigensystem eigenVectors_eigenValues(m_InnerProduct, identityMatrix);
MatrixOfDoubleType eigenVectorsOfInnerProductMatrix = eigenVectors_eigenValues.V;
//--------------------------------------------------------------------
//Calculate the principal shape variations
//
//m_EigenVectors capture the principal shape variantions
//m_EigenValues capture the relative weight of each variation
//Multiply original image vetors with the eigenVectorsOfInnerProductMatrix
//to derive the principal shapes.
//--------------------------------------------------------------------
m_EigenVectors.set_size(m_NumberOfPixels, m_NumberOfTrainingImages);
m_EigenVectors.fill(0);
double pix_value;
InputImageConstIterator tempImageItA;
for ( unsigned int img_number = 0; img_number < m_NumberOfTrainingImages; img_number++ )
{
tempImageItA = m_InputImageIteratorArray[img_number];
for ( unsigned int pix_number = 0; pix_number < m_NumberOfPixels; pix_number++ )
{
pix_value = tempImageItA.Get();
for ( unsigned int vec_number = 0; vec_number < m_NumberOfTrainingImages; vec_number++ )
{
m_EigenVectors[pix_number][vec_number] +=
( pix_value * eigenVectorsOfInnerProductMatrix[img_number][vec_number] );
}
++tempImageItA;
}
}
m_EigenVectors.normalize_columns();
m_EigenValues.set_size(m_NumberOfTrainingImages);
//Extract the diagonal elements into the Eigen value vector
m_EigenValues = ( eigenVectors_eigenValues.D ).diagonal();
//Flip the eigen values since the eigen vectors output
//is ordered in decending order of their corresponding eigen values.
m_EigenValues.flip();
//--------------------------------------------------------------------
//Normalize the eigen values
//--------------------------------------------------------------------
m_EigenVectorNormalizedEnergy = m_EigenValues;
m_EigenVectorNormalizedEnergy.normalize();
} // end EstimatePCAShapeModelParameters
//-----------------------------------------------------------------
} // namespace itk
#endif
|