OTB  9.0.0
Orfeo Toolbox
otbStreamingResampleImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbStreamingResampleImageFilter_hxx
22 #define otbStreamingResampleImageFilter_hxx
23 
25 #include "itkProgressAccumulator.h"
26 #include "otbImage.h"
27 
28 namespace otb
29 {
30 
31 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
33 {
34  // internal filters instantiation
35  m_DisplacementFilter = DisplacementFieldGeneratorType::New();
36  m_WarpFilter = WarpImageFilterType::New();
37  m_SignedOutputSpacing = m_DisplacementFilter->GetOutputSpacing();
38  // Initialize the displacement field spacing to zero : inconsistent
39  // value
40  this->SetDisplacementFieldSpacing(itk::NumericTraits<SpacingType>::ZeroValue());
41 
42  // Wire minipipeline
43  m_WarpFilter->SetDisplacementField(m_DisplacementFilter->GetOutput());
44 }
45 
46 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
48 {
49  // Set up progress reporting
50  typename itk::ProgressAccumulator::Pointer progress = itk::ProgressAccumulator::New();
51  progress->SetMiniPipelineFilter(this);
52  progress->RegisterInternalFilter(m_WarpFilter, 1.f);
53 
54  m_WarpFilter->GraftOutput(this->GetOutput());
55  m_WarpFilter->UpdateOutputData(m_WarpFilter->GetOutput());
56  this->GraftOutput(m_WarpFilter->GetOutput());
57 }
58 
62 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
64 {
65  // check the output spacing of the displacement field
66  if (this->GetDisplacementFieldSpacing() == itk::NumericTraits<SpacingType>::ZeroValue())
67  {
68  this->SetDisplacementFieldSpacing(2. * this->GetOutputSpacing());
69  }
70 
71  // Retrieve output largest region
72  SizeType largestSize = this->GetOutputSize();
73 
74  // Set up displacement field filter
75  SizeType displacementFieldLargestSize;
76 
77  for (unsigned int dim = 0; dim < InputImageType::ImageDimension; ++dim)
78  {
79  // std::ceil to avoid numerical problems due to division of
80  // spacings
81  // + 1 : We need to enlarge the displacement field size cause
82  // itk::WarpImageFilter::EvaluateDisplacementAtPhysicalPoint needs
83  // 4 neighbors and in the edges we can need 1 neighbor pixel
84  // outside the field
85  displacementFieldLargestSize[dim] =
86  static_cast<unsigned int>(std::ceil(largestSize[dim] * std::abs(this->GetOutputSpacing()[dim] / this->GetDisplacementFieldSpacing()[dim]))) + 1;
87  }
88  m_DisplacementFilter->SetOutputSize(displacementFieldLargestSize);
89  m_DisplacementFilter->SetOutputIndex(this->GetOutputStartIndex());
90 
91  m_WarpFilter->SetInput(this->GetInput());
92  m_WarpFilter->GraftOutput(this->GetOutput());
93  m_WarpFilter->UpdateOutputInformation();
94  this->GraftOutput(m_WarpFilter->GetOutput());
95 }
96 
97 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
99 {
100  if (this->m_Updating)
101  return;
102 
103  m_WarpFilter->GetOutput()->SetRequestedRegion(output);
104  m_WarpFilter->GetOutput()->PropagateRequestedRegion();
105 }
106 
111 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
113 {
114  this->SetOutputOrigin(image->GetOrigin());
115  this->SetOutputSpacing(internal::GetSignedSpacing(image));
116  this->SetOutputStartIndex(image->GetLargestPossibleRegion().GetIndex());
117  this->SetOutputSize(image->GetLargestPossibleRegion().GetSize());
118 }
120 
121 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
123 {
124  m_SignedOutputSpacing = outputSpacing;
125  typename TInputImage::DirectionType direction = this->m_DisplacementFilter->GetOutputDirection();
126  for (unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
127  {
128  if (outputSpacing[i] < 0)
129  {
130  if (direction[i][i] > 0)
131  {
132  for (unsigned int j = 0; j < TInputImage::ImageDimension; ++j)
133  {
134  direction[j][i] = -direction[j][i];
135  }
136  }
137  outputSpacing[i] = -outputSpacing[i];
138  }
139  }
140  this->m_DisplacementFilter->SetOutputSpacing(outputSpacing);
141  this->m_DisplacementFilter->SetOutputDirection(direction);
142  this->Modified();
143 }
144 
145 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
147 {
148  Superclass::PrintSelf(os, indent);
149  os << indent << "OutputOrigin: " << this->GetOutputOrigin() << std::endl;
150  os << indent << "OutputSpacing: " << this->GetOutputSpacing() << std::endl;
151  os << indent << "OutputStartIndex: " << this->GetOutputStartIndex() << std::endl;
152  os << indent << "OutputSize: " << this->GetOutputSize() << std::endl;
153 }
154 }
155 #endif
otbStreamingResampleImageFilter.h
otbImage.h
otb::StreamingResampleImageFilter::SetDisplacementFieldSpacing
void SetDisplacementFieldSpacing(SpacingType spacing)
Definition: otbStreamingResampleImageFilter.hxx:122
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::StreamingResampleImageFilter::SpacingType
DisplacementFieldGeneratorType::SpacingType SpacingType
Definition: otbStreamingResampleImageFilter.h:89
otb::StreamingResampleImageFilter::ImageBaseType
itk::ImageBase< OutputImageType::ImageDimension > ImageBaseType
Definition: otbStreamingResampleImageFilter.h:100
otb::StreamingResampleImageFilter::StreamingResampleImageFilter
StreamingResampleImageFilter()
Definition: otbStreamingResampleImageFilter.hxx:32
otb::StreamingResampleImageFilter::GenerateData
void GenerateData() override
Definition: otbStreamingResampleImageFilter.hxx:47
otb::StreamingResampleImageFilter::SetOutputParametersFromImage
void SetOutputParametersFromImage(const ImageBaseType *image)
Definition: otbStreamingResampleImageFilter.hxx:112
otb::StreamingResampleImageFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbStreamingResampleImageFilter.hxx:63
otb::internal::GetSignedSpacing
ImageType::SpacingType GetSignedSpacing(const ImageType *input)
Definition: otbImage.h:41
otb::StreamingResampleImageFilter::PropagateRequestedRegion
void PropagateRequestedRegion(itk::DataObject *output) override
Definition: otbStreamingResampleImageFilter.hxx:98
otb::StreamingResampleImageFilter::SizeType
DisplacementFieldGeneratorType::SizeType SizeType
Definition: otbStreamingResampleImageFilter.h:88
otb::StreamingResampleImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbStreamingResampleImageFilter.hxx:146