Orfeo Toolbox  3.16
itkOptGrayscaleDilateImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkOptGrayscaleDilateImageFilter.txx,v $
5  Language: C++
6  Date: $Date: 2008-09-30 18:07:03 $
7  Version: $Revision: 1.2 $
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 #ifndef __itkOptGrayscaleDilateImageFilter_txx
18 #define __itkOptGrayscaleDilateImageFilter_txx
19 
21 #include "itkNumericTraits.h"
22 #include "itkProgressAccumulator.h"
23 #include <string>
24 
25 namespace itk {
26 
27 
28 template<class TInputImage, class TOutputImage, class TKernel>
31 {
32  m_BasicFilter = BasicFilterType::New();
33  m_HistogramFilter = HistogramFilterType::New();
34  m_AnchorFilter = AnchorFilterType::New();
35  m_VHGWFilter = VHGWFilterType::New();
36  m_Algorithm = HISTO;
37 
38  this->SetBoundary( NumericTraits< PixelType >::NonpositiveMin() );
39 }
40 
41 template< class TInputImage, class TOutputImage, class TKernel>
42 void
45 {
46  Superclass::SetNumberOfThreads( nb );
47  m_HistogramFilter->SetNumberOfThreads( nb );
48  m_AnchorFilter->SetNumberOfThreads( nb );
49  m_VHGWFilter->SetNumberOfThreads( nb );
50  m_BasicFilter->SetNumberOfThreads( nb );
51 }
52 
53 
54 template< class TInputImage, class TOutputImage, class TKernel>
55 void
57 ::SetKernel( const KernelType& kernel )
58 {
59  const FlatKernelType * flatKernel = NULL;
60  try
61  { flatKernel = dynamic_cast< const FlatKernelType* >( & kernel ); }
62  catch( ... ) {}
63 
64 
65  if( flatKernel != NULL && flatKernel->GetDecomposable() )
66  {
67  m_AnchorFilter->SetKernel( *flatKernel );
68  m_Algorithm = ANCHOR;
69  }
70  else if( m_HistogramFilter->GetUseVectorBasedAlgorithm() )
71  {
72  // histogram based filter is as least as good as the basic one, so always use it
73  m_Algorithm = HISTO;
74  m_HistogramFilter->SetKernel( kernel );
75  }
76  else
77  {
78  // basic filter can be better than the histogram based one
79  // apply a poor heuristic to find the best one. What is very important is to
80  // select the histogram for large kernels
81 
82  // we need to set the kernel on the histogram filter to compare basic and histogram algorithm
83  m_HistogramFilter->SetKernel( kernel );
84 
85  if( ( ImageDimension == 2 && this->GetKernel().Size() < m_HistogramFilter->GetPixelsPerTranslation() * 5.4 )
86  || ( ImageDimension == 3 && this->GetKernel().Size() < m_HistogramFilter->GetPixelsPerTranslation() * 4.5 ) )
87  {
88  m_BasicFilter->SetKernel( kernel );
89  m_Algorithm = BASIC;
90  }
91  else
92  {
93  m_Algorithm = HISTO;
94  }
95  }
96 
97  Superclass::SetKernel( kernel );
98 }
99 
100 template< class TInputImage, class TOutputImage, class TKernel>
101 void
103 ::SetBoundary( const PixelType value )
104 {
105  m_Boundary = value;
106  m_HistogramFilter->SetBoundary( value );
107  m_AnchorFilter->SetBoundary(value);
108  m_VHGWFilter->SetBoundary(value);
109  m_BoundaryCondition.SetConstant( value );
110  m_BasicFilter->OverrideBoundaryCondition( &m_BoundaryCondition );
111 }
112 
113 template< class TInputImage, class TOutputImage, class TKernel>
114 void
116 ::SetAlgorithm( int algo )
117 {
118  const FlatKernelType * flatKernel = NULL;
119  try
120  { flatKernel = dynamic_cast< const FlatKernelType* >( & this->GetKernel() ); }
121  catch( ... ) {}
122 
123  if( m_Algorithm != algo )
124  {
125 
126  if( algo == BASIC )
127  {
128  m_BasicFilter->SetKernel( this->GetKernel() );
129  }
130  else if( algo == HISTO )
131  {
132  m_HistogramFilter->SetKernel( this->GetKernel() );
133  }
134  else if( flatKernel != NULL && flatKernel->GetDecomposable() && algo == ANCHOR )
135  {
136  m_AnchorFilter->SetKernel( *flatKernel );
137  }
138  else if( flatKernel != NULL && flatKernel->GetDecomposable() && algo == VHGW )
139  {
140  m_VHGWFilter->SetKernel( *flatKernel );
141  }
142  else
143  { itkExceptionMacro( << "Invalid algorithm" ); }
144 
145  m_Algorithm = algo;
146  this->Modified();
147 
148  }
149 }
150 
151 template<class TInputImage, class TOutputImage, class TKernel>
152 void
155 {
156  // Create a process accumulator for tracking the progress of this minipipeline
158  progress->SetMiniPipelineFilter(this);
159 
160  // Allocate the output
161  this->AllocateOutputs();
162 
163  // Delegate to a dilate filter.
164  if( m_Algorithm == BASIC )
165  {
166  itkDebugMacro("Running BasicDilateImageFilter");
167  m_BasicFilter->SetInput( this->GetInput() );
168  progress->RegisterInternalFilter( m_BasicFilter, 1.0f );
169 
170  m_BasicFilter->GraftOutput( this->GetOutput() );
171  m_BasicFilter->Update();
172  this->GraftOutput( m_BasicFilter->GetOutput() );
173  }
174  else if( m_Algorithm == HISTO )
175  {
176  itkDebugMacro("Running MovingHistogramDilateImageFilter");
177  m_HistogramFilter->SetInput( this->GetInput() );
178  progress->RegisterInternalFilter( m_HistogramFilter, 1.0f );
179 
180  m_HistogramFilter->GraftOutput( this->GetOutput() );
181  m_HistogramFilter->Update();
182  this->GraftOutput( m_HistogramFilter->GetOutput() );
183  }
184  else if( m_Algorithm == ANCHOR )
185  {
186  itkDebugMacro("Running AnchorDilateImageFilter");
187  m_AnchorFilter->SetInput( this->GetInput() );
188  progress->RegisterInternalFilter( m_AnchorFilter, 0.9f );
189 
190  typename CastFilterType::Pointer cast = CastFilterType::New();
191  cast->SetInput( m_AnchorFilter->GetOutput() );
192  progress->RegisterInternalFilter( cast, 0.1f );
193 
194  cast->GraftOutput( this->GetOutput() );
195  cast->Update();
196  this->GraftOutput( cast->GetOutput() );
197  }
198  else if( m_Algorithm == VHGW )
199  {
200  itkDebugMacro("Running VanHerkGilWermanDilateImageFilter");
201  m_VHGWFilter->SetInput( this->GetInput() );
202  progress->RegisterInternalFilter( m_VHGWFilter, 0.9f );
203 
204  typename CastFilterType::Pointer cast = CastFilterType::New();
205  cast->SetInput( m_VHGWFilter->GetOutput() );
206  progress->RegisterInternalFilter( cast, 0.1f );
207 
208  cast->GraftOutput( this->GetOutput() );
209  cast->Update();
210  this->GraftOutput( cast->GetOutput() );
211  }
212 
213 }
214 
215 template<class TInputImage, class TOutputImage, class TKernel>
216 void
218 ::Modified() const
219 {
220  Superclass::Modified();
221  m_BasicFilter->Modified();
222  m_HistogramFilter->Modified();
223  m_AnchorFilter->Modified();
224  m_VHGWFilter->Modified();
225 }
226 
227 template<class TInputImage, class TOutputImage, class TKernel>
228 void
230 ::PrintSelf(std::ostream &os, Indent indent) const
231 {
232  Superclass::PrintSelf(os, indent);
233 
234  os << indent << "Boundary: " << static_cast<typename NumericTraits<PixelType>::PrintType>( m_Boundary ) << std::endl;
235  os << indent << "Algorithm: " << m_Algorithm << std::endl;
236 }
237 
238 }// end namespace itk
239 #endif

Generated at Sat Feb 2 2013 23:57:06 for Orfeo Toolbox with doxygen 1.8.1.1