OTB  5.4.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->SetTileSizeAlignment(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_ShrinkedOutput = OutputImageType::New();
123  m_ShrinkedOutput->CopyInformation(inputPtr);
124 
125  const typename InputImageType::SpacingType&
126  inputSpacing = inputPtr->GetSpacing();
127  const typename InputImageType::SizeType& inputSize
128  = inputPtr->GetLargestPossibleRegion().GetSize();
129 
130  typename OutputImageType::SpacingType shrinkedOutputSpacing;
131  typename OutputImageType::RegionType shrinkedOutputLargestPossibleRegion;
132  typename OutputImageType::SizeType shrinkedOutputSize;
133  typename OutputImageType::IndexType shrinkedOutputStartIndex;
134  typename OutputImageType::PointType shrinkedOutputOrigin;
135 
136  for (unsigned int i = 0; i < OutputImageType::ImageDimension; ++i)
137  {
138  shrinkedOutputSpacing[i] = inputSpacing[i] * static_cast<double>(m_ShrinkFactor);
139  shrinkedOutputSize[i] = inputSize[i] > m_ShrinkFactor ? inputSize[i] / m_ShrinkFactor : 1;
140 
141  shrinkedOutputOrigin[i] = inputPtr->GetOrigin()[i] + inputSpacing[i] *
142  (static_cast<double>(inputPtr->GetLargestPossibleRegion().GetIndex(i)) - 0.5)
143  + shrinkedOutputSpacing[i] * 0.5;
144 
145  // we choose to output a region with a start index [0,0]
146  // the origin is set accordingly
147  shrinkedOutputStartIndex[i] = 0;
148  }
149 
150  m_ShrinkedOutput->SetSpacing(shrinkedOutputSpacing);
151  m_ShrinkedOutput->SetOrigin(shrinkedOutputOrigin);
152 
153  shrinkedOutputLargestPossibleRegion.SetSize(shrinkedOutputSize);
154  shrinkedOutputLargestPossibleRegion.SetIndex(shrinkedOutputStartIndex);
155 
156  m_ShrinkedOutput->SetRegions(shrinkedOutputLargestPossibleRegion);
157  m_ShrinkedOutput->Allocate();
158 }
159 
160 template<class TInputImage, class TOutputImage>
161 void
164 {
165 }
166 
167 template<class TInputImage, class TOutputImage>
168 void
171 {
172 }
173 
174 template<class TInputImage, class TOutputImage>
175 void
177 ::ThreadedGenerateData(const RegionType& outputRegionForThread, itk::ThreadIdType threadId)
178 {
179  //std::cout << "outputRegionForThread " << threadId << " " << outputRegionForThread << std::endl;
180  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
181  const InputImageType* inputPtr = this->GetInput();
182 
183  itk::ImageRegionConstIteratorWithIndex<InputImageType> inIt(inputPtr, outputRegionForThread);
184  for(inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt, progress.CompletedPixel())
185  {
186  const IndexType& inIndex = inIt.GetIndex();
187  // TODO the pixel value should be taken near the centre of the cell, not at the corners
188  if (inIndex[0] % m_ShrinkFactor == 0
189  && inIndex[1] % m_ShrinkFactor == 0 )
190  {
191  IndexType shrinkedIndex;
192  shrinkedIndex[0] = inIndex[0] / m_ShrinkFactor;
193  shrinkedIndex[1] = inIndex[1] / m_ShrinkFactor;
194  if (m_ShrinkedOutput->GetLargestPossibleRegion().IsInside(shrinkedIndex))
195  m_ShrinkedOutput->SetPixel(shrinkedIndex, inIt.Get());
196  }
197  }
198 }
199 
200 template<class TInputImage, class TOutputImage>
201 void
204 {
205 }
206 
207 template <class TImage, class TOutputImage>
208 void
210 ::PrintSelf(std::ostream& os, itk::Indent indent) const
211 {
212  Superclass::PrintSelf(os, indent);
213  os << indent << "Shrink factor: " << m_ShrinkFactor << std::endl;
214 }
215 
216 } // End namespace otb
217 #endif
virtual void PrintSelf(std::ostream &os, itk::Indent indent) const
PixelType Get(void) const
virtual void PrepareStreaming(itk::DataObject *input, const RegionType &region)
const IndexType & GetIndex() const
void ThreadedGenerateData(const RegionType &outputRegionForThread, itk::ThreadIdType threadId)
TInputImage InputImageType
unsigned int ThreadIdType
TOutputImage OutputImageType
#define otbMsgDevMacro(x)
Definition: otbMacro.h:95