]> Creatis software - FrontAlgorithms.git/blob - appli/examples/example_Image_RegionGrow_AllRGBPixels.cxx
CMake updated. Some other filters added.
[FrontAlgorithms.git] / appli / examples / example_Image_RegionGrow_AllRGBPixels.cxx
1 #include <cstdlib>
2 #include <iostream>
3 #include <limits>
4 #include <string>
5
6 #include <itkImage.h>
7 #include <itkRGBPixel.h>
8 #include <itkImageToVTKImageFilter.h>
9
10 #include <fpa/Image/RegionGrow.h>
11 #include <fpa/Image/Functors/RegionGrowAllBelongsFunction.h>
12 #include <fpa/VTK/Image2DObserver.h>
13
14 #include "fpa_Utility.h"
15
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;
21
22 // -------------------------------------------------------------------------
23 int main( int argc, char* argv[] )
24 {
25   if( argc < 4 )
26   {
27     std::cerr
28       << "Usage: " << argv[ 0 ]
29       << " input_image neighborhood_order stop_at_one_front"
30       << std::endl;
31     return( 1 );
32
33   } // fi
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 );
37
38   // Read image
39   TColorImage::Pointer input_image;
40   std::string err = fpa_Utility::ReadImage( input_image, input_image_fn );
41   if( err != "" )
42   {
43     std::cerr << err << std::endl;
44     return( 1 );
45
46   } // fi
47
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( );
53
54   fpa_Utility::Viewer2DWithSeeds viewer;
55   viewer.SetImage( vtk_input_image->GetOutput( ) );
56   while( viewer.GetNumberOfSeeds( ) == 0 )
57     viewer.Start( );
58
59   // Region growing types
60   typedef fpa::Image::RegionGrow< TColorImage, TImage > TFilter;
61   typedef fpa::Image::Functors::
62     RegionGrowAllBelongsFunction< TColorImage > TFunction;
63
64   // Prepare region grow function
65   TFunction::Pointer function = TFunction::New( );
66
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 );
75
76   // Associate seeds
77   for( unsigned long s = 0; s < viewer.GetNumberOfSeeds( ); ++s )
78   {
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 );
84
85   } // rof
86
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( );
94
95   // Go!
96   filter->Update( );
97
98   // Some more interaction and finish
99   viewer.Start( );
100   return( 0 );
101 }
102
103 // eof - $RCSfile$