OTB  9.0.0
Orfeo Toolbox
otbSimpleRcsPanSharpeningFusionImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbSimpleRcsPanSharpeningFusionImageFilter_hxx
23 #define otbSimpleRcsPanSharpeningFusionImageFilter_hxx
24 
26 #include "otbNoDataHelper.h"
27 
28 namespace otb
29 {
30 template <class TPanImageType, class TXsImageType, class TOutputImageType, class TInternalPrecision>
32 {
33  // Fix number of required inputs
34  this->SetNumberOfRequiredInputs(2);
35  this->m_UseNoData = false;
36 
37  // Instantiate convolution filter
38  m_ConvolutionFilter = ConvolutionFilterType::New();
39  m_ConvolutionFilter->NormalizeFilterOn();
40 
41  // Set-up default parameters
42  m_Radius.Fill(9);
43  m_Filter.SetSize((2 * m_Radius[0] + 1) * (2 * m_Radius[1] + 1));
44  m_Filter.Fill(1);
45 }
46 
47 template <class TPanImageType, class TXsImageType, class TOutputImageType, class TInternalPrecision>
49 {
50  // We have 2 inputs: an image and a vector image
51 
52  // Process object is not const-correct so the const_cast is required here
53  this->itk::ProcessObject::SetNthInput(1, const_cast<TPanImageType*>(image));
54  this->Modified();
55 }
56 
57 template <class TPanImageType, class TXsImageType, class TOutputImageType, class TInternalPrecision>
59 {
60  if (this->GetNumberOfInputs() < 2)
61  {
62  return nullptr;
63  }
64 
65  return static_cast<const TPanImageType*>(this->itk::ProcessObject::GetInput(1));
66 }
67 
68 template <class TPanImageType, class TXsImageType, class TOutputImageType, class TInternalPrecision>
70 {
71  // We have 2 inputs: an image and a vector image
72 
73  // Process object is not const-correct so the const_cast is required here
74  this->itk::ProcessObject::SetNthInput(0, const_cast<TXsImageType*>(image));
75  this->Modified();
76 }
77 
78 template <class TPanImageType, class TXsImageType, class TOutputImageType, class TInternalPrecision>
80 {
81  if (this->GetNumberOfInputs() < 1)
82  {
83  return nullptr;
84  }
85 
86  return static_cast<const TXsImageType*>(this->itk::ProcessObject::GetInput(0));
87 }
88 
89 template <class TPanImageType, class TXsImageType, class TOutputImageType, class TInternalPrecision>
91 {
92  // Check if size is correct
93  typename TPanImageType::SizeType sizePan;
94  typename TXsImageType::SizeType sizeXs;
95  sizePan = this->GetPanInput()->GetLargestPossibleRegion().GetSize();
96  sizeXs = this->GetXsInput()->GetLargestPossibleRegion().GetSize();
97  if ((sizePan[0] != sizeXs[0]) || (sizePan[1] != sizeXs[1]))
98  {
99  itkExceptionMacro(<< "SimpleRcsPanSharpeningFusionImageFilter: Wrong Pan/Xs size");
100  }
101 
102  // Set-up progress reporting
103  m_ProgressAccumulator = itk::ProgressAccumulator::New();
104  m_ProgressAccumulator->SetMiniPipelineFilter(this);
105  m_ProgressAccumulator->RegisterInternalFilter(m_ConvolutionFilter, 0.9);
106 
107  m_ConvolutionFilter->SetInput(this->GetPanInput());
108  m_ConvolutionFilter->SetRadius(this->m_Radius);
109  m_ConvolutionFilter->SetFilter(this->m_Filter);
110 
111  typedef typename TPanImageType::InternalPixelType PanPixelType;
112  typedef typename TXsImageType::InternalPixelType XsPixelType;
113 
114  // Write no-data flags for Pan image
115  std::vector<bool> tmpNoDataValuePanAvailable;
116  std::vector<double> tmpNoDataValuePan;
117  bool noDataValuePanAvailable = false;
118  PanPixelType noDataValuePan = 0;
119 
120  bool retPan = ReadNoDataFlags(this->GetPanInput()->GetImageMetadata(), tmpNoDataValuePanAvailable, tmpNoDataValuePan);
121 
122  if (retPan && tmpNoDataValuePanAvailable.size() > 0 && tmpNoDataValuePan.size() > 0)
123  {
124  noDataValuePanAvailable = tmpNoDataValuePanAvailable[0] && retPan;
125  noDataValuePan = static_cast<PanPixelType>(tmpNoDataValuePan[0]);
126  }
127 
128  // Write no-data flags for Xs image
129  std::vector<bool> noDataValuesXsAvailable;
130  std::vector<double> tmpNoDataValuesXs;
131  std::vector<XsPixelType> noDataValuesXs;
132 
133  bool retXs = ReadNoDataFlags(this->GetXsInput()->GetImageMetadata(), noDataValuesXsAvailable, tmpNoDataValuesXs);
134 
135  // Check if noData is needed and update noDataValuesAvailable with return function value
136  if (retPan || retXs)
137  {
138  m_UseNoData = noDataValuePanAvailable;
139  for (unsigned int i = 0; i < tmpNoDataValuesXs.size() && i < noDataValuesXsAvailable.size(); ++i)
140  {
141  noDataValuesXs.push_back(static_cast<XsPixelType>(tmpNoDataValuesXs[i]));
142  m_UseNoData |= (noDataValuesXsAvailable[i] = (noDataValuesXsAvailable[i] && retXs));
143  }
144  }
145 
146  // Instantiate fusion filter
147  if (m_UseNoData)
148  {
149  m_NoDataFusionFilter = NoDataFusionFilterType::New();
150  m_ProgressAccumulator->RegisterInternalFilter(m_NoDataFusionFilter, 0.1);
151 
152  m_NoDataFusionFilter->SetInput2(m_ConvolutionFilter->GetOutput());
153  m_NoDataFusionFilter->GetModifiableFunctor().SetNoDataValuesXsAvailable(noDataValuesXsAvailable);
154  m_NoDataFusionFilter->GetModifiableFunctor().SetNoDataValuePanAvailable(noDataValuePanAvailable);
155  m_NoDataFusionFilter->GetModifiableFunctor().SetNoDataValuePan(noDataValuePan);
156  m_NoDataFusionFilter->GetModifiableFunctor().SetNoDataValuesXs(noDataValuesXs);
157 
158  m_NoDataFusionFilter->SetInput1(this->GetXsInput());
159  m_NoDataFusionFilter->SetInput2(m_ConvolutionFilter->GetOutput());
160  m_NoDataFusionFilter->SetInput3(this->GetPanInput());
161 
162  // Wire composite filter
163  m_NoDataFusionFilter->GraftOutput(this->GetOutput());
164  m_NoDataFusionFilter->Update();
165  this->GraftOutput(m_NoDataFusionFilter->GetOutput());
166  }
167  else
168  {
169  m_FusionFilter = FusionFilterType::New();
170  m_ProgressAccumulator->RegisterInternalFilter(m_FusionFilter, 0.1);
171 
172  m_FusionFilter->SetInput1(this->GetXsInput());
173  m_FusionFilter->SetInput2(m_ConvolutionFilter->GetOutput());
174  m_FusionFilter->SetInput3(this->GetPanInput());
175 
176  // Wire composite filter
177  m_FusionFilter->GraftOutput(this->GetOutput());
178  m_FusionFilter->Update();
179  this->GraftOutput(m_FusionFilter->GetOutput());
180  }
181 }
182 
183 template <class TPanImageType, class TXsImageType, class TOutputImageType, class TInternalPrecision>
185  itk::Indent indent) const
186 {
187  Superclass::PrintSelf(os, indent);
188  os << indent << "Radius:" << this->m_Radius << std::endl;
189 }
190 
191 } // end namespace otb
192 
193 #endif
otb::SimpleRcsPanSharpeningFusionImageFilter::SetPanInput
virtual void SetPanInput(const TPanImageType *image)
Definition: otbSimpleRcsPanSharpeningFusionImageFilter.hxx:48
otb::SimpleRcsPanSharpeningFusionImageFilter::GetXsInput
const TXsImageType * GetXsInput(void) const
Definition: otbSimpleRcsPanSharpeningFusionImageFilter.hxx:79
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ReadNoDataFlags
bool OTBMetadata_EXPORT ReadNoDataFlags(const ImageMetadata &imd, std::vector< bool > &flags, std::vector< double > &values)
otbNoDataHelper.h
otbSimpleRcsPanSharpeningFusionImageFilter.h
otb::SimpleRcsPanSharpeningFusionImageFilter::GetPanInput
const TPanImageType * GetPanInput(void) const
Definition: otbSimpleRcsPanSharpeningFusionImageFilter.hxx:58
otb::SimpleRcsPanSharpeningFusionImageFilter::GenerateData
void GenerateData() override
Definition: otbSimpleRcsPanSharpeningFusionImageFilter.hxx:90
otb::SimpleRcsPanSharpeningFusionImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbSimpleRcsPanSharpeningFusionImageFilter.hxx:184
otb::SimpleRcsPanSharpeningFusionImageFilter::SimpleRcsPanSharpeningFusionImageFilter
SimpleRcsPanSharpeningFusionImageFilter()
Definition: otbSimpleRcsPanSharpeningFusionImageFilter.hxx:31
otb::SimpleRcsPanSharpeningFusionImageFilter::SetXsInput
virtual void SetXsInput(const TXsImageType *path)
Definition: otbSimpleRcsPanSharpeningFusionImageFilter.hxx:69