OTB  9.0.0
Orfeo Toolbox
otbVectorRescaleIntensityImageFilter.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 otbVectorRescaleIntensityImageFilter_hxx
23 #define otbVectorRescaleIntensityImageFilter_hxx
24 
26 #include "itkSampleToHistogramFilter.h"
27 #include "itkHistogram.h"
28 #include "itkListSample.h"
29 #include "otbObjectList.h"
30 #include "otbMacro.h"
31 #include "itkImageRegionConstIterator.h"
32 
33 namespace otb
34 {
38 template <class TInputImage, class TOutputImage>
40 {
41  m_ClampThreshold = 0.01;
42  m_Gamma = 1.0;
43  m_AutomaticInputMinMaxComputation = true;
44 }
45 
49 template <class TInputImage, class TOutputImage>
51 {
52  this->Superclass::GenerateOutputInformation();
53  this->GetOutput()->SetNumberOfComponentsPerPixel(this->GetInput()->GetNumberOfComponentsPerPixel());
54 }
55 
59 template <class TInputImage, class TOutputImage>
61 {
62  if (this->GetInput())
63  {
64  typename TInputImage::Pointer input = const_cast<TInputImage*>(this->GetInput());
65  typename TInputImage::RegionType inputRegion;
66  this->CallCopyOutputRegionToInputRegion(inputRegion, this->GetOutput()->GetRequestedRegion());
67  input->SetRequestedRegion(inputRegion);
68  }
69 }
70 
74 template <class TInputImage, class TOutputImage>
76 {
77  if (m_ClampThreshold < 0.)
78  {
79  itkExceptionMacro(<< "Invalid Clamp Threshold must be greater than 0.0");
80  }
81 
82  if (m_AutomaticInputMinMaxComputation)
83  {
84 
85  // typedef typename Superclass::InputImageType InputImageType;
86  // typedef typename Superclass::InputImagePointer InputImagePointer;
87 
88  // Get the input
89  InputImagePointer inputImage = this->GetInput();
90 
91  typedef itk::ImageRegionConstIterator<InputImageType> InputIterator;
92  typedef itk::Vector<typename InputImageType::InternalPixelType, 1> MeasurementVectorType;
93  typedef itk::Statistics::ListSample<MeasurementVectorType> ListSampleType;
94  typedef float HistogramMeasurementType;
95  typedef itk::Statistics::Histogram<HistogramMeasurementType, itk::Statistics::DenseFrequencyContainer2> HistogramType;
96  typedef itk::Statistics::SampleToHistogramFilter<ListSampleType, HistogramType> HistogramGeneratorType;
97 
98  typedef ObjectList<ListSampleType> ListSampleListType;
99 
100  m_InputMinimum.SetSize(inputImage->GetNumberOfComponentsPerPixel());
101  m_InputMaximum.SetSize(inputImage->GetNumberOfComponentsPerPixel());
102  typename ListSampleListType::Pointer sl = ListSampleListType::New();
103 
104  sl->Reserve(inputImage->GetNumberOfComponentsPerPixel());
105 
106  for (unsigned int i = 0; i < m_InputMaximum.GetSize(); ++i)
107  {
108  sl->PushBack(ListSampleType::New());
109  }
110 
111  InputIterator it(inputImage, inputImage->GetBufferedRegion());
112 
113  it.GoToBegin();
114 
115  while (!it.IsAtEnd())
116  {
117  InputPixelType pixel = it.Get();
118  for (unsigned int i = 0; i < m_InputMaximum.GetSize(); ++i)
119  {
120  sl->GetNthElement(i)->PushBack(pixel[i]);
121  }
122  ++it;
123  }
124 
125  for (unsigned int i = 0; i < m_InputMaximum.GetSize(); ++i)
126  {
127  typename HistogramGeneratorType::Pointer generator = HistogramGeneratorType::New();
128  generator->SetInput(sl->GetNthElement(i));
129  typename HistogramGeneratorType::HistogramType::SizeType size;
130 
131  // Initialize the size of the SizeType size
132  size.SetSize(sl->GetNthElement(i)->GetMeasurementVectorSize());
133 
134  if (m_ClampThreshold > 0.)
135  size.Fill(static_cast<unsigned int>(std::ceil(1 / m_ClampThreshold) * 10));
136  else
137  size.Fill(256);
138 
139  generator->SetHistogramSize(size);
140 
141  generator->Update();
142  m_InputMinimum[i] = static_cast<typename InputImageType::InternalPixelType>(generator->GetOutput()->Quantile(0, m_ClampThreshold));
143  m_InputMaximum[i] = static_cast<typename InputImageType::InternalPixelType>(generator->GetOutput()->Quantile(0, 1 - m_ClampThreshold));
144  }
145  }
146 
147  // set up the functor values
148  this->GetFunctor().SetInputMinimum(m_InputMinimum);
149  this->GetFunctor().SetInputMaximum(m_InputMaximum);
150  this->GetFunctor().SetOutputMaximum(m_OutputMaximum);
151  this->GetFunctor().SetOutputMinimum(m_OutputMinimum);
152  this->GetFunctor().SetGamma(m_Gamma);
153 }
154 
otb::VectorRescaleIntensityImageFilter::VectorRescaleIntensityImageFilter
VectorRescaleIntensityImageFilter()
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbMacro.h
otb::VectorRescaleIntensityImageFilter::GenerateOutputInformation
void GenerateOutputInformation(void) override
otb::VectorRescaleIntensityImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData(void) override
otbObjectList.h
otbVectorRescaleIntensityImageFilter.h
otb::VectorRescaleIntensityImageFilter::GenerateInputRequestedRegion
void GenerateInputRequestedRegion(void) override