]> Creatis software - FrontAlgorithms.git/blob - appli/examples/example_Image_RegionGrow_ConnectedRGBPixels.cxx
0dc013771158ae6f97a5a43e59272e9e1042303a
[FrontAlgorithms.git] / appli / examples / example_Image_RegionGrow_ConnectedRGBPixels.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/RegionGrowThresholdFunction.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 < 3 )
26   {
27     std::cerr
28       << "Usage: " << argv[ 0 ]
29       << " input_image neighborhood_order"
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
37   // Read image
38   TColorImage::Pointer input_image;
39   std::string err = fpa_Utility::ReadImage( input_image, input_image_fn );
40   if( err != "" )
41   {
42     std::cerr << err << std::endl;
43     return( 1 );
44
45   } // fi
46
47   // Show image and wait for, at least, one seed
48   itk::ImageToVTKImageFilter< TColorImage >::Pointer vtk_input_image =
49     itk::ImageToVTKImageFilter< TColorImage >::New( );
50   vtk_input_image->SetInput( input_image );
51   vtk_input_image->Update( );
52
53   fpa_Utility::Viewer2DWithSeeds viewer;
54   viewer.SetImage( vtk_input_image->GetOutput( ) );
55   while( viewer.GetNumberOfSeeds( ) == 0 )
56     viewer.Start( );
57
58   // Region growing types
59   typedef fpa::Image::RegionGrow< TColorImage, TImage > TFilter;
60   typedef fpa::Image::Functors::
61     RegionGrowThresholdFunction< TColorImage > TFunction;
62
63   // Prepare region grow function
64   TFunction::Pointer function = TFunction::New( );
65
66   // Prepare region grow filter
67   TFilter::Pointer filter = TFilter::New( );
68   filter->SetInput( input_image );
69   filter->SetGrowingFunction( function );
70   filter->SetNeighborhoodOrder( neighborhood_order );
71   filter->SetOutsideValue( TPixel( 0 ) );
72   filter->SetInsideValue( std::numeric_limits< TPixel >::max( ) );
73   filter->StopAtOneFrontOff( );
74
75   // Associate seed
76   TImage::PointType pnt;
77   TImage::IndexType idx;
78   viewer.GetSeed( pnt, 0 );
79   if( input_image->TransformPhysicalPointToIndex( pnt, idx ) )
80     filter->AddSeed( idx, 0 );
81   function->SetLowerThreshold( input_image->GetPixel( idx ) );
82   function->SetUpperThreshold( input_image->GetPixel( idx ) );
83
84   // Prepare graphical debugger
85   typedef fpa::VTK::Image2DObserver< TFilter, vtkRenderWindow > TDebugger;
86   TDebugger::Pointer debugger = TDebugger::New( );
87   debugger->SetRenderWindow( viewer.Window );
88   debugger->SetRenderPercentage( 0.01 );
89   filter->AddObserver( itk::AnyEvent( ), debugger );
90   filter->ThrowEventsOn( );
91
92   // Go!
93   filter->Update( );
94
95   // Some more interaction and finish
96   viewer.Start( );
97   return( 0 );
98 }
99
100 // eof - $RCSfile$