KWStyle - itkCovarianceImageFunction.txx
 
Matrix View
Description

1 /*=========================================================================
2
3     Program:   Insight Segmentation & Registration Toolkit
4     Module:    $RCSfile: itkCovarianceImageFunction.txx.html,v $
5     Language:  C++
6     Date:      $Date: 2006/01/17 19:15:34 $
7     Version:   $Revision: 1.4 $
8
9     Copyright (c) Insight Software Consortium. All rights reserved.
10     See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12        This software is distributed WITHOUT ANY WARRANTY; without even 
13        the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14        PURPOSE.  See the above copyright notices for more information.
15
16   =========================================================================*/
17 DEF #ifndef _itkCovarianceImageFunction_txx
18 DEF #define _itkCovarianceImageFunction_txx
19 #include "itkCovarianceImageFunction.h"
20 #include "itkMatrix.h"
21 #include "itkNumericTraits.h"
22 #include "itkConstNeighborhoodIterator.h"
23
24 namespace itk
25 {
26
27 /**
28 IND **** Constructor
29 IND ****/
30 template <class TInputImage, class TCoordRep>
31 CovarianceImageFunction<TInputImage,TCoordRep>
32 ::CovarianceImageFunction()
33 {
34   m_NeighborhoodRadius = 1;
35 }
36
37
38 /**
39 IND ****
40 IND ****/
41 template <class TInputImage, class TCoordRep>
42 void
43 CovarianceImageFunction<TInputImage,TCoordRep>
44 ::PrintSelf(std::ostream& os, Indent indent) const
45 {
46   this->Superclass::PrintSelf(os,indent);
47   os << indent << "NeighborhoodRadius: "  << m_NeighborhoodRadius << std::endl;
48 }
49
50
51 /**
52  *
53  */
54 template <class TInputImage, class TCoordRep>
55 typename CovarianceImageFunction<TInputImage,TCoordRep>
56 ::RealType
57 CovarianceImageFunction<TInputImage,TCoordRep>
58 ::EvaluateAtIndex(const IndexType& index) const
59 {
60   typedef  typename TInputImage::PixelType  PixelType;
61   typedef  typename PixelType::ValueType    PixelComponentType;
62
63 LEN   typedef  typename NumericTraits< PixelComponentType >::RealType PixelComponentRealType;
64
65   const unsigned int VectorDimension = 
66 IND ******::itk::GetVectorDimension< PixelType >::VectorDimension;
67
68   RealType covariance = RealType( VectorDimension, VectorDimension );
69   
70
71
72   if( !this->GetInputImage() )
73     {
74     itkExceptionMacro( << "No image connected to CovarianceImageFunction");
75     covariance.fill( NumericTraits< PixelComponentRealType >::max() );
76     return covariance;
77     }
78   
79   if ( !this->IsInsideBuffer( index ) )
80     {
81     covariance.fill( NumericTraits< PixelComponentRealType >::max() );
82     return covariance;
83     }
84
85   covariance.fill( NumericTraits< PixelComponentRealType >::Zero );
86
87
88   typedef vnl_vector< PixelComponentRealType >    MeanVectorType;
89   MeanVectorType mean = MeanVectorType( VectorDimension ); 
90   mean.fill( NumericTraits< PixelComponentRealType >::Zero );
91
92   // Create an N-d neighborhood kernel, using a zeroflux boundary condition
93   typename InputImageType::SizeType kernelSize;
94   kernelSize.Fill( m_NeighborhoodRadius );
95   
96   ConstNeighborhoodIterator<InputImageType>
97 LEN,IND ****it(kernelSize, this->GetInputImage(), this->GetInputImage()->GetBufferedRegion());
98
99   // Set the iterator at the desired location
100   it.SetLocation(index);
101
102   // Walk the neighborhood
103   const unsigned int size = it.Size();
104   for (unsigned int i = 0; i < size; ++i)
105     {
106     const PixelType pixel = it.GetPixel(i);
107
108     for(unsigned int dimx=0; dimx<VectorDimension; dimx++)
109       {
110       mean[ dimx ] += pixel[ dimx ];
111       for(unsigned int dimy=0; dimy<VectorDimension; dimy++)
112         {
113         covariance[dimx][dimy] += 
114             static_cast<PixelComponentRealType>( pixel[dimx] ) *
115 IND ************static_cast<PixelComponentRealType>( pixel[dimy] );
116         }
117       }
118     }
119
120   const PixelComponentRealType rsize = 
121 IND **********static_cast< PixelComponentRealType >( size );
122
123   mean /= rsize;
124
125   for(unsigned int dimx=0; dimx<VectorDimension; dimx++)
126     {
127     for(unsigned int dimy=0; dimy<VectorDimension; dimy++)
128       {
129       covariance[dimx][dimy] /= rsize;
130       covariance[dimx][dimy] -= mean[dimx] * mean[dimy];
131       }
132     }
133              
134   return ( covariance );
135 }
136
137
138 // namespace itk
139
140 #endif
141

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:04PM
© Kitware Inc.