OTB  5.11.0
Orfeo Toolbox
otbStreamingShrinkImageFilter.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 otbStreamingShrinkImageFilter_txx
19 #define otbStreamingShrinkImageFilter_txx
20 
23 #include "otbMacro.h"
24 #include "itkProgressReporter.h"
25 
26 namespace otb
27 {
28 
29 template <class TImage>
31  : m_ShrinkFactor(10)
32 {
33 }
34 
35 template <class TImage>
37 {
38 }
39 
40 template <class TImage>
41 void
43 {
44  typedef otb::StreamingShrinkImageRegionSplitter TileSplitterType;
45  TileSplitterType::Pointer splitter = TileSplitterType::New();
46  splitter->SetShrinkFactor(m_ShrinkFactor);
47  this->m_Splitter = splitter;
48 
49  unsigned long nbDivisions = this->EstimateOptimalNumberOfDivisions(input, region, 0);
50  this->m_ComputedNumberOfSplits = this->m_Splitter->GetNumberOfSplits(region, nbDivisions);
51  otbMsgDevMacro(<< "Number of split : " << this->m_ComputedNumberOfSplits)
52 
53  // Save the region to generate the splits later
54  this->m_Region = region;
55 }
56 
57 
59 template <class TInputImage, class TOutputImage>
62  : m_ShrinkFactor(10)
63 {
64  this->SetNumberOfRequiredInputs(1);
65  this->SetNumberOfRequiredOutputs(1);
66 }
68 
70 template <class TInputImage, class TOutputImage>
73 {
74 
75 }
76 
77 template<class TInputImage, class TOutputImage>
78 void
81 {
82  Superclass::GenerateOutputInformation();
83 
84  const InputImageType* input = this->GetInput();
85 
86  OutputImageType* output = this->GetOutput();
87 
88  if (input)
89  {
90  output->CopyInformation(input);
91  output->SetLargestPossibleRegion(input->GetLargestPossibleRegion());
92 
93  if (output->GetRequestedRegion().GetNumberOfPixels() == 0)
94  {
95  output->SetRequestedRegion(output->GetLargestPossibleRegion());
96  }
97  }
98 }
99 
100 template<class TInputImage, class TOutputImage>
101 void
104 {
105  // This is commented to prevent the streaming of the whole image for the first stream strip
106  // It shall not cause any problem because the output image of this filter is not intended to be used.
107  //InputImagePointer image = const_cast< TInputImage * >( this->GetInput() );
108  //this->GraftOutput( image );
109  // Nothing that needs to be allocated for the remaining outputs
110 }
111 
112 
113 template<class TInputImage, class TOutputImage>
114 void
117 {
118  // Get pointers to the input and output
119  InputImageType* inputPtr = const_cast<InputImageType*>(this->GetInput());
120  inputPtr->UpdateOutputInformation();
121 
122  m_ShrunkOutput = OutputImageType::New();
123  m_ShrunkOutput->CopyInformation(inputPtr);
124 
125  const typename InputImageType::SpacingType&
126  inputSpacing = inputPtr->GetSpacing();
127  const typename InputImageType::SizeType& inputSize
128  = inputPtr->GetLargestPossibleRegion().GetSize();
129  const typename InputImageType::IndexType& inputIndex
130  = inputPtr->GetLargestPossibleRegion().GetIndex();
131  typename InputImageType::IndexType startIndex;
132 
133  typename OutputImageType::SpacingType shrunkOutputSpacing;
134  typename OutputImageType::RegionType shrunkOutputLargestPossibleRegion;
135  typename OutputImageType::SizeType shrunkOutputSize;
136  typename OutputImageType::IndexType shrunkOutputStartIndex;
137  typename OutputImageType::PointType shrunkOutputOrigin;
138 
139  for (unsigned int i = 0; i < OutputImageType::ImageDimension; ++i)
140  {
141  startIndex[i] = inputIndex[i] + (m_ShrinkFactor - 1) / 2;
142  if (m_ShrinkFactor > inputSize[i])
143  startIndex[i] = inputIndex[i] + (inputSize[i] - 1) / 2;
144  m_Offset[i] = startIndex[i] % m_ShrinkFactor;
145  shrunkOutputSpacing[i] = inputSpacing[i] * static_cast<double>(m_ShrinkFactor);
146  shrunkOutputSize[i] = inputSize[i] > m_ShrinkFactor ? inputSize[i] / m_ShrinkFactor : 1;
147 
148  shrunkOutputOrigin[i] = inputPtr->GetOrigin()[i] + inputSpacing[i] * startIndex[i];
149 
150  // we choose to output a region with a start index [0,0]
151  // the origin is set accordingly
152  shrunkOutputStartIndex[i] = 0;
153  }
154 
155  m_ShrunkOutput->SetSpacing(shrunkOutputSpacing);
156  m_ShrunkOutput->SetOrigin(shrunkOutputOrigin);
157 
158  shrunkOutputLargestPossibleRegion.SetSize(shrunkOutputSize);
159  shrunkOutputLargestPossibleRegion.SetIndex(shrunkOutputStartIndex);
160 
161  m_ShrunkOutput->SetRegions(shrunkOutputLargestPossibleRegion);
162  m_ShrunkOutput->Allocate();
163 }
164 
165 template<class TInputImage, class TOutputImage>
166 void
169 {
170 }
171 
172 template<class TInputImage, class TOutputImage>
173 void
176 {
177 }
178 
179 template<class TInputImage, class TOutputImage>
180 void
182 ::ThreadedGenerateData(const RegionType& outputRegionForThread, itk::ThreadIdType threadId)
183 {
184  //std::cout << "outputRegionForThread " << threadId << " " << outputRegionForThread << std::endl;
185  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
186  const InputImageType* inputPtr = this->GetInput();
187 
188  itk::ImageRegionConstIteratorWithIndex<InputImageType> inIt(inputPtr, outputRegionForThread);
189  for(inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt, progress.CompletedPixel())
190  {
191  const IndexType& inIndex = inIt.GetIndex();
192  // TODO the pixel value should be taken near the centre of the cell, not at the corners
193  if ((inIndex[0] - m_Offset[0]) % m_ShrinkFactor == 0
194  && (inIndex[1] - m_Offset[1]) % m_ShrinkFactor == 0 )
195  {
196  IndexType shrunkIndex;
197  shrunkIndex[0] = (inIndex[0] - m_Offset[0]) / m_ShrinkFactor;
198  shrunkIndex[1] = (inIndex[1] - m_Offset[1]) / m_ShrinkFactor;
199  if (m_ShrunkOutput->GetLargestPossibleRegion().IsInside(shrunkIndex))
200  m_ShrunkOutput->SetPixel(shrunkIndex, inIt.Get());
201  }
202  }
203 }
204 
205 template<class TInputImage, class TOutputImage>
206 void
209 {
210 }
211 
212 template <class TImage, class TOutputImage>
213 void
215 ::PrintSelf(std::ostream& os, itk::Indent indent) const
216 {
217  Superclass::PrintSelf(os, indent);
218  os << indent << "Shrink factor: " << m_ShrinkFactor << std::endl;
219 }
220 
221 } // End namespace otb
222 #endif
itk::Size< Monteverdi_DIMENSION > SizeType
Definition: mvdTypes.h:144
void ThreadedGenerateData(const RegionType &outputRegionForThread, itk::ThreadIdType threadId) ITK_OVERRIDE
PixelType Get(void) const
const IndexType & GetIndex() const
void PrintSelf(std::ostream &os, itk::Indent indent) const ITK_OVERRIDE
itk::Index< Monteverdi_DIMENSION > IndexType
Definition: mvdTypes.h:140
TInputImage InputImageType
unsigned int ThreadIdType
VectorImageType::SpacingType SpacingType
Definition: mvdTypes.h:188
TOutputImage OutputImageType
void PrepareStreaming(itk::DataObject *input, const RegionType &region) ITK_OVERRIDE
VectorImageType::PointType PointType
Definition: mvdTypes.h:196
const SizeValueType * GetSize() const
#define otbMsgDevMacro(x)
Definition: otbMacro.h:95