OTB  6.1.0
Orfeo Toolbox
otbMatrixImageFilter.txx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2017 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_txx
22 #define otbMatrixImageFilter_txx
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>
76  const OutputImageRegionType& outputRegionForThread,
77  itk::ThreadIdType threadId
78  )
79 {
80  // images pointer
81  typename OutputImageType::Pointer outputPtr = this->GetOutput();
82  typename InputImageType::ConstPointer inputPtr = this->GetInput();
83 
84  typename itk::ImageRegionConstIterator<InputImageType> inIt( inputPtr, outputRegionForThread);
85  itk::ImageRegionIterator<OutputImageType> outIt( outputPtr, outputRegionForThread);
86 
87 
88  // support progress methods/callbacks
89  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
90 
91  inIt.GoToBegin();
92  outIt.GoToBegin();
93 
94  const unsigned int inSize = m_MatrixByVector ? m_Matrix.cols() : m_Matrix.rows();
95  const unsigned int outSize = m_MatrixByVector ? m_Matrix.rows() : m_Matrix.cols();
96 
97  VectorType inVect(inSize, InputRealType(0.));
98  VectorType outVect(outSize, InputRealType(0.));
99 
100  while (!outIt.IsAtEnd())
101  {
102  const InputPixelType & inPix = inIt.Get();
103  OutputPixelType outPix;
104  outPix.SetSize(outSize);
105 
106  for(unsigned int i=0; i<inSize; ++i)
107  {
108  inVect[i] = static_cast<InputRealType>(inPix[i]);
109  }
110 
111  outVect = m_MatrixByVector ? m_Matrix*inVect : inVect*m_Matrix;
112 
113  for(unsigned int i=0; i<outSize; ++i)
114  {
115  outPix[i] = static_cast<OutputInternalPixelType>(outVect[i]);
116  }
117  outIt.Set(outPix);
118 
119  ++inIt;
120  ++outIt;
121  progress.CompletedPixel();
122  }
123 }
124 
125 
129 template <class TInputImage, class TOutputImage, class TMatrix>
130 void
132 {
133  Superclass::PrintSelf(os, indent);
134  os << indent << "Matrix: " << m_Matrix << std::endl;
135  os << indent << "MatrixByVector: " << m_MatrixByVector << std::endl;
136 }
138 
139 } // end namespace otb
140 
141 #endif
OutputImageType::InternalPixelType OutputInternalPixelType
vnl_vector< InputRealType > VectorType
void GenerateOutputInformation() ITK_OVERRIDE
InputImageType::PixelType InputPixelType
void Set(const PixelType &value) const
itk::NumericTraits< InputInternalPixelType >::RealType InputRealType
void PrintSelf(std::ostream &os, itk::Indent indent) const ITK_OVERRIDE
unsigned int ThreadIdType
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) ITK_OVERRIDE
OutputImageType::RegionType OutputImageRegionType
bool IsAtEnd(void) const
OutputImageType::PixelType OutputPixelType
PixelType Get(void) const