OTB  9.0.0
Orfeo Toolbox
otbStreamingMinMaxImageFilter.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 otbStreamingMinMaxImageFilter_hxx
23 #define otbStreamingMinMaxImageFilter_hxx
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>
36 {
37  // TODO : SetNumberOfRequiredOutputs
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 & index types
44  for (int i = 1; i < 5; ++i)
45  {
46  this->itk::ProcessObject::SetNthOutput(i, this->MakeOutput(i));
47  }
48 
49  this->GetMinimumOutput()->Set(itk::NumericTraits<PixelType>::max());
50  this->GetMaximumOutput()->Set(itk::NumericTraits<PixelType>::NonpositiveMin());
51 
52  this->Reset();
53 }
54 
55 template <class TInputImage>
57 {
58  itk::DataObject::Pointer ret;
59  switch (output)
60  {
61  case 0:
62  ret = static_cast<itk::DataObject*>(TInputImage::New().GetPointer());
63  break;
64  case 1:
65  case 2:
66  ret = static_cast<itk::DataObject*>(PixelObjectType::New().GetPointer());
67  break;
68  case 3:
69  case 4:
70  ret = static_cast<itk::DataObject*>(IndexObjectType::New().GetPointer());
71  break;
72  }
73  return ret;
74 }
75 
76 template <class TInputImage>
78 {
79  return static_cast<PixelObjectType*>(this->itk::ProcessObject::GetOutput(1));
80 }
81 
82 template <class TInputImage>
84 {
85  return static_cast<const PixelObjectType*>(this->itk::ProcessObject::GetOutput(1));
86 }
87 
88 template <class TInputImage>
90 {
91  return static_cast<PixelObjectType*>(this->itk::ProcessObject::GetOutput(2));
92 }
93 
94 template <class TInputImage>
96 {
97  return static_cast<const PixelObjectType*>(this->itk::ProcessObject::GetOutput(2));
98 }
99 
100 
101 template <class TInputImage>
103 {
104  return static_cast<IndexObjectType*>(this->itk::ProcessObject::GetOutput(3));
105 }
106 
107 template <class TInputImage>
109 {
110  return static_cast<const IndexObjectType*>(this->itk::ProcessObject::GetOutput(3));
111 }
112 
113 template <class TInputImage>
115 {
116  return static_cast<IndexObjectType*>(this->itk::ProcessObject::GetOutput(4));
117 }
118 
119 template <class TInputImage>
121 {
122  return static_cast<const IndexObjectType*>(this->itk::ProcessObject::GetOutput(4));
123 }
124 
125 template <class TInputImage>
127 {
128  Superclass::GenerateOutputInformation();
129  if (this->GetInput())
130  {
131  this->GetOutput()->CopyInformation(this->GetInput());
132  this->GetOutput()->SetLargestPossibleRegion(this->GetInput()->GetLargestPossibleRegion());
133 
134  if (this->GetOutput()->GetRequestedRegion().GetNumberOfPixels() == 0)
135  {
136  this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetLargestPossibleRegion());
137  }
138  }
139 }
140 template <class TInputImage>
142 {
143  // This is commented to prevent the streaming of the whole image for the first stream strip
144  // It shall not cause any problem because the output image of this filter is not intended to be used.
145  // InputImagePointer image = const_cast< TInputImage * >( this->GetInput() );
146  // this->GraftOutput( image );
147  // Nothing that needs to be allocated for the remaining outputs
148 }
149 
150 template <class TInputImage>
152 {
153  int i;
154  int numberOfThreads = this->GetNumberOfThreads();
155 
156  PixelType minimum = itk::NumericTraits<PixelType>::max();
157  PixelType maximum = itk::NumericTraits<PixelType>::NonpositiveMin();
158  IndexType minimumIdx;
159  IndexType maximumIdx;
160 
161  for (i = 0; i < numberOfThreads; ++i)
162  {
163  if (m_ThreadMin[i] < minimum)
164  {
165  minimum = m_ThreadMin[i];
166  minimumIdx = m_ThreadMinIndex[i];
167  }
168  if (m_ThreadMax[i] > maximum)
169  {
170  maximum = m_ThreadMax[i];
171  maximumIdx = m_ThreadMaxIndex[i];
172  }
173  }
174 
175  // Set the outputs
176  this->GetMinimumOutput()->Set(minimum);
177  this->GetMaximumOutput()->Set(maximum);
178  this->GetMinimumIndexOutput()->Set(minimumIdx);
179  this->GetMaximumIndexOutput()->Set(maximumIdx);
180 }
181 
182 template <class TInputImage>
184 {
185  int numberOfThreads = this->GetNumberOfThreads();
186 
187  m_ThreadMin.resize(numberOfThreads);
188  m_ThreadMax.resize(numberOfThreads);
189  std::fill(m_ThreadMin.begin(), m_ThreadMin.end(), itk::NumericTraits<PixelType>::max());
190  std::fill(m_ThreadMax.begin(), m_ThreadMax.end(), itk::NumericTraits<PixelType>::NonpositiveMin());
191 
192  IndexType zeroIdx;
193  zeroIdx.Fill(0);
194  m_ThreadMinIndex.resize(numberOfThreads);
195  m_ThreadMaxIndex.resize(numberOfThreads);
196  std::fill(m_ThreadMinIndex.begin(), m_ThreadMinIndex.end(), zeroIdx);
197  std::fill(m_ThreadMaxIndex.begin(), m_ThreadMaxIndex.end(), zeroIdx);
198 }
199 
200 template <class TInputImage>
201 void PersistentMinMaxImageFilter<TInputImage>::ThreadedGenerateData(const RegionType& outputRegionForThread, itk::ThreadIdType threadId)
202 {
203  // support progress methods/callbacks
204  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
205 
206  InputImagePointer inputPtr = const_cast<TInputImage*>(this->GetInput(0));
207  itk::ImageRegionConstIterator<TInputImage> it(inputPtr, outputRegionForThread);
208  it.GoToBegin();
209  // do the work
210  while (!it.IsAtEnd())
211  {
212  PixelType value = it.Get();
213  if (value < m_ThreadMin[threadId])
214  {
215  m_ThreadMin[threadId] = value;
216  m_ThreadMinIndex[threadId] = it.GetIndex();
217  }
218  if (value > m_ThreadMax[threadId])
219  {
220  m_ThreadMax[threadId] = value;
221  m_ThreadMaxIndex[threadId] = it.GetIndex();
222  }
223  ++it;
224  progress.CompletedPixel();
225  }
226 }
227 
228 template <class TImage>
229 void PersistentMinMaxImageFilter<TImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
230 {
231  Superclass::PrintSelf(os, indent);
232 
233  os << indent << "Minimum: " << static_cast<typename itk::NumericTraits<PixelType>::PrintType>(this->GetMinimum()) << std::endl;
234  os << indent << "Maximum: " << static_cast<typename itk::NumericTraits<PixelType>::PrintType>(this->GetMaximum()) << std::endl;
235  os << indent << "Minimum Index: " << this->GetMinimumIndex() << std::endl;
236  os << indent << "Maximum Index: " << this->GetMaximumIndex() << std::endl;
237 }
238 
239 } // end namespace otb
240 #endif
otb::PersistentMinMaxImageFilter::GetMinimumIndexOutput
IndexObjectType * GetMinimumIndexOutput()
Definition: otbStreamingMinMaxImageFilter.hxx:102
otb::PersistentMinMaxImageFilter::PixelType
TInputImage::PixelType PixelType
Definition: otbStreamingMinMaxImageFilter.h:74
otb::PersistentMinMaxImageFilter::PixelObjectType
itk::SimpleDataObjectDecorator< PixelType > PixelObjectType
Definition: otbStreamingMinMaxImageFilter.h:86
otb::PersistentMinMaxImageFilter::PersistentMinMaxImageFilter
PersistentMinMaxImageFilter()
Definition: otbStreamingMinMaxImageFilter.hxx:35
otb::PersistentMinMaxImageFilter::IndexObjectType
itk::SimpleDataObjectDecorator< IndexType > IndexObjectType
Definition: otbStreamingMinMaxImageFilter.h:87
otbStreamingMinMaxImageFilter.h
otb::PersistentMinMaxImageFilter::AllocateOutputs
void AllocateOutputs() override
Definition: otbStreamingMinMaxImageFilter.hxx:141
otb::PersistentMinMaxImageFilter::ThreadedGenerateData
void ThreadedGenerateData(const RegionType &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbStreamingMinMaxImageFilter.hxx:201
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbMacro.h
otb::PersistentMinMaxImageFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbStreamingMinMaxImageFilter.hxx:126
otb::PersistentMinMaxImageFilter::Synthetize
void Synthetize(void) override
Definition: otbStreamingMinMaxImageFilter.hxx:151
otb::PersistentMinMaxImageFilter::RegionType
TInputImage::RegionType RegionType
Definition: otbStreamingMinMaxImageFilter.h:71
otb::PersistentMinMaxImageFilter::GetMinimumOutput
PixelObjectType * GetMinimumOutput()
Definition: otbStreamingMinMaxImageFilter.hxx:77
otb::PersistentMinMaxImageFilter::DataObjectPointerArraySizeType
itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
Definition: otbStreamingMinMaxImageFilter.h:83
otb::PersistentMinMaxImageFilter::GetMaximumIndexOutput
IndexObjectType * GetMaximumIndexOutput()
Definition: otbStreamingMinMaxImageFilter.hxx:114
otb::PersistentMinMaxImageFilter::GetMaximumOutput
PixelObjectType * GetMaximumOutput()
Definition: otbStreamingMinMaxImageFilter.hxx:89
otb::PersistentMinMaxImageFilter::Reset
void Reset(void) override
Definition: otbStreamingMinMaxImageFilter.hxx:183
otb::PersistentMinMaxImageFilter::MakeOutput
DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) override
Definition: otbStreamingMinMaxImageFilter.hxx:56
otb::PersistentMinMaxImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbStreamingMinMaxImageFilter.hxx:229
otb::PersistentMinMaxImageFilter::IndexType
TInputImage::IndexType IndexType
Definition: otbStreamingMinMaxImageFilter.h:73
otb::PersistentMinMaxImageFilter::InputImagePointer
TInputImage::Pointer InputImagePointer
Definition: otbStreamingMinMaxImageFilter.h:69