OTB  5.0.0
Orfeo Toolbox
otbMatrixImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbMatrixImageFilter_txx
19 #define __otbMatrixImageFilter_txx
20 
21 #include "otbMatrixImageFilter.h"
22 #include "itkImageRegionIterator.h"
23 #include "itkProgressReporter.h"
24 
25 namespace otb
26 {
27 
31 template <class TInputImage, class TOutputImage, class TMatrix>
33 {
34 }
35 
36 template <class TInputImage, class TOutputImage, class TMatrix>
38 {
39  // call the superclass' implementation of this method
40  Superclass::GenerateOutputInformation();
41 
42  if ( m_MatrixByVector )
43  {
44  if( this->GetInput()->GetNumberOfComponentsPerPixel() != m_Matrix.cols() )
45  {
46  itkExceptionMacro("Invalid Matrix size. Number of columns must be the same as the image number of channels.");
47  }
48 
49  if( m_Matrix.rows() == 0 )
50  {
51  itkExceptionMacro("Invalid Matrix size. Number of rows can't be null.");
52  }
53  this->GetOutput()->SetNumberOfComponentsPerPixel( m_Matrix.rows() );
54  }
55 
56  if ( !m_MatrixByVector )
57  {
58  if( this->GetInput()->GetNumberOfComponentsPerPixel() != m_Matrix.rows() )
59  {
60  itkExceptionMacro("Invalid Matrix size. Number of rows must be the same as the image number of channels.");
61  }
62 
63  if( m_Matrix.cols() == 0 )
64  {
65  itkExceptionMacro("Invalid Matrix size. Number of columns can't be null.");
66  }
67  this->GetOutput()->SetNumberOfComponentsPerPixel( m_Matrix.cols() );
68  }
69 }
70 
71 template<class TInputImage, class TOutputImage, class TMatrix>
73  const OutputImageRegionType& outputRegionForThread,
74  itk::ThreadIdType threadId
75  )
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
129 {
130  Superclass::PrintSelf(os, indent);
131  os << indent << "Matrix: " << m_Matrix << std::endl;
132  os << indent << "MatrixByVector: " << m_MatrixByVector << std::endl;
133 }
135 
136 } // end namespace otb
137 
138 #endif
OutputImageType::InternalPixelType OutputInternalPixelType
vnl_vector< InputRealType > VectorType
InputImageType::PixelType InputPixelType
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId)
void Set(const PixelType &value) const
itk::NumericTraits< InputInternalPixelType >::RealType InputRealType
void PrintSelf(std::ostream &os, itk::Indent indent) const
OutputImageType::RegionType OutputImageRegionType
bool IsAtEnd(void) const
OutputImageType::PixelType OutputPixelType
PixelType Get(void) const
unsigned int ThreadIdType