1 #include "DoubleFloodImageFilter.h"
2 #include <cpPlugins/Interface/Image.h>
3 #include <cpExtensions/Interaction/ImageInteractorStyle.h>
4 #include <cpExtensions/Visualization/ImageSliceActors.h>
6 #include <vtkRenderWindowInteractor.h>
8 #include <itkFloodFilledImageFunctionConditionalConstIterator.h>
9 #include <itkImageFunction.h>
11 #ifdef cpPlugins_Interface_QT4
12 #include <QDialogButtonBox>
14 // -------------------------------------------------------------------------
15 cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
16 DoubleFloodImageFilter_Dialog(
17 QWidget* parent, DoubleFloodImageFilter* filter, Qt::WindowFlags f
19 : QDialog( parent, f | Qt::WindowStaysOnTopHint ),
23 this->m_Title = new QLabel( this );
24 this->m_Title->setText( "Execute simple vessel segmentation filter" );
26 this->m_MainLayout = new QGridLayout( this );
27 this->m_ToolsLayout = new QVBoxLayout( );
28 this->m_ToolsLayout->addWidget( this->m_Title );
29 this->m_MainLayout->addLayout( this->m_ToolsLayout, 0, 0, 1, 1 );
32 QDialogButtonBox* bb = new QDialogButtonBox(
33 QDialogButtonBox::Cancel | QDialogButtonBox::Ok
35 QObject::connect( bb, SIGNAL( accepted( ) ), this, SLOT( accept( ) ) );
36 QObject::connect( bb, SIGNAL( rejected( ) ), this, SLOT( reject( ) ) );
37 this->m_ToolsLayout->addWidget( bb );
41 // -------------------------------------------------------------------------
42 cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
43 ~DoubleFloodImageFilter_Dialog( )
47 delete this->m_ToolsLayout;
48 delete this->m_MainLayout;
52 // -------------------------------------------------------------------------
53 void cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
57 // Get interactive widget
58 if( this->m_Filter == NULL )
61 typedef cpExtensions::Interaction::ImageInteractorStyle _TImageStyle;
62 typedef cpExtensions::Visualization::ImageSliceActors _TImageActors;
65 unsigned int nTotalSeeds = 0;
66 auto iIt = this->m_Filter->m_Interactors.begin( );
67 for( ; iIt != this->m_Filter->m_Interactors.end( ); ++iIt )
69 _TImageStyle* istyle =
70 dynamic_cast< _TImageStyle* >(
71 ( *iIt )->GetInteractorStyle( )
77 _TImageActors* actors =
78 dynamic_cast< _TImageActors* >( istyle->GetAssociatedObject( ) );
81 this->m_Filter->GetParameters( )->SetReal( "Window", actors->GetWindow( ) );
82 this->m_Filter->GetParameters( )->SetReal( "Level", actors->GetLevel( ) );
86 unsigned int nSeeds = istyle->GetNumberOfSeeds( );
87 for( unsigned int s = 0; s < nSeeds; ++s )
92 istyle->GetSeedAsPoint( s, seed );
93 if( nTotalSeeds == 0 )
94 this->m_Filter->GetParameters( )->SetPoint( "Seed0", 3, seed );
96 this->m_Filter->GetParameters( )->SetPoint( "Seed1", 3, seed );
106 auto plugins = this->m_Filter->GetPlugins( );
107 if( plugins != NULL )
109 auto app = plugins->GetApplication( );
111 app->UpdateActualFilter( );
117 // -------------------------------------------------------------------------
118 void cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
122 auto plugins = this->m_Filter->GetPlugins( );
123 if( plugins != NULL )
124 plugins->DeactivateFilter( );
125 this->Superclass::reject( );
128 #endif // cpPlugins_Interface_QT4
130 // -------------------------------------------------------------------------
131 cpPlugins::BasicFilters::DoubleFloodImageFilter::
132 DialogResult cpPlugins::BasicFilters::DoubleFloodImageFilter::
133 ExecConfigurationDialog( QWidget* parent )
136 #ifdef cpPlugins_Interface_QT4
138 typedef cpExtensions::Interaction::ImageInteractorStyle _TImageStyle;
140 // Activate seed widgets
141 bool at_least_one = false;
142 auto iIt = this->m_Interactors.begin( );
143 for( ; iIt != this->m_Interactors.end( ); ++iIt )
145 _TImageStyle* istyle =
146 dynamic_cast< _TImageStyle* >(
147 ( *iIt )->GetInteractorStyle( )
151 istyle->SeedWidgetOn( );
161 this->m_Dialog = new DoubleFloodImageFilter_Dialog( NULL, this );
162 this->m_Dialog->show( );
165 #else // cpPlugins_Interface_QT4
167 #endif // cpPlugins_Interface_QT4
172 // -------------------------------------------------------------------------
173 cpPlugins::BasicFilters::DoubleFloodImageFilter::
174 DoubleFloodImageFilter( )
178 this->_AddInput( "Input" );
179 this->_MakeOutput< cpPlugins::Interface::Image >( "Output" );
181 this->m_Parameters->ConfigureAsPoint( "Seed0" );
182 this->m_Parameters->ConfigureAsPoint( "Seed1" );
183 this->m_Parameters->ConfigureAsReal( "Window" );
184 this->m_Parameters->ConfigureAsReal( "Level" );
185 this->m_Parameters->ConfigureAsUint( "InsideValue" );
186 this->m_Parameters->ConfigureAsUint( "OutsideValue" );
188 this->m_Parameters->SetReal( "Window", 0 );
189 this->m_Parameters->SetReal( "Level", 0 );
190 this->m_Parameters->SetUint( "InsideValue", 255 );
191 this->m_Parameters->SetUint( "OutsideValue", 0 );
195 // -------------------------------------------------------------------------
196 cpPlugins::BasicFilters::DoubleFloodImageFilter::
197 ~DoubleFloodImageFilter( )
201 // -------------------------------------------------------------------------
202 std::string cpPlugins::BasicFilters::DoubleFloodImageFilter::
206 cpPlugins::Interface::Image* image =
207 this->GetInput< cpPlugins::Interface::Image >( "Input" );
209 return( "DoubleFloodImageFilter: No input image." );
211 itk::DataObject* itk_image = NULL;
213 cpPlugins_Image_Demangle_AllScalarTypes( 2, image, itk_image, r, _GD0 );
214 else cpPlugins_Image_Demangle_AllScalarTypes( 3, image, itk_image, r, _GD0 );
215 else r = "DoubleFloodImageFilter: Input image type not supported.";
221 // -------------------------------------------------------------------------
223 std::string cpPlugins::BasicFilters::DoubleFloodImageFilter::
224 _GD0( itk::DataObject* image )
228 this->_RealGD< I, itk::Image< unsigned char, I::ImageDimension > >(
237 // -------------------------------------------------------------------------
239 template< class I, class R = float >
240 class cpPlugins_BasicFilters_DoubleFloodImageFilter_Function
241 : public itk::ImageFunction< I, bool, R >
244 typedef cpPlugins_BasicFilters_DoubleFloodImageFilter_Function Self;
245 typedef itk::ImageFunction< I, bool, R > Superclass;
246 typedef itk::SmartPointer< Self > Pointer;
247 typedef itk::SmartPointer< const Self > ConstPointer;
249 typedef typename Superclass::PointType TPoint;
250 typedef typename Superclass::IndexType TIndex;
251 typedef typename Superclass::ContinuousIndexType TCIndex;
256 cpPlugins_BasicFilters_DoubleFloodImageFilter_Function,
260 itkSetMacro( Window, double );
261 itkSetMacro( Level, double );
264 virtual bool Evaluate( const TPoint& point ) const
268 virtual bool EvaluateAtIndex( const TIndex& index ) const
270 if( !( this->IsInsideBuffer( index ) ) )
273 const I* image = this->GetInputImage( );
274 double w2 = this->m_Window / double( 2 );
275 double min = this->m_Level - w2;
276 double max = this->m_Level + w2;
277 unsigned char val = double( 0 );
278 double x = double( image->GetPixel( index ) );
279 double m = double( 100 ) / this->m_Window;
280 double b = ( this->m_Window - ( double( 2 ) * this->m_Level ) );
281 b *= double( 50 ) / this->m_Window;
282 if( x > min && x < max )
283 val = ( unsigned char )( ( m * x ) + b );
287 this->m_StartValue = val;
288 this->m_Start = false;
292 return( std::abs( this->m_StartValue - val ) <= 2 );
294 virtual bool EvaluateAtContinuousIndex( const TCIndex& index ) const
300 cpPlugins_BasicFilters_DoubleFloodImageFilter_Function( )
302 m_Window( double( 0 ) ),
303 m_Level( double( 0 ) ),
307 virtual ~cpPlugins_BasicFilters_DoubleFloodImageFilter_Function( )
312 // Purposely not implemented
313 cpPlugins_BasicFilters_DoubleFloodImageFilter_Function( const Self& other );
314 Self& operator=( const Self& other );
319 mutable unsigned char m_StartValue;
320 mutable bool m_Start;
324 // -------------------------------------------------------------------------
325 template< class I, class O >
326 inline std::string cpPlugins::BasicFilters::DoubleFloodImageFilter::
327 _RealGD( itk::DataObject* image )
330 typedef typename O::PixelType _OP;
331 typedef cpPlugins_BasicFilters_DoubleFloodImageFilter_Function< I > _F;
332 typedef itk::FloodFilledImageFunctionConditionalConstIterator< I, _F > _It;
334 typename I::PointType pseed0, pseed1;
335 pseed0 = this->m_Parameters->GetPoint< typename I::PointType >(
336 "Seed0", I::ImageDimension
338 pseed1 = this->m_Parameters->GetPoint< typename I::PointType >(
339 "Seed1", I::ImageDimension
341 double window = this->m_Parameters->GetReal( "Window" );
342 double level = this->m_Parameters->GetReal( "Level" );
343 _OP in_val = _OP( this->m_Parameters->GetUint( "InsideValue" ) );
344 _OP out_val = _OP( this->m_Parameters->GetUint( "OutsideValue" ) );
346 const I* in = dynamic_cast< const I* >( image );
347 typename I::IndexType seed0, seed1;
348 in->TransformPhysicalPointToIndex( pseed0, seed0 );
349 in->TransformPhysicalPointToIndex( pseed1, seed1 );
351 typename O::Pointer out = O::New( );
352 out->SetLargestPossibleRegion( in->GetLargestPossibleRegion( ) );
353 out->SetRequestedRegion( in->GetRequestedRegion( ) );
354 out->SetBufferedRegion( in->GetBufferedRegion( ) );
355 out->SetOrigin( in->GetOrigin( ) );
356 out->SetDirection( in->GetDirection( ) );
357 out->SetSpacing( in->GetSpacing( ) );
359 out->FillBuffer( out_val );
361 typename _F::Pointer f = _F::New( );
362 f->SetInputImage( in );
363 f->SetWindow( window );
364 f->SetLevel( level );
369 for( i.GoToBegin( ); !i.IsAtEnd( ); ++i )
370 out->SetPixel( i.GetIndex( ), in_val );
373 cpPlugins::Interface::Image* out_port =
374 this->GetOutput< cpPlugins::Interface::Image >( "Output" );
375 if( out_port != NULL )
377 out_port->SetITK< O >( out );
381 return( "DoubleFloodImageFilter: output not correctly created." );