File: otbStreamingShrinkImageFilter.hxx

package info (click to toggle)
otb 7.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,005,476 kB
  • sloc: cpp: 270,143; xml: 128,722; ansic: 4,367; sh: 1,768; python: 1,084; perl: 92; makefile: 72
file content (198 lines) | stat: -rw-r--r-- 7,416 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef otbStreamingShrinkImageFilter_hxx
#define otbStreamingShrinkImageFilter_hxx

#include "otbStreamingShrinkImageFilter.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "otbMacro.h"
#include "itkProgressReporter.h"

namespace otb
{

template <class TImage>
StreamingShrinkStreamingManager<TImage>::StreamingShrinkStreamingManager() : m_ShrinkFactor(10)
{
}

template <class TImage>
StreamingShrinkStreamingManager<TImage>::~StreamingShrinkStreamingManager()
{
}

template <class TImage>
void StreamingShrinkStreamingManager<TImage>::PrepareStreaming(itk::DataObject* input, const RegionType& region)
{
  typedef otb::StreamingShrinkImageRegionSplitter TileSplitterType;
  TileSplitterType::Pointer                       splitter = TileSplitterType::New();
  splitter->SetShrinkFactor(m_ShrinkFactor);
  this->m_Splitter = splitter;

  unsigned long nbDivisions      = this->EstimateOptimalNumberOfDivisions(input, region, 0);
  this->m_ComputedNumberOfSplits = this->m_Splitter->GetNumberOfSplits(region, nbDivisions);
  otbMsgDevMacro(<< "Number of split : " << this->m_ComputedNumberOfSplits)

      // Save the region to generate the splits later
      this->m_Region = region;
}


/** Constructor */
template <class TInputImage, class TOutputImage>
PersistentShrinkImageFilter<TInputImage, TOutputImage>::PersistentShrinkImageFilter() : m_ShrinkFactor(10)
{
  this->SetNumberOfRequiredInputs(1);
  this->SetNumberOfRequiredOutputs(1);
}

/** Destructor */
template <class TInputImage, class TOutputImage>
PersistentShrinkImageFilter<TInputImage, TOutputImage>::~PersistentShrinkImageFilter()
{
}

template <class TInputImage, class TOutputImage>
void PersistentShrinkImageFilter<TInputImage, TOutputImage>::GenerateOutputInformation()
{
  Superclass::GenerateOutputInformation();

  const InputImageType* input = this->GetInput();

  OutputImageType* output = this->GetOutput();

  if (input)
  {
    output->CopyInformation(input);
    output->SetLargestPossibleRegion(input->GetLargestPossibleRegion());

    if (output->GetRequestedRegion().GetNumberOfPixels() == 0)
    {
      output->SetRequestedRegion(output->GetLargestPossibleRegion());
    }
  }
}

template <class TInputImage, class TOutputImage>
void PersistentShrinkImageFilter<TInputImage, TOutputImage>::AllocateOutputs()
{
  // This is commented to prevent the streaming of the whole image for the first stream strip
  // It shall not cause any problem because the output image of this filter is not intended to be used.
  // InputImagePointer image = const_cast< TInputImage * >( this->GetInput() );
  // this->GraftOutput( image );
  // Nothing that needs to be allocated for the remaining outputs
}


template <class TInputImage, class TOutputImage>
void PersistentShrinkImageFilter<TInputImage, TOutputImage>::Reset()
{
  // Get pointers to the input and output
  InputImageType* inputPtr = const_cast<InputImageType*>(this->GetInput());
  inputPtr->UpdateOutputInformation();

  m_ShrunkOutput = OutputImageType::New();
  m_ShrunkOutput->CopyInformation(inputPtr);

  const typename InputImageType::SpacingType& inputSpacing = inputPtr->GetSignedSpacing();
  const typename InputImageType::SizeType&    inputSize    = inputPtr->GetLargestPossibleRegion().GetSize();
  const typename InputImageType::IndexType&   inputIndex   = inputPtr->GetLargestPossibleRegion().GetIndex();
  typename InputImageType::IndexType          startIndex;

  typename OutputImageType::SpacingType shrunkOutputSpacing;
  typename OutputImageType::RegionType  shrunkOutputLargestPossibleRegion;
  typename OutputImageType::SizeType    shrunkOutputSize;
  typename OutputImageType::IndexType   shrunkOutputStartIndex;
  typename OutputImageType::PointType   shrunkOutputOrigin;

  for (unsigned int i = 0; i < OutputImageType::ImageDimension; ++i)
  {
    startIndex[i] = inputIndex[i] + (m_ShrinkFactor - 1) / 2;
    if (m_ShrinkFactor > inputSize[i])
      startIndex[i]        = inputIndex[i] + (inputSize[i] - 1) / 2;
    m_Offset[i]            = startIndex[i] % m_ShrinkFactor;
    shrunkOutputSpacing[i] = inputSpacing[i] * static_cast<double>(m_ShrinkFactor);
    shrunkOutputSize[i]    = inputSize[i] > m_ShrinkFactor ? inputSize[i] / m_ShrinkFactor : 1;

    shrunkOutputOrigin[i] = inputPtr->GetOrigin()[i] + inputSpacing[i] * startIndex[i];

    // we choose to output a region with a start index [0,0]
    // the origin is set accordingly
    shrunkOutputStartIndex[i] = 0;
  }

  m_ShrunkOutput->SetSignedSpacing(shrunkOutputSpacing);
  m_ShrunkOutput->SetOrigin(shrunkOutputOrigin);

  shrunkOutputLargestPossibleRegion.SetSize(shrunkOutputSize);
  shrunkOutputLargestPossibleRegion.SetIndex(shrunkOutputStartIndex);

  m_ShrunkOutput->SetRegions(shrunkOutputLargestPossibleRegion);
  m_ShrunkOutput->Allocate();
}

template <class TInputImage, class TOutputImage>
void PersistentShrinkImageFilter<TInputImage, TOutputImage>::Synthetize()
{
}

template <class TInputImage, class TOutputImage>
void PersistentShrinkImageFilter<TInputImage, TOutputImage>::BeforeThreadedGenerateData()
{
}

template <class TInputImage, class TOutputImage>
void PersistentShrinkImageFilter<TInputImage, TOutputImage>::ThreadedGenerateData(const RegionType& outputRegionForThread, itk::ThreadIdType threadId)
{
  // std::cout << "outputRegionForThread " << threadId << "  " << outputRegionForThread << std::endl;
  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
  const InputImageType* inputPtr = this->GetInput();

  itk::ImageRegionConstIteratorWithIndex<InputImageType> inIt(inputPtr, outputRegionForThread);
  for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt, progress.CompletedPixel())
  {
    const IndexType& inIndex = inIt.GetIndex();
    // TODO the pixel value should be taken near the centre of the cell, not at the corners
    if ((inIndex[0] - m_Offset[0]) % m_ShrinkFactor == 0 && (inIndex[1] - m_Offset[1]) % m_ShrinkFactor == 0)
    {
      IndexType shrunkIndex;
      shrunkIndex[0] = (inIndex[0] - m_Offset[0]) / m_ShrinkFactor;
      shrunkIndex[1] = (inIndex[1] - m_Offset[1]) / m_ShrinkFactor;
      if (m_ShrunkOutput->GetLargestPossibleRegion().IsInside(shrunkIndex))
        m_ShrunkOutput->SetPixel(shrunkIndex, inIt.Get());
    }
  }
}

template <class TInputImage, class TOutputImage>
void PersistentShrinkImageFilter<TInputImage, TOutputImage>::AfterThreadedGenerateData()
{
}

template <class TImage, class TOutputImage>
void PersistentShrinkImageFilter<TImage, TOutputImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
{
  Superclass::PrintSelf(os, indent);
  os << indent << "Shrink factor: " << m_ShrinkFactor << std::endl;
}

} // End namespace otb
#endif