OTB  7.1.0
Orfeo Toolbox
otbImageAndVectorImageOperationFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 
23 #ifndef otbImageAndVectorImageOperationFilter_h
24 #define otbImageAndVectorImageOperationFilter_h
25 
26 #include "itkBinaryFunctorImageFilter.h"
27 #include "itkImageToImageFilter.h"
28 
29 #include "otbMacro.h"
30 
31 namespace otb
32 {
33 namespace Functor
34 {
35 template <typename TInput, typename TVectorInput, typename TOutput>
37 {
38 public:
39  typedef typename TVectorInput::ValueType InternalInputPixelType;
40  typedef typename TOutput::ValueType InternalOutputPixelType;
41  typedef enum { MULTIPLICATION, ADDITION, DIVISION, SUBTRACTION } OperatorType;
42 
44  {
45  m_Operator = ADDITION; // 1;
46  };
48  {
49  }
50 
51  void SetOperator(OperatorType oper)
52  {
53  m_Operator = oper;
54  }
55  OperatorType GetOperator()
56  {
57  return m_Operator;
58  }
59 
60  inline TOutput operator()(const TInput& inPix, const TVectorInput& vInPix)
61  {
62  TOutput out;
63  out.SetSize(vInPix.Size());
64  TVectorInput vInTmp = vInPix;
65 
66  switch (m_Operator)
67  {
68  case MULTIPLICATION:
69  {
70  vInTmp *= static_cast<InternalInputPixelType>(inPix);
71  break;
72  }
73  case ADDITION:
74  {
75  vInTmp += static_cast<InternalInputPixelType>(inPix);
76  break;
77  }
78  case DIVISION:
79  {
80  if (inPix != 0)
81  vInTmp /= static_cast<InternalInputPixelType>(inPix);
82  else
83  {
84  vInTmp.Fill(0);
85  }
86  break;
87  }
88  case SUBTRACTION:
89  {
90  vInTmp -= static_cast<InternalInputPixelType>(inPix);
91  break;
92  }
93  default:
94  {
95  }
96  }
97 
98  for (unsigned int i = 0; i < vInTmp.Size(); ++i)
99  {
100  out[i] = static_cast<InternalInputPixelType>(vInTmp[i]);
101  }
102  return out;
103  }
104 
105 protected:
106  OperatorType m_Operator;
107 };
108 }
109 
126 template <class TInputImage, class TVectorInputImage, class TOutputImage>
128  : public itk::BinaryFunctorImageFilter<TInputImage, TVectorInputImage, TOutputImage,
129  Functor::ImageAndVectorImageOperationFunctor<typename TInputImage::PixelType, typename TVectorInputImage::PixelType,
130  typename TOutputImage::PixelType>>
131 // ImageToImageFilter< TVectorInputImage, TOutputImage >
132 {
133 public:
136  // typedef itk::ImageToImageFilter<TVectorInputImage, TOutputImage> Superclass;
139  typedef itk::BinaryFunctorImageFilter<TInputImage, TVectorInputImage, TOutputImage, FunctorType> Superclass;
140  typedef itk::SmartPointer<Self> Pointer;
141  typedef itk::SmartPointer<const Self> ConstPointer;
142 
144  itkNewMacro(Self);
145 
147  itkTypeMacro(ImageAndVectorImageOperationFilter, itk::BinaryFunctorImageFilter);
148 
150  typedef TInputImage InputImageType;
151  typedef typename InputImageType::PixelType InputPixelType;
152  typedef TVectorInputImage VectorInputImageType;
153  typedef typename VectorInputImageType::PixelType VectorInputPixelType;
154  typedef TOutputImage OutputImageType;
155  typedef typename OutputImageType::PixelType OutputPixelType;
156 
159 
161  using Superclass::SetInput;
162  void SetInput(const InputImageType* input) override;
163  void SetVectorInput(const VectorInputImageType* input);
165 
167  const InputImageType* GetInput();
168  const VectorInputImageType* GetVectorInput();
170 
172  itkGetMacro(UseAddition, bool);
173  itkGetMacro(UseMultiplication, bool);
174  itkGetMacro(UseDivision, bool);
175  itkGetMacro(UseSubtraction, bool);
177 
178  void UseAddition()
179  {
180  m_UseAddition = true;
181  m_UseMultiplication = false;
182  m_UseDivision = false;
183  m_UseSubtraction = false;
184  this->GetFunctor().SetOperator(static_cast<OperatorType>(1));
185  this->Modified();
186  }
188  {
189  m_UseAddition = false;
190  m_UseMultiplication = true;
191  m_UseDivision = false;
192  m_UseSubtraction = false;
193  this->GetFunctor().SetOperator(static_cast<OperatorType>(0));
194  this->Modified();
195  }
196  void UseDivision()
197  {
198  m_UseAddition = false;
199  m_UseMultiplication = false;
200  m_UseDivision = true;
201  m_UseSubtraction = false;
202  this->GetFunctor().SetOperator(static_cast<OperatorType>(2));
203  this->Modified();
204  }
206  {
207  m_UseAddition = false;
208  m_UseMultiplication = false;
209  m_UseDivision = false;
210  m_UseSubtraction = true;
211  this->GetFunctor().SetOperator(static_cast<OperatorType>(3));
212  this->Modified();
213  }
214 
215 protected:
218 
222  void GenerateOutputInformation() override;
223 
224 private:
226  void operator=(const ImageAndVectorImageOperationFilter&) = delete;
227 
232 };
233 
234 } // end namespace otb
235 
236 #ifndef OTB_MANUAL_INSTANTIATION
238 #endif
239 
240 #endif
Provides simple pixel to pixel operation between Image and VectorImage.
TOutput operator()(const TInput &inPix, const TVectorInput &vInPix)
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Functor::ImageAndVectorImageOperationFunctor< typename TInputImage::PixelType, typename TVectorInputImage::PixelType, typename TOutputImage::PixelType > FunctorType
itk::BinaryFunctorImageFilter< TInputImage, TVectorInputImage, TOutputImage, FunctorType > Superclass