File: itkMSEImageCalculator.txx

package info (click to toggle)
volview 3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 25,204 kB
  • sloc: cpp: 132,585; ansic: 11,612; tcl: 236; sh: 64; makefile: 25; xml: 8
file content (126 lines) | stat: -rw-r--r-- 2,977 bytes parent folder | download
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
/*=========================================================================

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/VolViewCopyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#ifndef __itkMSEImageCalculator_txx
#define __itkMSEImageCalculator_txx

#include "itkMSEImageCalculator.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkNumericTraits.h"

namespace itk
{ 
    
/**
 * Constructor
 */
template<class TInputImage1, class TInputImage2>
MSEImageCalculator<TInputImage1, TInputImage2>
::MSEImageCalculator()
{
  m_MSE = 0;
  m_RegionSetByUser = false;
  m_Radius = 100000000.0; // some Large value.
  m_Center.Fill(0);
}


/**
 * Compute Min and Max of m_Image
 */
template<class TInputImage1, class TInputImage2>
double
MSEImageCalculator<TInputImage1, TInputImage2>
::Compute()
{
  if( !m_RegionSetByUser )
    {
    m_Region = m_Image1->GetRequestedRegion();
    m_Region.Crop(m_Image2->GetRequestedRegion());
    }

  ImageRegionConstIteratorWithIndex< TInputImage1 >  it1( m_Image1, m_Region );
  ImageRegionConstIterator< TInputImage2 >  it2( m_Image2, m_Region );

  Index1Type index;
  PointType point;
  VectorType distanceVector;
  typename VectorType::RealValueType distanceSq;
  const typename VectorType::RealValueType radiusSq = m_Radius * m_Radius;

  
  it1.GoToBegin();
  it2.GoToBegin();

  m_MSE = 0;
  unsigned long nPixels = 0;

  while( !it1.IsAtEnd() )
    {

    // Check if we are within the bounds
    index = it1.GetIndex();
    m_Image1->TransformIndexToPhysicalPoint( index, point );
    distanceVector = point - m_Center;
    distanceSq = distanceVector.GetSquaredNorm();
    if (distanceSq > radiusSq) 
      {
      ++it1;
      ++it2;
      continue;
      }

    const double v1 = it1.Get();  
    const double v2 = it2.Get();  
    const double diff = v1 - v2;
    m_MSE += (diff * diff);
    ++it1;
    ++it2;
    ++nPixels;
    }

  if( nPixels == 0 )
    {
    itkExceptionMacro(<< "No pixels to evaluate MSE");
    }  

  const double n = static_cast< double >(nPixels);
  m_MSE /= n;

  return m_MSE;
}

template<class TInputImage1, class TInputImage2>
void
MSEImageCalculator<TInputImage1, TInputImage2>
::SetRegion( const RegionType & region )
{
  m_Region = region;
  m_RegionSetByUser = true;
}


 
template<class TInputImage1, class TInputImage2>
void
MSEImageCalculator<TInputImage1, TInputImage2>
::PrintSelf( std::ostream& os, Indent indent ) const
{
  Superclass::PrintSelf(os,indent);
  os << indent << "Region: " << std::endl;
  m_Region.Print(os,indent.GetNextIndent());
  os << indent << "Region set by User: " << m_RegionSetByUser << std::endl;
}

} // end namespace itk

#endif