[Insight-users] Absolute Value Image

Luis Ibanez luis . ibanez at kitware . com
Tue, 04 Nov 2003 10:33:25 -0500


This is a multi-part message in MIME format.
--------------050401040803090500020104
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Hi Glen,

Bad  news:  There is not an absolute value filter in ITK

Good news:  You can write one in ten minutes.
             (7 minutes if you have good coffee at hand)

Even better news: One is attached.


--------------------------------------------------------

Simply take any of the filters deriving from the UnaryFunctorFilter
and replace the Functor with the "Abs()" function.
Take for example the "Sin" image filter

     Insight/Code/BasicFilters/itkSinImageFilter.h


This will look like:


namespace Function {

template< class TInput, class TOutput>
class Abs
{
public:
   Abs() {}
   ~Abs() {}
   inline TOutput operator()( const TInput & A )
   { return (TOutput)( ( A > 0 ) ? A : -A ); }
};
}

Then use this functor for instantiating the filter.
This is shown in the attached file. Note that there
is only a header file, and that's all what is needed.
Here is where Generic Programming pays off.   :-)


---------------------------------------------------------------------


Given that Abs is such a simple operation you may want
to use ImageAdaptors instead of ImageFilters.

You will find details on who to write functional
ImageAdaptors in the SoftwareGuide

    http://www . itk . org/ItkSoftwareGuide . pdf

Please look at the ImageAdaptors chapter,
Chapter 12, pdf-page 522, paper-page 548.

Functional adaptors are described in section 12.4
The example of this section is available in

   Insight/Examples/DataRepresentation/Image/ImageAdaptor4.cxx


Please let us know if you run into any problem.



    Thanks



      Luis




----------------------
Glen Lehmann wrote:
> Hello,
> 
> Here's an easy one;  Is there an ITK class to find that absolute value 
> of an image?
> 
> i.e. in vtk I'd use vtkImageMathematics->SetOperationToAbsoluteValue.
> 
> I didn't have any luck in the archives.
> 
> Thanks,
> Glen
> 
\


--------------050401040803090500020104
Content-Type: text/plain;
 name="itkAbsImageFilter.h"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="itkAbsImageFilter.h"

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

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkAbsImageFilter.h,v $
  Language:  C++
  Date:      $Date: 2003/09/10 14:28:56 $
  Version:   $Revision: 1.16 $

  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 __itkAbsImageFilter_h
#define __itkAbsImageFilter_h

#include "itkUnaryFunctorImageFilter.h"
#include "vnl/vnl_math.h"

namespace itk
{
  
/** \class AbsImageFilter
 * \brief Computes the ABS(x) pixel-wise
 * 
 * \ingroup IntensityImageFilters  Multithreaded
 */
namespace Function {  
  
template< class TInput, class TOutput>
class Abs
{
public:
  Abs() {}
  ~Abs() {}
  inline TOutput operator()( const TInput & A )
  { return (TOutput)( ( A > 0 ) ? A : -A ); }
}; 
}

template <class TInputImage, class TOutputImage>
class ITK_EXPORT AbsImageFilter :
    public
UnaryFunctorImageFilter<TInputImage,TOutputImage, 
                        Function::Abs< 
  typename TInputImage::PixelType, 
  typename TOutputImage::PixelType>   >
{
public:
  /** Standard class typedefs. */
  typedef AbsImageFilter  Self;
  typedef UnaryFunctorImageFilter<TInputImage,TOutputImage, 
                                  Function::Abs< typename TInputImage::PixelType, 
                                                 typename TOutputImage::PixelType> >  Superclass;
  typedef SmartPointer<Self>   Pointer;
  typedef SmartPointer<const Self>  ConstPointer;

  /** Method for creation through the object factory. */
  itkNewMacro(Self);
  
protected:
  AbsImageFilter() {}
  virtual ~AbsImageFilter() {}

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

};

} // end namespace itk


#endif

--------------050401040803090500020104--