OTB  9.0.0
Orfeo Toolbox
otbMatrixImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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 "otbMatrixImageFilter.h"
25 #include "itkImageRegionIterator.h"
26 #include "itkProgressReporter.h"
27 
28 namespace otb
29 {
30 
34 template <class TInputImage, class TOutputImage, class TMatrix>
36 {
37 }
38 
39 template <class TInputImage, class TOutputImage, class TMatrix>
41 {
42  // call the superclass' implementation of this method
43  Superclass::GenerateOutputInformation();
44 
45  if (m_MatrixByVector)
46  {
47  if (this->GetInput()->GetNumberOfComponentsPerPixel() != m_Matrix.cols())
48  {
49  itkExceptionMacro("Invalid Matrix size. Number of columns must be the same as the image number of channels.");
50  }
51 
52  if (m_Matrix.rows() == 0)
53  {
54  itkExceptionMacro("Invalid Matrix size. Number of rows can't be null.");
55  }
56  this->GetOutput()->SetNumberOfComponentsPerPixel(m_Matrix.rows());
57  }
58 
59  if (!m_MatrixByVector)
60  {
61  if (this->GetInput()->GetNumberOfComponentsPerPixel() != m_Matrix.rows())
62  {
63  itkExceptionMacro("Invalid Matrix size. Number of rows must be the same as the image number of channels.");
64  }
65 
66  if (m_Matrix.cols() == 0)
67  {
68  itkExceptionMacro("Invalid Matrix size. Number of columns can't be null.");
69  }
70  this->GetOutput()->SetNumberOfComponentsPerPixel(m_Matrix.cols());
71  }
72 }
73 
74 template <class TInputImage, class TOutputImage, class TMatrix>
75 void MatrixImageFilter<TInputImage, TOutputImage, TMatrix>::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
76 {
77  // images pointer
78  typename OutputImageType::Pointer outputPtr = this->GetOutput();
79  typename InputImageType::ConstPointer inputPtr = this->GetInput();
80 
81  typename itk::ImageRegionConstIterator<InputImageType> inIt(inputPtr, outputRegionForThread);
82  itk::ImageRegionIterator<OutputImageType> outIt(outputPtr, outputRegionForThread);
83 
84 
85  // support progress methods/callbacks
86  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
87 
88  inIt.GoToBegin();
89  outIt.GoToBegin();
90 
91  const unsigned int inSize = m_MatrixByVector ? m_Matrix.cols() : m_Matrix.rows();
92  const unsigned int outSize = m_MatrixByVector ? m_Matrix.rows() : m_Matrix.cols();
93 
94  VectorType inVect(inSize, InputRealType(0.));
95  VectorType outVect(outSize, InputRealType(0.));
96 
97  while (!outIt.IsAtEnd())
98  {
99  const InputPixelType& inPix = inIt.Get();
100  OutputPixelType outPix;
101  outPix.SetSize(outSize);
102 
103  for (unsigned int i = 0; i < inSize; ++i)
104  {
105  inVect[i] = static_cast<InputRealType>(inPix[i]);
106  }
107 
108  outVect = m_MatrixByVector ? m_Matrix * inVect : inVect * m_Matrix;
109 
110  for (unsigned int i = 0; i < outSize; ++i)
111  {
112  outPix[i] = static_cast<OutputInternalPixelType>(outVect[i]);
113  }
114  outIt.Set(outPix);
115 
116  ++inIt;
117  ++outIt;
118  progress.CompletedPixel();
119  }
120 }
121 
122 
126 template <class TInputImage, class TOutputImage, class TMatrix>
127 void MatrixImageFilter<TInputImage, TOutputImage, TMatrix>::PrintSelf(std::ostream& os, itk::Indent indent) const
128 {
129  Superclass::PrintSelf(os, indent);
130  os << indent << "Matrix: " << m_Matrix << std::endl;
131  os << indent << "MatrixByVector: " << m_MatrixByVector << std::endl;
132 }
134 
135 } // end namespace otb
136 
137 #endif
otb::MatrixImageFilter::OutputImageRegionType
OutputImageType::RegionType OutputImageRegionType
Definition: otbMatrixImageFilter.h:89
otbMatrixImageFilter.h
otb::MatrixImageFilter::InputPixelType
InputImageType::PixelType InputPixelType
Definition: otbMatrixImageFilter.h:74
otb::MatrixImageFilter::VectorType
vnl_vector< InputRealType > VectorType
Definition: otbMatrixImageFilter.h:86
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::MatrixImageFilter::InputRealType
itk::NumericTraits< InputInternalPixelType >::RealType InputRealType
Definition: otbMatrixImageFilter.h:84
otb::MatrixImageFilter::OutputInternalPixelType
OutputImageType::InternalPixelType OutputInternalPixelType
Definition: otbMatrixImageFilter.h:80
otb::MatrixImageFilter::ThreadedGenerateData
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbMatrixImageFilter.hxx:75
otb::MatrixImageFilter::MatrixImageFilter
MatrixImageFilter()
Definition: otbMatrixImageFilter.hxx:35
otb::MatrixImageFilter::OutputPixelType
OutputImageType::PixelType OutputPixelType
Definition: otbMatrixImageFilter.h:78
otb::MatrixImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbMatrixImageFilter.hxx:127
otb::MatrixImageFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbMatrixImageFilter.hxx:40