otbLmvmPanSharpeningFusionImageFilter.h 9.57 KB
Newer Older
Angelos Tzotsos's avatar
Angelos Tzotsos committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.

  Some parts of this code are derived from ITK. See ITKCopyright.txt
  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 __otbLmvmPanSharpeningFusionImageFilter_h
#define __otbLmvmPanSharpeningFusionImageFilter_h

#include "otbConvolutionImageFilter.h"
#include "itkTernaryFunctorImageFilter.h"
#include "otbPerBandVectorImageFilter.h"
#include "itkNoiseImageFilter.h"
#include "otbVectorImage.h"

#include "itkProgressAccumulator.h"


namespace otb
{
/**
 * \class LmvmPanSharpeningFusionImageFilter
 * \brief This class performs a Local Mean and Variance Matching (LMVM) Pan sharpening operation
 *
 * Given a Pan image and the corresponding Xs image (oversampled to have the
OTB Bot's avatar
STYLE  
OTB Bot committed
40
 * same number of pixels), this filter realizes an Local Mean and Variance
Angelos Tzotsos's avatar
Angelos Tzotsos committed
41 42
 * Matching Pan sharpening operation as described in:
 *
OTB Bot's avatar
STYLE  
OTB Bot committed
43
 * de Bethune, S. and Muller, F. and Donnay, J.P. 1998. Fusion of multispectral and panchromatic images
Angelos Tzotsos's avatar
Angelos Tzotsos committed
44 45 46 47 48 49 50
 * by local mean and variance matching filtering techniques. Fusion of Earth Data, 28-30 August 1998.
 *
 * \ingroup Streamed
 * \ingroup Multithreaded
 * \ingroup Fusion
 *
 *
51 52
 *
 * \ingroup OTBPanSharpening
Angelos Tzotsos's avatar
Angelos Tzotsos committed
53 54 55 56 57 58 59 60
 **/

template <class TPanImageType, class TXsImageType, class TOutputImageType, class TInternalPrecision = float>
class ITK_EXPORT LmvmPanSharpeningFusionImageFilter :
  public itk::ImageToImageFilter<TXsImageType, TOutputImageType>
{
public:
  /** Standard class typedefs */
OTB Bot's avatar
STYLE  
OTB Bot committed
61
  typedef LmvmPanSharpeningFusionImageFilter                       Self;
Angelos Tzotsos's avatar
Angelos Tzotsos committed
62 63 64 65 66 67 68
  typedef itk::ImageToImageFilter<TXsImageType, TOutputImageType> Superclass;
  typedef itk::SmartPointer<Self>                                 Pointer;
  typedef itk::SmartPointer<const Self>                           ConstPointer;

  /** Internal image type used as Pan smoothing and local standard deviation filter output */
  typedef otb::Image<TInternalPrecision,
    TPanImageType::ImageDimension>                InternalImageType;
69

Angelos Tzotsos's avatar
Angelos Tzotsos committed
70
  /** Single band Xs image type */
71
  typedef typename TXsImageType::InternalPixelType  XsPixelType;
72
  typedef otb::Image<XsPixelType,
Angelos Tzotsos's avatar
Angelos Tzotsos committed
73
    TXsImageType::ImageDimension>                XsBandImageType;
74

Angelos Tzotsos's avatar
Angelos Tzotsos committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  /** Internal image type used as Xs smoothing and local standard deviation filter output */
  typedef otb::VectorImage<TInternalPrecision,
    TXsImageType::ImageDimension>                InternalVectorImageType;

  /** Typedef for the radius of the smoothing and local standard deviation filter */
  typedef typename itk::Array<TInternalPrecision> ArrayType;

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

  /** Run-time type information */
  itkTypeMacro(LmvmPanSharpeningFusionImageFilter,
               itk::ImageToImageFilter);

  /** Define the radius type for the smoothing and local standard deviation operation */
  typedef typename InternalImageType::SizeType RadiusType;

  /** Set the smoothing and local standard deviation filter radius  */
  itkGetMacro(Radius, RadiusType);
  itkSetMacro(Radius, RadiusType);

  /** Set the kernel used for the smoothing and local standard deviation filter */
  itkSetMacro(Filter, ArrayType);
  itkGetConstReferenceMacro(Filter, ArrayType);

  virtual void SetPanInput(const TPanImageType * image);
  const TPanImageType * GetPanInput(void) const;

  virtual void SetXsInput(const TXsImageType * path);
  const TXsImageType * GetXsInput(void) const;

protected:
  /** Constructor */
  LmvmPanSharpeningFusionImageFilter();

  /** Destructor */
  virtual ~LmvmPanSharpeningFusionImageFilter() {};

  /** Call to generate data, wiring composite internal minipipeline */
  void GenerateData();

  /** PrintSelf method */
  void PrintSelf(std::ostream& os, itk::Indent indent) const;

private:
  LmvmPanSharpeningFusionImageFilter(Self &);   // intentionally not implemented
OTB Bot's avatar
STYLE  
OTB Bot committed
121
  void operator =(const Self&);                 // intentionally not implemented
Angelos Tzotsos's avatar
Angelos Tzotsos committed
122

123
  /** \class FusionFunctor1
Angelos Tzotsos's avatar
Angelos Tzotsos committed
124 125
   * This functor applies the LMVM
   * operation. It is intended for internal use only.
126 127
 *
 * \ingroup OTBPanSharpening
Angelos Tzotsos's avatar
Angelos Tzotsos committed
128
   */
129
  class FusionFunctor1
Angelos Tzotsos's avatar
Angelos Tzotsos committed
130 131 132
  {
  public:
    // Implement the fusion as a six arguments operator
133 134 135 136
    typename TOutputImageType::PixelType operator()(
      const typename InternalVectorImageType::PixelType& stdXsPixel,
      const TInternalPrecision& smoothPanchroPixel,
      const typename TPanImageType::PixelType& sharpPanchroPixel) const
Angelos Tzotsos's avatar
Angelos Tzotsos committed
137 138
    {
      // Build output pixel
139 140 141 142 143 144
      typename TOutputImageType::PixelType output(stdXsPixel.Size());

      // Perform fusion for each band with appropriate casting
      for(unsigned int i = 0; i < stdXsPixel.Size(); ++i)
        {
        output[i] = static_cast<typename TOutputImageType::InternalPixelType>(
145
          ((sharpPanchroPixel - smoothPanchroPixel) * stdXsPixel[i]));
146 147 148 149 150
        }
      // Returns the output pixel
      return output;
    }
  };
151

152 153 154 155 156 157
  class FusionFunctor2
  {
  public:
    // Implement the fusion as a six arguments operator
    typename TOutputImageType::PixelType operator()(const typename TOutputImageType::PixelType& functor1Pixel,
                                                    const typename InternalVectorImageType::PixelType& smoothXsPixel,
OTB Bot's avatar
STYLE  
OTB Bot committed
158
                                              const TInternalPrecision& stdPanchroPixel) const
159 160 161
    {
      // Build output pixel
      typename TOutputImageType::PixelType output(smoothXsPixel.Size());
Angelos Tzotsos's avatar
Angelos Tzotsos committed
162 163 164 165 166 167 168 169 170

      TInternalPrecision scale = 1.;

      if(vcl_abs(stdPanchroPixel) > 1e-10)
      {
        scale = 1.0/stdPanchroPixel;
      }

      // Perform fusion for each band with appropriate casting
171
      for(unsigned int i = 0; i < smoothXsPixel.Size(); ++i)
Angelos Tzotsos's avatar
Angelos Tzotsos committed
172 173
        {
        output[i] = static_cast<typename TOutputImageType::InternalPixelType>(
174
          ((functor1Pixel[i] * scale) + smoothXsPixel[i]));
Angelos Tzotsos's avatar
Angelos Tzotsos committed
175 176 177 178 179 180 181 182 183 184
        }
      // Returns the output pixel
      return output;
    }
  };

  /**
   *  Typedef of the TernaryFunctorImageFilter applying the fusion functor to
   *  p, p_smooth, p_std, xs_smooth, xs_std and xs.
   */
185 186 187
  typedef itk::TernaryFunctorImageFilter<InternalVectorImageType,
                                         InternalImageType,
                                         TPanImageType,
Angelos Tzotsos's avatar
Angelos Tzotsos committed
188
                                         TOutputImageType,
189
                                         FusionFunctor1>     FusionStep1FilterType;
Angelos Tzotsos's avatar
Angelos Tzotsos committed
190 191

  /** Pointer to the fusion filter */
192
  typename FusionStep1FilterType::Pointer      m_FusionStep1Filter;
193 194


195 196
  typedef itk::TernaryFunctorImageFilter<TOutputImageType,
                                         InternalVectorImageType,
OTB Bot's avatar
STYLE  
OTB Bot committed
197 198
                                    InternalImageType,
                                    TOutputImageType,
199 200 201
                                         FusionFunctor2>     FusionStep2FilterType;

  /** Pointer to the fusion filter */
202
  typename FusionStep2FilterType::Pointer      m_FusionStep2Filter;
203

Angelos Tzotsos's avatar
Angelos Tzotsos committed
204 205 206 207 208 209 210 211 212 213

  /** Typedef of the convolution filter performing Pan smoothing */
  typedef otb::ConvolutionImageFilter
      <TPanImageType,
       InternalImageType,
       itk::ZeroFluxNeumannBoundaryCondition<TPanImageType>,
       TInternalPrecision>                                  PanConvolutionFilterType;

  /** Pointer to the internal Pan convolution filter */
  typename PanConvolutionFilterType::Pointer m_PanConvolutionFilter;
214

Angelos Tzotsos's avatar
Angelos Tzotsos committed
215 216 217 218 219 220 221 222 223
  /** Typedef of the convolution filter performing Xs smoothing */
  typedef otb::ConvolutionImageFilter
      <XsBandImageType,
       InternalImageType,
       itk::ZeroFluxNeumannBoundaryCondition<TPanImageType>,
       TInternalPrecision>                                  XsConvolutionFilterType;

  /** Pointer to the internal Xs convolution filter */
  typename XsConvolutionFilterType::Pointer m_XsConvolutionFilter;
224

Angelos Tzotsos's avatar
Angelos Tzotsos committed
225 226
  /** Typedef of the Pan local standard deviation filter*/
  typedef itk::NoiseImageFilter
OTB Bot's avatar
STYLE  
OTB Bot committed
227
      <TPanImageType, InternalImageType>               PanNoiseFilterType;
228

Angelos Tzotsos's avatar
Angelos Tzotsos committed
229 230
  /** Pointer to the Pan local standard deviation filter */
  typename PanNoiseFilterType::Pointer m_PanNoiseFilter;
231

Angelos Tzotsos's avatar
Angelos Tzotsos committed
232 233
  /** Typedef of the Xs local standard deviation filter*/
  typedef itk::NoiseImageFilter
OTB Bot's avatar
STYLE  
OTB Bot committed
234
      <XsBandImageType, InternalImageType>               XsNoiseFilterType;
235

OTB Bot's avatar
STYLE  
OTB Bot committed
236
  /** Pointer to the Xs local standard deviation filter */
Angelos Tzotsos's avatar
Angelos Tzotsos committed
237
  typename XsNoiseFilterType::Pointer m_XsNoiseFilter;
238

Angelos Tzotsos's avatar
Angelos Tzotsos committed
239 240 241
  /** Typedef of a helper filter to apply to a vector image*/
  typedef otb::PerBandVectorImageFilter
      < TXsImageType, InternalVectorImageType, XsConvolutionFilterType> XsVectorConvolutionFilterType;
242

OTB Bot's avatar
STYLE  
OTB Bot committed
243
  /** Pointer to the helper vector image filter */
Angelos Tzotsos's avatar
Angelos Tzotsos committed
244
  typename XsVectorConvolutionFilterType::Pointer m_XsVectorConvolutionFilter;
245

Angelos Tzotsos's avatar
Angelos Tzotsos committed
246 247 248
  /** Typedef of a helper filter to apply to a vector image*/
  typedef otb::PerBandVectorImageFilter
      < TXsImageType, InternalVectorImageType, XsNoiseFilterType> XsVectorNoiseFilterType;
249

OTB Bot's avatar
STYLE  
OTB Bot committed
250
  /** Pointer to the helper vector image filter */
Angelos Tzotsos's avatar
Angelos Tzotsos committed
251
  typename XsVectorNoiseFilterType::Pointer m_XsVectorNoiseFilter;
252

Angelos Tzotsos's avatar
Angelos Tzotsos committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  /** Radius used for the smoothing filter */
  RadiusType m_Radius;

  /** Kernel used for the smoothing filter */
  ArrayType  m_Filter;

  /** The internal progress accumulator */
  typename itk::ProgressAccumulator::Pointer m_ProgressAccumulator;
};

} // end namespace otb

#ifndef OTB_MANUAL_INSTANTIATION
#include "otbLmvmPanSharpeningFusionImageFilter.txx"
#endif

#endif