]> Creatis software - FrontAlgorithms.git/blob - examples/RegionGrow_00.cxx
Mori is alivegit status!
[FrontAlgorithms.git] / examples / RegionGrow_00.cxx
1 #include <itkCommand.h>
2 #include <itkImage.h>
3 #include <itkImageFileWriter.h>
4 #include <fpa/Image/RegionGrow.h>
5
6 // -------------------------------------------------------------------------
7 typedef itk::Image< unsigned char, 2 >           TImage;
8 typedef fpa::Image::RegionGrow< TImage, TImage > TFilter;
9 typedef itk::ImageFileWriter< TImage >           TWriter;
10
11 // -------------------------------------------------------------------------
12 /**
13  */
14 class MyObserver
15   : public itk::Command
16 {
17 public:
18   typedef TFilter::TStartEvent     TStartEvent;
19   typedef TFilter::TEndEvent       TEndEvent;
20   typedef TFilter::TStartLoopEvent TStartLoopEvent;
21   typedef TFilter::TEndLoopEvent   TEndLoopEvent;
22   typedef TFilter::TPushEvent      TPushEvent;
23   typedef TFilter::TPopEvent       TPopEvent;
24   typedef TFilter::TMarkEvent      TMarkEvent;
25
26 public:
27   itkNewMacro( MyObserver );
28
29 public:
30   virtual void Execute(
31     itk::Object* caller, const itk::EventObject& event
32     ) override
33     {
34       this->Execute( const_cast< const itk::Object* >( caller ), event );
35     }
36   virtual void Execute(
37     const itk::Object* object, const itk::EventObject& event
38     ) override
39     {
40       if( TStartEvent( ).CheckEvent( &event ) )
41         std::cout << "Start" << std::endl;
42       else if( TEndEvent( ).CheckEvent( &event ) )
43         std::cout << "End" << std::endl;
44       else if( TStartLoopEvent( ).CheckEvent( &event ) )
45         std::cout << "StartLoop" << std::endl;
46       else if( TEndLoopEvent( ).CheckEvent( &event ) )
47         std::cout << "EndLoop" << std::endl;
48       else if( TMarkEvent( ).CheckEvent( &event ) )
49       {
50         const TMarkEvent* mark = dynamic_cast< const TMarkEvent* >( &event );
51         std::cout << "Mark: " << mark->Vertex << std::endl;
52
53       } // fi
54       /* TODO
55          TPushEvent;
56          TPopEvent;
57       */
58     }
59 };
60
61 // -------------------------------------------------------------------------
62 int main( int argc, char* argv[] )
63 {
64   TImage::SizeType size;
65   size.Fill( 10 );
66
67   TImage::Pointer input = TImage::New( );
68   input->SetRegions( size );
69   input->Allocate( );
70   input->FillBuffer( 0 );
71
72   TImage::RegionType region = input->GetLargestPossibleRegion( );
73   TImage::PointType p0, p1, p2;
74   input->TransformIndexToPhysicalPoint( region.GetIndex( ), p0 );
75   input->TransformIndexToPhysicalPoint(
76     region.GetIndex( ) + region.GetSize( ), p1
77     );
78   p2 = ( p0.GetVectorFromOrigin( ) + p1.GetVectorFromOrigin( ) ) * 0.5;
79   TImage::IndexType seed;
80   input->TransformPhysicalPointToIndex( p2, seed );
81
82   TFilter::Pointer filter = TFilter::New( );
83   filter->SetInput( input );
84   filter->SetInsideValue( 255 );
85   filter->SetOutsideValue( 0 );
86   filter->AddSeed( seed, filter->GetInsideValue( ) );
87
88   MyObserver::Pointer obs = MyObserver::New( );
89   filter->AddObserver( itk::AnyEvent( ), obs );
90   filter->Update( );
91
92   TWriter::Pointer writer = TWriter::New( );
93   writer->SetInput( filter->GetOutput( ) );
94   writer->SetFileName( "RegionGrow_00.png" );
95   writer->Update( );
96
97   return( 0 );
98 }
99
100 // eof - $RCSfile$