]> Creatis software - FrontAlgorithms.git/blob - appli/examples/example_ImageAlgorithmRegionGrow_GaussianModelEstimator.cxx
04fa936b43f9a1a0ca6bd8b95ac6836402ee9094
[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 #include <cpPlugins/Extensions/Algorithms/RGBToYPbPrFunction.h>
26
27 // -------------------------------------------------------------------------
28 const unsigned int Dim = 2;
29 typedef unsigned char TChannel;
30 typedef itk::RGBPixel< TChannel > TPixel;
31 typedef double TScalar;
32 typedef itk::Image< TChannel, Dim > TImage;
33 typedef itk::Image< TPixel, Dim >   TColorImage;
34 typedef itk::ImageToVTKImageFilter< TColorImage > TVTKImage;
35
36 typedef itk::ImageFileReader< TColorImage >   TColorImageReader;
37 typedef fpa::Image::RegionGrow< TColorImage, TImage, fpa::Image::Functors::CastVertexValueToConstantCost< TPixel, bool > > TFrontAlgorithm;
38
39 typedef
40 fpa::VTK::Image2DObserver< TFrontAlgorithm, vtkRenderWindow >
41 TObserver;
42
43 // -------------------------------------------------------------------------
44 template< class I, class S >
45 class GaussianFunction
46   : public fpa::Image::Functors::RegionGrowAllBelongsFunction< I >
47 {
48 public:
49   // Type-related and pointers
50   typedef GaussianFunction                                        Self;
51   typedef fpa::Image::Functors::RegionGrowAllBelongsFunction< I > Superclass;
52   typedef itk::SmartPointer< Self >                               Pointer;
53   typedef itk::SmartPointer< const Self >                         ConstPointer;
54
55   // Superclass' types
56   typedef typename Superclass::TInputImage  TInputImage;
57   typedef typename Superclass::TOutputValue TOutputValue;
58   typedef typename Superclass::TIndex       TIndex;
59
60   typedef cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, 3 > TEstimator;
61   typedef cpPlugins::Extensions::Algorithms::RGBToYPbPrFunction< S > TYPbPrFunction;
62
63 public:
64   itkNewMacro( Self );
65   itkTypeMacro(
66     GaussianFunction,
67     fpa_Image_Functors_RegionGrowAllBelongsFunction
68     );
69
70   itkGetConstMacro( ModelSupport, unsigned long );
71   itkSetMacro( ModelSupport, unsigned long );
72
73   typedef itk::Image< bool, I::ImageDimension > TMarks;
74
75 public:
76   virtual void SetInputImage( TInputImage* img )
77     {
78       std::cout << "Set!!!" << std::endl;
79       this->Superclass::SetInputImage( img );
80       this->m_Marks = TMarks::New( );
81       this->m_Marks->SetLargestPossibleRegion( img->GetLargestPossibleRegion( ) );
82       this->m_Marks->SetRequestedRegion( img->GetRequestedRegion( ) );
83       this->m_Marks->SetBufferedRegion( img->GetBufferedRegion( ) );
84       this->m_Marks->SetOrigin( img->GetOrigin( ) );
85       this->m_Marks->SetSpacing( img->GetSpacing( ) );
86       this->m_Marks->SetDirection( img->GetDirection( ) );
87       this->m_Marks->Allocate( );
88       this->m_Marks->FillBuffer( false );
89     }
90
91
92   virtual TOutputValue Evaluate( const TIndex& idx ) const
93     {
94       const TInputImage* img = this->GetInputImage( );
95       typename TInputImage::PixelType rgb = img->GetPixel( idx );
96
97       if( !this->m_Estimating )
98       {
99         if( !( this->m_Marks->GetPixel( idx ) ) )
100         {
101           this->m_Estimator->AddSample( this->m_YPbPrFunction( rgb ) );
102           this->m_Marks->SetPixel( idx, true );
103           if( this->m_Estimator->GetNumberOfSamples( ) == this->m_ModelSupport )
104           {
105             this->m_Estimating = true;
106             this->m_Estimator->UpdateModel( );
107             std::cout << this->m_Estimator->GetMinimumProbability( ) << std::endl;
108             std::cout << this->m_Estimator->GetMaximumProbability( ) << std::endl;
109
110           } // fi
111
112         } // fi
113         return( true );
114       }
115       else
116       {
117         S p = this->m_Estimator->Probability( this->m_YPbPrFunction( rgb ) );
118         return( p > this->m_Estimator->GetMinimumProbability( ) );
119
120       } // fi
121     }
122
123 protected:
124   GaussianFunction( )
125     : Superclass( ),
126       m_ModelSupport( 10 )
127     {
128       this->m_Estimator = TEstimator::New( );
129       this->m_Estimator->Clear( );
130       this->m_Estimating = false;
131     }
132   virtual ~GaussianFunction( )
133     { }
134
135 private:
136   // Purposely not implemented
137   GaussianFunction( const Self& );
138   void operator=( const Self& );
139
140 protected:
141   typename TEstimator::Pointer m_Estimator;
142   TYPbPrFunction m_YPbPrFunction;
143
144   unsigned long m_ModelSupport;
145   mutable bool m_Estimating;
146   typename TMarks::Pointer m_Marks;
147 };
148
149 typedef GaussianFunction< TColorImage, TScalar > TMembershipFunction;
150
151 // -------------------------------------------------------------------------
152 int main( int argc, char* argv[] )
153 {
154   if( argc < 3 )
155   {
156     std::cerr
157       << "Usage: " << argv[ 0 ]
158       << " input_image support" << std::endl;
159     return( 1 );
160
161   } // fi
162   std::string input_image_fn = argv[ 1 ];
163   unsigned long support = std::atoi( argv[ 2 ] );
164
165   // Read image
166   TColorImageReader::Pointer input_image_reader = TColorImageReader::New( );
167   input_image_reader->SetFileName( input_image_fn );
168   try
169   {
170     input_image_reader->Update( );
171   }
172   catch( itk::ExceptionObject& err )
173   {
174     std::cerr << "Error caught: " << err << std::endl;
175     return( 1 );
176
177   } // yrt
178   TColorImage::ConstPointer input_image = input_image_reader->GetOutput( );
179
180   TVTKImage::Pointer vtk_image = TVTKImage::New( );
181   vtk_image->SetInput( input_image );
182   vtk_image->Update( );
183
184   // VTK visualization
185   vtkSmartPointer< vtkImageActor > actor =
186     vtkSmartPointer< vtkImageActor >::New( );
187   actor->SetInputData( vtk_image->GetOutput( ) );
188
189   vtkSmartPointer< vtkRenderer > renderer =
190     vtkSmartPointer< vtkRenderer >::New( );
191   renderer->SetBackground( 0.1, 0.2, 0.7 );
192   renderer->AddActor( actor );
193   vtkSmartPointer< vtkRenderWindow > window =
194     vtkSmartPointer< vtkRenderWindow >::New( );
195   window->SetSize( 800, 800 );
196   window->AddRenderer( renderer );
197
198   // VTK interaction
199   vtkSmartPointer< vtkInteractorStyleImage > imageStyle =
200     vtkSmartPointer< vtkInteractorStyleImage >::New( );
201   vtkSmartPointer< vtkRenderWindowInteractor > interactor =
202     vtkSmartPointer< vtkRenderWindowInteractor >::New( );
203   interactor->SetInteractorStyle( imageStyle );
204   window->SetInteractor( interactor );
205   window->Render( );
206
207   // Create the widget and its representation
208   vtkSmartPointer< vtkPointHandleRepresentation3D > handle =
209     vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
210   handle->GetProperty( )->SetColor( 1, 0, 0 );
211   vtkSmartPointer< vtkSeedRepresentation > rep =
212     vtkSmartPointer< vtkSeedRepresentation >::New( );
213   rep->SetHandleRepresentation( handle );
214
215   vtkSmartPointer< vtkSeedWidget > widget =
216     vtkSmartPointer< vtkSeedWidget >::New( );
217   widget->SetInteractor( interactor );
218   widget->SetRepresentation( rep );
219
220   // Let some interaction
221   interactor->Initialize( );
222   window->Render( );
223   widget->On( );
224   interactor->Start( );
225
226   // Configure observer
227   TObserver::Pointer obs = TObserver::New( );
228   obs->SetImage( input_image, window );
229
230   // Configure membership function
231   TMembershipFunction::Pointer membership = TMembershipFunction::New( );
232   membership->SetModelSupport( support );
233   membership->SetInputImage( const_cast< TColorImage* >( input_image.GetPointer( ) ) );
234
235   // Configure algorithm
236   TFrontAlgorithm::Pointer algorithm = TFrontAlgorithm::New( );
237   for( unsigned int s = 0; s < rep->GetNumberOfSeeds( ); s++ )
238   {
239     double pos[ 3 ];
240     rep->GetSeedWorldPosition( s, pos );
241
242     TColorImage::PointType pnt;
243     pnt[ 0 ] = TScalar( pos[ 0 ] );
244     pnt[ 1 ] = TScalar( pos[ 1 ] );
245
246     TColorImage::IndexType idx;
247     if( input_image->TransformPhysicalPointToIndex( pnt, idx ) )
248       algorithm->AddSeed( idx, 0 );
249
250   } // rof
251   algorithm->AddObserver( itk::AnyEvent( ), obs );
252   algorithm->ThrowEventsOn( );
253   algorithm->SetInput( input_image );
254   algorithm->SetNeighborhoodOrder( 1 );
255   algorithm->SetMembershipFunction( membership );
256   algorithm->Update( );
257
258   // One last interaction
259   interactor->Start( );
260
261   return( 0 );
262 }
263
264 // eof - $RCSfile$