otbImageAndVectorImageOperationFilter.h 7.09 KB
Newer Older
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
/*=========================================================================

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

#include "itkBinaryFunctorImageFilter.h"
#include "itkImageToImageFilter.h"

namespace otb
{
namespace Functor
{
OTB Bot's avatar
STYLE  
OTB Bot committed
32
template <typename TInput, typename TVectorInput, typename TOutput>
33 34 35
class ITK_EXPORT ImageAndVectorImageOperationFunctor
{
public:
OTB Bot's avatar
STYLE  
OTB Bot committed
36 37 38 39
  typedef typename TVectorInput::ValueType                        InternalInputPixelType;
  typedef typename TOutput::ValueType                             InternalOutputPixelType;
  typedef enum {MULTIPLICATION, ADDITION, DIVISON, SUBSTRACTION } OperatorType;

40 41
  ImageAndVectorImageOperationFunctor()
    {
OTB Bot's avatar
STYLE  
OTB Bot committed
42
    m_Operator = ADDITION;  //1;
43
    };
OTB Bot's avatar
STYLE  
OTB Bot committed
44
  virtual ~ImageAndVectorImageOperationFunctor(){}
45 46

  void SetOperator(OperatorType oper)
OTB Bot's avatar
STYLE  
OTB Bot committed
47 48 49
  {
    m_Operator = oper;
  }
50
  OperatorType GetOperator()
OTB Bot's avatar
STYLE  
OTB Bot committed
51 52 53
  {
    return m_Operator;
  }
54

OTB Bot's avatar
STYLE  
OTB Bot committed
55
  inline TOutput operator ()(const TInput& inPix, const TVectorInput& vInPix)
56 57
  {
    TOutput out;
OTB Bot's avatar
STYLE  
OTB Bot committed
58
    out.SetSize(vInPix.Size());
59 60
    TVectorInput vInTmp = vInPix;

OTB Bot's avatar
STYLE  
OTB Bot committed
61
    switch (m_Operator)
62
      {
OTB Bot's avatar
STYLE  
OTB Bot committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      case MULTIPLICATION:
        {
        vInTmp *= static_cast<InternalInputPixelType>(inPix);
        break;
        }
      case ADDITION:
        {
        vInTmp += static_cast<InternalInputPixelType>(inPix);
        break;
        }
      case DIVISON:
        {
        if (inPix != 0) vInTmp /= static_cast<InternalInputPixelType>(inPix);
        else
          {
          vInTmp.Fill(0);
          }
        break;
        }
      case SUBSTRACTION:
        {
        vInTmp -= static_cast<InternalInputPixelType>(inPix);
        break;
        }
      default:
OTB Bot's avatar
STYLE  
OTB Bot committed
88 89
        {
        }
90 91
      }

92
    for (unsigned int i = 0; i < vInTmp.Size(); ++i)
93
      {
OTB Bot's avatar
STYLE  
OTB Bot committed
94
      out[i] = static_cast<InternalInputPixelType>(vInTmp[i]);
95 96 97 98
      }
    return out;
  }

OTB Bot's avatar
STYLE  
OTB Bot committed
99
protected:
100 101 102 103 104 105 106
  OperatorType m_Operator;
};
}

/** \class ImageAndVectorImageOperationFilter
 * \brief Provides simple pixel to pixel operation between Image and VectorImage.
 *
Emmanuel Christophe's avatar
Emmanuel Christophe committed
107
 * Apply an operation (multiplication, division, addition or substraction) between
108 109 110 111 112 113 114 115 116 117 118
 * the input image and each channel of the vector input image.
 * Use SetOperation( MULTIPLICATION, ADDITION, DIVISON or SUBSTRACTION ) to select the wanted operation.
 * Default is an addition.
 *
 * This class is templated over the input Image and VectorImage and output VectorImage types.
 *
 * \sa itkMultiplyImageFilter
 * \ingroup itkBinaryFunctorImageFilter
 */

template <class TInputImage, class TVectorInputImage, class TOutputImage>
OTB Bot's avatar
STYLE  
OTB Bot committed
119 120
class ITK_EXPORT ImageAndVectorImageOperationFilter :
  public itk::BinaryFunctorImageFilter<TInputImage,
OTB Bot's avatar
STYLE  
OTB Bot committed
121 122
      TVectorInputImage,
      TOutputImage,
123 124
      Functor::ImageAndVectorImageOperationFunctor<typename TInputImage::PixelType,
          typename TVectorInputImage::
OTB Bot's avatar
STYLE  
OTB Bot committed
125
          PixelType,
126
          typename TOutputImage::
OTB Bot's avatar
STYLE  
OTB Bot committed
127
          PixelType> >
