6 #include <itkImageFileReader.h>
7 #include <itkSignedDanielssonDistanceMapImageFilter.h>
8 #include <itkImageToVTKImageFilter.h>
10 #include <vtkImageActor.h>
11 #include <vtkInteractorStyleImage.h>
12 #include <vtkPointHandleRepresentation3D.h>
13 #include <vtkProperty.h>
14 #include <vtkRenderer.h>
15 #include <vtkRenderWindow.h>
16 #include <vtkRenderWindowInteractor.h>
17 #include <vtkSeedRepresentation.h>
18 #include <vtkSeedWidget.h>
19 #include <vtkSmartPointer.h>
21 #include <fpa/Image/RegionGrow.h>
22 #include <fpa/Image/Functors/RegionGrowAllBelongsFunction.h>
23 #include <fpa/VTK/Image2DObserver.h>
24 #include <cpPlugins/Extensions/Algorithms/IterativeGaussianModelEstimator.h>
26 // -------------------------------------------------------------------------
27 const unsigned int Dim = 2;
28 typedef unsigned char TChannel;
29 typedef itk::RGBPixel< TChannel > TPixel;
30 typedef double TScalar;
31 typedef itk::Image< TChannel, Dim > TImage;
32 typedef itk::Image< TPixel, Dim > TColorImage;
33 typedef itk::ImageToVTKImageFilter< TColorImage > TVTKImage;
35 typedef itk::ImageFileReader< TColorImage > TColorImageReader;
36 typedef fpa::Image::RegionGrow< TColorImage, TImage, fpa::Image::Functors::CastVertexValueToConstantCost< TPixel, bool > > TFrontAlgorithm;
39 fpa::VTK::Image2DObserver< TFrontAlgorithm, vtkRenderWindow >
42 // -------------------------------------------------------------------------
43 template< class I, class S >
44 class GaussianFunction
45 : public fpa::Image::Functors::RegionGrowAllBelongsFunction< I >
48 // Type-related and pointers
49 typedef GaussianFunction Self;
50 typedef fpa::Image::Functors::RegionGrowAllBelongsFunction< I > Superclass;
51 typedef itk::SmartPointer< Self > Pointer;
52 typedef itk::SmartPointer< const Self > ConstPointer;
55 typedef typename Superclass::TInputImage TInputImage;
56 typedef typename Superclass::TOutputValue TOutputValue;
57 typedef typename Superclass::TIndex TIndex;
59 typedef cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, 3 > TEstimator;
65 fpa_Image_Functors_RegionGrowAllBelongsFunction
68 itkGetConstMacro( ModelSupport, unsigned long );
69 itkSetMacro( ModelSupport, unsigned long );
71 typedef itk::Image< bool, I::ImageDimension > TMarks;
74 virtual void SetInputImage( TInputImage* img )
76 std::cout << "Set!!!" << std::endl;
77 this->Superclass::SetInputImage( img );
78 this->m_Marks = TMarks::New( );
79 this->m_Marks->SetLargestPossibleRegion( img->GetLargestPossibleRegion( ) );
80 this->m_Marks->SetRequestedRegion( img->GetRequestedRegion( ) );
81 this->m_Marks->SetBufferedRegion( img->GetBufferedRegion( ) );
82 this->m_Marks->SetOrigin( img->GetOrigin( ) );
83 this->m_Marks->SetSpacing( img->GetSpacing( ) );
84 this->m_Marks->SetDirection( img->GetDirection( ) );
85 this->m_Marks->Allocate( );
86 this->m_Marks->FillBuffer( false );
90 virtual TOutputValue Evaluate( const TIndex& idx ) const
92 const TInputImage* img = this->GetInputImage( );
93 typename TInputImage::PixelType rgb = img->GetPixel( idx );
95 if( !this->m_Estimating )
97 if( !( this->m_Marks->GetPixel( idx ) ) )
99 this->m_Estimator->AddSample(
101 S( rgb.GetGreen( ) ),
104 this->m_Marks->SetPixel( idx, true );
105 #error CONTINUE HERE!!!!
106 std::cout << this->m_Estimator->GetNumberOfSamples( ) << " " << this->m_ModelSupport << std::endl;
107 if( this->m_Estimator->GetNumberOfSamples( ) == this->m_ModelSupport )
108 this->m_Estimating = true;
122 this->m_Estimator = TEstimator::New( );
123 this->m_Estimator->Clear( );
124 this->m_Estimating = false;
126 virtual ~GaussianFunction( )
130 // Purposely not implemented
131 GaussianFunction( const Self& );
132 void operator=( const Self& );
135 typename TEstimator::Pointer m_Estimator;
136 unsigned long m_ModelSupport;
137 mutable bool m_Estimating;
138 typename TMarks::Pointer m_Marks;
141 typedef GaussianFunction< TColorImage, TScalar > TMembershipFunction;
143 // -------------------------------------------------------------------------
144 int main( int argc, char* argv[] )
149 << "Usage: " << argv[ 0 ]
150 << " input_image support" << std::endl;
154 std::string input_image_fn = argv[ 1 ];
155 unsigned long support = std::atoi( argv[ 2 ] );
158 TColorImageReader::Pointer input_image_reader = TColorImageReader::New( );
159 input_image_reader->SetFileName( input_image_fn );
162 input_image_reader->Update( );
164 catch( itk::ExceptionObject& err )
166 std::cerr << "Error caught: " << err << std::endl;
170 TColorImage::ConstPointer input_image = input_image_reader->GetOutput( );
172 TVTKImage::Pointer vtk_image = TVTKImage::New( );
173 vtk_image->SetInput( input_image );
174 vtk_image->Update( );
177 vtkSmartPointer< vtkImageActor > actor =
178 vtkSmartPointer< vtkImageActor >::New( );
179 actor->SetInputData( vtk_image->GetOutput( ) );
181 vtkSmartPointer< vtkRenderer > renderer =
182 vtkSmartPointer< vtkRenderer >::New( );
183 renderer->SetBackground( 0.1, 0.2, 0.7 );
184 renderer->AddActor( actor );
185 vtkSmartPointer< vtkRenderWindow > window =
186 vtkSmartPointer< vtkRenderWindow >::New( );
187 window->SetSize( 800, 800 );
188 window->AddRenderer( renderer );
191 vtkSmartPointer< vtkInteractorStyleImage > imageStyle =
192 vtkSmartPointer< vtkInteractorStyleImage >::New( );
193 vtkSmartPointer< vtkRenderWindowInteractor > interactor =
194 vtkSmartPointer< vtkRenderWindowInteractor >::New( );
195 interactor->SetInteractorStyle( imageStyle );
196 window->SetInteractor( interactor );
199 // Create the widget and its representation
200 vtkSmartPointer< vtkPointHandleRepresentation3D > handle =
201 vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
202 handle->GetProperty( )->SetColor( 1, 0, 0 );
203 vtkSmartPointer< vtkSeedRepresentation > rep =
204 vtkSmartPointer< vtkSeedRepresentation >::New( );
205 rep->SetHandleRepresentation( handle );
207 vtkSmartPointer< vtkSeedWidget > widget =
208 vtkSmartPointer< vtkSeedWidget >::New( );
209 widget->SetInteractor( interactor );
210 widget->SetRepresentation( rep );
212 // Let some interaction
213 interactor->Initialize( );
216 interactor->Start( );
218 // Configure observer
219 TObserver::Pointer obs = TObserver::New( );
220 obs->SetImage( input_image, window );
222 // Configure membership function
223 TMembershipFunction::Pointer membership = TMembershipFunction::New( );
224 membership->SetModelSupport( support );
225 membership->SetInputImage( const_cast< TColorImage* >( input_image.GetPointer( ) ) );
227 // Configure algorithm
228 TFrontAlgorithm::Pointer algorithm = TFrontAlgorithm::New( );
229 for( unsigned int s = 0; s < rep->GetNumberOfSeeds( ); s++ )
232 rep->GetSeedWorldPosition( s, pos );
234 TColorImage::PointType pnt;
235 pnt[ 0 ] = TScalar( pos[ 0 ] );
236 pnt[ 1 ] = TScalar( pos[ 1 ] );
238 TColorImage::IndexType idx;
239 if( input_image->TransformPhysicalPointToIndex( pnt, idx ) )
240 algorithm->AddSeed( idx, 0 );
243 algorithm->AddObserver( itk::AnyEvent( ), obs );
244 algorithm->ThrowEventsOn( );
245 algorithm->SetInput( input_image );
246 algorithm->SetNeighborhoodOrder( 1 );
247 algorithm->SetMembershipFunction( membership );
248 algorithm->Update( );
250 // One last interaction
251 interactor->Start( );