Skip to content

Commit

Permalink
Use ImageSinc for a base class of StatisticsImageFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
blowekamp committed Apr 17, 2015
1 parent aea2ca6 commit dae948b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 109 deletions.
50 changes: 19 additions & 31 deletions include/itkStreamingStatisticsImageFilter.h
Expand Up @@ -15,8 +15,8 @@
* limitations under the License.
*
*=========================================================================*/
#ifndef itkStatisticsImageFilter_h
#define itkStatisticsImageFilter_h
#ifndef itkStreamingStatisticsImageFilter_h
#define itkStreamingStatisticsImageFilter_h

#include "itkImageToImageFilter.h"
#include "itkNumericTraits.h"
Expand All @@ -25,10 +25,10 @@

namespace itk
{
/** \class StatisticsImageFilter
/** \class StreamingStatisticsImageFilter
* \brief Compute min. max, variance and mean of an Image.
*
* StatisticsImageFilter computes the minimum, maximum, sum, mean, variance
* StreamingStatisticsImageFilter computes the minimum, maximum, sum, mean, variance
* sigma of an image. The filter needs all of its input image. It
* behaves as a filter with an input and output. Thus it can be inserted
* in a pipline with other filters and the statistics will only be
Expand All @@ -38,26 +38,26 @@ namespace itk
* threaded. It computes statistics in each thread then combines them in
* its AfterThreadedGenerate method.
*
* \ingroup MathematicalStatisticsImageFilters
* \ingroup MathematicalStreamingStatisticsImageFilters
*
* \ingroup StreamingSinc
*/
template< typename TInputImage >
class StatisticsImageFilter:
public ImageToImageFilter< TInputImage, TInputImage >
class StreamingStatisticsImageFilter:
public ImageSinc< TInputImage >
{
public:
/** Standard Self typedef */
typedef StatisticsImageFilter Self;
typedef ImageToImageFilter< TInputImage, TInputImage > Superclass;
typedef SmartPointer< Self > Pointer;
typedef SmartPointer< const Self > ConstPointer;
typedef StreamingStatisticsImageFilter Self;
typedef ImageSinc< TInputImage > Superclass;
typedef SmartPointer< Self > Pointer;
typedef SmartPointer< const Self > ConstPointer;

/** Method for creation through the object factory. */
itkNewMacro(Self);

/** Runtime information support. */
itkTypeMacro(StatisticsImageFilter, ImageToImageFilter);
itkTypeMacro(StreamingStatisticsImageFilter, ImageSinc);

/** Image related typedefs. */
typedef typename TInputImage::Pointer InputImagePointer;
Expand Down Expand Up @@ -137,35 +137,23 @@ class StatisticsImageFilter:
#endif

protected:
StatisticsImageFilter();
~StatisticsImageFilter(){}
StreamingStatisticsImageFilter();
~StreamingStatisticsImageFilter(){}
void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;

/** Pass the input through unmodified. Do this by Grafting in the
* AllocateOutputs method.
*/
void AllocateOutputs() ITK_OVERRIDE;

/** Initialize some accumulators before the threads run. */
void BeforeThreadedGenerateData() ITK_OVERRIDE;
void BeforeStreamedGenerateData() ITK_OVERRIDE;

/** Do final mean and variance computation from data accumulated in threads.
*/
void AfterThreadedGenerateData() ITK_OVERRIDE;
void AfterStreamedGenerateData() ITK_OVERRIDE;

/** Multi-thread version GenerateData. */
void ThreadedGenerateData(const RegionType &
outputRegionForThread,
ThreadIdType threadId) ITK_OVERRIDE;

// Override since the filter needs all the data for the algorithm
void GenerateInputRequestedRegion() ITK_OVERRIDE;
void ThreadedStreamedGenerateData(const RegionType &inputRegion, ThreadIdType threadId) ITK_OVERRIDE;

// Override since the filter produces all of its output
void EnlargeOutputRequestedRegion(DataObject *data) ITK_OVERRIDE;

private:
StatisticsImageFilter(const Self &); //purposely not implemented
StreamingStatisticsImageFilter(const Self &); //purposely not implemented
void operator=(const Self &); //purposely not implemented

Array< RealType > m_ThreadSum;
Expand All @@ -177,7 +165,7 @@ class StatisticsImageFilter:
} // end namespace itk

#ifndef ITK_MANUAL_INSTANTIATION
#include "itkStatisticsImageFilter.hxx"
#include "itkStreamingStatisticsImageFilter.hxx"
#endif

#endif
118 changes: 40 additions & 78 deletions include/itkStreamingStatisticsImageFilter.hxx
Expand Up @@ -15,9 +15,9 @@
* limitations under the License.
*
*=========================================================================*/
#ifndef itkStatisticsImageFilter_hxx
#define itkStatisticsImageFilter_hxx
#include "itkStatisticsImageFilter.h"
#ifndef itkStreamingStatisticsImageFilter_hxx
#define itkStreamingStatisticsImageFilter_hxx
#include "itkStreamingStatisticsImageFilter.h"


#include "itkImageScanlineIterator.h"
Expand All @@ -26,8 +26,8 @@
namespace itk
{
template< typename TInputImage >
StatisticsImageFilter< TInputImage >
::StatisticsImageFilter():m_ThreadSum(1), m_SumOfSquares(1), m_Count(1), m_ThreadMin(1), m_ThreadMax(1)
StreamingStatisticsImageFilter< TInputImage >
::StreamingStatisticsImageFilter():m_ThreadSum(1), m_SumOfSquares(1), m_Count(1), m_ThreadMin(1), m_ThreadMax(1)
{
// first output is a copy of the image, DataObject created by
// superclass
Expand Down Expand Up @@ -59,7 +59,7 @@ StatisticsImageFilter< TInputImage >

template< typename TInputImage >
DataObject::Pointer
StatisticsImageFilter< TInputImage >
StreamingStatisticsImageFilter< TInputImage >
::MakeOutput(DataObjectPointerArraySizeType output)
{
switch ( output )
Expand Down Expand Up @@ -87,142 +87,105 @@ StatisticsImageFilter< TInputImage >
}

template< typename TInputImage >
typename StatisticsImageFilter< TInputImage >::PixelObjectType *
StatisticsImageFilter< TInputImage >
typename StreamingStatisticsImageFilter< TInputImage >::PixelObjectType *
StreamingStatisticsImageFilter< TInputImage >
::GetMinimumOutput()
{
return static_cast< PixelObjectType * >( this->ProcessObject::GetOutput(1) );
}

template< typename TInputImage >
const typename StatisticsImageFilter< TInputImage >::PixelObjectType *
StatisticsImageFilter< TInputImage >
const typename StreamingStatisticsImageFilter< TInputImage >::PixelObjectType *
StreamingStatisticsImageFilter< TInputImage >
::GetMinimumOutput() const
{
return static_cast< const PixelObjectType * >( this->ProcessObject::GetOutput(1) );
}

template< typename TInputImage >
typename StatisticsImageFilter< TInputImage >::PixelObjectType *
StatisticsImageFilter< TInputImage >
typename StreamingStatisticsImageFilter< TInputImage >::PixelObjectType *
StreamingStatisticsImageFilter< TInputImage >
::GetMaximumOutput()
{
return static_cast< PixelObjectType * >( this->ProcessObject::GetOutput(2) );
}

template< typename TInputImage >
const typename StatisticsImageFilter< TInputImage >::PixelObjectType *
StatisticsImageFilter< TInputImage >
const typename StreamingStatisticsImageFilter< TInputImage >::PixelObjectType *
StreamingStatisticsImageFilter< TInputImage >
::GetMaximumOutput() const
{
return static_cast< const PixelObjectType * >( this->ProcessObject::GetOutput(2) );
}

template< typename TInputImage >
typename StatisticsImageFilter< TInputImage >::RealObjectType *
StatisticsImageFilter< TInputImage >
typename StreamingStatisticsImageFilter< TInputImage >::RealObjectType *
StreamingStatisticsImageFilter< TInputImage >
::GetMeanOutput()
{
return static_cast< RealObjectType * >( this->ProcessObject::GetOutput(3) );
}

template< typename TInputImage >
const typename StatisticsImageFilter< TInputImage >::RealObjectType *
StatisticsImageFilter< TInputImage >
const typename StreamingStatisticsImageFilter< TInputImage >::RealObjectType *
StreamingStatisticsImageFilter< TInputImage >
::GetMeanOutput() const
{
return static_cast< const RealObjectType * >( this->ProcessObject::GetOutput(3) );
}

template< typename TInputImage >
typename StatisticsImageFilter< TInputImage >::RealObjectType *
StatisticsImageFilter< TInputImage >
typename StreamingStatisticsImageFilter< TInputImage >::RealObjectType *
StreamingStatisticsImageFilter< TInputImage >
::GetSigmaOutput()
{
return static_cast< RealObjectType * >( this->ProcessObject::GetOutput(4) );
}

template< typename TInputImage >
const typename StatisticsImageFilter< TInputImage >::RealObjectType *
StatisticsImageFilter< TInputImage >
const typename StreamingStatisticsImageFilter< TInputImage >::RealObjectType *
StreamingStatisticsImageFilter< TInputImage >
::GetSigmaOutput() const
{
return static_cast< const RealObjectType * >( this->ProcessObject::GetOutput(4) );
}

template< typename TInputImage >
typename StatisticsImageFilter< TInputImage >::RealObjectType *
StatisticsImageFilter< TInputImage >
typename StreamingStatisticsImageFilter< TInputImage >::RealObjectType *
StreamingStatisticsImageFilter< TInputImage >
::GetVarianceOutput()
{
return static_cast< RealObjectType * >( this->ProcessObject::GetOutput(5) );
}

template< typename TInputImage >
const typename StatisticsImageFilter< TInputImage >::RealObjectType *
StatisticsImageFilter< TInputImage >
const typename StreamingStatisticsImageFilter< TInputImage >::RealObjectType *
StreamingStatisticsImageFilter< TInputImage >
::GetVarianceOutput() const
{
return static_cast< const RealObjectType * >( this->ProcessObject::GetOutput(5) );
}

template< typename TInputImage >
typename StatisticsImageFilter< TInputImage >::RealObjectType *
StatisticsImageFilter< TInputImage >
typename StreamingStatisticsImageFilter< TInputImage >::RealObjectType *
StreamingStatisticsImageFilter< TInputImage >
::GetSumOutput()
{
return static_cast< RealObjectType * >( this->ProcessObject::GetOutput(6) );
}

template< typename TInputImage >
const typename StatisticsImageFilter< TInputImage >::RealObjectType *
StatisticsImageFilter< TInputImage >
const typename StreamingStatisticsImageFilter< TInputImage >::RealObjectType *
StreamingStatisticsImageFilter< TInputImage >
::GetSumOutput() const
{
return static_cast< const RealObjectType * >( this->ProcessObject::GetOutput(6) );
}

template< typename TInputImage >
void
StatisticsImageFilter< TInputImage >
::GenerateInputRequestedRegion()
{
Superclass::GenerateInputRequestedRegion();
if ( this->GetInput() )
{
InputImagePointer image =
const_cast< typename Superclass::InputImageType * >( this->GetInput() );
image->SetRequestedRegionToLargestPossibleRegion();
}
}

template< typename TInputImage >
void
StatisticsImageFilter< TInputImage >
::EnlargeOutputRequestedRegion(DataObject *data)
{
Superclass::EnlargeOutputRequestedRegion(data);
data->SetRequestedRegionToLargestPossibleRegion();
}

template< typename TInputImage >
void
StatisticsImageFilter< TInputImage >
::AllocateOutputs()
{
// Pass the input through as the output
InputImagePointer image =
const_cast< TInputImage * >( this->GetInput() );

this->GraftOutput(image);

// Nothing that needs to be allocated for the remaining outputs
}

template< typename TInputImage >
void
StatisticsImageFilter< TInputImage >
::BeforeThreadedGenerateData()
StreamingStatisticsImageFilter< TInputImage >
::BeforeStreamedGenerateData()
{
ThreadIdType numberOfThreads = this->GetNumberOfThreads();

Expand All @@ -243,8 +206,8 @@ StatisticsImageFilter< TInputImage >

template< typename TInputImage >
void
StatisticsImageFilter< TInputImage >
::AfterThreadedGenerateData()
StreamingStatisticsImageFilter< TInputImage >
::AfterStreamedGenerateData()
{
ThreadIdType i;
SizeValueType count;
Expand Down Expand Up @@ -300,11 +263,10 @@ StatisticsImageFilter< TInputImage >

template< typename TInputImage >
void
StatisticsImageFilter< TInputImage >
::ThreadedGenerateData(const RegionType & outputRegionForThread,
ThreadIdType threadId)
StreamingStatisticsImageFilter< TInputImage >
::ThreadedStreamedGenerateData(const RegionType & inputRegion, ThreadIdType threadId)
{
const SizeValueType size0 = outputRegionForThread.GetSize(0);
const SizeValueType size0 =inputRegion.GetSize(0);
if( size0 == 0)
{
return;
Expand All @@ -318,10 +280,10 @@ StatisticsImageFilter< TInputImage >
PixelType min = NumericTraits< PixelType >::max();
PixelType max = NumericTraits< PixelType >::NonpositiveMin();

ImageScanlineConstIterator< TInputImage > it (this->GetInput(), outputRegionForThread);
ImageScanlineConstIterator< TInputImage > it (this->GetInput(), inputRegion);

// support progress methods/callbacks
const size_t numberOfLinesToProcess = outputRegionForThread.GetNumberOfPixels() / size0;
const size_t numberOfLinesToProcess =inputRegion.GetNumberOfPixels() / size0;
ProgressReporter progress( this, threadId, numberOfLinesToProcess );

// do the work
Expand Down Expand Up @@ -358,7 +320,7 @@ StatisticsImageFilter< TInputImage >

template< typename TImage >
void
StatisticsImageFilter< TImage >
StreamingStatisticsImageFilter< TImage >
::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
Expand Down

0 comments on commit dae948b

Please sign in to comment.