KWStyle - itkAnnulusOperator.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkAnnulusOperator.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:32 $
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 _itkAnnulusOperator_txx
18 DEF #define _itkAnnulusOperator_txx
19
20 #include "itkAnnulusOperator.h"
21 #include "itkSphereSpatialFunction.h"
22
23 namespace itk
24 {
25
26
27 EML
28 //Create the operator
29 template <class TPixel, unsigned int VDimension, class TAllocator>
30 void
31 AnnulusOperator<TPixel, VDimension, TAllocator>
32 ::CreateOperator()
33 {
34   CoefficientVector coefficients;
35   
36   coefficients = this->GenerateCoefficients();
37
38   this->Fill(coefficients);
39   
40 }
41
42 //This function fills the coefficients into the corresponding neighborhodd.
43 template <class TPixel, unsigned int VDimension, class TAllocator>
44 void  
45 AnnulusOperator <TPixel, VDimension, TAllocator>
46 ::Fill(const CoefficientVector &coeff)
47 {
48
49   typename Superclass::CoefficientVector::const_iterator it;
50
51   std::slice* temp_slice;
52   temp_slice = new std::slice(0, coeff.size(),1);
53   
54   typename Self::SliceIteratorType data(this, *temp_slice);
55   delete temp_slice;
56  
57   it = coeff.begin();
58
59   // Copy the coefficients into the neighborhood
60   for (data = data.Begin(); data < data.End(); ++data, ++it)
61     {
62     *data = *it;
63     }
64
65 }
66
67 template <class TPixel, unsigned int VDimension, class TAllocator>
68 typename AnnulusOperator<TPixel, VDimension, TAllocator>
69 ::CoefficientVector
70 AnnulusOperator<TPixel, VDimension, TAllocator>
71 ::GenerateCoefficients()
72 {
73   // Determine the initial kernel values...
74   double interiorV, annulusV, exteriorV;
75   if (m_Normalize)
76     {
77     double bright = (m_BrightCenter ? 1.0 : -1.0);
78     
79     // Initial values for a normalized kernel
80     interiorV = bright;
81     annulusV = -1.0*bright;
82     exteriorV = 0.0;
83     }
84   else
85     {
86     // values for a specified kernel
87     interiorV = m_InteriorValue;
88     annulusV = m_AnnulusValue;
89     exteriorV = m_ExteriorValue;
90     }
91
92   // Compute the size of the kernel in pixels
93   typedef typename SizeType::SizeValueType SizeValueType;
94   SizeType r;
95   unsigned int i, j;
96   double outerRadius = m_InnerRadius + m_Thickness;
97   for (i=0; i < VDimension; ++i)
98     {
99     r[i] = static_cast<SizeValueType>(ceil(outerRadius / m_Spacing[i]));
100     }
101   this->SetRadius(r);
102
103   // Use a couple of sphere spatial functions...
104   typedef SphereSpatialFunction<VDimension> SphereType;
105   typename SphereType::Pointer innerS = SphereType::New();
106   typename SphereType::Pointer  outerS = SphereType::New();
107   
108   innerS->SetRadius( m_InnerRadius );
109   outerS->SetRadius( m_InnerRadius + m_Thickness );
110   
111   // Walk the neighborhood (this) and evaluate the sphere spatial
112   // functions
113   bool inInner, inOuter;
114   double sumNotExterior = 0.0;
115   double sumNotExteriorSq = 0.0;
116   unsigned int countNotExterior = 0;
117   
118   unsigned int w;
119   w = this->Size();
120
121   std::vector<bool> outside(w);
122   CoefficientVector coeffP(w);
123   OffsetType offset;
124   typename SphereType::InputType point;
125
126   for (i=0; i < w; ++i)
127     {
128     // get the offset from the center pixel
129     offset = this->GetOffset(i);
130
131     // convert to a position
132     for (j=0; j < VDimension; ++j)
133       {
134       point[j] = m_Spacing[j] * offset[j];
135       }
136
137     // evaluate the spheres
138     inInner = innerS->Evaluate(point);
139     inOuter = outerS->Evaluate(point);
140
141     // set the coefficients
142     if (!inOuter)
143       {
144       // outside annulus
145       coeffP[i] = exteriorV;
146       outside[i] = true;
147       }
148     else if (!inInner)
149       {
150       // inside the outer circle but outside the inner circle
151       coeffP[i] = annulusV;
152       sumNotExterior += annulusV;
153       sumNotExteriorSq += (annulusV*annulusV);
154       countNotExterior++;
155       outside[i] = false;
156       }
157     else
158       {
159       // inside inner circle
160       coeffP[i] = interiorV;
161       sumNotExterior += interiorV;
162       sumNotExteriorSq += (interiorV*interiorV);
163       countNotExterior++;
164       outside[i] = false;
165       }
166     }
167
168   // Normalize the kernel if necessary
169   if (m_Normalize)
170     {
171     // Calculate the mean and standard deviation of kernel values NOT
172     // the exterior
173     double num = static_cast<double>(countNotExterior);
174     double mean = sumNotExterior / num;
175     double var = ( sumNotExteriorSq - ( sumNotExterior*sumNotExterior / num ) )
176 IND ******/ ( num - 1.0 );
177     double std = sqrt(var);
178
179     // convert std to a scaling factor k such that
180     //
181     //        || (coeffP - mean) / k || = 1.0
182     //
183     double k = std * sqrt(num-1.0);
184
185     // Run through the kernel again, shifting and normalizing the
186     // elements that are not exterior to the annulus.  This forces the
187     // kernel to have mean zero and norm 1 AND forces the region
188     // outside the annulus to have no influence.
189     for (i=0; i < w; ++i)
190       {
191       // normalize the coefficient if it is inside the outer circle
192       // (exterior to outer circle is already zero)
193       if (!outside[i])
194         {
195         coeffP[i] = (coeffP[i] - mean) / k;
196         }
197       }
198     }
199  
200
201   return coeffP;
202 }
203
204 // namespace itk
205
206 #endif
207

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