OTB  9.0.0
Orfeo Toolbox
otbStreamingMatrixTransposeMatrixImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2022 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 #ifndef otbStreamingMatrixTransposeMatrixImageFilter_hxx
23 #define otbStreamingMatrixTransposeMatrixImageFilter_hxx
24 
26 
27 #include "itkImageRegionIterator.h"
28 #include "itkNumericTraits.h"
29 #include "itkProgressReporter.h"
30 
31 namespace otb
32 {
33 
34 template <class TInputImage, class TInputImage2>
36 {
37  this->SetNumberOfRequiredInputs(2);
38 
39  // first output is a copy of the image, DataObject created by
40  // superclass
41  //
42  // allocate the data objects for the outputs which are
43  // just decorators around pixel types
44 
45  typename ImageType::Pointer output1 = static_cast<ImageType*>(this->MakeOutput(0).GetPointer());
46  this->itk::ProcessObject::SetNthOutput(0, output1.GetPointer());
47  typename MatrixObjectType::Pointer output2 = static_cast<MatrixObjectType*>(this->MakeOutput(1).GetPointer());
48  this->itk::ProcessObject::SetNthOutput(1, output2.GetPointer());
49 
50  // false means no pad added
51  m_UsePadFirstInput = false;
52  m_UsePadSecondInput = false;
53 
54  // Number of component initialization
55  m_NumberOfComponents1 = 0;
56  m_NumberOfComponents2 = 0;
57 }
58 
59 template <class TInputImage, class TInputImage2>
61 {
62  switch (output)
63  {
64  case 0:
65  return static_cast<itk::DataObject*>(TInputImage::New().GetPointer());
66  break;
67  case 1:
68  return static_cast<itk::DataObject*>(MatrixObjectType::New().GetPointer());
69  break;
70  default:
71  // might as well make an image
72  return static_cast<itk::DataObject*>(TInputImage::New().GetPointer());
73  break;
74  }
75 }
76 template <class TInputImage, class TInputImage2>
79 {
80  return static_cast<MatrixObjectType*>(this->itk::ProcessObject::GetOutput(1));
81 }
82 
83 template <class TInputImage, class TInputImage2>
86 {
87  return static_cast<const MatrixObjectType*>(this->itk::ProcessObject::GetOutput(1));
88 }
89 
90 template <class TInputImage, class TInputImage2>
92 {
93  Superclass::GenerateInputRequestedRegion();
94 
95  if (this->GetFirstInput() && this->GetSecondInput())
96  {
97  InputImagePointer image = const_cast<typename Superclass::InputImageType*>(this->GetFirstInput());
98  InputImagePointer image2 = const_cast<typename Superclass::InputImageType*>(this->GetSecondInput());
99  image->SetRequestedRegion(this->GetOutput()->GetRequestedRegion());
100  image2->SetRequestedRegion(this->GetOutput()->GetRequestedRegion());
101  }
102 }
103 template <class TInputImage, class TInputImage2>
105 {
106  Superclass::GenerateOutputInformation();
107  if (this->GetFirstInput())
108  {
109  this->GetOutput()->CopyInformation(this->GetFirstInput());
110  this->GetOutput()->SetLargestPossibleRegion(this->GetFirstInput()->GetLargestPossibleRegion());
111  }
112 
113  if (this->GetOutput()->GetRequestedRegion().GetNumberOfPixels() == 0)
114  {
115  this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetLargestPossibleRegion());
116  }
117 }
118 
119 template <class TInputImage, class TInputImage2>
121 {
122  // This is commented to prevent the streaming of the whole image for the first stream strip
123  // It shall not cause any problem because the output image of this filter is not intended to be used.
124  // InputImagePointer image = const_cast< TInputImage * >( this->GetInput() );
125  // this->GraftOutput( image );
126  // Nothing that needs to be allocated for the remaining outputs
127 }
128 
129 template <class TInputImage, class TInputImage2>
131 {
132 
133  TInputImage* inputPtr1 = const_cast<TInputImage*>(this->GetFirstInput());
134  inputPtr1->UpdateOutputInformation();
135  TInputImage2* inputPtr2 = const_cast<TInputImage2*>(this->GetSecondInput());
136  inputPtr2->UpdateOutputInformation();
137 
138  if (this->GetOutput()->GetRequestedRegion().GetNumberOfPixels() == 0)
139  {
140  this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetLargestPossibleRegion());
141  }
142 
143  if (inputPtr1->GetLargestPossibleRegion().GetSize() != inputPtr2->GetLargestPossibleRegion().GetSize())
144  {
145  itkExceptionMacro(<< " Can't multiply the transposed matrix of a " << inputPtr1->GetLargestPossibleRegion().GetSize() << " and a "
146  << inputPtr2->GetLargestPossibleRegion().GetSize() << " matrix ");
147  }
148 
149  m_NumberOfComponents1 = inputPtr1->GetNumberOfComponentsPerPixel();
150  m_NumberOfComponents2 = inputPtr2->GetNumberOfComponentsPerPixel();
151  unsigned int numberOfThreads = this->GetNumberOfThreads();
152 
153  if (m_UsePadFirstInput == true)
154  {
155  m_NumberOfComponents1++;
156  }
157  if (m_UsePadSecondInput == true)
158  {
159  m_NumberOfComponents2++;
160  }
161 
162  MatrixType tempMatrix, initMatrix;
163  tempMatrix.SetSize(m_NumberOfComponents1, m_NumberOfComponents2);
164  tempMatrix.Fill(itk::NumericTraits<RealType>::Zero);
165  m_ThreadSum = ArrayMatrixType(numberOfThreads, tempMatrix);
166 
167  initMatrix.SetSize(m_NumberOfComponents2, m_NumberOfComponents2);
168  initMatrix.Fill(itk::NumericTraits<RealType>::Zero);
169  this->GetResultOutput()->Set(initMatrix);
170 }
171 
172 template <class TInputImage, class TInputImage2>
174 {
175  unsigned int numberOfThreads = this->GetNumberOfThreads();
176  MatrixType resultMatrix;
177  resultMatrix.SetSize(m_NumberOfComponents1, m_NumberOfComponents2);
178  resultMatrix.Fill(itk::NumericTraits<RealType>::Zero);
179 
180  for (unsigned int thread = 0; thread < numberOfThreads; thread++)
181  {
186  for (unsigned int i = 0; i < resultMatrix.Rows(); ++i)
187  {
188  for (unsigned int j = 0; j < resultMatrix.Cols(); ++j)
189  {
190  resultMatrix(i, j) += m_ThreadSum[thread](i, j);
191  }
192  }
193 
194 /********END TODO ******/
195  }
196  this->GetResultOutput()->Set(resultMatrix);
197 }
198 
199 template <class TInputImage, class TInputImage2>
201  itk::ThreadIdType threadId)
202 {
206  InputImagePointer input1Ptr = const_cast<TInputImage*>(this->GetFirstInput());
207  InputImagePointer input2Ptr = const_cast<TInputImage2*>(this->GetSecondInput());
209 
210  // support progress methods/callbacks
211  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
212 
213  itk::ImageRegionConstIterator<TInputImage> it1(input1Ptr, outputRegionForThread);
214  itk::ImageRegionConstIterator<TInputImage2> it2(input2Ptr, outputRegionForThread);
215  it1.GoToBegin();
216  it2.GoToBegin();
217 
218  // loop the second image and get one pixel a time
219  while (!it1.IsAtEnd())
220  {
221  PixelType vectorValue1 = it1.Get();
222  PixelType2 vectorValue2 = it2.Get();
223 
224  // Add a first component to vectorValue2 and vectorValue1 filled with ones.
225  if (m_UsePadFirstInput == true)
226  {
227  PixelType vectortemp1(vectorValue1.Size() + 1);
228  vectortemp1[0] = 1;
229  for (unsigned int n = 0; n < vectorValue1.Size(); ++n)
230  {
231  vectortemp1[n + 1] = vectorValue1[n];
232  }
233  vectorValue1.SetSize(vectortemp1.Size());
234  vectorValue1 = vectortemp1;
235  }
236 
237  if (m_UsePadSecondInput == true)
238  {
239  PixelType2 vectortemp2(vectorValue2.Size() + 1);
240  vectortemp2[0] = 1;
241  for (unsigned int m = 0; m < vectorValue2.Size(); m++)
242  {
243  vectortemp2[m + 1] = vectorValue2[m];
244  }
245  vectorValue2.SetSize(vectortemp2.Size());
246  vectorValue2 = vectortemp2;
247  }
248 
249  for (unsigned int i = 0; i < vectorValue1.Size(); ++i)
250  {
251  for (unsigned int j = 0; j < vectorValue2.Size(); ++j)
252  {
253  m_ThreadSum[threadId](i, j) += static_cast<RealType>(vectorValue1[i]) * static_cast<RealType>(vectorValue2[j]);
254  }
255  }
256  ++it1;
257  ++it2;
258  progress.CompletedPixel();
259  }
260 }
261 
262 template <class TInputImage, class TInputImage2>
264 {
265  Superclass::PrintSelf(os, indent);
266 
267  os << indent << "Result: " << this->GetResultOutput()->Get() << std::endl;
268 }
269 
270 } // end namespace otb
271 #endif
otb::PersistentMatrixTransposeMatrixImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbStreamingMatrixTransposeMatrixImageFilter.hxx:263
otb::PersistentMatrixTransposeMatrixImageFilter::MakeOutput
DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) override
Definition: otbStreamingMatrixTransposeMatrixImageFilter.hxx:60
otb::PersistentMatrixTransposeMatrixImageFilter::PixelType2
TInputImage2::PixelType PixelType2
Definition: otbStreamingMatrixTransposeMatrixImageFilter.h:81
otb::PersistentMatrixTransposeMatrixImageFilter::Synthetize
void Synthetize(void) override
Definition: otbStreamingMatrixTransposeMatrixImageFilter.hxx:173
otb::PersistentMatrixTransposeMatrixImageFilter::MatrixObjectType
itk::SimpleDataObjectDecorator< MatrixType > MatrixObjectType
Definition: otbStreamingMatrixTransposeMatrixImageFilter.h:111
otb::PersistentMatrixTransposeMatrixImageFilter::AllocateOutputs
void AllocateOutputs() override
Definition: otbStreamingMatrixTransposeMatrixImageFilter.hxx:120
otb::PersistentMatrixTransposeMatrixImageFilter::Reset
void Reset(void) override
Definition: otbStreamingMatrixTransposeMatrixImageFilter.hxx:130
otbStreamingMatrixTransposeMatrixImageFilter.h
otb::PersistentMatrixTransposeMatrixImageFilter::GetResultOutput
MatrixObjectType * GetResultOutput()
Definition: otbStreamingMatrixTransposeMatrixImageFilter.hxx:78
otb::PersistentMatrixTransposeMatrixImageFilter::PersistentMatrixTransposeMatrixImageFilter
PersistentMatrixTransposeMatrixImageFilter()
Definition: otbStreamingMatrixTransposeMatrixImageFilter.hxx:35
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::PersistentMatrixTransposeMatrixImageFilter::RealType
double RealType
Definition: otbStreamingMatrixTransposeMatrixImageFilter.h:96
otb::PersistentMatrixTransposeMatrixImageFilter::MatrixType
itk::VariableSizeMatrix< RealType > MatrixType
Definition: otbStreamingMatrixTransposeMatrixImageFilter.h:105
otb::PersistentMatrixTransposeMatrixImageFilter::InputImagePointer
TInputImage::Pointer InputImagePointer
Definition: otbStreamingMatrixTransposeMatrixImageFilter.h:73
otb::PersistentMatrixTransposeMatrixImageFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbStreamingMatrixTransposeMatrixImageFilter.hxx:104
otb::PersistentMatrixTransposeMatrixImageFilter::ImageType
TInputImage ImageType
Definition: otbStreamingMatrixTransposeMatrixImageFilter.h:68
otb::PersistentMatrixTransposeMatrixImageFilter::RegionType
TInputImage::RegionType RegionType
Definition: otbStreamingMatrixTransposeMatrixImageFilter.h:74
otb::PersistentMatrixTransposeMatrixImageFilter::ThreadedGenerateData
void ThreadedGenerateData(const RegionType &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbStreamingMatrixTransposeMatrixImageFilter.hxx:200
otb::PersistentMatrixTransposeMatrixImageFilter::ArrayMatrixType
std::vector< MatrixType > ArrayMatrixType
Definition: otbStreamingMatrixTransposeMatrixImageFilter.h:106
otb::PersistentImageFilter< TInputImage, TInputImage >::InputImageType
TInputImage InputImageType
Definition: otbPersistentImageFilter.h:57
otb::PersistentMatrixTransposeMatrixImageFilter::DataObjectPointerArraySizeType
itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
Definition: otbStreamingMatrixTransposeMatrixImageFilter.h:101
otb::PersistentMatrixTransposeMatrixImageFilter::PixelType
TInputImage::PixelType PixelType
Definition: otbStreamingMatrixTransposeMatrixImageFilter.h:77
otb::PersistentMatrixTransposeMatrixImageFilter::GenerateInputRequestedRegion
void GenerateInputRequestedRegion() override
Definition: otbStreamingMatrixTransposeMatrixImageFilter.hxx:91