OTB  10.0.0
Orfeo Toolbox
otbMatrixImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbMatrixImageFilter_hxx
22 #define otbMatrixImageFilter_hxx
23 
24 #include "otbMacro.h" //for
25 #include "otbMatrixImageFilter.h"
26 #include "itkImageRegionIterator.h"
27 #include "itkProgressReporter.h"
28 
29 namespace otb
30 {
31 
35 template <class TInputImage, class TOutputImage, class TMatrix>
37 {
38  this->DynamicMultiThreadingOn();
39 }
40 
41 template <class TInputImage, class TOutputImage, class TMatrix>
43 {
44  // call the superclass' implementation of this method
45  Superclass::GenerateOutputInformation();
46 
47  if (m_MatrixByVector)
48  {
49  if (this->GetInput()->GetNumberOfComponentsPerPixel() != m_Matrix.cols())
50  {
51  itkExceptionMacro("Invalid Matrix size. Number of columns must be the same as the image number of channels.");
52  }
53 
54  if (m_Matrix.rows() == 0)
55  {
56  itkExceptionMacro("Invalid Matrix size. Number of rows can't be null.");
57  }
58  this->GetOutput()->SetNumberOfComponentsPerPixel(m_Matrix.rows());
59  }
60 
61  if (!m_MatrixByVector)
62  {
63  if (this->GetInput()->GetNumberOfComponentsPerPixel() != m_Matrix.rows())
64  {
65  itkExceptionMacro("Invalid Matrix size. Number of rows must be the same as the image number of channels.");
66  }
67 
68  if (m_Matrix.cols() == 0)
69  {
70  itkExceptionMacro("Invalid Matrix size. Number of columns can't be null.");
71  }
72  this->GetOutput()->SetNumberOfComponentsPerPixel(m_Matrix.cols());
73  }
74 }
75 
76 template <class TInputImage, class TOutputImage, class TMatrix>
78 {
79  // images pointer
80  typename OutputImageType::Pointer outputPtr = this->GetOutput();
81  typename InputImageType::ConstPointer inputPtr = this->GetInput();
82 
83  typename itk::ImageRegionConstIterator<InputImageType> inIt(inputPtr, outputRegionForThread);
84  itk::ImageRegionIterator<OutputImageType> outIt(outputPtr, outputRegionForThread);
85 
86  inIt.GoToBegin();
87  outIt.GoToBegin();
88 
89  const unsigned int inSize = m_MatrixByVector ? m_Matrix.cols() : m_Matrix.rows();
90  const unsigned int outSize = m_MatrixByVector ? m_Matrix.rows() : m_Matrix.cols();
91 
92  VectorType inVect(inSize, InputRealType(0.));
93  VectorType outVect(outSize, InputRealType(0.));
94 
95  while (!outIt.IsAtEnd())
96  {
97  const InputPixelType& inPix = inIt.Get();
98  OutputPixelType outPix;
99  outPix.SetSize(outSize);
100 
101  for (unsigned int i = 0; i < inSize; ++i)
102  {
103  inVect[i] = static_cast<InputRealType>(inPix[i]);
104  }
105 
106  outVect = m_MatrixByVector ? m_Matrix * inVect : inVect * m_Matrix;
107 
108  for (unsigned int i = 0; i < outSize; ++i)
109  {
110  outPix[i] = static_cast<OutputInternalPixelType>(outVect[i]);
111  }
112  outIt.Set(outPix);
113 
114  ++inIt;
115  ++outIt;
116  }
117 }
118 
119 
123 template <class TInputImage, class TOutputImage, class TMatrix>
124 void MatrixImageFilter<TInputImage, TOutputImage, TMatrix>::PrintSelf(std::ostream& os, itk::Indent indent) const
125 {
126  Superclass::PrintSelf(os, indent);
127  os << indent << "Matrix: " << m_Matrix << std::endl;
128  os << indent << "MatrixByVector: " << m_MatrixByVector << std::endl;
129 }
131 
132 } // end namespace otb
133 
134 #endif
OutputImageType::PixelType OutputPixelType
vnl_vector< InputRealType > VectorType
InputImageType::PixelType InputPixelType
void GenerateOutputInformation() override
OutputImageType::InternalPixelType OutputInternalPixelType
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) override
void PrintSelf(std::ostream &os, itk::Indent indent) const override
OutputImageType::RegionType OutputImageRegionType
itk::NumericTraits< InputInternalPixelType >::RealType InputRealType
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.