[Insight-users] ITK example that sums multiple metaheader images

Dan Mueller dan.muel at gmail.com
Wed Oct 22 10:21:09 EDT 2008


Hi Stéphane,

I have quickly put together the file (itkNaryMeanImageFilter.h) which
should do what you want. (Be warned, I have not compiled or tested it,
so it may have some minor problems).

Hope this helps.

Regards, Dan

/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkNaryMeanImageFilter.h,v $
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef __itkNaryMeanImageFilter_h
#define __itkNaryMeanImageFilter_h

#include "itkNaryFunctorImageFilter.h"
#include "itkNumericTraits.h"

namespace itk
{

/** \class NaryMeanImageFilter
 * \brief Implements an operator for pixel-wise mean of N images.
 *
 * This class is parametrized over the types of the two
 * input images and the type of the output image.
 * Numeric conversions (castings) are done by the C++ defaults.
 *
 * The pixel type of the input 1 image must have a valid defintion of
 * the operator+ with a pixel type of the image 2. This condition is
 * required because internally this filter will perform the operation
 *
 *        sum of input pixels / number of inputs
 *
 * Additionally the type resulting from the mean, will be cast to
 * the pixel type of the output image.
 *
 * The total operation over one pixel will be
 *
 *  output_pixel = static_cast<OutputPixelType>( sum of input pixels /
num inputs )
 *
 * \warning No numeric overflow checking is performed in this filter.
 *
 * \ingroup IntensityImageFilters  Multithreaded
 */

namespace Functor {

template< class TInput, class TOutput >
class Mean
{
public:
  typedef typename NumericTraits< TInput >::AccumulateType AccumulatorType;
  Mean() {}
  ~Mean() {}
  inline TOutput operator()( const std::vector< TInput > & input )
  {
    AccumulatorType mean = NumericTraits< TOutput >::Zero;
    for( unsigned int i=0; i< input.size(); i++ )
      {
      mean += static_cast< TOutput >(input[i]);
      }
    return static_cast<TOutput>( sum / input.size() );
  }
  bool operator== (const Mean&) const
  {
    return true;
  }
  bool operator!= (const Mean&) const
  {
    return false;
  }
};
}
template <class TInputImage, class TOutputImage>
class ITK_EXPORT NaryMeanImageFilter :
    public
NaryFunctorImageFilter<TInputImage,TOutputImage,
                       Functor::Mean<typename TInputImage::PixelType,
typename TInputImage::PixelType > >
{
public:
  /** Standard class typedefs. */
  typedef NaryMeanImageFilter  Self;
  typedef NaryFunctorImageFilter<TInputImage,TOutputImage,
                                 Functor::Mean<typename TInputImage::PixelType,
                                 typename TInputImage::PixelType  > >
Superclass;
  typedef SmartPointer<Self>   Pointer;
  typedef SmartPointer<const Self>  ConstPointer;

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

  /** Runtime information support. */
  itkTypeMacro(NaryMeanImageFilter,
               NaryFunctorImageFilter);

#ifdef ITK_USE_CONCEPT_CHECKING
  /** Begin concept checking */
  itkConceptMacro(InputConvertibleToOutputCheck,
    (Concept::Convertible<typename TInputImage::PixelType,
                          typename TOutputImage::PixelType>));
  itkConceptMacro(InputHasZeroCheck,
    (Concept::HasZero<typename TInputImage::PixelType>));
  /** End concept checking */
#endif

protected:
  NaryMeanImageFilter() {}
  virtual ~NaryMeanImageFilter() {}

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

};

} // end namespace itk


#endif


2008/10/22 Stéphane CALANDE <scalande at gmail.com>:
> Oh yes, I replied to quick... I see that there are some code in the ".h".
>
> I'm sorry, in my logical the code is in the .cxx...
>
> Sorry ;-)
>
> Thank you !!
>
>
>
> 2008/10/22 Stéphane CALANDE <scalande at gmail.com>
>>
>> Thank you for the explanation of "Nary" ;-)
>>
>> I'm not an expert... but to change something in the code I need
>> "itkNaryAddImageFilter.cxx" and not "itkNaryAddImageFilter.h" ?
>>
>> Do I talk nonsense?
>>
>>
>>
>> Thank you,
>>
>> Stéphane
>>
>>
>>
>>
>> 2008/10/22 Dan Mueller <dan.muel at gmail.com>
>>>
>>> Hi Stéphane,
>>>
>>> > - Why do you tell me about "NaryAddImageFilter" ? I was using
>>> > AddImageFilter.
>>> I mentioned NaryAddImageFilter in my first response to your question
>>> (which I see now I forgot to send to the mailing list).
>>>
>>> > What's the difference ?
>>> AddImageFilter takes 2 inputs, NaryAddImageFilter takes N inputs (ie.
>>> unary, binary, "n"-ary).
>>>
>>> > - I have no "NaryAddImageFilter.cxx" or "AddImageFilter.cxx" in my ITK
>>> > repertory. I didn't find it on the Internet either. Where can I find it
>>> > if I
>>> > want to transform "Add" to "Mean" ?
>>> You will find it in Code\BasicFilters\itkNaryAddImageFilter.h.
>>>
>>> Hope this helps.
>>>
>>> Regards, Dan


More information about the Insight-users mailing list