Orfeo Toolbox  4.2
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 }
66 
68 template <class TInputImage, class TOutputImage>
71 {
72 
73 }
74 
75 template<class TInputImage, class TOutputImage>
76 void
79 {
80  Superclass::GenerateOutputInformation();
81 
82  const InputImageType* input = this->GetInput();
83 
84  OutputImageType* output = this->GetOutput();
85 
86  if (input)
87  {
88  output->CopyInformation(input);
89  output->SetLargestPossibleRegion(input->GetLargestPossibleRegion());
90 
91  if (output->GetRequestedRegion().GetNumberOfPixels() == 0)
92  {
93  output->SetRequestedRegion(output->GetLargestPossibleRegion());
94  }
95  }
96 }
97 
98 template<class TInputImage, class TOutputImage>
99 void
102 {
103  // This is commented to prevent the streaming of the whole image for the first stream strip
104  // It shall not cause any problem because the output image of this filter is not intended to be used.
105  //InputImagePointer image = const_cast< TInputImage * >( this->GetInput() );
106  //this->GraftOutput( image );
107  // Nothing that needs to be allocated for the remaining outputs
108 }
109 
110 
111 template<class TInputImage, class TOutputImage>
112 void
115 {
116  // Get pointers to the input and output
117  InputImageType* inputPtr = const_cast<InputImageType*>(this->GetInput());
118  inputPtr->UpdateOutputInformation();
119 
120  m_ShrinkedOutput = OutputImageType::New();
121  m_ShrinkedOutput->CopyInformation(inputPtr);
122 
123  const typename InputImageType::SpacingType&
124  inputSpacing = inputPtr->GetSpacing();
125  const typename InputImageType::SizeType& inputSize
126  = inputPtr->GetLargestPossibleRegion().GetSize();
127 
128  typename OutputImageType::SpacingType shrinkedOutputSpacing;
129  typename OutputImageType::RegionType shrinkedOutputLargestPossibleRegion;
130  typename OutputImageType::SizeType shrinkedOutputSize;
131  typename OutputImageType::IndexType shrinkedOutputStartIndex;
132  typename OutputImageType::PointType shrinkedOutputOrigin;
133 
134  for (unsigned int i = 0; i < OutputImageType::ImageDimension; ++i)
135  {
136  shrinkedOutputSpacing[i] = inputSpacing[i] * static_cast<double>(m_ShrinkFactor);
137  shrinkedOutputSize[i] = inputSize[i] > m_ShrinkFactor ? inputSize[i] / m_ShrinkFactor : 1;
138 
139  shrinkedOutputOrigin[i] = inputPtr->GetOrigin()[i] + inputSpacing[i] *
140  (static_cast<double>(inputPtr->GetLargestPossibleRegion().GetIndex(i)) - 0.5)
141  + shrinkedOutputSpacing[i] * 0.5;
142 
143  // we choose to output a region with a start index [0,0]
144  // the origin is set accordingly
145  shrinkedOutputStartIndex[i] = 0;
146  }
147 
148  m_ShrinkedOutput->SetSpacing(shrinkedOutputSpacing);
149  m_ShrinkedOutput->SetOrigin(shrinkedOutputOrigin);
150 
151  shrinkedOutputLargestPossibleRegion.SetSize(shrinkedOutputSize);
152  shrinkedOutputLargestPossibleRegion.SetIndex(shrinkedOutputStartIndex);
153 
154  m_ShrinkedOutput->SetRegions(shrinkedOutputLargestPossibleRegion);
155  m_ShrinkedOutput->Allocate();
156 }
157 
158 template<class TInputImage, class TOutputImage>
159 void
162 {
163 }
164 
165 template<class TInputImage, class TOutputImage>
166 void
169 {
170 }
171 
172 template<class TInputImage, class TOutputImage>
173 void
175 ::ThreadedGenerateData(const RegionType& outputRegionForThread, itk::ThreadIdType threadId)
176 {
177  //std::cout << "outputRegionForThread " << threadId << " " << outputRegionForThread << std::endl;
178  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
179  const InputImageType* inputPtr = this->GetInput();
180 
181  itk::ImageRegionConstIteratorWithIndex<InputImageType> inIt(inputPtr, outputRegionForThread);
182  for(inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt, progress.CompletedPixel())
183  {
184  const IndexType& inIndex = inIt.GetIndex();
185  // TODO the pixel value should be taken near the centre of the cell, not at the corners
186  if (inIndex[0] % m_ShrinkFactor == 0
187  && inIndex[1] % m_ShrinkFactor == 0 )
188  {
189  IndexType shrinkedIndex;
190  shrinkedIndex[0] = inIndex[0] / m_ShrinkFactor;
191  shrinkedIndex[1] = inIndex[1] / m_ShrinkFactor;
192  if (m_ShrinkedOutput->GetLargestPossibleRegion().IsInside(shrinkedIndex))
193  m_ShrinkedOutput->SetPixel(shrinkedIndex, inIt.Get());
194  }
195  }
196 }
197 
198 template<class TInputImage, class TOutputImage>
199 void
202 {
203 }
204 
205 template <class TImage, class TOutputImage>
206 void
208 ::PrintSelf(std::ostream& os, itk::Indent indent) const
209 {
210  Superclass::PrintSelf(os, indent);
211  os << indent << "Shrink factor: " << m_ShrinkFactor << std::endl;
212 }
213 
214 } // End namespace otb
215 #endif

Generated at Sat Aug 30 2014 16:27:19 for Orfeo Toolbox with doxygen 1.8.3.1