128 129 130 131 132
//ImageToImageFilter< TVectorInputImage, TOutputImage >
{
public:

  /** Standard class typedefs. */
OTB Bot's avatar
STYLE  
OTB Bot committed
133
  typedef ImageAndVectorImageOperationFilter Self;
134
  //typedef itk::ImageToImageFilter<TVectorInputImage, TOutputImage> Superclass;
135 136 137
  typedef Functor::ImageAndVectorImageOperationFunctor<typename TInputImage::PixelType,
      typename TVectorInputImage::PixelType,
      typename TOutputImage::PixelType> FunctorType;
138
  typedef itk::BinaryFunctorImageFilter<TInputImage, TVectorInputImage, TOutputImage, FunctorType> Superclass;
OTB Bot's avatar
STYLE  
OTB Bot committed
139 140
  typedef itk::SmartPointer<Self>                                                                  Pointer;
  typedef itk::SmartPointer<const Self>                                                            ConstPointer;
141 142 143 144 145

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

  /** Run-time type information (and related methods). */
OTB Bot's avatar
STYLE  
OTB Bot committed
146
  itkTypeMacro(ImageAndVectorImageOperationFilter, itk::BinaryFunctorImageFilter);
147 148 149 150

  /** Typedef for the images.   */
  typedef TInputImage                              InputImageType;
  typedef typename InputImageType::PixelType       InputPixelType;
Emmanuel Christophe's avatar
Emmanuel Christophe committed
151
  typedef TVectorInputImage                        VectorInputImageType;
152 153 154 155 156
  typedef typename VectorInputImageType::PixelType VectorInputPixelType;
  typedef TOutputImage                             OutputImageType;
  typedef typename OutputImageType::PixelType      OutputPixelType;

  /** Operation type typedef. */
OTB Bot's avatar
STYLE  
OTB Bot committed
157
  typedef typename FunctorType::OperatorType OperatorType;
158 159

  /** Set the input images of this process object.  */
OTB Bot's avatar
STYLE  
OTB Bot committed
160 161
  void SetInput(const InputImageType *input);
  void SetVectorInput(const VectorInputImageType *input);
162 163 164 165 166 167 168 169 170 171 172 173

  /** Get the input images of this process object.  */
  const InputImageType * GetInput();
  const VectorInputImageType * GetVectorInput();

  /** Accessors */
  itkGetMacro(UseAddition, bool);
  itkGetMacro(UseMultiplication, bool);
  itkGetMacro(UseDivision, bool);
  itkGetMacro(UseSubstraction, bool);

  void UseAddition()
OTB Bot's avatar
STYLE  
OTB Bot committed
174 175 176 177 178 179 180 181
  {
    m_UseAddition = true;
    m_UseMultiplication = false;
    m_UseDivision = false;
    m_UseSubstraction = false;
    this->GetFunctor().SetOperator(static_cast<OperatorType>(1));
    this->Modified();
  }
182
  void UseMultiplication()
OTB Bot's avatar
STYLE  
OTB Bot committed
183 184 185 186 187 188 189 190
  {
    m_UseAddition = false;
    m_UseMultiplication = true;
    m_UseDivision = false;
    m_UseSubstraction = false;
    this->GetFunctor().SetOperator(static_cast<OperatorType>(0));
    this->Modified();
  }
191
  void UseDivision()
OTB Bot's avatar
STYLE  
OTB Bot committed
192 193 194 195 196 197 198 199
  {
    m_UseAddition = false;
    m_UseMultiplication = false;
    m_UseDivision = true;
    m_UseSubstraction = false;
    this->GetFunctor().SetOperator(static_cast<OperatorType>(2));
    this->Modified();
  }
200
  void UseSubstraction()
OTB Bot's avatar
STYLE  
OTB Bot committed
201 202 203 204 205 206 207 208
  {
    m_UseAddition = false;
    m_UseMultiplication = false;
    m_UseDivision = false;
    m_UseSubstraction = true;
    this->GetFunctor().SetOperator(static_cast<OperatorType>(3));
    this->Modified();
  }
209 210 211

protected:
  ImageAndVectorImageOperationFilter();
212
  virtual ~ImageAndVectorImageOperationFilter();
213 214 215 216 217 218 219 220

  /** This is a source, so it must set the spacing, size, and largest possible
   * region for the output image that it will produce.
   * \sa ProcessObject::GenerateOutputInformation() */
  virtual void GenerateOutputInformation();

private:
  ImageAndVectorImageOperationFilter(const ImageAndVectorImageOperationFilter &); //purposely not implemented
OTB Bot's avatar
STYLE  
OTB Bot committed
221
  void operator =(const ImageAndVectorImageOperationFilter&); //purposely not implemented
222 223 224 225

  bool m_UseAddition;
  bool m_UseMultiplication;
  bool m_UseDivision;
Emmanuel Christophe's avatar
Emmanuel Christophe committed
226
  bool m_UseSubstraction;
227 228 229 230
};

} // end namespace otb

231
#ifndef OTB_MANUAL_INSTANTIATION
232 233 234 235
#include "otbImageAndVectorImageOperationFilter.txx"
#endif

#endif