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>
25 #include <cpPlugins/Extensions/Algorithms/RGBToYPbPrFunction.h>
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;
36 typedef itk::ImageFileReader< TColorImage > TColorImageReader;
37 typedef fpa::Image::RegionGrow< TColorImage, TImage, fpa::Image::Functors::CastVertexValueToConstantCost< TPixel, bool > > TFrontAlgorithm;
40 fpa::VTK::Image2DObserver< TFrontAlgorithm, vtkRenderWindow >
43 // -------------------------------------------------------------------------
44 template< class I, class S >
45 class GaussianFunction
46 : public fpa::Image::Functors::RegionGrowAllBelongsFunction< I >
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;
56 typedef typename Superclass::TInputImage TInputImage;
57 typedef typename Superclass::TOutputValue TOutputValue;
58 typedef typename Superclass::TIndex TIndex;
60 typedef cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, 3 > TEstimator;
61 typedef cpPlugins::Extensions::Algorithms::RGBToYPbPrFunction< S > TYPbPrFunction;
67 fpa_Image_Functors_RegionGrowAllBelongsFunction
70 itkGetConstMacro( ModelSupport, unsigned long );
71 itkSetMacro( ModelSupport, unsigned long );
73 typedef itk::Image< bool, I::ImageDimension > TMarks;
76 virtual void SetInputImage( TInputImage* img )
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 );
92 virtual TOutputValue Evaluate( const TIndex& idx ) const
94 const TInputImage* img = this->GetInputImage( );
95 typename TInputImage::PixelType rgb = img->GetPixel( idx );
97 if( !this->m_Estimating )
99 if( !( this->m_Marks->GetPixel( idx ) ) )
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 )
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;
117 S p = this->m_Estimator->Probability( this->m_YPbPrFunction( rgb ) );
118 return( p > this->m_Estimator->GetMinimumProbability( ) );
128 this->m_Estimator = TEstimator::New( );
129 this->m_Estimator->Clear( );
130 this->m_Estimating = false;
132 virtual ~GaussianFunction( )
136 // Purposely not implemented
137 GaussianFunction( const Self& );
138 void operator=( const Self& );
141 typename TEstimator::Pointer m_Estimator;
142 TYPbPrFunction m_YPbPrFunction;
144 unsigned long m_ModelSupport;
145 mutable bool m_Estimating;
146 typename TMarks::Pointer m_Marks;
149 typedef GaussianFunction< TColorImage, TScalar > TMembershipFunction;
151 // -------------------------------------------------------------------------
152 int main( int argc, char* argv[] )
157 << "Usage: " << argv[ 0 ]
158 << " input_image support" << std::endl;
162 std::string input_image_fn = argv[ 1 ];
163 unsigned long support = std::atoi( argv[ 2 ] );
166 TColorImageReader::Pointer input_image_reader = TColorImageReader::New( );
167 input_image_reader->SetFileName( input_image_fn );
170 input_image_reader->Update( );
172 catch( itk::ExceptionObject& err )
174 std::cerr << "Error caught: " << err << std::endl;
178 TColorImage::ConstPointer input_image = input_image_reader->GetOutput( );
180 TVTKImage::Pointer vtk_image = TVTKImage::New( );
181 vtk_image->SetInput( input_image );
182 vtk_image->Update( );
185 vtkSmartPointer< vtkImageActor > actor =
186 vtkSmartPointer< vtkImageActor >::New( );
187 actor->SetInputData( vtk_image->GetOutput( ) );
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 );
199 vtkSmartPointer< vtkInteractorStyleImage > imageStyle =
200 vtkSmartPointer< vtkInteractorStyleImage >::New( );
201 vtkSmartPointer< vtkRenderWindowInteractor > interactor =
202 vtkSmartPointer< vtkRenderWindowInteractor >::New( );
203 interactor->SetInteractorStyle( imageStyle );
204 window->SetInteractor( interactor );
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 );
215 vtkSmartPointer< vtkSeedWidget > widget =
216 vtkSmartPointer< vtkSeedWidget >::New( );
217 widget->SetInteractor( interactor );
218 widget->SetRepresentation( rep );
220 // Let some interaction
221 interactor->Initialize( );
224 interactor->Start( );
226 // Configure observer
227 TObserver::Pointer obs = TObserver::New( );
228 obs->SetImage( input_image, window );
230 // Configure membership function
231 TMembershipFunction::Pointer membership = TMembershipFunction::New( );
232 membership->SetModelSupport( support );
233 membership->SetInputImage( const_cast< TColorImage* >( input_image.GetPointer( ) ) );
235 // Configure algorithm
236 TFrontAlgorithm::Pointer algorithm = TFrontAlgorithm::New( );
237 for( unsigned int s = 0; s < rep->GetNumberOfSeeds( ); s++ )
240 rep->GetSeedWorldPosition( s, pos );
242 TColorImage::PointType pnt;
243 pnt[ 0 ] = TScalar( pos[ 0 ] );
244 pnt[ 1 ] = TScalar( pos[ 1 ] );
246 TColorImage::IndexType idx;
247 if( input_image->TransformPhysicalPointToIndex( pnt, idx ) )
248 algorithm->AddSeed( idx, 0 );
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( );
258 // One last interaction
259 interactor->Start( );