From: Leonardo Flórez-Valencia Date: Mon, 6 Feb 2017 22:47:37 +0000 (-0500) Subject: ... X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=a446849d62347e0401f681748ca13289091ac3b9;p=FrontAlgorithms.git ... --- diff --git a/data/test_mori_2D.png b/data/test_mori_2D.png new file mode 100644 index 0000000..943e9f0 Binary files /dev/null and b/data/test_mori_2D.png differ diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 24e670a..4a8ae35 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -4,6 +4,7 @@ IF(BUILD_Examples) SET( _examples RegionGrow_00 + MoriRegionGrow_00 ) INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/lib diff --git a/examples/MoriRegionGrow_00.cxx b/examples/MoriRegionGrow_00.cxx new file mode 100644 index 0000000..d0dabff --- /dev/null +++ b/examples/MoriRegionGrow_00.cxx @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include + +// ------------------------------------------------------------------------- +typedef itk::Image< unsigned char, 2 > TImage; +typedef itk::ImageFileReader< TImage > TReader; +typedef itk::ImageFileWriter< TImage > TWriter; +typedef fpa::Image::MoriRegionGrow< TImage, TImage > TFilter; + +// ------------------------------------------------------------------------- +/** + */ +class MyObserver + : public itk::Command +{ +public: + typedef TFilter::TStartEvent TStartEvent; + typedef TFilter::TEndEvent TEndEvent; + typedef TFilter::TStartLoopEvent TStartLoopEvent; + typedef TFilter::TEndLoopEvent TEndLoopEvent; + typedef TFilter::TPushEvent TPushEvent; + typedef TFilter::TPopEvent TPopEvent; + typedef TFilter::TMarkEvent TMarkEvent; + +public: + itkNewMacro( MyObserver ); + +public: + virtual void Execute( + itk::Object* caller, const itk::EventObject& event + ) override + { + this->Execute( const_cast< const itk::Object* >( caller ), event ); + } + virtual void Execute( + const itk::Object* object, const itk::EventObject& event + ) override + { + /* TODO + if( TStartEvent( ).CheckEvent( &event ) ) + std::cout << "Start" << std::endl; + else if( TEndEvent( ).CheckEvent( &event ) ) + std::cout << "End" << std::endl; + else if( TStartLoopEvent( ).CheckEvent( &event ) ) + std::cout << "StartLoop" << std::endl; + else if( TEndLoopEvent( ).CheckEvent( &event ) ) + std::cout << "EndLoop" << std::endl; + else if( TMarkEvent( ).CheckEvent( &event ) ) + { + const TMarkEvent* mark = dynamic_cast< const TMarkEvent* >( &event ); + std::cout << "Mark: " << mark->Vertex << std::endl; + + } // fi + */ + /* TODO + TPushEvent; + TPopEvent; + */ + } +}; + +// ------------------------------------------------------------------------- +int main( int argc, char* argv[] ) +{ + if( argc < 3 ) + { + std::cerr + << "Usage: " << argv[ 0 ] + << " input_filename output_filename" << std::endl; + return( 1 ); + + } // fi + std::string in_fname = argv[ 1 ]; + std::string out_fname = argv[ 2 ]; + int seed_x = 111; + int seed_y = 91; + + TReader::Pointer reader = TReader::New( ); + reader->SetFileName( in_fname ); + + TImage::IndexType seed; + seed[ 0 ] = seed_x; + seed[ 1 ] = seed_y; + + TFilter::Pointer filter = TFilter::New( ); + filter->SetInput( reader->GetOutput( ) ); + filter->SetLower( 0 ); + filter->SetUpper( 255 ); + filter->SetStep( 1 ); + filter->SetInsideValue( 255 ); + filter->SetOutsideValue( 0 ); + filter->AddSeed( seed, filter->GetInsideValue( ) ); + + /* TODO + MyObserver::Pointer obs = MyObserver::New( ); + filter->AddObserver( itk::AnyEvent( ), obs ); + */ + + TWriter::Pointer writer = TWriter::New( ); + writer->SetInput( filter->GetOutput( ) ); + writer->SetFileName( out_fname ); + writer->Update( ); + + return( 0 ); +} + +// eof - $RCSfile$ diff --git a/lib/fpa/Base/MoriRegionGrow.h b/lib/fpa/Base/MoriRegionGrow.h deleted file mode 100644 index 40f59cd..0000000 --- a/lib/fpa/Base/MoriRegionGrow.h +++ /dev/null @@ -1,61 +0,0 @@ -#ifndef __fpa__Base__MoriRegionGrow__h__ -#define __fpa__Base__MoriRegionGrow__h__ - -#include -#include -#include - -namespace fpa -{ - namespace Base - { - /** - */ - template< class _TSuperclass > - class MoriRegionGrow - : public fpa::Base::RegionGrow< _TSuperclass > - { - public: - typedef MoriRegionGrow Self; - typedef fpa::Base::RegionGrow< _TSuperclass > Superclass; - typedef itk::SmartPointer< Self > Pointer; - typedef itk::SmartPointer< const Self > ConstPointer; - - typedef typename Superclass::TOutput TOutput; - typedef typename Superclass::TVertex TVertex; - - protected: - typedef typename Superclass::_TQueueNode _TQueueNode; - typedef typename Superclass::_TQueue _TQueue; - - public: - itkTypeMacro( MoriRegionGrow, Algorithm ); - - protected: - MoriRegionGrow( ); - virtual ~MoriRegionGrow( ); - - virtual bool _UpdateValue( - _TQueueNode& v, const _TQueueNode& p - ) override; - - private: - // Purposely not defined - MoriRegionGrow( const Self& other ); - Self& operator=( const Self& other ); - - protected: - _TQueue m_AuxilaryQueue; - }; - - } // ecapseman - -} // ecapseman - -#ifndef ITK_MANUAL_INSTANTIATION -# include -#endif // ITK_MANUAL_INSTANTIATION - -#endif // __fpa__Base__MoriRegionGrow__h__ - -// eof - $RCSfile$ diff --git a/lib/fpa/Base/MoriRegionGrow.hxx b/lib/fpa/Base/MoriRegionGrow.hxx deleted file mode 100644 index 9e1c300..0000000 --- a/lib/fpa/Base/MoriRegionGrow.hxx +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef __fpa__Base__MoriRegionGrow__hxx__ -#define __fpa__Base__MoriRegionGrow__hxx__ - -// ------------------------------------------------------------------------- -template< class _TSuperclass > -fpa::Base::MoriRegionGrow< _TSuperclass >:: -MoriRegionGrow( ) - : Superclass( ) -{ -} - -// ------------------------------------------------------------------------- -template< class _TSuperclass > -fpa::Base::MoriRegionGrow< _TSuperclass >:: -~MoriRegionGrow( ) -{ -} - -// ------------------------------------------------------------------------- -template< class _TSuperclass > -bool fpa::Base::MoriRegionGrow< _TSuperclass >:: -_UpdateValue( _TQueueNode& v, const _TQueueNode& p ) -{ - bool ret = this->Superclass::_UpdateValue( v, p ); - if( !ret ) - this->m_AuxilaryQueue.push( v ); - return( ret ); -} - - -#endif // __fpa__Base__MoriRegionGrow__hxx__ - -// eof - $RCSfile$ diff --git a/lib/fpa/Image/MoriRegionGrow.h b/lib/fpa/Image/MoriRegionGrow.h index 5708582..50e8bb1 100644 --- a/lib/fpa/Image/MoriRegionGrow.h +++ b/lib/fpa/Image/MoriRegionGrow.h @@ -1,8 +1,8 @@ #ifndef __fpa__Image__MoriRegionGrow__h__ #define __fpa__Image__MoriRegionGrow__h__ -#include -#include +#include +#include namespace fpa { @@ -12,34 +12,62 @@ namespace fpa */ template< class _TInputImage, class _TOutputImage > class MoriRegionGrow - : public fpa::Base::MoriRegionGrow< fpa::Image::Algorithm< _TInputImage, _TOutputImage > > + : public fpa::Image::RegionGrow< _TInputImage, _TOutputImage > { public: - typedef fpa::Image::Algorithm< _TInputImage, _TOutputImage > TAlgorithm; - typedef MoriRegionGrow Self; - typedef fpa::Base::MoriRegionGrow< TAlgorithm > Superclass; - typedef itk::SmartPointer< Self > Pointer; - typedef itk::SmartPointer< const Self > ConstPointer; + typedef MoriRegionGrow Self; + typedef fpa::Image::RegionGrow< _TInputImage, _TOutputImage > Superclass; + typedef itk::SmartPointer< Self > Pointer; + typedef itk::SmartPointer< const Self > ConstPointer; - typedef typename Superclass::TOutput TOutput; - typedef typename Superclass::TVertex TVertex; + typedef typename Superclass::TOutput TOutput; + typedef typename Superclass::TVertex TVertex; + typedef typename Superclass::TGrowFunction TGrowFunction; + typedef + fpa::Image::Functors::RegionGrow::BinaryThreshold< _TInputImage > + TBinThresholdFunction; + typedef typename _TInputImage::PixelType TPixel; - typedef fpa::Image::Functors::Base< _TInputImage, typename Superclass::TGrowFunction > TGrowFunction; + protected: + typedef typename Superclass::_TQueueNode _TQueueNode; + typedef typename Superclass::_TQueue _TQueue; public: itkNewMacro( Self ); - itkTypeMacro( fpa::Image::MoriRegionGrow, fpa::Base::MoriRegionGrow ); + itkTypeMacro( fpa::Image::MoriRegionGrow, fpa::Image::RegionGrow ); + + itkGetConstMacro( Lower, TPixel ); + itkGetConstMacro( Upper, TPixel ); + itkGetConstMacro( Step, TPixel ); + + itkSetMacro( Lower, TPixel ); + itkSetMacro( Upper, TPixel ); + itkSetMacro( Step, TPixel ); protected: MoriRegionGrow( ); virtual ~MoriRegionGrow( ); - virtual void _BeforeGenerateData( ) fpa_OVERRIDE; + virtual bool _ContinueGenerateData( ) override; + virtual void _BeforeGenerateData( ) override; + virtual void _AfterGenerateData( ) override; + virtual void _BeforeLoop( ) override; + virtual void _AfterLoop( ) override; + virtual bool _UpdateValue( _TQueueNode& v, const _TQueueNode& p ) override; + virtual void _UpdateResult( const _TQueueNode& n ) override; private: // Purposely not defined MoriRegionGrow( const Self& other ); Self& operator=( const Self& other ); + + protected: + TPixel m_Lower; + TPixel m_Upper; + TPixel m_Step; + + _TQueue m_NextQueue; + unsigned long m_ActualCount; }; } // ecapseman diff --git a/lib/fpa/Image/MoriRegionGrow.hxx b/lib/fpa/Image/MoriRegionGrow.hxx index 29f46ac..3b06e41 100644 --- a/lib/fpa/Image/MoriRegionGrow.hxx +++ b/lib/fpa/Image/MoriRegionGrow.hxx @@ -5,8 +5,17 @@ template< class _TInputImage, class _TOutputImage > fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >:: MoriRegionGrow( ) - : Superclass( ) + : Superclass( ), + m_Step( TPixel( 1 ) ) { + this->m_Upper = std::numeric_limits< TPixel >::max( ); + if( std::numeric_limits< TPixel >::is_integer ) + this->m_Lower = std::numeric_limits< TPixel >::min( ); + else + this->m_Lower = -this->m_Upper; + typename TBinThresholdFunction::Pointer functor = + TBinThresholdFunction::New( ); + this->SetGrowFunction( functor ); } // ------------------------------------------------------------------------- @@ -16,6 +25,32 @@ fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >:: { } +// ------------------------------------------------------------------------- +template< class _TInputImage, class _TOutputImage > +bool fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >:: +_ContinueGenerateData( ) +{ + TBinThresholdFunction* functor = + dynamic_cast< TBinThresholdFunction* >( this->GetGrowFunction( ) ); + TPixel u = functor->GetUpper( ); + + std::cout << long( u ) << " " << this->m_ActualCount << std::endl; + + if( u < this->m_Upper ) + { + u += this->m_Step; + if( u > this->m_Upper ) + u = this->m_Upper; + functor->SetUpper( u ); + this->m_Queue = this->m_NextQueue; + while( this->m_NextQueue.size( ) > 0 ) + this->m_NextQueue.pop( ); + return( true ); + } + else + return( false ); +} + // ------------------------------------------------------------------------- template< class _TInputImage, class _TOutputImage > void fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >:: @@ -23,10 +58,63 @@ _BeforeGenerateData( ) { this->Superclass::_BeforeGenerateData( ); - TGrowFunction* grow = - dynamic_cast< TGrowFunction* >( this->GetGrowFunction( ) ); - if( grow != NULL ) - grow->SetImage( this->GetInput( ) ); + while( this->m_NextQueue.size( ) > 0 ) + this->m_NextQueue.pop( ); + this->m_ActualCount = 0; + TBinThresholdFunction* functor = + dynamic_cast< TBinThresholdFunction* >( this->GetGrowFunction( ) ); + functor->SetLower( this->m_Lower ); + functor->SetUpper( this->m_Lower + this->m_Step ); +} + +// ------------------------------------------------------------------------- +template< class _TInputImage, class _TOutputImage > +void fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >:: +_AfterGenerateData( ) +{ + this->Superclass::_AfterGenerateData( ); + while( this->m_NextQueue.size( ) > 0 ) + this->m_NextQueue.pop( ); +} + +// ------------------------------------------------------------------------- +template< class _TInputImage, class _TOutputImage > +void fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >:: +_BeforeLoop( ) +{ + this->Superclass::_BeforeLoop( ); +} + +// ------------------------------------------------------------------------- +template< class _TInputImage, class _TOutputImage > +void fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >:: +_AfterLoop( ) +{ + this->Superclass::_AfterLoop( ); +} + +// ------------------------------------------------------------------------- +template< class _TInputImage, class _TOutputImage > +bool fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >:: +_UpdateValue( _TQueueNode& v, const _TQueueNode& p ) +{ + bool ret = this->Superclass::_UpdateValue( v, p ); + if( !ret ) + { + v.Result = this->m_InsideValue; + this->m_NextQueue.push( v ); + + } // fi + return( ret ); +} + +// ------------------------------------------------------------------------- +template< class _TInputImage, class _TOutputImage > +void fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >:: +_UpdateResult( const _TQueueNode& n ) +{ + this->Superclass::_UpdateResult( n ); + this->m_ActualCount += 1; } #endif // __fpa__Image__MoriRegionGrow__hxx__ diff --git a/lib/fpa/Image/RegionGrow.hxx b/lib/fpa/Image/RegionGrow.hxx index b1f5da0..5c8b76e 100644 --- a/lib/fpa/Image/RegionGrow.hxx +++ b/lib/fpa/Image/RegionGrow.hxx @@ -21,8 +21,8 @@ template< class _TInputImage, class _TOutputImage > void fpa::Image::RegionGrow< _TInputImage, _TOutputImage >:: _BeforeGenerateData( ) { + this->m_InitResult = this->m_OutsideValue; this->Superclass::_BeforeGenerateData( ); - TGrowFunction* grow = dynamic_cast< TGrowFunction* >( this->GetGrowFunction( ) ); if( grow != NULL ) diff --git a/plugins/ImageAlgorithms/ImageAlgorithms.i b/plugins/ImageAlgorithms/ImageAlgorithms.i index 9f44dd1..41cd37c 100644 --- a/plugins/ImageAlgorithms/ImageAlgorithms.i +++ b/plugins/ImageAlgorithms/ImageAlgorithms.i @@ -4,7 +4,6 @@ tinclude fpa/Image/Functors/SimpleNeighborhood:h|hxx instances fpa::Image::Functors::SimpleNeighborhood< itk::Image< #scalar_types#, #pdims# > > cinclude fpa/Base/RegionGrow.hxx -cinclude fpa/Base/MoriRegionGrow.hxx define all_int_types=#int_types#;#uint_types# tinclude fpa/Image/RegionGrow:h|hxx tinclude fpa/Image/MoriRegionGrow:h|hxx