7 #include <itkRGBPixel.h>
8 #include <itkImageToVTKImageFilter.h>
10 #include <fpa/Image/RegionGrow.h>
11 #include <fpa/Image/Functors/RegionGrowAllBelongsFunction.h>
12 #include <fpa/VTK/Image2DObserver.h>
14 #include "fpa_Utility.h"
16 // -------------------------------------------------------------------------
17 const unsigned int Dim = 2;
18 typedef unsigned char TPixel;
19 typedef itk::Image< itk::RGBPixel< TPixel >, Dim > TColorImage;
20 typedef itk::Image< TPixel, Dim > TImage;
22 // -------------------------------------------------------------------------
23 int main( int argc, char* argv[] )
28 << "Usage: " << argv[ 0 ]
29 << " input_image neighborhood_order stop_at_one_front"
34 std::string input_image_fn = argv[ 1 ];
35 unsigned int neighborhood_order = std::atoi( argv[ 2 ] );
36 bool stop_at_one_front = ( std::atoi( argv[ 3 ] ) != 0 );
39 TColorImage::Pointer input_image;
40 std::string err = fpa_Utility::ReadImage( input_image, input_image_fn );
43 std::cerr << err << std::endl;
48 // Show image and wait for, at least, one seed
49 itk::ImageToVTKImageFilter< TColorImage >::Pointer vtk_input_image =
50 itk::ImageToVTKImageFilter< TColorImage >::New( );
51 vtk_input_image->SetInput( input_image );
52 vtk_input_image->Update( );
54 fpa_Utility::Viewer2DWithSeeds viewer;
55 viewer.SetImage( vtk_input_image->GetOutput( ) );
56 while( viewer.GetNumberOfSeeds( ) == 0 )
59 // Region growing types
60 typedef fpa::Image::RegionGrow< TColorImage, TImage > TFilter;
61 typedef fpa::Image::Functors::
62 RegionGrowAllBelongsFunction< TColorImage > TFunction;
64 // Prepare region grow function
65 TFunction::Pointer function = TFunction::New( );
67 // Prepare region grow filter
68 TFilter::Pointer filter = TFilter::New( );
69 filter->SetInput( input_image );
70 filter->SetGrowingFunction( function );
71 filter->SetNeighborhoodOrder( neighborhood_order );
72 filter->SetOutsideValue( TPixel( 0 ) );
73 filter->SetInsideValue( std::numeric_limits< TPixel >::max( ) );
74 filter->SetStopAtOneFront( stop_at_one_front );
77 for( unsigned long s = 0; s < viewer.GetNumberOfSeeds( ); ++s )
79 TImage::PointType pnt;
80 TImage::IndexType idx;
81 viewer.GetSeed( pnt, s );
82 if( input_image->TransformPhysicalPointToIndex( pnt, idx ) )
83 filter->AddSeed( idx, 0 );
87 // Prepare graphical debugger
88 typedef fpa::VTK::Image2DObserver< TFilter, vtkRenderWindow > TDebugger;
89 TDebugger::Pointer debugger = TDebugger::New( );
90 debugger->SetRenderWindow( viewer.Window );
91 debugger->SetRenderPercentage( 0.01 );
92 filter->AddObserver( itk::AnyEvent( ), debugger );
93 filter->ThrowEventsOn( );
98 // Some more interaction and finish