NifTK  16.4.1 - 0798f20
CMIC's Translational Medical Imaging Platform
itkBasicImageComparisonFunctions.h
Go to the documentation of this file.
1 /*=============================================================================
2 
3  NifTK: A software platform for medical image computing.
4 
5  Copyright (c) University College London (UCL). All rights reserved.
6 
7  This software is distributed WITHOUT ANY WARRANTY; without even
8  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9  PURPOSE.
10 
11  See LICENSE.txt in the top level directory for details.
12 
13 =============================================================================*/
14 
15 #ifndef itkBasicImageComparisonFunctions_h
16 #define itkBasicImageComparisonFunctions_h
17 
18 namespace itk
19 {
20 
27 template <class TInputImage1, class TInputImage2>
28 ITK_EXPORT bool IsSameRegionSize(typename TInputImage1::Pointer image1, typename TInputImage2::Pointer image2)
29 {
30  image1->Update();
31  image2->Update();
32 
33  if (image1->GetLargestPossibleRegion().GetSize() == image2->GetLargestPossibleRegion().GetSize())
34  return true;
35 
36  return false;
37 }
38 
45 template <class TInputImage1, class TInputImage2>
46 ITK_EXPORT bool IsSameVoxelSize(typename TInputImage1::Pointer image1, typename TInputImage2::Pointer image2)
47 {
48  image1->Update();
49  image2->Update();
50 
51  typename TInputImage1::SpacingType samplingSpacing1 = image1->GetSpacing();
52  typename TInputImage1::SpacingType::ConstIterator samplingSpacingIterator1 = samplingSpacing1.Begin();
53  typename TInputImage2::SpacingType samplingSpacing2 = image2->GetSpacing();
54  typename TInputImage2::SpacingType::ConstIterator samplingSpacingIterator2 = samplingSpacing2.Begin();
55 
56  // Random max relative error allowed.
57  const double maxRelativeError = 0.00005;
58 
59  if (samplingSpacing1.Size() != samplingSpacing2.Size())
60  return false;
61 
62  for (samplingSpacingIterator1 = samplingSpacing1.Begin(), samplingSpacingIterator2 = samplingSpacing2.Begin();
63  samplingSpacingIterator1 != samplingSpacing1.End();
64  ++samplingSpacingIterator1, ++samplingSpacingIterator2)
65  {
66  double spacing1 = static_cast<double>(*samplingSpacingIterator1);
67  double spacing2 = static_cast<double>(*samplingSpacingIterator2);
68 
69  if (spacing1 != spacing2)
70  {
71  double relativeError = fabs(spacing1-spacing2)/
72  std::max(fabs(spacing1), fabs(spacing2));
73 
74  if (relativeError > maxRelativeError)
75  return false;
76  }
77  }
78 
79  return true;
80 }
81 
82 }
83 #endif /*ITKBASICIMAGECOMPARISONFUNCTIONS_H_*/
Definition: niftkITKAffineResampleImage.cxx:74
ITK_EXPORT bool IsSameVoxelSize(typename TInputImage1::Pointer image1, typename TInputImage2::Pointer image2)
Definition: itkBasicImageComparisonFunctions.h:46
ITK_EXPORT bool IsSameRegionSize(typename TInputImage1::Pointer image1, typename TInputImage2::Pointer image2)
Definition: itkBasicImageComparisonFunctions.h:28