From 31c007a5d5a410c96807cedc4efb2acda2489319 Mon Sep 17 00:00:00 2001 From: Leonardo Florez-Valencia <florez-l@javeriana.edu.co> Date: Fri, 1 Apr 2016 14:00:10 -0500 Subject: [PATCH] ... --- .../Algorithms/ImageFunctionFilter.h | 74 +++++-------------- .../Algorithms/ImageFunctionFilter.hxx | 67 +++++++++++++++++ 2 files changed, 85 insertions(+), 56 deletions(-) create mode 100644 lib/cpExtensions/Algorithms/ImageFunctionFilter.hxx diff --git a/lib/cpExtensions/Algorithms/ImageFunctionFilter.h b/lib/cpExtensions/Algorithms/ImageFunctionFilter.h index 39007a5..731f0f5 100644 --- a/lib/cpExtensions/Algorithms/ImageFunctionFilter.h +++ b/lib/cpExtensions/Algorithms/ImageFunctionFilter.h @@ -6,9 +6,6 @@ #define __CPEXTENSIONS__ALGORITHMS__IMAGEFUNCTIONFILTER__H__ #include <itkImageToImageFilter.h> -#include <itkImageScanlineConstIterator.h> -#include <itkImageScanlineIterator.h> -#include <itkProgressReporter.h> namespace cpExtensions { @@ -16,73 +13,38 @@ namespace cpExtensions { /** */ - template< class I, class O, class F > + template< class _TInput, class _TOutput, class _TFunction > class ImageFunctionFilter - : public itk::ImageToImageFilter< I, O > + : public itk::ImageToImageFilter< _TInput, _TOutput > { public: - typedef ImageFunctionFilter Self; - typedef itk::ImageToImageFilter< I, O > Superclass; - typedef itk::SmartPointer< Self > Pointer; - typedef itk::SmartPointer< const Self > ConstPointer; + typedef ImageFunctionFilter Self; + typedef itk::ImageToImageFilter< _TInput, _TOutput > Superclass; + typedef itk::SmartPointer< Self > Pointer; + typedef itk::SmartPointer< const Self > ConstPointer; - typedef I TInputImage; - typedef O TOutputImage; - typedef F TFunction; + typedef _TInput TInput; + typedef _TOutput TOutput; + typedef _TFunction TFunction; - typedef typename O::RegionType TRegion; + typedef typename TOutput::RegionType TRegion; public: itkNewMacro( Self ); itkTypeMacro( ImageFunctionFilter, itkImageToImageFilter ); - itkGetObjectMacro( Function, F ); - itkSetObjectMacro( Function, F ); + itkGetObjectMacro( Function, TFunction ); + itkSetObjectMacro( Function, TFunction ); protected: - ImageFunctionFilter( ) - : Superclass( ) - { - } - virtual ~ImageFunctionFilter( ) - { - } + ImageFunctionFilter( ); + virtual ~ImageFunctionFilter( ); + virtual void BeforeThreadedGenerateData( ) ITK_OVERRIDE; virtual void ThreadedGenerateData( const TRegion& region, itk::ThreadIdType threadId - ) - { - const typename TRegion::SizeType& regionSize = region.GetSize( ); - if( regionSize[ 0 ] == 0 ) - return; - const I* in = this->GetInput( ); - O* out = this->GetOutput( 0 ); - - const size_t nLines = region.GetNumberOfPixels( ) / regionSize[ 0 ]; - itk::ProgressReporter progress( this, threadId, nLines ); - - // Define the iterators - itk::ImageScanlineConstIterator< I > inIt( in, region ); - itk::ImageScanlineIterator< O > outIt( out, region ); - - inIt.GoToBegin( ); - outIt.GoToBegin( ); - while( !inIt.IsAtEnd( ) ) - { - while( !inIt.IsAtEndOfLine( ) ) - { - outIt.Set( this->m_Function->EvaluateAtIndex( inIt.GetIndex( ) ) ); - ++inIt; - ++outIt; - - } // elihw - inIt.NextLine( ); - outIt.NextLine( ); - progress.CompletedPixel( ); - - } // elihw - } + ) ITK_OVERRIDE; private: // Purposely not implemented. @@ -90,7 +52,7 @@ namespace cpExtensions void operator=( const Self& ); protected: - typename F::Pointer m_Function; + typename TFunction::Pointer m_Function; }; } // ecapseman @@ -98,7 +60,7 @@ namespace cpExtensions } // ecapseman #ifndef ITK_MANUAL_INSTANTIATION -// TODO: #include <cpExtensions/Algorithms/ImageFunctionFilter.hxx> +#include <cpExtensions/Algorithms/ImageFunctionFilter.hxx> #endif // ITK_MANUAL_INSTANTIATION #endif // __CPEXTENSIONS__ALGORITHMS__IMAGEFUNCTIONFILTER__H__ diff --git a/lib/cpExtensions/Algorithms/ImageFunctionFilter.hxx b/lib/cpExtensions/Algorithms/ImageFunctionFilter.hxx new file mode 100644 index 0000000..c96c092 --- /dev/null +++ b/lib/cpExtensions/Algorithms/ImageFunctionFilter.hxx @@ -0,0 +1,67 @@ +// ------------------------------------------------------------------------- +// @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co) +// ------------------------------------------------------------------------- + +#ifndef __CPEXTENSIONS__ALGORITHMS__IMAGEFUNCTIONFILTER__HXX__ +#define __CPEXTENSIONS__ALGORITHMS__IMAGEFUNCTIONFILTER__HXX__ + +#include <itkMacro.h> +#include <itkImageRegionConstIteratorWithIndex.h> +#include <itkImageRegionIteratorWithIndex.h> +#include <itkProgressReporter.h> + +// ------------------------------------------------------------------------- +template< class _TInput, class _TOutput, class _TFilter > +cpExtensions::Algorithms::ImageFunctionFilter< _TInput, _TOutput, _TFilter >:: +ImageFunctionFilter( ) + : Superclass( ) +{ +} + +// ------------------------------------------------------------------------- +template< class _TInput, class _TOutput, class _TFilter > +cpExtensions::Algorithms::ImageFunctionFilter< _TInput, _TOutput, _TFilter >:: +~ImageFunctionFilter( ) +{ +} + +// ------------------------------------------------------------------------- +template< class _TInput, class _TOutput, class _TFilter > +void cpExtensions::Algorithms::ImageFunctionFilter< _TInput, _TOutput, _TFilter >:: +BeforeThreadedGenerateData( ) +{ + if( this->m_Function.IsNull( ) ) + itkExceptionMacro( << "Please set a valid itk::ImageFunction" ); + this->m_Function->SetInputImage( this->GetInput( ) ); +} + +// ------------------------------------------------------------------------- +template< class _TInput, class _TOutput, class _TFilter > +void cpExtensions::Algorithms::ImageFunctionFilter< _TInput, _TOutput, _TFilter >:: +ThreadedGenerateData( const TRegion& region, itk::ThreadIdType threadId ) +{ + const typename TRegion::SizeType& regionSize = region.GetSize( ); + if( regionSize[ 0 ] == 0 ) + return; + const _TInput* in = this->GetInput( ); + _TOutput* out = this->GetOutput( 0 ); + + itk::ProgressReporter pr( this, threadId, region.GetNumberOfPixels( ) ); + + // Define the iterators + itk::ImageRegionConstIteratorWithIndex< TInput > iIt( in, region ); + itk::ImageRegionIteratorWithIndex< TOutput > oIt( out, region ); + + iIt.GoToBegin( ); + oIt.GoToBegin( ); + for( ; !iIt.IsAtEnd( ) && !oIt.IsAtEnd( ); ++iIt, ++oIt ) + { + oIt.Set( this->m_Function->EvaluateAtIndex( iIt.GetIndex( ) ) ); + pr.CompletedPixel( ); + + } // rof +} + +#endif // __CPEXTENSIONS__ALGORITHMS__IMAGEFUNCTIONFILTER__HXX__ + +// eof - $RCSfile$ -- 2.49.0