21 #ifndef otbApplyGainFilter_hxx
22 #define otbApplyGainFilter_hxx
25 #include "itkImageRegionIterator.h"
26 #include "itkImageRegionConstIteratorWithIndex.h"
27 #include "itkContinuousIndex.h"
33 template <
class TInputImage,
class TLut,
class TOutputImage>
36 this->SetNumberOfRequiredInputs(2);
37 m_Min = std::numeric_limits<InputPixelType>::quiet_NaN();
38 m_Max = std::numeric_limits<InputPixelType>::quiet_NaN();
39 m_NoData = std::numeric_limits<InputPixelType>::quiet_NaN();
41 m_ThumbSizeFromSpacing =
true;
45 template <
class TInputImage,
class TLut,
class TOutputImage>
52 template <
class TInputImage,
class TLut,
class TOutputImage>
55 return static_cast<const InputImageType*
>(this->itk::ProcessObject::GetInput(0));
58 template <
class TInputImage,
class TLut,
class TOutputImage>
62 this->SetNthInput(1,
const_cast<LutType*
>(lut));
65 template <
class TInputImage,
class TLut,
class TOutputImage>
68 return static_cast<const LutType*
>(this->itk::ProcessObject::GetInput(1));
71 template <
class TInputImage,
class TLut,
class TOutputImage>
74 typename InputImageType::Pointer input(
const_cast<InputImageType*
>(GetInputImage()));
75 typename LutType::Pointer lut(
const_cast<LutType*
>(GetInputLut()));
76 typename OutputImageType::Pointer output(this->GetOutput());
78 lut->SetRequestedRegion(lut->GetLargestPossibleRegion());
79 input->SetRequestedRegion(output->GetRequestedRegion());
80 if (input->GetRequestedRegion().GetNumberOfPixels() == 0)
82 input->SetRequestedRegionToLargestPossibleRegion();
86 template <
class TInputImage,
class TLut,
class TOutputImage>
89 typename LutType::ConstPointer lut(GetInputLut());
90 typename InputImageType::ConstPointer input(GetInputImage());
91 if (m_ThumbSizeFromSpacing)
93 m_ThumbSize[0] = std::round(lut->GetSignedSpacing()[0] / input->GetSignedSpacing()[0]);
94 m_ThumbSize[1] = std::round(lut->GetSignedSpacing()[1] / input->GetSignedSpacing()[1]);
96 m_Step =
static_cast<double>(m_Max - m_Min) /
static_cast<double>(lut->GetVectorLength() - 1);
99 template <
class TInputImage,
class TLut,
class TOutputImage>
101 itk::ThreadIdType itkNotUsed(threadId))
109 typename InputImageType::ConstPointer input(GetInputImage());
110 typename LutType::ConstPointer lut(GetInputLut());
111 typename OutputImageType::Pointer output(this->GetOutput());
112 typename InputImageType::RegionType inputRegionForThread(outputRegionForThread);
115 itk::ImageRegionConstIteratorWithIndex<InputImageType> it(input, inputRegionForThread);
116 itk::ImageRegionIterator<OutputImageType> oit(output, outputRegionForThread);
118 unsigned int pixelLutValue(0);
119 double gain(0.0), newValue(0);
122 for (it.GoToBegin(), oit.GoToBegin(); !oit.IsAtEnd() || !it.IsAtEnd(); ++oit, ++it)
124 currentPixel = it.Get();
125 newValue =
static_cast<double>(currentPixel);
126 if (!((currentPixel == m_NoData && m_NoDataFlag) || currentPixel > m_Max || currentPixel < m_Min))
128 pixelLutValue =
static_cast<unsigned int>(std::round((currentPixel - m_Min) / m_Step));
129 gain = InterpolateGain(lut, pixelLutValue, it.GetIndex());
134 assert(oit.IsAtEnd() && it.IsAtEnd());
137 template <
class TInputImage,
class TLut,
class TOutputImage>
139 typename InputImageType::IndexType index)
141 typename InputImageType::PointType pixelPoint;
142 typename itk::ContinuousIndex<double, 2> pixelIndex;
143 typename InputImageType::ConstPointer input(GetInputImage());
144 typename LutType::ConstPointer lut(GetInputLut());
145 input->TransformIndexToPhysicalPoint(index, pixelPoint);
146 lut->TransformPhysicalPointToContinuousIndex(pixelPoint, pixelIndex);
147 std::vector<typename LutType::IndexType> neighbors(4);
148 neighbors[0][0] = std::floor(pixelIndex[0]);
149 neighbors[0][1] = std::floor(pixelIndex[1]);
150 neighbors[1][0] = neighbors[0][0] + 1;
151 neighbors[1][1] = neighbors[0][1];
152 neighbors[2][0] = neighbors[0][0];
153 neighbors[2][1] = neighbors[0][1] + 1;
154 neighbors[3][0] = neighbors[0][0] + 1;
155 neighbors[3][1] = neighbors[0][1] + 1;
156 float gain(0.f), w(0.f), wtm(0.f);
157 typename LutType::IndexType maxIndex;
158 maxIndex[0] = lut->GetLargestPossibleRegion().GetSize()[0];
159 maxIndex[1] = lut->GetLargestPossibleRegion().GetSize()[1];
160 for (
auto i : neighbors)
162 if (i[0] < 0 || i[1] < 0 || i[0] >= maxIndex[0] || i[1] >= maxIndex[1])
164 if (gridLut->GetPixel(i)[pixelLutValue] == -1)
166 wtm = (1 - std::abs(pixelIndex[0] - i[0])) * (1 - std::abs(pixelIndex[1] - i[1]));
167 gain += gridLut->GetPixel(i)[pixelLutValue] * wtm;
182 template <
class TInputImage,
class TLut,
class TOutputImage>
185 Superclass::PrintSelf(os, indent);
186 os << indent <<
"Is no data activated : " << m_NoDataFlag << std::endl;
187 os << indent <<
"No Data : " << m_NoData << std::endl;
188 os << indent <<
"Minimum : " << m_Min << std::endl;
189 os << indent <<
"Maximum : " << m_Max << std::endl;
190 os << indent <<
"Step : " << m_Step << std::endl;
191 os << indent <<
"Look up table size : " << m_LutSize << std::endl;
192 os << indent <<
"Is ThumbSize from sapcing is activated : " << m_NoDataFlag << std::endl;
193 os << indent <<
"Thumbnail size : " << m_ThumbSize << std::endl;