1 #include "DoubleFloodImageFilter.h"
2 #include <cpPlugins/Interface/Image.h>
3 #include <cpPlugins/Interface/BaseApplication.h>
4 #include <cpPlugins/Interface/Plugins.h>
5 #include <cpExtensions/Interaction/ImageInteractorStyle.h>
6 #include <cpExtensions/Visualization/ImageSliceActors.h>
8 #include <vtkRenderWindowInteractor.h>
10 #include <itkFloodFilledImageFunctionConditionalConstIterator.h>
11 #include <itkImageFunction.h>
13 #ifdef cpPlugins_Interface_QT4
14 #include <QDialogButtonBox>
16 // -------------------------------------------------------------------------
17 cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
18 DoubleFloodImageFilter_Dialog(
19 QWidget* parent, DoubleFloodImageFilter* filter, Qt::WindowFlags f
21 : QDialog( parent, f | Qt::WindowStaysOnTopHint ),
24 this->m_Title = new QLabel( this );
25 this->m_Title->setText( "Execute simple vessel segmentation filter" );
27 this->m_MainLayout = new QGridLayout( this );
28 this->m_ToolsLayout = new QVBoxLayout( );
29 this->m_ToolsLayout->addWidget( this->m_Title );
30 this->m_MainLayout->addLayout( this->m_ToolsLayout, 0, 0, 1, 1 );
33 QDialogButtonBox* bb = new QDialogButtonBox(
34 QDialogButtonBox::Cancel | QDialogButtonBox::Ok
36 QObject::connect( bb, SIGNAL( accepted( ) ), this, SLOT( accept( ) ) );
37 QObject::connect( bb, SIGNAL( rejected( ) ), this, SLOT( reject( ) ) );
38 this->m_ToolsLayout->addWidget( bb );
41 // -------------------------------------------------------------------------
42 cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
43 ~DoubleFloodImageFilter_Dialog( )
46 delete this->m_ToolsLayout;
47 delete this->m_MainLayout;
50 // -------------------------------------------------------------------------
51 void cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
54 // Get interactive widget
55 if( this->m_Filter == NULL )
58 typedef cpExtensions::Interaction::ImageInteractorStyle _TImageStyle;
59 typedef cpExtensions::Visualization::ImageSliceActors _TImageActors;
62 unsigned int nTotalSeeds = 0;
63 auto iIt = this->m_Filter->m_Interactors.begin( );
64 for( ; iIt != this->m_Filter->m_Interactors.end( ); ++iIt )
66 _TImageStyle* istyle =
67 dynamic_cast< _TImageStyle* >(
68 ( *iIt )->GetInteractorStyle( )
74 _TImageActors* actors =
75 dynamic_cast< _TImageActors* >( istyle->GetAssociatedObject( ) );
78 this->m_Filter->GetParameters( )->SetReal( "Window", actors->GetWindow( ) );
79 this->m_Filter->GetParameters( )->SetReal( "Level", actors->GetLevel( ) );
83 unsigned int nSeeds = istyle->GetNumberOfSeeds( );
84 for( unsigned int s = 0; s < nSeeds; ++s )
89 istyle->GetSeed( s, seed );
90 if( nTotalSeeds == 0 )
91 this->m_Filter->GetParameters( )->SetPoint( "Seed0", 3, seed );
93 this->m_Filter->GetParameters( )->SetPoint( "Seed1", 3, seed );
103 auto plugins = this->m_Filter->GetPlugins( );
104 if( plugins != NULL )
106 auto app = plugins->GetApplication( );
108 app->UpdateActualFilter( );
113 // -------------------------------------------------------------------------
114 void cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
117 auto plugins = this->m_Filter->GetPlugins( );
118 if( plugins != NULL )
119 plugins->DeactivateFilter( );
120 this->Superclass::reject( );
123 #endif // cpPlugins_Interface_QT4
125 // -------------------------------------------------------------------------
126 cpPlugins::BasicFilters::DoubleFloodImageFilter::
127 DialogResult cpPlugins::BasicFilters::DoubleFloodImageFilter::
128 ExecConfigurationDialog( QWidget* parent )
130 #ifdef cpPlugins_Interface_QT4
132 typedef cpExtensions::Interaction::ImageInteractorStyle _TImageStyle;
134 // Activate seed widgets
135 bool at_least_one = false;
136 auto iIt = this->m_Interactors.begin( );
137 for( ; iIt != this->m_Interactors.end( ); ++iIt )
139 _TImageStyle* istyle =
140 dynamic_cast< _TImageStyle* >(
141 ( *iIt )->GetInteractorStyle( )
145 istyle->SeedWidgetOn( );
152 return( Self::DialogResult_Cancel );
155 this->m_Dialog = new DoubleFloodImageFilter_Dialog( NULL, this );
156 this->m_Dialog->show( );
158 return( Self::DialogResult_Modal );
159 #else // cpPlugins_Interface_QT4
160 return( Self::DialogResult_Cancel );
161 #endif // cpPlugins_Interface_QT4
164 // -------------------------------------------------------------------------
165 cpPlugins::BasicFilters::DoubleFloodImageFilter::
166 DoubleFloodImageFilter( )
169 this->_AddInput( "Input" );
170 this->_MakeOutput< cpPlugins::Interface::Image >( "Output" );
172 double seed[ 3 ] = { double( 0 ) };
173 this->m_Parameters->ConfigureAsPoint( "Seed0", 3, seed );
174 this->m_Parameters->ConfigureAsPoint( "Seed1", 3, seed );
175 this->m_Parameters->ConfigureAsReal( "Window", 0 );
176 this->m_Parameters->ConfigureAsReal( "Level", 0 );
177 this->m_Parameters->ConfigureAsUint( "InsideValue", 255 );
178 this->m_Parameters->ConfigureAsUint( "OutsideValue", 0 );
181 // -------------------------------------------------------------------------
182 cpPlugins::BasicFilters::DoubleFloodImageFilter::
183 ~DoubleFloodImageFilter( )
187 // -------------------------------------------------------------------------
188 std::string cpPlugins::BasicFilters::DoubleFloodImageFilter::
191 cpPlugins::Interface::Image* image =
192 this->GetInput< cpPlugins::Interface::Image >( "Input" );
194 return( "DoubleFloodImageFilter: No input image." );
196 itk::DataObject* itk_image = NULL;
198 cpPlugins_Image_Demangle_AllScalarTypes( 2, image, itk_image, r, _GD0 );
199 else cpPlugins_Image_Demangle_AllScalarTypes( 3, image, itk_image, r, _GD0 );
200 else r = "DoubleFloodImageFilter: Input image type not supported.";
204 // -------------------------------------------------------------------------
206 std::string cpPlugins::BasicFilters::DoubleFloodImageFilter::
207 _GD0( itk::DataObject* image )
210 this->_RealGD< I, itk::Image< unsigned char, I::ImageDimension > >(
216 // -------------------------------------------------------------------------
217 template< class I, class R = float >
218 class cpPlugins_BasicFilters_DoubleFloodImageFilter_Function
219 : public itk::ImageFunction< I, bool, R >
222 typedef cpPlugins_BasicFilters_DoubleFloodImageFilter_Function Self;
223 typedef itk::ImageFunction< I, bool, R > Superclass;
224 typedef itk::SmartPointer< Self > Pointer;
225 typedef itk::SmartPointer< const Self > ConstPointer;
227 typedef typename Superclass::PointType TPoint;
228 typedef typename Superclass::IndexType TIndex;
229 typedef typename Superclass::ContinuousIndexType TCIndex;
234 cpPlugins_BasicFilters_DoubleFloodImageFilter_Function,
238 itkSetMacro( Window, double );
239 itkSetMacro( Level, double );
242 virtual bool Evaluate( const TPoint& point ) const
246 virtual bool EvaluateAtIndex( const TIndex& index ) const
248 if( !( this->IsInsideBuffer( index ) ) )
251 const I* image = this->GetInputImage( );
252 double w2 = this->m_Window / double( 2 );
253 double min = this->m_Level - w2;
254 double max = this->m_Level + w2;
255 unsigned char val = double( 0 );
256 double x = double( image->GetPixel( index ) );
257 double m = double( 100 ) / this->m_Window;
258 double b = ( this->m_Window - ( double( 2 ) * this->m_Level ) );
259 b *= double( 50 ) / this->m_Window;
260 if( x > min && x < max )
261 val = ( unsigned char )( ( m * x ) + b );
265 this->m_StartValue = val;
266 this->m_Start = false;
270 return( std::abs( this->m_StartValue - val ) <= 2 );
272 virtual bool EvaluateAtContinuousIndex( const TCIndex& index ) const
278 cpPlugins_BasicFilters_DoubleFloodImageFilter_Function( )
280 m_Window( double( 0 ) ),
281 m_Level( double( 0 ) ),
285 virtual ~cpPlugins_BasicFilters_DoubleFloodImageFilter_Function( )
290 // Purposely not implemented
291 cpPlugins_BasicFilters_DoubleFloodImageFilter_Function( const Self& other );
292 Self& operator=( const Self& other );
297 mutable unsigned char m_StartValue;
298 mutable bool m_Start;
301 // -------------------------------------------------------------------------
302 template< class I, class O >
303 inline std::string cpPlugins::BasicFilters::DoubleFloodImageFilter::
304 _RealGD( itk::DataObject* image )
306 typedef typename O::PixelType _OP;
307 typedef cpPlugins_BasicFilters_DoubleFloodImageFilter_Function< I > _F;
308 typedef itk::FloodFilledImageFunctionConditionalConstIterator< I, _F > _It;
310 typename I::PointType pseed0, pseed1;
311 pseed0 = this->m_Parameters->GetPoint< typename I::PointType >(
312 "Seed0", I::ImageDimension
314 pseed1 = this->m_Parameters->GetPoint< typename I::PointType >(
315 "Seed1", I::ImageDimension
317 double window = this->m_Parameters->GetReal( "Window" );
318 double level = this->m_Parameters->GetReal( "Level" );
319 _OP in_val = _OP( this->m_Parameters->GetUint( "InsideValue" ) );
320 _OP out_val = _OP( this->m_Parameters->GetUint( "OutsideValue" ) );
322 const I* in = dynamic_cast< const I* >( image );
323 typename I::IndexType seed0, seed1;
324 in->TransformPhysicalPointToIndex( pseed0, seed0 );
325 in->TransformPhysicalPointToIndex( pseed1, seed1 );
327 typename O::Pointer out = O::New( );
328 out->SetLargestPossibleRegion( in->GetLargestPossibleRegion( ) );
329 out->SetRequestedRegion( in->GetRequestedRegion( ) );
330 out->SetBufferedRegion( in->GetBufferedRegion( ) );
331 out->SetOrigin( in->GetOrigin( ) );
332 out->SetDirection( in->GetDirection( ) );
333 out->SetSpacing( in->GetSpacing( ) );
335 out->FillBuffer( out_val );
337 typename _F::Pointer f = _F::New( );
338 f->SetInputImage( in );
339 f->SetWindow( window );
340 f->SetLevel( level );
345 for( i.GoToBegin( ); !i.IsAtEnd( ); ++i )
346 out->SetPixel( i.GetIndex( ), in_val );
349 cpPlugins::Interface::Image* out_port =
350 this->GetOutput< cpPlugins::Interface::Image >( "Output" );
351 if( out_port != NULL )
353 out_port->SetITK< O >( out );
357 return( "DoubleFloodImageFilter: output not correctly created." );