[Insight-users] Weighted sum of two PNG images

Luis Ibanez luis.ibanez at kitware.com
Sun, 02 May 2004 10:06:47 -0400


Hi Jayant:


Here is how you could write this Functor:

//---------------------------------------------

template< class TInput1, class TInput2, class TOutput >
class WeightedAdd2
{
public:
   typedef typename NumericTraits< TInput1 >::AccumulateType 
AccumulatorType;
   typedef typename NumericTraits< TInput1 >::RealType       RealType;
   WeightedAdd2() {};
   ~WeightedAdd2() {};
   inline TOutput operator()( const TInput1 & A, const TInput2 & B)
   {
     const RealType sum1 = A * m_Alpha;
     const RealType sum2 = B * m_Beta;
     return static_cast<TOutput>( sum1 + sum2 );
   }
   void SetAlpha( RealType alpha ) {
        m_Alpha = alpha;
        m_Beta  = NumericTraits< RealType >::One - m_Alpha;
        }
   RealType GetAlpha() const { return m_Alpha; }
private:
   RealType  m_Alpha;
   RealType  m_Beta;    // auxiliary var to avoid a subtraction at every 
pixel
};

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

Since this is a quite desirable functionality, we just added
such a filter to the Toolkit. You will find it under

   Insight/Code/BasicFitlers/
                  itkWeightedAddImageFilter.h


You will have to update your CVS checkout in order to get
this new filter.



Please let us know if you find any problems.


   Thanks



      Luis



-----------------------
Jayant Chauhan wrote:

> Hey fellas,
> A lil confused. To make my own Filter to find the weighted mean of two 
> images (PNG), what changes do I need to make in the AddImageFilter.h ?!!
> And do I just have to include the header file that I create in my directory.
>  
> I am not sure as to how to use the AccumulorType, as in, what all 
> operations are defined for it. So could you please let me know what 
> changes to be made in the inline operator() part of the code.
>  
> /*=========================================================================
>   Program:   Insight Segmentation & Registration Toolkit
>   Module:    $RCSfile: itkAddImageFilter.h,v $
>   Language:  C++
>   Date:      $Date: 2003/09/10 14:28:43 $
>   Version:   $Revision: 1.7 $
>   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 __itkWeightedAddImageFilter_h
> #define __itkWeigthedAddImageFilter_h
> #include "itkBinaryFunctorImageFilter.h"
> #include "itkNumericTraits.h"
> 
> namespace itk
> {
>  
> namespace Functor { 
>  
> template< class TInput1, class TInput2, class TOutput, float >
> class WeightedAdd2
> {
> public:
>   typedef typename NumericTraits< TInput1 >::AccumulateType AccumulatorType;
>   WeightedAdd2() {};
>   ~WeightedAdd2() {};
>   inline TOutput operator()( const TInput1 & A, const TInput2 & B, float 
> & gamma)
>   {
> /***********WHAT to be done here???1!!!!**************************/
>     const AccumulatorType sum = A;
>     return static_cast<TOutput>( gamma*sum +(1-gamma)* B ); // How to 
> achieve this ???
>   }
> };
> }
> template <class TInputImage1, class TInputImage2, class TOutputImage, float>
> class ITK_EXPORT WeightedAddImageFilter :
>     public
> BinaryFunctorImageFilter<TInputImage1,TInputImage2,TOutputImage,
>                          Functor::WeightedAdd2<
>   typename TInputImage1::PixelType,
>   typename TInputImage2::PixelType,
>   typename TOutputImage::PixelType,
>   float>   >
> 
> {
> public:
>   /** Standard class typedefs. */
>   typedef WeightedAddImageFilter  Self;
>   typedef BinaryFunctorImageFilter<TInputImage1,TInputImage2,TOutputImage,
>                                    Functor::WeightedAdd2<
>     typename TInputImage1::PixelType,
>     typename TInputImage2::PixelType,
>     typename TOutputImage::PixelType, float>  
>   >  Superclass;
>   typedef SmartPointer<Self>   Pointer;
>   typedef SmartPointer<const Self>  ConstPointer;
>   /** Method for creation through the object factory. */
>   itkNewMacro(Self);
>  
> protected:
>   WeightedAddImageFilter() {}
>   virtual ~WeightedAddImageFilter() {}
> private:
>   WeightedAddImageFilter(const Self&); //purposely not implemented
>   void operator=(const Self&); //purposely not implemented
> };
> } // end namespace itk
> 
> #endif
> 
> ------------------------------------------------------------------------
> Deals can't get any better. Products at Lowest Prices. Only on 
> baazee.com <http://g.msn.com/8HMBENIN/2749??PS=> 
> _______________________________________________ Insight-users mailing 
> list Insight-users at itk.org 
> http://www.itk.org/mailman/listinfo/insight-users