/usr/include/ITK-4.5/itkImageMomentsCalculator.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 | /*=========================================================================
*
* 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 __itkImageMomentsCalculator_hxx
#define __itkImageMomentsCalculator_hxx
#include "itkImageMomentsCalculator.h"
#include "vnl/algo/vnl_real_eigensystem.h"
#include "vnl/algo/vnl_symmetric_eigensystem.h"
#include "itkImageRegionConstIteratorWithIndex.h"
namespace itk
{
class InvalidImageMomentsError:public ExceptionObject
{
public:
/**
* Constructor. Needed to ensure the exception object can be copied.
*/
InvalidImageMomentsError(const char *file, unsigned int lineNumber):ExceptionObject(file,
lineNumber) { this->
SetDescription(
"No valid image moments are available."); }
/**
* Constructor. Needed to ensure the exception object can be copied.
*/
InvalidImageMomentsError(const std::string & file, unsigned int lineNumber):ExceptionObject(file,
lineNumber) { this->
SetDescription(
"No valid image moments are available."); }
itkTypeMacro(InvalidImageMomentsError, ExceptionObject);
};
//----------------------------------------------------------------------
// Construct without computing moments
template< typename TImage >
ImageMomentsCalculator< TImage >::ImageMomentsCalculator(void)
{
m_Valid = false;
m_Image = NULL;
m_SpatialObjectMask = NULL;
m_M0 = NumericTraits< ScalarType >::Zero;
m_M1.Fill(NumericTraits< typename VectorType::ValueType >::Zero);
m_M2.Fill(NumericTraits< typename MatrixType::ValueType >::Zero);
m_Cg.Fill(NumericTraits< typename VectorType::ValueType >::Zero);
m_Cm.Fill(NumericTraits< typename MatrixType::ValueType >::Zero);
m_Pm.Fill(NumericTraits< typename VectorType::ValueType >::Zero);
m_Pa.Fill(NumericTraits< typename MatrixType::ValueType >::Zero);
}
//----------------------------------------------------------------------
// Destructor
template< typename TImage >
ImageMomentsCalculator< TImage >::
~ImageMomentsCalculator()
{}
template< typename TInputImage >
void
ImageMomentsCalculator< TInputImage >
::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Image: " << m_Image.GetPointer() << std::endl;
os << indent << "Valid: " << m_Valid << std::endl;
os << indent << "Zeroth Moment about origin: " << m_M0 << std::endl;
os << indent << "First Moment about origin: " << m_M1 << std::endl;
os << indent << "Second Moment about origin: " << m_M2 << std::endl;
os << indent << "Center of Gravity: " << m_Cg << std::endl;
os << indent << "Second central moments: " << m_Cm << std::endl;
os << indent << "Principal Moments: " << m_Pm << std::endl;
os << indent << "Principal axes: " << m_Pa << std::endl;
}
//----------------------------------------------------------------------
// Compute moments for a new or modified image
template< typename TImage >
void
ImageMomentsCalculator< TImage >::Compute()
{
m_M0 = NumericTraits< ScalarType >::Zero;
m_M1.Fill(NumericTraits< typename VectorType::ValueType >::Zero);
m_M2.Fill(NumericTraits< typename MatrixType::ValueType >::Zero);
m_Cg.Fill(NumericTraits< typename VectorType::ValueType >::Zero);
m_Cm.Fill(NumericTraits< typename MatrixType::ValueType >::Zero);
typedef typename ImageType::IndexType IndexType;
if ( !m_Image )
{
return;
}
ImageRegionConstIteratorWithIndex< ImageType > it( m_Image,
m_Image->GetRequestedRegion() );
while ( !it.IsAtEnd() )
{
double value = it.Value();
IndexType indexPosition = it.GetIndex();
Point< double, ImageDimension > physicalPosition;
m_Image->TransformIndexToPhysicalPoint(indexPosition, physicalPosition);
if ( m_SpatialObjectMask.IsNull()
|| m_SpatialObjectMask->IsInside(physicalPosition) )
{
m_M0 += value;
for ( unsigned int i = 0; i < ImageDimension; i++ )
{
m_M1[i] += static_cast< double >( indexPosition[i] ) * value;
for ( unsigned int j = 0; j < ImageDimension; j++ )
{
double weight = value * static_cast< double >( indexPosition[i] )
* static_cast< double >( indexPosition[j] );
m_M2[i][j] += weight;
}
}
for ( unsigned int i = 0; i < ImageDimension; i++ )
{
m_Cg[i] += physicalPosition[i] * value;
for ( unsigned int j = 0; j < ImageDimension; j++ )
{
double weight = value * physicalPosition[i] * physicalPosition[j];
m_Cm[i][j] += weight;
}
}
}
++it;
}
// Throw an error if the total mass is zero
if ( m_M0 == 0.0 )
{
itkExceptionMacro(
<< "Compute(): Total Mass of the image was zero. Aborting here to prevent division by zero later on.");
}
// Normalize using the total mass
for ( unsigned int i = 0; i < ImageDimension; i++ )
{
m_Cg[i] /= m_M0;
m_M1[i] /= m_M0;
for ( unsigned int j = 0; j < ImageDimension; j++ )
{
m_M2[i][j] /= m_M0;
m_Cm[i][j] /= m_M0;
}
}
// Center the second order moments
for ( unsigned int i = 0; i < ImageDimension; i++ )
{
for ( unsigned int j = 0; j < ImageDimension; j++ )
{
m_M2[i][j] -= m_M1[i] * m_M1[j];
m_Cm[i][j] -= m_Cg[i] * m_Cg[j];
}
}
// Compute principal moments and axes
vnl_symmetric_eigensystem< double > eigen( m_Cm.GetVnlMatrix() );
vnl_diag_matrix< double > pm = eigen.D;
for ( unsigned int i = 0; i < ImageDimension; i++ )
{
m_Pm[i] = pm(i, i) * m_M0;
}
m_Pa = eigen.V.transpose();
// Add a final reflection if needed for a proper rotation,
// by multiplying the last row by the determinant
vnl_real_eigensystem eigenrot( m_Pa.GetVnlMatrix() );
vnl_diag_matrix< vcl_complex< double > > eigenval = eigenrot.D;
vcl_complex< double > det(1.0, 0.0);
for ( unsigned int i = 0; i < ImageDimension; i++ )
{
det *= eigenval(i, i);
}
for ( unsigned int i = 0; i < ImageDimension; i++ )
{
m_Pa[ImageDimension - 1][i] *= std::real(det);
}
/* Remember that the moments are valid */
m_Valid = 1;
}
//---------------------------------------------------------------------
// Get sum of intensities
template< typename TImage >
typename ImageMomentsCalculator< TImage >::ScalarType
ImageMomentsCalculator< TImage >::GetTotalMass() const
{
if ( !m_Valid )
{
itkExceptionMacro(<< "GetTotalMass() invoked, but the moments have not been computed. Call Compute() first.");
}
return m_M0;
}
//--------------------------------------------------------------------
// Get first moments about origin, in index coordinates
template< typename TImage >
typename ImageMomentsCalculator< TImage >::VectorType
ImageMomentsCalculator< TImage >::GetFirstMoments() const
{
if ( !m_Valid )
{
itkExceptionMacro(<< "GetFirstMoments() invoked, but the moments have not been computed. Call Compute() first.");
}
return m_M1;
}
//--------------------------------------------------------------------
// Get second moments about origin, in index coordinates
template< typename TImage >
typename ImageMomentsCalculator< TImage >::MatrixType
ImageMomentsCalculator< TImage >::GetSecondMoments() const
{
if ( !m_Valid )
{
itkExceptionMacro(<< "GetSecondMoments() invoked, but the moments have not been computed. Call Compute() first.");
}
return m_M2;
}
//--------------------------------------------------------------------
// Get center of gravity, in physical coordinates
template< typename TImage >
typename ImageMomentsCalculator< TImage >::VectorType
ImageMomentsCalculator< TImage >::GetCenterOfGravity() const
{
if ( !m_Valid )
{
itkExceptionMacro(<< "GetCenterOfGravity() invoked, but the moments have not been computed. Call Compute() first.");
}
return m_Cg;
}
//--------------------------------------------------------------------
// Get second central moments, in physical coordinates
template< typename TImage >
typename ImageMomentsCalculator< TImage >::MatrixType
ImageMomentsCalculator< TImage >::GetCentralMoments() const
{
if ( !m_Valid )
{
itkExceptionMacro(<< "GetCentralMoments() invoked, but the moments have not been computed. Call Compute() first.");
}
return m_Cm;
}
//--------------------------------------------------------------------
// Get principal moments, in physical coordinates
template< typename TImage >
typename ImageMomentsCalculator< TImage >::VectorType
ImageMomentsCalculator< TImage >::GetPrincipalMoments() const
{
if ( !m_Valid )
{
itkExceptionMacro(<< "GetPrincipalMoments() invoked, but the moments have not been computed. Call Compute() first.");
}
return m_Pm;
}
//--------------------------------------------------------------------
// Get principal axes, in physical coordinates
template< typename TImage >
typename ImageMomentsCalculator< TImage >::MatrixType
ImageMomentsCalculator< TImage >::GetPrincipalAxes() const
{
if ( !m_Valid )
{
itkExceptionMacro(<< "GetPrincipalAxes() invoked, but the moments have not been computed. Call Compute() first.");
}
return m_Pa;
}
//--------------------------------------------------------------------
// Get principal axes to physical axes transform
template< typename TImage >
typename ImageMomentsCalculator< TImage >::AffineTransformPointer
ImageMomentsCalculator< TImage >::GetPrincipalAxesToPhysicalAxesTransform(void) const
{
typename AffineTransformType::MatrixType matrix;
typename AffineTransformType::OffsetType offset;
for ( unsigned int i = 0; i < ImageDimension; i++ )
{
offset[i] = m_Cg[i];
for ( unsigned int j = 0; j < ImageDimension; j++ )
{
matrix[j][i] = m_Pa[i][j]; // Note the transposition
}
}
AffineTransformPointer result = AffineTransformType::New();
result->SetMatrix(matrix);
result->SetOffset(offset);
return result;
}
//--------------------------------------------------------------------
// Get physical axes to principal axes transform
template< typename TImage >
typename ImageMomentsCalculator< TImage >::AffineTransformPointer
ImageMomentsCalculator< TImage >::GetPhysicalAxesToPrincipalAxesTransform(void) const
{
typename AffineTransformType::MatrixType matrix;
typename AffineTransformType::OffsetType offset;
for ( unsigned int i = 0; i < ImageDimension; i++ )
{
offset[i] = m_Cg[i];
for ( unsigned int j = 0; j < ImageDimension; j++ )
{
matrix[j][i] = m_Pa[i][j]; // Note the transposition
}
}
AffineTransformPointer result = AffineTransformType::New();
result->SetMatrix(matrix);
result->SetOffset(offset);
AffineTransformPointer inverse = AffineTransformType::New();
result->GetInverse(inverse);
return inverse;
}
} // end namespace itk
#endif
|