]> Creatis software - FrontAlgorithms.git/blob - appli/examples/example_ImageAlgorithmRegionGrow_GaussianModelEstimator.cxx
Segmentation guided by a gaussian estimator added -- Not yet finished
[FrontAlgorithms.git] / appli / examples / example_ImageAlgorithmRegionGrow_GaussianModelEstimator.cxx
1 #include <iostream>
2 #include <limits>
3 #include <string>
4
5 #include <itkImage.h>
6 #include <itkImageFileReader.h>
7 #include <itkSignedDanielssonDistanceMapImageFilter.h>
8 #include <itkImageToVTKImageFilter.h>
9
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>
20
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>
25
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;
34
35 typedef itk::ImageFileReader< TColorImage >   TColorImageReader;
36 typedef fpa::Image::RegionGrow< TColorImage, TImage, fpa::Image::Functors::CastVertexValueToConstantCost< TPixel, bool > > TFrontAlgorithm;
37
38 typedef
39 fpa::VTK::Image2DObserver< TFrontAlgorithm, vtkRenderWindow >
40 TObserver;
41
42 // -------------------------------------------------------------------------
43 template< class I, class S >
44 class GaussianFunction
45   : public fpa::Image::Functors::RegionGrowAllBelongsFunction< I >
46 {
47 public:
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;
53
54   // Superclass' types
55   typedef typename Superclass::TInputImage  TInputImage;
56   typedef typename Superclass::TOutputValue TOutputValue;
57   typedef typename Superclass::TIndex       TIndex;
58
59   typedef cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, 3 > TEstimator;
60
61 public:
62   itkNewMacro( Self );
63   itkTypeMacro(
64     GaussianFunction,
65     fpa_Image_Functors_RegionGrowAllBelongsFunction
66     );
67
68   itkGetConstMacro( ModelSupport, unsigned long );
69   itkSetMacro( ModelSupport, unsigned long );
70
71   typedef itk::Image< bool, I::ImageDimension > TMarks;
72
73 public:
74   virtual void SetInputImage( TInputImage* img )
75     {
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 );
87     }
88
89
90   virtual TOutputValue Evaluate( const TIndex& idx ) const
91     {
92       const TInputImage* img = this->GetInputImage( );
93       typename TInputImage::PixelType rgb = img->GetPixel( idx );
94
95       if( !this->m_Estimating )
96       {
97         if( !( this->m_Marks->GetPixel( idx ) ) )
98         {
99           this->m_Estimator->AddSample(
100             S( rgb.GetRed( ) ),
101             S( rgb.GetGreen( ) ),
102             S( rgb.GetBlue( ) )
103             );
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;
109
110         } // fi
111         return( true );
112       }
113       else
114         return( false );
115     }
116
117 protected:
118   GaussianFunction( )
119     : Superclass( ),
120       m_ModelSupport( 10 )
121     {
122       this->m_Estimator = TEstimator::New( );
123       this->m_Estimator->Clear( );
124       this->m_Estimating = false;
125     }
126   virtual ~GaussianFunction( )
127     { }
128
129 private:
130   // Purposely not implemented
131   GaussianFunction( const Self& );
132   void operator=( const Self& );
133
134 protected:
135   typename TEstimator::Pointer m_Estimator;
136   unsigned long m_ModelSupport;
137   mutable bool m_Estimating;
138   typename TMarks::Pointer m_Marks;
139 };
140
141 typedef GaussianFunction< TColorImage, TScalar > TMembershipFunction;
142
143 // -------------------------------------------------------------------------
144 int main( int argc, char* argv[] )
145 {
146   if( argc < 3 )
147   {
148     std::cerr
149       << "Usage: " << argv[ 0 ]
150       << " input_image support" << std::endl;
151     return( 1 );
152
153   } // fi
154   std::string input_image_fn = argv[ 1 ];
155   unsigned long support = std::atoi( argv[ 2 ] );
156
157   // Read image
158   TColorImageReader::Pointer input_image_reader = TColorImageReader::New( );
159   input_image_reader->SetFileName( input_image_fn );
160   try
161   {
162     input_image_reader->Update( );
163   }
164   catch( itk::ExceptionObject& err )
165   {
166     std::cerr << "Error caught: " << err << std::endl;
167     return( 1 );
168
169   } // yrt
170   TColorImage::ConstPointer input_image = input_image_reader->GetOutput( );
171
172   TVTKImage::Pointer vtk_image = TVTKImage::New( );
173   vtk_image->SetInput( input_image );
174   vtk_image->Update( );
175
176   // VTK visualization
177   vtkSmartPointer< vtkImageActor > actor =
178     vtkSmartPointer< vtkImageActor >::New( );
179   actor->SetInputData( vtk_image->GetOutput( ) );
180
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 );
189
190   // VTK interaction
191   vtkSmartPointer< vtkInteractorStyleImage > imageStyle =
192     vtkSmartPointer< vtkInteractorStyleImage >::New( );
193   vtkSmartPointer< vtkRenderWindowInteractor > interactor =
194     vtkSmartPointer< vtkRenderWindowInteractor >::New( );
195   interactor->SetInteractorStyle( imageStyle );
196   window->SetInteractor( interactor );
197   window->Render( );
198
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 );
206
207   vtkSmartPointer< vtkSeedWidget > widget =
208     vtkSmartPointer< vtkSeedWidget >::New( );
209   widget->SetInteractor( interactor );
210   widget->SetRepresentation( rep );
211
212   // Let some interaction
213   interactor->Initialize( );
214   window->Render( );
215   widget->On( );
216   interactor->Start( );
217
218   // Configure observer
219   TObserver::Pointer obs = TObserver::New( );
220   obs->SetImage( input_image, window );
221
222   // Configure membership function
223   TMembershipFunction::Pointer membership = TMembershipFunction::New( );
224   membership->SetModelSupport( support );
225   membership->SetInputImage( const_cast< TColorImage* >( input_image.GetPointer( ) ) );
226
227   // Configure algorithm
228   TFrontAlgorithm::Pointer algorithm = TFrontAlgorithm::New( );
229   for( unsigned int s = 0; s < rep->GetNumberOfSeeds( ); s++ )
230   {
231     double pos[ 3 ];
232     rep->GetSeedWorldPosition( s, pos );
233
234     TColorImage::PointType pnt;
235     pnt[ 0 ] = TScalar( pos[ 0 ] );
236     pnt[ 1 ] = TScalar( pos[ 1 ] );
237
238     TColorImage::IndexType idx;
239     if( input_image->TransformPhysicalPointToIndex( pnt, idx ) )
240       algorithm->AddSeed( idx, 0 );
241
242   } // rof
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( );
249
250   // One last interaction
251   interactor->Start( );
252
253   return( 0 );
254 }
255
256 // eof - $RCSfile$