7 #include <itkImageToVTKImageFilter.h>
8 #include <itkRGBPixel.h>
10 #include <itkImageFileReader.h>
12 #include <vtkCamera.h>
13 #include <vtkImageActor.h>
14 #include <vtkInteractorStyleImage.h>
15 #include <vtkPointHandleRepresentation3D.h>
16 #include <vtkProperty.h>
17 #include <vtkRenderer.h>
18 #include <vtkRenderWindow.h>
19 #include <vtkRenderWindowInteractor.h>
20 #include <vtkSeedRepresentation.h>
21 #include <vtkSeedWidget.h>
22 #include <vtkSmartPointer.h>
24 #include <cpPlugins/Extensions/Algorithms/IterativeGaussianModelEstimator.h>
25 #include <cpPlugins/Extensions/Algorithms/RGBToYPbPrFunction.h>
27 #include <fpa/Image/RegionGrow.h>
28 #include <fpa/Base/Functors/TautologyFunction.h>
29 #include <fpa/VTK/Image2DObserver.h>
31 // -------------------------------------------------------------------------
32 const unsigned int Dim = 2;
33 typedef unsigned char TPixel;
34 typedef float TScalar;
36 typedef itk::Image< itk::RGBPixel< TPixel >, Dim > TColorImage;
37 typedef itk::Image< TPixel, Dim > TImage;
38 typedef itk::ImageToVTKImageFilter< TColorImage > TVTKImage;
40 // -------------------------------------------------------------------------
41 template< class I, class S >
42 class GaussianFunction
43 : public itk::FunctionBase< typename I::PixelType, bool >
46 // Type-related and pointers
47 typedef GaussianFunction Self;
48 typedef itk::FunctionBase< typename I::PixelType, bool > Superclass;
49 typedef itk::SmartPointer< Self > Pointer;
50 typedef itk::SmartPointer< const Self > ConstPointer;
53 typedef cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, 3 > TEstimator;
54 typedef cpPlugins::Extensions::Algorithms::RGBToYPbPrFunction< S > TYPbPrFunction;
58 itkTypeMacro( GaussianFunction, itkFunctionBase );
60 itkGetConstMacro( ModelSupport, unsigned long );
61 itkSetMacro( ModelSupport, unsigned long );
64 virtual bool Evaluate( const typename I::PixelType& rgb ) const
67 if( !this->m_Estimating )
69 this->m_Estimator->AddSample( this->m_YPbPrFunction( rgb ) );
70 if( this->m_Estimator->GetNumberOfSamples( ) == this->m_ModelSupport )
72 this->m_Estimating = true;
73 this->m_Estimator->UpdateModel( );
74 std::cout << this->m_Estimator->GetMinimumProbability( ) << std::endl;
75 std::cout << this->m_Estimator->GetMaximumProbability( ) << std::endl;
82 S p = this->m_Estimator->Probability( this->m_YPbPrFunction( rgb ) );
83 return( p > this->m_Estimator->GetMinimumProbability( ) );
95 this->m_Estimator = TEstimator::New( );
96 this->m_Estimator->Clear( );
97 this->m_Estimating = false;
99 virtual ~GaussianFunction( )
103 // Purposely not implemented
104 GaussianFunction( const Self& );
105 void operator=( const Self& );
108 typename TEstimator::Pointer m_Estimator;
109 TYPbPrFunction m_YPbPrFunction;
111 unsigned long m_ModelSupport;
112 mutable bool m_Estimating;
115 // -------------------------------------------------------------------------
116 int main( int argc, char* argv[] )
121 << "Usage: " << argv[ 0 ]
122 << " input_image neighborhood_order support"
127 std::string input_image_fn = argv[ 1 ];
128 unsigned int neighborhood_order = std::atoi( argv[ 2 ] );
129 unsigned long support = std::atoi( argv[ 3 ] );
132 itk::ImageFileReader< TColorImage >::Pointer input_image_reader =
133 itk::ImageFileReader< TColorImage >::New( );
134 input_image_reader->SetFileName( input_image_fn );
137 input_image_reader->Update( );
139 catch( itk::ExceptionObject& err )
142 << "Error while reading image from " << input_image_fn << ": "
147 TColorImage::ConstPointer input_image = input_image_reader->GetOutput( );
148 TVTKImage::Pointer vtk_input_image = TVTKImage::New( );
149 vtk_input_image->SetInput( input_image );
150 vtk_input_image->Update( );
153 vtkSmartPointer< vtkImageActor > actor =
154 vtkSmartPointer< vtkImageActor >::New( );
155 actor->SetInputData( vtk_input_image->GetOutput( ) );
157 vtkSmartPointer< vtkRenderer > renderer =
158 vtkSmartPointer< vtkRenderer >::New( );
159 renderer->SetBackground( 0.1, 0.2, 0.7 );
160 renderer->AddActor( actor );
161 vtkSmartPointer< vtkRenderWindow > window =
162 vtkSmartPointer< vtkRenderWindow >::New( );
163 window->SetSize( 800, 800 );
164 window->AddRenderer( renderer );
166 // Correct camera due to the loaded image
167 vtkCamera* camera = renderer->GetActiveCamera( );
168 camera->SetViewUp( 0, -1, 0 );
169 camera->SetPosition( 0, 0, -1 );
170 camera->SetFocalPoint( 0, 0, 0 );
173 vtkSmartPointer< vtkInteractorStyleImage > imageStyle =
174 vtkSmartPointer< vtkInteractorStyleImage >::New( );
175 vtkSmartPointer< vtkRenderWindowInteractor > interactor =
176 vtkSmartPointer< vtkRenderWindowInteractor >::New( );
177 interactor->SetInteractorStyle( imageStyle );
178 window->SetInteractor( interactor );
180 // Create the widget and its representation
181 vtkSmartPointer< vtkPointHandleRepresentation3D > handle =
182 vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
183 handle->GetProperty( )->SetColor( 1, 0, 0 );
184 vtkSmartPointer< vtkSeedRepresentation > rep =
185 vtkSmartPointer< vtkSeedRepresentation >::New( );
186 rep->SetHandleRepresentation( handle );
188 vtkSmartPointer< vtkSeedWidget > widget =
189 vtkSmartPointer< vtkSeedWidget >::New( );
190 widget->SetInteractor( interactor );
191 widget->SetRepresentation( rep );
193 // Let some interaction
194 interactor->Initialize( );
195 renderer->ResetCamera( );
198 interactor->Start( );
200 // Prepare region grow function
201 typedef GaussianFunction< TColorImage, TScalar > TFunction;
202 TFunction::Pointer function = TFunction::New( );
203 function->SetModelSupport( support );
205 // Prepare region grow filter
206 typedef fpa::Image::RegionGrow< TColorImage, TImage > TFilter;
207 TFilter::Pointer filter = TFilter::New( );
208 filter->SetInput( input_image );
209 filter->SetMembershipFunction( function );
210 filter->SetNeighborhoodOrder( neighborhood_order );
211 filter->SetOutsideValue( TPixel( 0 ) );
212 filter->SetInsideValue( std::numeric_limits< TPixel >::max( ) );
213 filter->StopAtOneFrontOff( );
215 // Get user-given seeds
216 for( unsigned int s = 0; s < rep->GetNumberOfSeeds( ); s++ )
219 rep->GetSeedWorldPosition( s, pos );
221 TImage::PointType pnt;
222 pnt[ 0 ] = TImage::PointType::ValueType( pos[ 0 ] );
223 pnt[ 1 ] = TImage::PointType::ValueType( pos[ 1 ] );
225 TImage::IndexType idx;
226 if( input_image->TransformPhysicalPointToIndex( pnt, idx ) )
227 filter->AddSeed( idx, 0 );
231 // Prepare graphical debugger
232 typedef fpa::VTK::Image2DObserver< TFilter, vtkRenderWindow > TDebugger;
233 TDebugger::Pointer debugger = TDebugger::New( );
234 debugger->SetRenderWindow( window );
235 debugger->SetRenderPercentage( 0.001 );
236 filter->AddObserver( itk::AnyEvent( ), debugger );
237 filter->ThrowEventsOn( );