KWStyle - itkFloodFilledFunctionConditionalConstIterator.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkFloodFilledFunctionConditionalConstIterator.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:35 $
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 _itkFloodFilledFunctionConditionalConstIterator_txx
18 DEF #define _itkFloodFilledFunctionConditionalConstIterator_txx
19
20 #include "itkFloodFilledFunctionConditionalConstIterator.h"
21 #include "itkImageRegionConstIterator.h"
22
23 namespace itk
24 {
25 template<class TImage, class TFunction>
26 FloodFilledFunctionConditionalConstIterator<TImage, TFunction>
27 ::FloodFilledFunctionConditionalConstIterator(const ImageType *imagePtr,
28                                               FunctionType *fnPtr,
29                                               IndexType startIndex)
30 {
31   this->m_Image = imagePtr;
32   m_Function = fnPtr;
33   m_StartIndices.push_back ( startIndex );
34
35   // Set up the temporary image
36   this->InitializeIterator();
37 }
38
39 template<class TImage, class TFunction>
40 FloodFilledFunctionConditionalConstIterator<TImage, TFunction>
41 ::FloodFilledFunctionConditionalConstIterator(const ImageType *imagePtr,
42                                               FunctionType *fnPtr,
43 LEN                                               std::vector<IndexType>& startIndex)
44 {
45   this->m_Image = imagePtr;
46   m_Function = fnPtr;
47   unsigned int i;
48   for (i = 0; i < startIndex.size(); i++ )
49     {
50     m_StartIndices.push_back ( startIndex[i] );
51     }
52
53   // Set up the temporary image
54   this->InitializeIterator();
55 }
56
57 template<class TImage, class TFunction>
58 FloodFilledFunctionConditionalConstIterator<TImage, TFunction>
59 ::FloodFilledFunctionConditionalConstIterator(const ImageType *imagePtr,
60                                               FunctionType *fnPtr)
61 {
62   this->m_Image = imagePtr;
63   m_Function = fnPtr;
64
65   // Set up the temporary image
66   this->InitializeIterator();
67 }
68
69 template<class TImage, class TFunction>
70 void
71 FloodFilledFunctionConditionalConstIterator<TImage, TFunction>
72 ::InitializeIterator()
73 {
74   // Get the origin and spacing from the image in simple arrays
75   m_ImageOrigin  = this->m_Image->GetOrigin();
76   m_ImageSpacing = this->m_Image->GetSpacing();
77   m_ImageRegion  = this->m_Image->GetBufferedRegion();
78
79   // Build a temporary image of chars for use in the flood algorithm
80   tempPtr = TTempImage::New();
81 LEN   typename TTempImage::RegionType tempRegion = this->m_Image->GetBufferedRegion();
82
83   tempPtr->SetLargestPossibleRegion( tempRegion );
84   tempPtr->SetBufferedRegion( tempRegion );
85   tempPtr->SetRequestedRegion( tempRegion );
86   tempPtr->Allocate();
87   tempPtr->FillBuffer(NumericTraits<ITK_TYPENAME TTempImage::PixelType>::Zero);
88
89   // Initialize the queue by adding the start index assuming one of
90   // the m_StartIndices is "inside" This might not be true, in which
91   // case it's up to the programmer to specify a correct starting
92   // position later (using FindSeedPixel).  Must make sure that the
93   // seed is inside the buffer before touching pixels.
94   this->m_IsAtEnd = true;
95   for ( unsigned int i = 0; i < m_StartIndices.size(); i++ )
96     {
97     if ( m_ImageRegion.IsInside ( m_StartIndices[i] ) )
98       {
99       m_IndexStack.push(m_StartIndices[i]);
100       this->m_IsAtEnd = false;
101       }
102     }
103 }
104
105 template<class TImage, class TFunction>
106 void
107 FloodFilledFunctionConditionalConstIterator<TImage, TFunction>
108 ::FindSeedPixel()
109 {
110   // Create an iterator that will walk the input image
111   typedef typename itk::ImageRegionConstIterator<TImage> IRIType;
112   IRIType it = IRIType(this->m_Image, this->m_Image->GetBufferedRegion() );
113   
114   // Now we search the input image for the first pixel which is inside
115   // the function of interest
116   m_StartIndices.clear();
117   for ( it.GoToBegin(); !it.IsAtEnd(); ++it)
118     {
119     if( this->IsPixelIncluded( it.GetIndex() ) )
120       {
121       m_StartIndices.push_back ( it.GetIndex() );
122
123       // We need to reset the "beginning" now that we have a real seed
124       this->GoToBegin();
125
126       return;
127       }
128     }
129 }
130
131 template<class TImage, class TFunction>
132 void
133 FloodFilledFunctionConditionalConstIterator<TImage, TFunction>
134 ::FindSeedPixels()
135 {
136   // Create an iterator that will walk the input image
137   typedef typename itk::ImageRegionConstIterator<TImage> IRIType;
138   IRIType it = IRIType(this->m_Image, this->m_Image->GetBufferedRegion() );
139   
140   // Now we search the input image for the first pixel which is inside
141   // the function of interest
142   m_StartIndices.clear();
143   bool found = false;
144   for ( it.GoToBegin(); !it.IsAtEnd(); ++it)
145     {
146     if( this->IsPixelIncluded( it.GetIndex() ) )
147       {
148       m_StartIndices.push_back ( it.GetIndex() );
149       found = true;
150       }
151     }
152   if ( found )
153     {
154     // We need to reset the "beginning" now that we have a real seed
155     this->GoToBegin();
156     }
157 }
158
159 template<class TImage, class TFunction>
160 void
161 FloodFilledFunctionConditionalConstIterator<TImage, TFunction>
162 ::DoFloodStep()
163 {
164   // The index in the front of the queue should always be
165   // valid and be inside since this is what the iterator
166   // uses in the Set/Get methods. This is ensured by the
167   // GoToBegin() method.
168  
169
170   // Take the index in the front of the queue  
171   const IndexType & topIndex = m_IndexStack.front();
172   
173   // Iterate through all possible dimensions
174   // NOTE: Replace this with a ShapeNeighborhoodIterator
175   for(unsigned int i=0; i<NDimensions; i++)
176     {
177     // The j loop establishes either left or right neighbor (+-1)
178     for(int j=-1; j<=1; j+=2)
179       {
180       IndexType tempIndex;
181
182       // build the index of a neighbor
183       for(unsigned int k=0; k<NDimensions; k++)
184         {
185         if( i!=k )
186           {
187           tempIndex.m_Index[k] = topIndex[k];
188           }
189         else
190           {
191           tempIndex.m_Index[k] = topIndex[k] + j;
192           }
193         } // end build the index of a neighbor
194
195       // If this is a valid index and have not been tested,
196       // then test it.
197       if( m_ImageRegion.IsInside( tempIndex ) )
198         {
199         if( tempPtr->GetPixel( tempIndex )==0 )
200           {
201           // if it is inside, push it into the queue  
202           if(  this->IsPixelIncluded( tempIndex ) )
203             {
204             m_IndexStack.push( tempIndex );
205             tempPtr->SetPixel( tempIndex, 2); 
206             }
207           else  // If the pixel is outside
208             {
209             // Mark the pixel as outside and remove it from the queue.
210             tempPtr->SetPixel( tempIndex, 1);
211             }
212           }
213         }
214       } // end left/right neighbor loop
215     } // end check all neighbors
216   
217   // Now that all the potential neighbors have been 
218   // inserted we can get rid of the pixel in the front
219   m_IndexStack.pop();
220     
221   if( m_IndexStack.empty() )
222     {
223     this->m_IsAtEnd = true;
224     }
225
226
227 }
228
229
230 // end namespace itk
231
232 #endif
233

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