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>
7 #include <vtkRenderWindowInteractor.h>
9 #include <itkFloodFilledImageFunctionConditionalConstIterator.h>
10 #include <itkImageFunction.h>
12 #ifdef cpPlugins_Interface_QT4
13 #include <QDialogButtonBox>
15 // -------------------------------------------------------------------------
16 cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
17 DoubleFloodImageFilter_Dialog(
18 QWidget* parent, DoubleFloodImageFilter* filter, Qt::WindowFlags f
20 : 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 );
40 // -------------------------------------------------------------------------
41 cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
42 ~DoubleFloodImageFilter_Dialog( )
45 delete this->m_ToolsLayout;
46 delete this->m_MainLayout;
49 // -------------------------------------------------------------------------
50 void cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
53 // Get interactive widget
54 if( this->m_Filter == NULL )
57 typedef cpExtensions::Interaction::ImageInteractorStyle _TImageStyle;
59 // Activate seed widgets
60 auto iIt = this->m_Filter->m_Interactors.begin( );
61 for( ; iIt != this->m_Filter->m_Interactors.end( ); ++iIt )
63 _TImageStyle* istyle =
64 dynamic_cast< _TImageStyle* >(
65 ( *iIt )->GetInteractorStyle( )
69 unsigned int nSeeds = istyle->GetNumberOfSeeds( );
70 for( unsigned int s = 0; s < nSeeds; ++s )
73 istyle->GetSeed( s, seed );
79 vtkPlaneWidget* wdg = this->m_Filter->m_PlaneWidget;
83 // Get/Set plane parameters
84 double center[ 3 ], normal[ 3 ];
85 wdg->GetCenter( center );
86 wdg->GetNormal( normal );
88 this->m_Filter->GetParameters( )->SetPoint( "PlaneCenter", 3, center );
89 this->m_Filter->GetParameters( )->SetVector( "PlaneNormal", 3, normal );
92 auto plugins = this->m_Filter->GetPlugins( );
95 auto app = plugins->GetApplication( );
97 app->UpdateActualFilter( );
103 // -------------------------------------------------------------------------
104 void cpPlugins::BasicFilters::DoubleFloodImageFilter_Dialog::
107 auto plugins = this->m_Filter->GetPlugins( );
108 if( plugins != NULL )
109 plugins->DeactivateFilter( );
110 this->Superclass::reject( );
113 #endif // cpPlugins_Interface_QT4
115 // -------------------------------------------------------------------------
116 cpPlugins::BasicFilters::DoubleFloodImageFilter::
117 DialogResult cpPlugins::BasicFilters::DoubleFloodImageFilter::
118 ExecConfigurationDialog( QWidget* parent )
120 #ifdef cpPlugins_Interface_QT4
122 typedef cpExtensions::Interaction::ImageInteractorStyle _TImageStyle;
124 // Activate seed widgets
125 bool at_least_one = false;
126 auto iIt = this->m_Interactors.begin( );
127 for( ; iIt != this->m_Interactors.end( ); ++iIt )
129 _TImageStyle* istyle =
130 dynamic_cast< _TImageStyle* >(
131 ( *iIt )->GetInteractorStyle( )
135 istyle->SeedWidgetOn( );
142 return( Self::DialogResult_Cancel );
145 this->m_Dialog = new DoubleFloodImageFilter_Dialog( NULL, this );
146 this->m_Dialog->show( );
148 return( Self::DialogResult_Modal );
149 #else // cpPlugins_Interface_QT4
150 return( Self::DialogResult_Cancel );
151 #endif // cpPlugins_Interface_QT4
154 // -------------------------------------------------------------------------
155 cpPlugins::BasicFilters::DoubleFloodImageFilter::
156 DoubleFloodImageFilter( )
159 this->_AddInput( "Input" );
160 this->_MakeOutput< cpPlugins::Interface::Image >( "Output" );
162 double seed[ 3 ] = { double( 0 ) };
163 this->m_Parameters->ConfigureAsPoint( "Seed0", 3, seed );
164 this->m_Parameters->ConfigureAsPoint( "Seed1", 3, seed );
165 this->m_Parameters->ConfigureAsReal( "Window", 0 );
166 this->m_Parameters->ConfigureAsReal( "Level", 0 );
167 this->m_Parameters->ConfigureAsUint( "InsideValue", 255 );
168 this->m_Parameters->ConfigureAsUint( "OutsideValue", 0 );
171 // -------------------------------------------------------------------------
172 cpPlugins::BasicFilters::DoubleFloodImageFilter::
173 ~DoubleFloodImageFilter( )
177 // -------------------------------------------------------------------------
178 std::string cpPlugins::BasicFilters::DoubleFloodImageFilter::
181 cpPlugins::Interface::Image* image =
182 this->GetInput< cpPlugins::Interface::Image >( "Input" );
184 return( "DoubleFloodImageFilter: No input image." );
186 itk::DataObject* itk_image = NULL;
188 cpPlugins_Image_Demangle_AllScalarTypes( 2, image, itk_image, r, _GD0 );
189 else cpPlugins_Image_Demangle_AllScalarTypes( 3, image, itk_image, r, _GD0 );
190 else r = "DoubleFloodImageFilter: Input image type not supported.";
194 // -------------------------------------------------------------------------
196 std::string cpPlugins::BasicFilters::DoubleFloodImageFilter::
197 _GD0( itk::DataObject* image )
200 this->_RealGD< I, itk::Image< unsigned char, I::ImageDimension > >(
206 // -------------------------------------------------------------------------
207 template< class I, class R = float >
208 class cpPlugins_BasicFilters_DoubleFloodImageFilter_Function
209 : public itk::ImageFunction< I, bool, R >
212 typedef cpPlugins_BasicFilters_DoubleFloodImageFilter_Function Self;
213 typedef itk::ImageFunction< I, bool, R > Superclass;
214 typedef itk::SmartPointer< Self > Pointer;
215 typedef itk::SmartPointer< const Self > ConstPointer;
217 typedef typename Superclass::PointType TPoint;
218 typedef typename Superclass::IndexType TIndex;
219 typedef typename Superclass::ContinuousIndexType TCIndex;
224 cpPlugins_BasicFilters_DoubleFloodImageFilter_Function,
228 itkSetMacro( Window, double );
229 itkSetMacro( Level, double );
232 virtual bool Evaluate( const TPoint& point ) const
236 virtual bool EvaluateAtIndex( const TIndex& index ) const
238 if( !( this->IsInsideBuffer( index ) ) )
241 const I* image = this->GetInputImage( );
242 double w2 = this->m_Window / double( 2 );
243 double min = this->m_Level - w2;
244 double max = this->m_Level + w2;
245 unsigned char val = double( 0 );
246 double x = double( image->GetPixel( index ) );
247 double m = double( 100 ) / this->m_Window;
248 double b = ( this->m_Window - ( double( 2 ) * this->m_Level ) );
249 b *= double( 50 ) / this->m_Window;
250 if( x > min && x < max )
251 val = ( unsigned char )( ( m * x ) + b );
255 this->m_StartValue = val;
256 this->m_Start = false;
260 return( std::abs( this->m_StartValue - val ) <= 2 );
262 virtual bool EvaluateAtContinuousIndex( const TCIndex& index ) const
268 cpPlugins_BasicFilters_DoubleFloodImageFilter_Function( )
270 m_Window( double( 0 ) ),
271 m_Level( double( 0 ) ),
275 virtual ~cpPlugins_BasicFilters_DoubleFloodImageFilter_Function( )
280 // Purposely not implemented
281 cpPlugins_BasicFilters_DoubleFloodImageFilter_Function( const Self& other );
282 Self& operator=( const Self& other );
287 mutable unsigned char m_StartValue;
288 mutable bool m_Start;
291 // -------------------------------------------------------------------------
292 template< class I, class O >
293 inline std::string cpPlugins::BasicFilters::DoubleFloodImageFilter::
294 _RealGD( itk::DataObject* image )
297 typedef typename O::PixelType _OP;
298 typedef cpPlugins_BasicFilters_DoubleFloodImageFilter_Function< I > _F;
299 typedef itk::FloodFilledImageFunctionConditionalConstIterator< I, _F > _It;
301 typename I::PointType pseed;
302 pseed = this->m_Parameters->GetPoint< typename I::PointType >(
303 "Seed", I::ImageDimension
305 double window = this->m_Parameters->GetReal( "Window" );
306 double level = this->m_Parameters->GetReal( "Level" );
307 _OP in_val = _OP( this->m_Parameters->GetUint( "InsideValue" ) );
308 _OP out_val = _OP( this->m_Parameters->GetUint( "OutsideValue" ) );
310 const I* in = dynamic_cast< const I* >( image );
311 typename I::IndexType seed;
312 in->TransformPhysicalPointToIndex( pseed, seed );
314 typename O::Pointer out = O::New( );
315 out->SetLargestPossibleRegion( in->GetLargestPossibleRegion( ) );
316 out->SetRequestedRegion( in->GetRequestedRegion( ) );
317 out->SetBufferedRegion( in->GetBufferedRegion( ) );
318 out->SetOrigin( in->GetOrigin( ) );
319 out->SetDirection( in->GetDirection( ) );
320 out->SetSpacing( in->GetSpacing( ) );
322 out->FillBuffer( out_val );
324 typename _F::Pointer f = _F::New( );
325 f->SetInputImage( in );
326 f->SetWindow( window );
327 f->SetLevel( level );
331 for( i.GoToBegin( ); !i.IsAtEnd( ); ++i )
332 out->SetPixel( i.GetIndex( ), in_val );
335 cpPlugins::Interface::Image* out_port =
336 this->GetOutput< cpPlugins::Interface::Image >( "Output" );
337 if( out_port != NULL )
339 out_port->SetITK< O >( out );
344 return( "DoubleFloodImageFilter: output not correctly created." );