OTB  5.0.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 {
32 }
33 
34 template <class TImage>
36 {
37 }
38 
39 template <class TImage>
40 void
42 {
43  typedef otb::StreamingShrinkImageRegionSplitter TileSplitterType;
44  TileSplitterType::Pointer splitter = TileSplitterType::New();
45  splitter->SetTileSizeAlignment(m_ShrinkFactor);
46  this->m_Splitter = splitter;
47 
48  unsigned long nbDivisions = this->EstimateOptimalNumberOfDivisions(input, region, 0);
49  this->m_ComputedNumberOfSplits = this->m_Splitter->GetNumberOfSplits(region, nbDivisions);
50  otbMsgDevMacro(<< "Number of split : " << this->m_ComputedNumberOfSplits)
51 
52  // Save the region to generate the splits later
53  this->m_Region = region;
54 }
55 
56 
58 template <class TInputImage, class TOutputImage>
61  : m_ShrinkFactor(10)
62 {
63  this->SetNumberOfRequiredInputs(1);
64  this->SetNumberOfRequiredOutputs(1);
65 }
67 
69 template <class TInputImage, class TOutputImage>
72 {
73 
74 }
75 
76 template<class TInputImage, class TOutputImage>
77 void
80 {
81  Superclass::GenerateOutputInformation();
82 
83  const InputImageType* input = this->GetInput();
84 
85  OutputImageType* output = this->GetOutput();
86 
87  if (input)
88  {
89  output->CopyInformation(input);
90  output->SetLargestPossibleRegion(input->GetLargestPossibleRegion());
91 
92  if (output->GetRequestedRegion().GetNumberOfPixels() == 0)
93  {
94  output->SetRequestedRegion(output->GetLargestPossibleRegion());
95  }
96  }
97 }
98 
99 template<class TInputImage, class TOutputImage>
100 void
103 {
104  // This is commented to prevent the streaming of the whole image for the first stream strip
105  // It shall not cause any problem because the output image of this filter is not intended to be used.
106  //InputImagePointer image = const_cast< TInputImage * >( this->GetInput() );
107  //this->GraftOutput( image );
108  // Nothing that needs to be allocated for the remaining outputs
109 }
110 
111 
112 template<class TInputImage, class TOutputImage>
113 void
116 {
117  // Get pointers to the input and output
118  InputImageType* inputPtr = const_cast<InputImageType*>(this->GetInput());
119  inputPtr->UpdateOutputInformation();
120 
121  m_ShrinkedOutput = OutputImageType::New();
122  m_ShrinkedOutput->CopyInformation(inputPtr);
123 
124  const typename InputImageType::SpacingType&
125  inputSpacing = inputPtr->GetSpacing();
126  const typename InputImageType::SizeType& inputSize
127  = inputPtr->GetLargestPossibleRegion().GetSize();
128 
129  typename OutputImageType::SpacingType shrinkedOutputSpacing;
130  typename OutputImageType::RegionType shrinkedOutputLargestPossibleRegion;
131  typename OutputImageType::SizeType shrinkedOutputSize;
132  typename OutputImageType::IndexType shrinkedOutputStartIndex;
133  typename OutputImageType::PointType shrinkedOutputOrigin;
134 
135  for (unsigned int i = 0; i < OutputImageType::ImageDimension; ++i)
136  {
137  shrinkedOutputSpacing[i] = inputSpacing[i] * static_cast<double>(m_ShrinkFactor);
138  shrinkedOutputSize[i] = inputSize[i] > m_ShrinkFactor ? inputSize[i] / m_ShrinkFactor : 1;
139 
140  shrinkedOutputOrigin[i] = inputPtr->GetOrigin()[i] + inputSpacing[i] *
141  (static_cast<double>(inputPtr->GetLargestPossibleRegion().GetIndex(i)) - 0.5)
142  + shrinkedOutputSpacing[i] * 0.5;
143 
144  // we choose to output a region with a start index [0,0]
145  // the origin is set accordingly
146  shrinkedOutputStartIndex[i] = 0;
147  }
148 
149  m_ShrinkedOutput->SetSpacing(shrinkedOutputSpacing);
150  m_ShrinkedOutput->SetOrigin(shrinkedOutputOrigin);
151 
152  shrinkedOutputLargestPossibleRegion.SetSize(shrinkedOutputSize);
153  shrinkedOutputLargestPossibleRegion.SetIndex(shrinkedOutputStartIndex);
154 
155  m_ShrinkedOutput->SetRegions(shrinkedOutputLargestPossibleRegion);
156  m_ShrinkedOutput->Allocate();
157 }
158 
159 template<class TInputImage, class TOutputImage>
160 void
163 {
164 }
165 
166 template<class TInputImage, class TOutputImage>
167 void
170 {
171 }
172 
173 template<class TInputImage, class TOutputImage>
174 void
176 ::ThreadedGenerateData(const RegionType& outputRegionForThread, itk::ThreadIdType threadId)
177 {
178  //std::cout << "outputRegionForThread " << threadId << " " << outputRegionForThread << std::endl;
179  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
180  const InputImageType* inputPtr = this->GetInput();
181 
182  itk::ImageRegionConstIteratorWithIndex<InputImageType> inIt(inputPtr, outputRegionForThread);
183  for(inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt, progress.CompletedPixel())
184  {
185  const IndexType& inIndex = inIt.GetIndex();
186  // TODO the pixel value should be taken near the centre of the cell, not at the corners
187  if (inIndex[0] % m_ShrinkFactor == 0
188  && inIndex[1] % m_ShrinkFactor == 0 )
189  {
190  IndexType shrinkedIndex;
191  shrinkedIndex[0] = inIndex[0] / m_ShrinkFactor;
192  shrinkedIndex[1] = inIndex[1] / m_ShrinkFactor;
193  if (m_ShrinkedOutput->GetLargestPossibleRegion().IsInside(shrinkedIndex))
194  m_ShrinkedOutput->SetPixel(shrinkedIndex, inIt.Get());
195  }
196  }
197 }
198 
199 template<class TInputImage, class TOutputImage>
200 void
203 {
204 }
205 
206 template <class TImage, class TOutputImage>
207 void
209 ::PrintSelf(std::ostream& os, itk::Indent indent) const
210 {
211  Superclass::PrintSelf(os, indent);
212  os << indent << "Shrink factor: " << m_ShrinkFactor << std::endl;
213 }
214 
215 } // End namespace otb
216 #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
TOutputImage OutputImageType
unsigned int ThreadIdType
#define otbMsgDevMacro(x)
Definition: otbMacro.h:95