OTB  6.3.0
Orfeo Toolbox
otbStreamingMinMaxImageFilter.txx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2017 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 otbStreamingMinMaxImageFilter_txx
23 #define otbStreamingMinMaxImageFilter_txx
25 
26 #include <algorithm>
27 #include "itkImageRegionIterator.h"
28 #include "itkProgressReporter.h"
29 #include "otbMacro.h"
30 
31 namespace otb
32 {
33 
34 template<class TInputImage>
37 {
38  // TODO : SetNumberOfRequiredOutputs
39 
40  // first output is a copy of the image, DataObject created by
41  // superclass
42  //
43  // allocate the data objects for the outputs which are
44  // just decorators around pixel & index types
45  for (int i = 1; i < 5; ++i)
46  {
47  this->itk::ProcessObject::SetNthOutput(i, this->MakeOutput(i));
48  }
49 
50  this->GetMinimumOutput()->Set(itk::NumericTraits<PixelType>::max());
51  this->GetMaximumOutput()->Set(itk::NumericTraits<PixelType>::NonpositiveMin());
52 
53  this->Reset();
54 }
55 
56 template<class TInputImage>
60 {
62  switch (output)
63  {
64  case 0:
65  ret = static_cast<itk::DataObject*>(TInputImage::New().GetPointer());
66  break;
67  case 1:
68  case 2:
69  ret = static_cast<itk::DataObject*>(PixelObjectType::New().GetPointer());
70  break;
71  case 3:
72  case 4:
73  ret = static_cast<itk::DataObject*>(IndexObjectType::New().GetPointer());
74  break;
75  }
76  return ret;
77 }
78 
79 template<class TInputImage>
83 {
84  return static_cast<PixelObjectType*>(this->itk::ProcessObject::GetOutput(1));
85 }
86 
87 template<class TInputImage>
91 {
92  return static_cast<const PixelObjectType*>(this->itk::ProcessObject::GetOutput(1));
93 }
94 
95 template<class TInputImage>
99 {
100  return static_cast<PixelObjectType*>(this->itk::ProcessObject::GetOutput(2));
101 }
102 
103 template<class TInputImage>
107 {
108  return static_cast<const PixelObjectType*>(this->itk::ProcessObject::GetOutput(2));
109 }
110 
111 
112 template<class TInputImage>
116 {
117  return static_cast<IndexObjectType*>(this->itk::ProcessObject::GetOutput(3));
118 }
119 
120 template<class TInputImage>
124 {
125  return static_cast<const IndexObjectType*>(this->itk::ProcessObject::GetOutput(3));
126 }
127 
128 template<class TInputImage>
132 {
133  return static_cast<IndexObjectType*>(this->itk::ProcessObject::GetOutput(4));
134 }
135 
136 template<class TInputImage>
140 {
141  return static_cast<const IndexObjectType*>(this->itk::ProcessObject::GetOutput(4));
142 }
143 
144 template<class TInputImage>
145 void
148 {
149  Superclass::GenerateOutputInformation();
150  if (this->GetInput())
151  {
152  this->GetOutput()->CopyInformation(this->GetInput());
153  this->GetOutput()->SetLargestPossibleRegion(this->GetInput()->GetLargestPossibleRegion());
154 
155  if (this->GetOutput()->GetRequestedRegion().GetNumberOfPixels() == 0)
156  {
157  this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetLargestPossibleRegion());
158  }
159  }
160 }
161 template<class TInputImage>
162 void
165 {
166  // This is commented to prevent the streaming of the whole image for the first stream strip
167  // It shall not cause any problem because the output image of this filter is not intended to be used.
168  //InputImagePointer image = const_cast< TInputImage * >( this->GetInput() );
169  //this->GraftOutput( image );
170  // Nothing that needs to be allocated for the remaining outputs
171 }
172 
173 template<class TInputImage>
174 void
177 {
178  int i;
179  int numberOfThreads = this->GetNumberOfThreads();
180 
183  IndexType minimumIdx;
184  IndexType maximumIdx;
185 
186  for (i = 0; i < numberOfThreads; ++i)
187  {
188  if (m_ThreadMin[i] < minimum)
189  {
190  minimum = m_ThreadMin[i];
191  minimumIdx = m_ThreadMinIndex[i];
192  }
193  if (m_ThreadMax[i] > maximum)
194  {
195  maximum = m_ThreadMax[i];
196  maximumIdx = m_ThreadMaxIndex[i];
197  }
198  }
199 
200  // Set the outputs
201  this->GetMinimumOutput()->Set(minimum);
202  this->GetMaximumOutput()->Set(maximum);
203  this->GetMinimumIndexOutput()->Set(minimumIdx);
204  this->GetMaximumIndexOutput()->Set(maximumIdx);
205 }
206 
207 template<class TInputImage>
208 void
211 {
212  int numberOfThreads = this->GetNumberOfThreads();
213 
214  m_ThreadMin.resize(numberOfThreads);
215  m_ThreadMax.resize(numberOfThreads);
216  std::fill(m_ThreadMin.begin(), m_ThreadMin.end(), itk::NumericTraits<PixelType>::max());
217  std::fill(m_ThreadMax.begin(), m_ThreadMax.end(), itk::NumericTraits<PixelType>::NonpositiveMin());
218 
219  IndexType zeroIdx;
220  zeroIdx.Fill(0);
221  m_ThreadMinIndex.resize(numberOfThreads);
222  m_ThreadMaxIndex.resize(numberOfThreads);
223  std::fill(m_ThreadMinIndex.begin(), m_ThreadMinIndex.end(), zeroIdx);
224  std::fill(m_ThreadMaxIndex.begin(), m_ThreadMaxIndex.end(), zeroIdx);
225 }
226 
227 template<class TInputImage>
228 void
230 ::ThreadedGenerateData(const RegionType& outputRegionForThread,
231  itk::ThreadIdType threadId)
232 {
233  // support progress methods/callbacks
234  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
235 
236  InputImagePointer inputPtr = const_cast<TInputImage *>(this->GetInput(0));
237  itk::ImageRegionConstIterator<TInputImage> it(inputPtr, outputRegionForThread);
238  it.GoToBegin();
239  // do the work
240  while (!it.IsAtEnd())
241  {
242  PixelType value = it.Get();
243  if (value < m_ThreadMin[threadId])
244  {
245  m_ThreadMin[threadId] = value;
246  m_ThreadMinIndex[threadId] = it.GetIndex();
247  }
248  if (value > m_ThreadMax[threadId])
249  {
250  m_ThreadMax[threadId] = value;
251  m_ThreadMaxIndex[threadId] = it.GetIndex();
252  }
253  ++it;
254  progress.CompletedPixel();
255  }
256 }
257 
258 template <class TImage>
259 void
261 ::PrintSelf(std::ostream& os, itk::Indent indent) const
262 {
263  Superclass::PrintSelf(os, indent);
264 
265  os << indent << "Minimum: "
266  << static_cast<typename itk::NumericTraits<PixelType>::PrintType>(this->GetMinimum()) << std::endl;
267  os << indent << "Maximum: "
268  << static_cast<typename itk::NumericTraits<PixelType>::PrintType>(this->GetMaximum()) << std::endl;
269  os << indent << "Minimum Index: " << this->GetMinimumIndex() << std::endl;
270  os << indent << "Maximum Index: " << this->GetMaximumIndex() << std::endl;
271 }
272 
273 } // end namespace otb
274 #endif
Superclass::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
const IndexType GetIndex() const
void PrintSelf(std::ostream &os, itk::Indent indent) const ITK_OVERRIDE
static ITK_CONSTEXPR_FUNC T max(const T &)
unsigned int ThreadIdType
static ITK_CONSTEXPR_FUNC T NonpositiveMin()
bool IsAtEnd(void) const
virtual void SetNthOutput(DataObjectPointerArraySizeType num, DataObject *output)
PixelType Get(void) const
void ThreadedGenerateData(const RegionType &outputRegionForThread, itk::ThreadIdType threadId) ITK_OVERRIDE
DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) ITK_OVERRIDE
DataObject * GetOutput(const DataObjectIdentifierType &key)