otbImageAndVectorImageOperationFilter.h 7.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 1999-2011 Insight Software Consortium
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
21 22


23 24
#ifndef otbImageAndVectorImageOperationFilter_h
#define otbImageAndVectorImageOperationFilter_h
25 26 27 28

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

29 30
#include "otbMacro.h"

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

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

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

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

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

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

OTB Bot's avatar
STYLE  
OTB Bot committed
102
protected:
103 104 105 106 107 108 109
  OperatorType m_Operator;
};
}

/** \class ImageAndVectorImageOperationFilter
 * \brief Provides simple pixel to pixel operation between Image and VectorImage.
 *
110
 * Apply an operation (multiplication, division, addition or subtraction) between
111
 * the input image and each channel of the vector input image.
112
 * Use SetOperation( MULTIPLICATION, ADDITION, DIVISION or SUBTRACTION ) to select the wanted operation.
113 114 115 116 117 118
 * Default is an addition.
 *
 * This class is templated over the input Image and VectorImage and output VectorImage types.
 *
 * \sa itkMultiplyImageFilter
 * \ingroup itkBinaryFunctorImageFilter
119 120
 *
 * \ingroup OTBCommon
121 122 123
 */

template <class TInputImage, class TVectorInputImage, class TOutputImage>
OTB Bot's avatar
STYLE  
OTB Bot committed
124 125
class ITK_EXPORT ImageAndVectorImageOperationFilter :
  public itk::BinaryFunctorImageFilter<TInputImage,
OTB Bot's avatar
STYLE  
OTB Bot committed
126 127
      TVectorInputImage,
      TOutputImage,
128 129
      Functor::ImageAndVectorImageOperationFunctor<typename TInputImage::PixelType,
          typename TVectorInputImage::
OTB Bot's avatar
STYLE  
OTB Bot committed
130
          PixelType,
131
          typename TOutputImage::
OTB Bot's avatar
STYLE  
OTB Bot committed
132
          PixelType> >
133 134 135 136 137
//ImageToImageFilter< TVectorInputImage, TOutputImage >
{
public:

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

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

  /** Run-time type information (and related methods). */
OTB Bot's avatar
STYLE  
OTB Bot committed
151
  itkTypeMacro(ImageAndVectorImageOperationFilter, itk::BinaryFunctorImageFilter);
152 153 154 155

  /** Typedef for the images.   */
  typedef TInputImage                              InputImageType;
  typedef typename InputImageType::PixelType       InputPixelType;
Emmanuel Christophe's avatar
Emmanuel Christophe committed
156
  typedef TVectorInputImage                        VectorInputImageType;
157 158 159 160 161
  typedef typename VectorInputImageType::PixelType VectorInputPixelType;
  typedef TOutputImage                             OutputImageType;
  typedef typename OutputImageType::PixelType      OutputPixelType;

  /** Operation type typedef. */
OTB Bot's avatar
STYLE  
OTB Bot committed
162
  typedef typename FunctorType::OperatorType OperatorType;
163 164

  /** Set the input images of this process object.  */
165
  using Superclass::SetInput;
166
  void SetInput(const InputImageType *input) override;
OTB Bot's avatar
STYLE  
OTB Bot committed
167
  void SetVectorInput(const VectorInputImageType *input);
168 169 170 171 172 173 174 175 176

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

  /** Accessors */
  itkGetMacro(UseAddition, bool);
  itkGetMacro(UseMultiplication, bool);
  itkGetMacro(UseDivision, bool);
177 178
  itkGetMacro(UseSubtraction, bool);

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

protected:
  ImageAndVectorImageOperationFilter();
218
  ~ImageAndVectorImageOperationFilter() override;
219 220 221 222

  /** 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() */
223
  void GenerateOutputInformation() override;
224 225

private:
226 227
  ImageAndVectorImageOperationFilter(const ImageAndVectorImageOperationFilter &) = delete;
  void operator =(const ImageAndVectorImageOperationFilter&) = delete;
228 229 230 231

  bool m_UseAddition;
  bool m_UseMultiplication;
  bool m_UseDivision;
232
  bool m_UseSubtraction;
233 234 235 236
};

} // end namespace otb

237
#ifndef OTB_MANUAL_INSTANTIATION
238 239 240 241
#include "otbImageAndVectorImageOperationFilter.txx"
#endif

#endif