ITK  4.2.0
Insight Segmentation and Registration Toolkit
itkAttributeMorphologyBaseImageFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkAttributeMorphologyBaseImageFilter_h
19 #define __itkAttributeMorphologyBaseImageFilter_h
20 
21 #include "itkImageToImageFilter.h"
22 #include <vector>
23 
24 #define PAMI
25 
26 namespace itk
27 {
62 template< class TInputImage, class TOutputImage, class TAttribute, class TFunction >
64  public ImageToImageFilter< TInputImage, TOutputImage >
65 {
66 public:
72 
76  typedef typename Superclass::InputImagePointer InputImagePointer;
77 
82  typedef typename TOutputImage::PixelType OutputPixelType;
83  typedef typename TOutputImage::InternalPixelType OutputInternalPixelType;
84  typedef typename TInputImage::PixelType InputPixelType;
85  typedef typename TInputImage::InternalPixelType InputInternalPixelType;
86  typedef typename TInputImage::IndexType IndexType;
87  typedef typename TInputImage::OffsetType OffsetType;
88  typedef typename TInputImage::SizeType SizeType;
89 
90  itkStaticConstMacro(ImageDimension, unsigned int,
91  TOutputImage::ImageDimension);
92 
96  typedef TInputImage InputImageType;
97  typedef TOutputImage OutputImageType;
98 // typedef typename TInputImage::IndexType IndexType;
99 // typedef typename TInputImage::SizeType SizeType;
100  typedef typename TOutputImage::RegionType RegionType;
101  typedef std::list< IndexType > ListType;
102  typedef TAttribute AttributeType;
103 
109 
114 
118  itkNewMacro(Self);
119 
126  itkSetMacro(FullyConnected, bool);
127  itkGetConstReferenceMacro(FullyConnected, bool);
128  itkBooleanMacro(FullyConnected);
130 
136  itkSetMacro(Lambda, AttributeType);
137  itkGetConstMacro(Lambda, AttributeType);
138 protected:
140  {
141  m_FullyConnected = false;
142  m_AttributeValuePerPixel = 1;
143  m_Lambda = 0;
144  }
146 
149  void PrintSelf(std::ostream & os, Indent indent) const;
150 
154  void GenerateData();
155 
159  void GenerateInputRequestedRegion();
160 
165  void EnlargeOutputRequestedRegion( DataObject * itkNotUsed(output) );
166 
168 private:
169 
172 
173  // some constants used several times in the code
174  itkStaticConstMacro(INACTIVE, OffsetValueType, -1);
175  itkStaticConstMacro(ACTIVE, OffsetValueType, -2);
176  itkStaticConstMacro(ROOT, OffsetValueType, -3);
177 
178  // Just used for area/volume openings at the moment
180 
181  typedef std::vector< OffsetType > OffsetVecType;
182  // offset in the linear array.
183  typedef std::vector< OffsetValueType > OffsetDirectVecType;
184 
185  void SetupOffsetVec(OffsetDirectVecType & PosOffsets, OffsetVecType & Offsets);
186 
188  {
189 public:
192  };
193 
194  GreyAndPos * m_SortPixels;
196 #ifndef PAMI
197  bool *m_Processed;
198 #endif
199  // This is a bit ugly, but I can't see an easy way around
201 
203  {
204 public:
205  TFunction m_TFunction;
206  bool operator()(GreyAndPos const & l, GreyAndPos const & r) const
207  {
208  if ( m_TFunction(l.Val, r.Val) )
209  {
210  return true;
211  }
212  if ( l.Val == r.Val )
213  {
214  return ( l.Pos < r.Pos );
215  }
216  return false;
217  }
218  };
219 
220 #ifdef PAMI
221  // version from PAMI. Note - using the AuxData array rather than the
222  // parent array to store area
223  void MakeSet(OffsetValueType x)
224  {
225  m_Parent[x] = ACTIVE;
226  m_AuxData[x] = m_AttributeValuePerPixel;
227  }
228 
230  {
231  if ( m_Parent[x] >= 0 )
232  {
233  m_Parent[x] = FindRoot(m_Parent[x]);
234  return ( m_Parent[x] );
235  }
236  else
237  {
238  return ( x );
239  }
240  }
241 
242  bool Criterion(OffsetValueType x, OffsetValueType y)
243  {
244  return ( ( m_Raw[x] == m_Raw[y] ) || ( m_AuxData[x] < m_Lambda ) );
245  }
246 
248  {
249  OffsetValueType r = FindRoot(n);
250 
251  if ( r != p )
252  {
253  if ( Criterion(r, p) )
254  {
255  m_AuxData[p] += m_AuxData[r];
256  m_Parent[r] = p;
257  }
258  else
259  {
260  m_AuxData[p] = m_Lambda;
261  }
262  }
263  }
264 
265 #else
266  // version from ISMM paper
267  void MakeSet(OffsetValueType x)
268  {
269  m_Parent[x] = ACTIVE;
270  m_AuxData[x] = m_AttributeValuePerPixel;
271  }
272 
273  void Link(OffsetValueType x, OffsetValueType y)
274  {
275  if ( ( m_Parent[y] == ACTIVE ) && ( m_Parent[x] == ACTIVE ) )
276  {
277  // should be a call to MergeAuxData
278  m_AuxData[y] = m_AuxData[x] + m_AuxData[y];
279  m_AuxData[x] = -m_AttributeValuePerPixel;
280  }
281  else if ( m_Parent[x] == ACTIVE )
282  {
283  m_AuxData[x] = -m_AttributeValuePerPixel;
284  }
285  else
286  {
287  m_AuxData[y] = -m_AttributeValuePerPixel;
288  m_Parent[y] = INACTIVE;
289  }
290  m_Parent[x] = y;
291  }
292 
293  OffsetValueType FindRoot(OffsetValueType x)
294  {
295  if ( m_Parent[x] >= 0 )
296  {
297  m_Parent[x] = FindRoot(m_Parent[x]);
298  return ( m_Parent[x] );
299  }
300  else
301  {
302  return ( x );
303  }
304  }
305 
306  bool Equiv(OffsetValueType x, OffsetValueType y)
307  {
308  return ( ( m_Raw[x] == m_Raw[y] ) || ( m_Parent[x] == ACTIVE ) );
309  }
310 
311  void Union(OffsetValueType n, OffsetValueType p)
312  {
313  OffsetValueType r = FindRoot(n);
314 
315  if ( r != p )
316  {
317  if ( Equiv(r, p) )
318  {
319  Link(r, p);
320  }
321  else if ( m_Parent[p] == ACTIVE )
322  {
323  m_Parent[p] = INACTIVE;
324  m_AuxData[p] = -m_AttributeValuePerPixel;
325  }
326  }
327  }
328 
329 #endif
330 };
331 } // end namespace itk
332 
333 #ifndef ITK_MANUAL_INSTANTIATION
334 #include "itkAttributeMorphologyBaseImageFilter.hxx"
335 #endif
336 
337 #endif
338