From: Leonardo Florez-Valencia Date: Mon, 2 Feb 2015 23:15:24 +0000 (-0500) Subject: Multiple thresholds region grow segmentation almost added. X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=2479b55370fe1bec58d94faa3cc268bac419f6d6;p=FrontAlgorithms.git Multiple thresholds region grow segmentation almost added. --- diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index f1821ee..ebff103 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -61,7 +61,7 @@ TARGET_LINK_LIBRARIES( ${LIB_NAME} ${${LIB_NAME}_LINK_LIBRARIES} ${ITK_LIBRARIES} - ${VTK_LIBRARIES} + vtkInteractionWidgets ) ## eof - $RCSfile$ diff --git a/lib/fpa/Base/RegionGrow.h b/lib/fpa/Base/RegionGrow.h index e52fb62..f7f1562 100644 --- a/lib/fpa/Base/RegionGrow.h +++ b/lib/fpa/Base/RegionGrow.h @@ -77,12 +77,10 @@ namespace fpa typedef typename TTraits::TFrontId _TFrontId; typedef typename TTraits::TNode _TNode; typedef typename TTraits::TNodes _TNodes; - - private: - typedef std::queue< _TNode > _TQueue; + typedef std::queue< _TNode > _TQueue; public: - itkTypeMacro( RegionGrow, Base ); + itkTypeMacro( RegionGrow, Algorithm ); protected: RegionGrow( ); @@ -102,7 +100,7 @@ namespace fpa RegionGrow( const Self& ); // Not impl. void operator=( const Self& ); // Not impl. - private: + protected: _TQueue m_Queue; }; diff --git a/lib/fpa/Base/RegionGrowWithMultipleCriteria.h b/lib/fpa/Base/RegionGrowWithMultipleCriteria.h new file mode 100644 index 0000000..69fbc8e --- /dev/null +++ b/lib/fpa/Base/RegionGrowWithMultipleCriteria.h @@ -0,0 +1,79 @@ +#ifndef __FPA__BASE__REGIONGROWWITHMULTIPLECRITERIA__H__ +#define __FPA__BASE__REGIONGROWWITHMULTIPLECRITERIA__H__ + +#include +#include +#include + +namespace fpa +{ + namespace Base + { + /** + * Region grow is a front propagation with no costs. + */ + template< class V, class R, class VV, class VC, class B > + class RegionGrowWithMultipleCriteria + : public RegionGrow< V, R, VV, VC, B > + { + public: + typedef V TVertex; + typedef R TResult; + typedef VV TVertexValue; + typedef B TBaseFilter; + + /// Standard class typdedefs + typedef RegionGrowWithMultipleCriteria Self; + typedef RegionGrow< V, R, VV, VC, B > Superclass; + typedef itk::SmartPointer< Self > Pointer; + typedef itk::SmartPointer< const Self > ConstPointer; + + typedef typename Superclass::TTraits TTraits; + typedef typename Superclass::TCost TCost; + typedef itk::FunctionBase< V, TCost > TMembershipFunction; + typedef + typename TMembershipFunction::Pointer + TMembershipFunctionPointer; + typedef std::vector< TMembershipFunctionPointer > TFunctions; + + protected: + typedef typename Superclass::_TFrontId _TFrontId; + typedef typename Superclass::_TNode _TNode; + typedef typename Superclass::_TNodes _TNodes; + typedef typename Superclass::_TQueue _TQueue; + + public: + itkTypeMacro( RegionGrowWithMultipleCriteria, RegionGrow ); + + public: + unsigned int GetNumberOfMembershipFunctions( ) const; + void ClearMembershipFunctions( ); + void AddMembershipFunction( TMembershipFunction* function ); + + protected: + RegionGrowWithMultipleCriteria( ); + virtual ~RegionGrowWithMultipleCriteria( ); + + virtual void _BeforeLoop( ); + virtual _TNode _QueuePop( ); + virtual bool _CheckMembership( const _TNode& n ) const; + + private: + RegionGrowWithMultipleCriteria( const Self& ); // Not impl. + void operator=( const Self& ); // Not impl. + + protected: + mutable _TQueue m_AuxiliaryQueue; + TFunctions m_Functions; + typename TFunctions::iterator m_ActualFunction; + }; + + } // ecapseman + +} // ecapseman + +#include + +#endif // __FPA__BASE__REGIONGROWWITHMULTIPLECRITERIA__H__ + +// eof - $RCSfile$ diff --git a/lib/fpa/Base/RegionGrowWithMultipleCriteria.hxx b/lib/fpa/Base/RegionGrowWithMultipleCriteria.hxx new file mode 100644 index 0000000..34420b2 --- /dev/null +++ b/lib/fpa/Base/RegionGrowWithMultipleCriteria.hxx @@ -0,0 +1,82 @@ +#ifndef __FPA__BASE__REGIONGROWWITHMULTIPLECRITERIA__HXX__ +#define __FPA__BASE__REGIONGROWWITHMULTIPLECRITERIA__HXX__ + +// ------------------------------------------------------------------------- +template< class V, class R, class VV, class VC, class B > +unsigned int fpa::Base::RegionGrowWithMultipleCriteria< V, R, VV, VC, B >:: +GetNumberOfMembershipFunctions( ) const +{ + return( this->m_Functions.size( ) ); +} + +// ------------------------------------------------------------------------- +template< class V, class R, class VV, class VC, class B > +void fpa::Base::RegionGrowWithMultipleCriteria< V, R, VV, VC, B >:: +ClearMembershipFunctions( ) +{ + this->m_Functions.clear( ); + this->Modified( ); +} + +// ------------------------------------------------------------------------- +template< class V, class R, class VV, class VC, class B > +void fpa::Base::RegionGrowWithMultipleCriteria< V, R, VV, VC, B >:: +AddMembershipFunction( TMembershipFunction* function ) +{ + this->m_Functions.push_back( function ); + this->Modified( ); +} + +// ------------------------------------------------------------------------- +template< class V, class R, class VV, class VC, class B > +fpa::Base::RegionGrowWithMultipleCriteria< V, R, VV, VC, B >:: +RegionGrowWithMultipleCriteria( ) + : Superclass( ) +{ +} + +// ------------------------------------------------------------------------- +template< class V, class R, class VV, class VC, class B > +fpa::Base::RegionGrowWithMultipleCriteria< V, R, VV, VC, B >:: +~RegionGrowWithMultipleCriteria( ) +{ +} + +// ------------------------------------------------------------------------- +template< class V, class R, class VV, class VC, class B > +void fpa::Base::RegionGrowWithMultipleCriteria< V, R, VV, VC, B >:: +_BeforeLoop( ) +{ + this->_BeforeLoop( ); + this->m_ActualFunction = this->m_Functions.begin( ); +} + +// ------------------------------------------------------------------------- +template< class V, class R, class VV, class VC, class B > +typename fpa::Base::RegionGrowWithMultipleCriteria< V, R, VV, VC, B >:: +_TNode fpa::Base::RegionGrowWithMultipleCriteria< V, R, VV, VC, B >:: +_QueuePop( ) +{ + _TNode node = this->Superclass::_QueuePop( ); + if( this->_IsQueueEmpty( ) ) + { + std::cerr << "ERROR!!!!" << std::endl; + + } // fi + return( node ); +} + +// ------------------------------------------------------------------------- +template< class V, class R, class VV, class VC, class B > +bool fpa::Base::RegionGrowWithMultipleCriteria< V, R, VV, VC, B >:: +_CheckMembership( const _TNode& n ) const +{ + bool ret = ( *( this->m_ActualFunction ) )->Evaluate( n.Vertex ); + if( !ret ) + this->m_AuxiliaryQueue.push( n ); + return( ret ); +} + +#endif // __FPA__BASE__REGIONGROWWITHMULTIPLECRITERIA__HXX__ + +// eof - $RCSfile$ diff --git a/lib/fpa/Image/Functors/ImageFunction.h b/lib/fpa/Image/Functors/ImageFunction.h new file mode 100644 index 0000000..a1cc9ec --- /dev/null +++ b/lib/fpa/Image/Functors/ImageFunction.h @@ -0,0 +1,62 @@ +#ifndef __FPA__IMAGE__FUNCTORS__IMAGEFUNCTION__H__ +#define __FPA__IMAGE__FUNCTORS__IMAGEFUNCTION__H__ + +#include + +namespace fpa +{ + namespace Image + { + namespace Functors + { + /** + */ + template< class I, class O > + class ImageFunction + : public itk::FunctionBase< typename I::IndexType, O > + { + public: + /// Type-related and pointers + typedef ImageFunction Self; + typedef itk::FunctionBase< typename I::IndexType, O > Superclass; + typedef itk::SmartPointer< Self > Pointer; + typedef itk::SmartPointer< const Self > ConstPointer; + + typedef I TInputImage; + typedef O TOutputValue; + typedef typename I::IndexType TIndex; + + public: + itkTypeMacro( ImageFunction, itkFunctionBase ); + + itkGetConstObjectMacro( InputImage, I ); + itkSetConstObjectMacro( InputImage, I ); + + public: + virtual O Evaluate( const TIndex& idx ) const = 0; + + protected: + ImageFunction( ) + : Superclass( ) + { } + virtual ~ImageFunction( ) + { } + + private: + // Purposely not implemented + ImageFunction( const Self& ); + void operator=( const Self& ); + + protected: + typename I::ConstPointer m_InputImage; + }; + + } // ecapseman + + } // ecapseman + +} // ecapseman + +#endif // __FPA__IMAGE__FUNCTORS__IMAGEFUNCTION__H__ + +// eof - $RCSfile$ diff --git a/lib/fpa/Image/Functors/RegionGrowAllBelongsFunction.h b/lib/fpa/Image/Functors/RegionGrowAllBelongsFunction.h index ca52050..e352f59 100644 --- a/lib/fpa/Image/Functors/RegionGrowAllBelongsFunction.h +++ b/lib/fpa/Image/Functors/RegionGrowAllBelongsFunction.h @@ -1,7 +1,7 @@ #ifndef __FPA__IMAGE__FUNCTORS__REGIONGROWALLBELONGSFUNCTION__H__ #define __FPA__IMAGE__FUNCTORS__REGIONGROWALLBELONGSFUNCTION__H__ -#include +#include namespace fpa { @@ -13,45 +13,26 @@ namespace fpa */ template< class I > class RegionGrowAllBelongsFunction - : public itk::ImageFunction< I, bool > + : public fpa::Image::Functors::ImageFunction< I, bool > { public: /// Type-related and pointers - typedef RegionGrowAllBelongsFunction Self; - typedef itk::ImageFunction< I, bool > Superclass; - typedef itk::SmartPointer< Self > Pointer; - typedef itk::SmartPointer< const Self > ConstPointer; + typedef RegionGrowAllBelongsFunction Self; + typedef fpa::Image::Functors::ImageFunction< I, bool > Superclass; + typedef itk::SmartPointer< Self > Pointer; + typedef itk::SmartPointer< const Self > ConstPointer; // Superclass' types - typedef typename Superclass::InputImageType InputImageType; - typedef typename Superclass::InputPixelType InputPixelType; - typedef typename Superclass::OutputType OutputType; - typedef typename Superclass::CoordRepType CoordRepType; - typedef typename Superclass::IndexType IndexType; - typedef typename Superclass::IndexValueType IndexValueType; - typedef typename Superclass::ContinuousIndexType ContinuousIndexType; - typedef typename Superclass::PointType PointType; + typedef typename Superclass::TInputImage TInputImage; + typedef typename Superclass::TOutputValue TOutputValue; + typedef typename Superclass::TIndex TIndex; public: itkNewMacro( Self ); itkTypeMacro( RegionGrowAllBelongsFunction, itkImageFunction ); public: - virtual OutputType Evaluate( const PointType& point ) const - { - IndexType index; - this->ConvertPointToNearestIndex( point, index ); - return( this->EvaluateAtIndex( index ) ); - } - virtual OutputType EvaluateAtContinuousIndex( - const ContinuousIndexType& cindex - ) const - { - IndexType index; - this->ConvertContinuousIndexToNearestIndex( cindex, index ); - return( this->EvaluateAtIndex( index ) ); - } - virtual OutputType EvaluateAtIndex( const IndexType& index ) const + virtual TOutputValue Evaluate( const TIndex& idx ) const { return( true ); } protected: diff --git a/lib/fpa/Image/Functors/RegionGrowThresholdFunction.h b/lib/fpa/Image/Functors/RegionGrowThresholdFunction.h index 00a813f..44cccc2 100644 --- a/lib/fpa/Image/Functors/RegionGrowThresholdFunction.h +++ b/lib/fpa/Image/Functors/RegionGrowThresholdFunction.h @@ -24,14 +24,10 @@ namespace fpa typedef itk::SmartPointer< const Self > ConstPointer; // Superclass' types - typedef typename Superclass::InputImageType InputImageType; - typedef typename Superclass::InputPixelType InputPixelType; - typedef typename Superclass::OutputType OutputType; - typedef typename Superclass::CoordRepType CoordRepType; - typedef typename Superclass::IndexType IndexType; - typedef typename Superclass::IndexValueType IndexValueType; - typedef typename Superclass::ContinuousIndexType ContinuousIndexType; - typedef typename Superclass::PointType PointType; + typedef typename Superclass::TInputImage TInputImage; + typedef typename Superclass::TOutputValue TOutputValue; + typedef typename Superclass::TIndex TIndex; + typedef typename I::PixelType TPixel; public: itkNewMacro( Self ); @@ -40,27 +36,29 @@ namespace fpa RegionGrowAllBelongsFunction ); - itkGetConstMacro( LowerThreshold, InputPixelType ); - itkGetConstMacro( UpperThreshold, InputPixelType ); + itkGetConstMacro( LowerThreshold, TPixel ); + itkGetConstMacro( UpperThreshold, TPixel ); - itkSetMacro( LowerThreshold, InputPixelType ); - itkSetMacro( UpperThreshold, InputPixelType ); + itkSetMacro( LowerThreshold, TPixel ); + itkSetMacro( UpperThreshold, TPixel ); public: - virtual OutputType EvaluateAtIndex( const IndexType& index ) const + virtual TOutputValue Evaluate( const TIndex& idx ) const { const I* img = this->GetInputImage( ); if( img != NULL ) { - if( this->IsInsideBuffer( index ) ) - { - InputPixelType v = img->GetPixel( index ); - return( - this->m_LowerThreshold <= v && - v <= this->m_UpperThreshold - ); - - } // fi + /* TODO + if( this->IsInsideBuffer( idx ) ) + { + */ + TPixel v = img->GetPixel( idx ); + return( + this->m_LowerThreshold <= v && + v <= this->m_UpperThreshold + ); + + // TODO: } // fi } // fi return( false ); @@ -69,8 +67,8 @@ namespace fpa protected: RegionGrowThresholdFunction( ) : Superclass( ), - m_LowerThreshold( std::numeric_limits< InputPixelType >::min( ) ), - m_UpperThreshold( std::numeric_limits< InputPixelType >::max( ) ) + m_LowerThreshold( std::numeric_limits< TPixel >::min( ) ), + m_UpperThreshold( std::numeric_limits< TPixel >::max( ) ) { } virtual ~RegionGrowThresholdFunction( ) { } @@ -81,8 +79,8 @@ namespace fpa void operator=( const Self& ); protected: - InputPixelType m_LowerThreshold; - InputPixelType m_UpperThreshold; + TPixel m_LowerThreshold; + TPixel m_UpperThreshold; }; } // ecapseman diff --git a/lib/fpa/Image/RegionGrowWithMultipleCriteria.h b/lib/fpa/Image/RegionGrowWithMultipleCriteria.h new file mode 100644 index 0000000..2b06837 --- /dev/null +++ b/lib/fpa/Image/RegionGrowWithMultipleCriteria.h @@ -0,0 +1,65 @@ +#ifndef __FPA__IMAGE__REGIONGROWWITHMULTIPLECRITERIA__H__ +#define __FPA__IMAGE__REGIONGROWWITHMULTIPLECRITERIA__H__ + +#include +#include +#include +#include +#include + +namespace fpa +{ + namespace Image + { + /** + * @param I Input image type + */ + template< class I > + class RegionGrowWithMultipleCriteria + : public Algorithm< I, fpa::Base::RegionGrowWithMultipleCriteria< typename I::IndexType, typename I::PixelType, typename I::PixelType, itk::Functor::IndexLexicographicCompare< I::ImageDimension >, itk::ImageToImageFilter< I, I > > > + { + public: + // Standard class typdedefs + typedef typename I::IndexType TVertex; + typedef typename I::PixelType TResult; + typedef typename I::PixelType TVertexValue; + typedef itk::ImageToImageFilter< I, I > TBaseFilter; + typedef fpa::Base::RegionGrowWithMultipleCriteria< TVertex, TResult, TVertexValue, itk::Functor::IndexLexicographicCompare< I::ImageDimension >, TBaseFilter > TBaseAlgorithm; + + typedef RegionGrowWithMultipleCriteria Self; + typedef Algorithm< I, TBaseAlgorithm > Superclass; + typedef itk::SmartPointer< Self > Pointer; + typedef itk::SmartPointer< const Self > ConstPointer; + + typedef + fpa::Image::Functors::ImageFunction< I, bool > + TMembershipFunction; + typedef typename Superclass::TFunctions TFunctions; + + public: + itkNewMacro( Self ); + itkTypeMacro( + RegionGrowWithMultipleCriteria, + fpaBaseRegionGrowWithMultipleCriteria + ); + + protected: + RegionGrowWithMultipleCriteria( ) + : Superclass( ) + { } + virtual ~RegionGrowWithMultipleCriteria( ) + { } + + private: + // Purposely not implemented + RegionGrowWithMultipleCriteria( const Self& ); + void operator=( const Self& ); + }; + + } // ecapseman + +} // ecapseman + +#endif // __FPA__IMAGE__REGIONGROWWITHMULTIPLECRITERIA__H__ + +// eof - $RCSfile$ diff --git a/lib/fpa/Image/RegionGrowWithMultipleThresholds.h b/lib/fpa/Image/RegionGrowWithMultipleThresholds.h index 3efd87a..41fba37 100644 --- a/lib/fpa/Image/RegionGrowWithMultipleThresholds.h +++ b/lib/fpa/Image/RegionGrowWithMultipleThresholds.h @@ -2,7 +2,7 @@ #define __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__H__ #include -#include +#include namespace fpa { @@ -13,18 +13,20 @@ namespace fpa */ template< class I > class RegionGrowWithMultipleThresholds - : public RegionGrow< I > + : public RegionGrowWithMultipleCriteria< I > { public: - typedef RegionGrowWithMultipleThresholds Self; - typedef RegionGrow< I > Superclass; - typedef itk::SmartPointer< Self > Pointer; - typedef itk::SmartPointer< const Self > ConstPointer; + typedef RegionGrowWithMultipleThresholds Self; + typedef RegionGrowWithMultipleCriteria< I > Superclass; + typedef itk::SmartPointer< Self > Pointer; + typedef itk::SmartPointer< const Self > ConstPointer; typedef typename I::PixelType TPixel; - typedef std::map< TPixel, unsigned long > THistogram; - typedef typename Superclass::TBaseAlgorithm TBaseAlgorithm; + typedef std::map< TPixel, unsigned long > THistogram; + typedef typename Superclass::TBaseAlgorithm TBaseAlgorithm; + typedef typename Superclass::TMembershipFunction TMembershipFunction; + typedef typename Superclass::TFunctions TFunctions; protected: typedef typename TBaseAlgorithm::_TNode _TNode; @@ -49,6 +51,7 @@ namespace fpa virtual ~RegionGrowWithMultipleThresholds( ); virtual bool _UpdateResult( _TNode& n ); + virtual void _BeforeLoop( ); virtual void _AfterLoop( ); private: diff --git a/lib/fpa/Image/RegionGrowWithMultipleThresholds.hxx b/lib/fpa/Image/RegionGrowWithMultipleThresholds.hxx index def89d9..51e474b 100644 --- a/lib/fpa/Image/RegionGrowWithMultipleThresholds.hxx +++ b/lib/fpa/Image/RegionGrowWithMultipleThresholds.hxx @@ -1,6 +1,7 @@ #ifndef __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__HXX__ #define __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__HXX__ +#include #include // ------------------------------------------------------------------------- @@ -8,22 +9,35 @@ template< class I > void fpa::Image::RegionGrowWithMultipleThresholds< I >:: AddThreshold( const TPixel& v ) { - this->m_Histogram[ v ] = 0; + typedef + fpa::Image::Functors::RegionGrowThresholdFunction< I > + TFunction; + typename TFunction::Pointer function = TFunction::New( ); + + if( this->GetNumberOfMembershipFunctions( ) > 0 ) + { + } + else + function->SetLowerThreshold( std::numeric_limits< TPixel >::min( ) ); + std::cout + << typeid( TPixel ).name( ) << " " + << function->GetLowerThreshold( ) + << std::endl; + function->SetUpperThreshold( v ); + this->AddMembershipFunction( function ); /* TODO - typedef - fpa::Image::Functors::RegionGrowThresholdFunction< I > - TFunction; + this->m_Histogram[ v ] = 0; + TFunction* function = dynamic_cast< TFunction* >( this->GetMembershipFunction( ) ); if( function != NULL ) { function->SetLowerThreshold( this->m_Histogram.begin( )->first ); - function->SetUpperThreshold( this->m_Histogram.rbegin( )->first ); } // fi + this->Modified( ); */ - this->Modified( ); } // ------------------------------------------------------------------------- @@ -43,11 +57,13 @@ RegionGrowWithMultipleThresholds( ) : Superclass( ), m_DerivativeThreshold( double( 3 ) ) { - typedef - fpa::Image::Functors::RegionGrowThresholdFunction< I > - TFunction; - typename TFunction::Pointer function = TFunction::New( ); - this->SetMembershipFunction( function ); + /* TODO + typedef + fpa::Image::Functors::RegionGrowThresholdFunction< I > + TFunction; + typename TFunction::Pointer function = TFunction::New( ); + this->SetMembershipFunction( function ); + */ } // ------------------------------------------------------------------------- @@ -64,28 +80,50 @@ _UpdateResult( _TNode& n ) { bool ret = this->Superclass::_UpdateResult( n ); - if( ret ) - { - TPixel v = TPixel( this->_Cost( n.Vertex, n.Vertex ) ); + /* TODO + if( ret ) + { + TPixel v = TPixel( this->_Cost( n.Vertex, n.Vertex ) ); - typename THistogram::reverse_iterator hIt = this->m_Histogram.rbegin( ); - while( hIt != this->m_Histogram.rend( ) ) - { - if( v <= hIt->first ) - { - hIt->second += 1; - hIt++; - } - else - hIt = this->m_Histogram.rend( ); - - } // elihw - this->GetOutput( )->SetPixel( n.Vertex, v ); - - } // fi + typename THistogram::reverse_iterator hIt = this->m_Histogram.rbegin( ); + while( hIt != this->m_Histogram.rend( ) ) + { + if( v <= hIt->first ) + { + hIt->second += 1; + hIt++; + } + else + hIt = this->m_Histogram.rend( ); + + } // elihw + this->GetOutput( )->SetPixel( n.Vertex, v ); + + } // fi + */ return( ret ); } +// ------------------------------------------------------------------------- +template< class I > +void fpa::Image::RegionGrowWithMultipleThresholds< I >:: +_BeforeLoop( ) +{ + std::cout << "**1" << std::endl; + const I* img = this->GetInput( ); + std::cout << "**2" << std::endl; + typename TFunctions::iterator fIt = this->m_Functions.begin( ); + for( ; fIt != this->m_Functions.end( ); ++fIt ) + { + TMembershipFunction* f = + dynamic_cast< TMembershipFunction* >( fIt->GetPointer( ) ); + if( f != NULL ) + f->SetInputImage( this->GetInput( ) ); + + } // rof + this->Superclass::_BeforeLoop( ); +} + // ------------------------------------------------------------------------- template< class I > void fpa::Image::RegionGrowWithMultipleThresholds< I >::