1 #include "FloodFillImageFilter.h"
2 #include <cpPlugins/Interface/Image.h>
3 #include <cpPlugins/Interface/PointList.h>
5 #include <itkFloodFilledImageFunctionConditionalConstIterator.h>
6 #include <itkImageFunction.h>
8 // -------------------------------------------------------------------------
9 cpPlugins::BasicFilters::FloodFillImageFilter::
10 FloodFillImageFilter( )
14 this->_AddInput( "Input" );
15 this->_AddInput( "Seed" );
16 this->_AddOutput< cpPlugins::Interface::Image >( "Output" );
18 this->m_Parameters->ConfigureAsReal( "Window" );
19 this->m_Parameters->ConfigureAsReal( "Level" );
20 this->m_Parameters->ConfigureAsUint( "InsideValue" );
21 this->m_Parameters->ConfigureAsUint( "OutsideValue" );
23 this->m_Parameters->SetReal( "Window", 0 );
24 this->m_Parameters->SetReal( "Level", 0 );
25 this->m_Parameters->SetUint( "InsideValue", 0 );
26 this->m_Parameters->SetUint( "OutsideValue", 255 );
30 // -------------------------------------------------------------------------
31 cpPlugins::BasicFilters::FloodFillImageFilter::
32 ~FloodFillImageFilter( )
36 // -------------------------------------------------------------------------
37 std::string cpPlugins::BasicFilters::FloodFillImageFilter::
41 cpPlugins::Interface::Image* image =
42 this->GetInput< cpPlugins::Interface::Image >( "Input" );
44 return( "FloodFillImageFilter: No input image." );
46 itk::DataObject* itk_image = NULL;
48 cpPlugins_Image_Demangle_AllScalarTypes( 2, image, itk_image, r, _GD0 );
49 else cpPlugins_Image_Demangle_AllScalarTypes( 3, image, itk_image, r, _GD0 );
50 else r = "FloodFillImageFilter: Input image type not supported.";
56 // -------------------------------------------------------------------------
58 std::string cpPlugins::BasicFilters::FloodFillImageFilter::
59 _GD0( itk::DataObject* image )
63 this->_RealGD< I, itk::Image< unsigned char, I::ImageDimension > >(
71 // -------------------------------------------------------------------------
73 template< class I, class R = float >
74 class cpPlugins_BasicFilters_FloodFillImageFilter_Function
75 : public itk::ImageFunction< I, bool, R >
78 typedef cpPlugins_BasicFilters_FloodFillImageFilter_Function Self;
79 typedef itk::ImageFunction< I, bool, R > Superclass;
80 typedef itk::SmartPointer< Self > Pointer;
81 typedef itk::SmartPointer< const Self > ConstPointer;
83 typedef typename Superclass::PointType TPoint;
84 typedef typename Superclass::IndexType TIndex;
85 typedef typename Superclass::ContinuousIndexType TCIndex;
90 cpPlugins_BasicFilters_FloodFillImageFilter_Function,
94 itkSetMacro( Window, double );
95 itkSetMacro( Level, double );
98 virtual bool Evaluate( const TPoint& point ) const
102 virtual bool EvaluateAtIndex( const TIndex& index ) const
104 if( !( this->IsInsideBuffer( index ) ) )
107 const I* image = this->GetInputImage( );
108 double w2 = this->m_Window / double( 2 );
109 double min = this->m_Level - w2;
110 double max = this->m_Level + w2;
111 unsigned char val = double( 0 );
112 double x = double( image->GetPixel( index ) );
113 double m = double( 100 ) / this->m_Window;
114 double b = ( this->m_Window - ( double( 2 ) * this->m_Level ) );
115 b *= double( 50 ) / this->m_Window;
116 if( x > min && x < max )
117 val = ( unsigned char )( ( m * x ) + b );
121 this->m_StartValue = val;
122 this->m_Start = false;
126 return( std::abs( this->m_StartValue - val ) <= 2 );
128 virtual bool EvaluateAtContinuousIndex( const TCIndex& index ) const
134 cpPlugins_BasicFilters_FloodFillImageFilter_Function( )
136 m_Window( double( 0 ) ),
137 m_Level( double( 0 ) ),
141 virtual ~cpPlugins_BasicFilters_FloodFillImageFilter_Function( )
146 // Purposely not implemented
147 cpPlugins_BasicFilters_FloodFillImageFilter_Function( const Self& other );
148 Self& operator=( const Self& other );
153 mutable unsigned char m_StartValue;
154 mutable bool m_Start;
158 // -------------------------------------------------------------------------
159 template< class I, class O >
160 inline std::string cpPlugins::BasicFilters::FloodFillImageFilter::
161 _RealGD( itk::DataObject* image )
164 typedef typename O::PixelType _OP;
165 typedef cpPlugins_BasicFilters_FloodFillImageFilter_Function< I > _F;
166 typedef itk::FloodFilledImageFunctionConditionalConstIterator< I, _F > _It;
168 typename I::PointType pseed;
169 pseed = this->m_Parameters->GetPoint< typename I::PointType >(
170 "Seed", I::ImageDimension
172 double window = this->m_Parameters->GetReal( "Window" );
173 double level = this->m_Parameters->GetReal( "Level" );
174 _OP in_val = _OP( this->m_Parameters->GetUint( "InsideValue" ) );
175 _OP out_val = _OP( this->m_Parameters->GetUint( "OutsideValue" ) );
177 const I* in = dynamic_cast< const I* >( image );
178 typename I::IndexType seed;
179 in->TransformPhysicalPointToIndex( pseed, seed );
181 typename O::Pointer out = O::New( );
182 out->SetLargestPossibleRegion( in->GetLargestPossibleRegion( ) );
183 out->SetRequestedRegion( in->GetRequestedRegion( ) );
184 out->SetBufferedRegion( in->GetBufferedRegion( ) );
185 out->SetOrigin( in->GetOrigin( ) );
186 out->SetDirection( in->GetDirection( ) );
187 out->SetSpacing( in->GetSpacing( ) );
189 out->FillBuffer( out_val );
191 typename _F::Pointer f = _F::New( );
192 f->SetInputImage( in );
193 f->SetWindow( window );
194 f->SetLevel( level );
198 for( i.GoToBegin( ); !i.IsAtEnd( ); ++i )
199 out->SetPixel( i.GetIndex( ), in_val );
202 cpPlugins::Interface::Image* out_port =
203 this->GetOutput< cpPlugins::Interface::Image >( "Output" );
204 if( out_port != NULL )
206 out_port->SetITK< O >( out );
210 return( "FloodFillImageFilter: output not correctly created." );