OTB  9.0.0
Orfeo Toolbox
otbClampVectorImageFilter.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 
22 #ifndef otbClampVectorImageFilter_hxx
23 #define otbClampVectorImageFilter_hxx
24 
26 #include "itkImageRegionIterator.h"
27 #include "itkNumericTraits.h"
28 #include "itkObjectFactory.h"
29 #include "itkProgressReporter.h"
30 
31 namespace otb
32 {
33 
37 template <class TInputImage, class TOutputImage>
39  :
40  m_Lower(itk::NumericTraits<OutputImageInternalPixelType>::NonpositiveMin()),
41  m_Upper(itk::NumericTraits<OutputImageInternalPixelType>::max()),
42  m_DLower(static_cast<double>(m_Lower)),
43  m_DUpper(static_cast<double>(m_Upper))
44 {}
45 
46 
50 template <class TInputImage, class TOutputImage>
51 void ClampVectorImageFilter<TInputImage, TOutputImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
52 {
53  Superclass::PrintSelf(os, indent);
54 
55  os << indent << "Lower: " << static_cast<typename itk::NumericTraits<OutputImageInternalPixelType>::PrintType>(m_Lower) << std::endl;
56  os << indent << "Upper: " << static_cast<typename itk::NumericTraits<OutputImageInternalPixelType>::PrintType>(m_Upper) << std::endl;
57 }
58 
62 template <class TInputImage, class TOutputImage>
64 {
65  if (m_Upper != thresh || m_Lower > itk::NumericTraits<OutputImageInternalPixelType>::NonpositiveMin())
66  {
67  m_Lower = itk::NumericTraits<OutputImageInternalPixelType>::NonpositiveMin();
68  m_Upper = thresh;
69  m_DUpper = static_cast<double>(m_Upper);
70  this->Modified();
71  }
72 }
74 
78 template <class TInputImage, class TOutputImage>
80 {
81  if (m_Lower != thresh || m_Upper < itk::NumericTraits<OutputImageInternalPixelType>::max())
82  {
83  m_Lower = thresh;
84  m_DLower = m_Lower;
85  m_Upper = itk::NumericTraits<OutputImageInternalPixelType>::max();
86  this->Modified();
87  }
88 }
90 
91 
95 template <class TInputImage, class TOutputImage>
97 {
98  if (lower > upper)
99  {
100  itkExceptionMacro(<< "Lower threshold cannot be greater than upper threshold.");
101  return;
102  }
104 
105  if (m_Lower != lower || m_Upper != upper)
106  {
107  m_Lower = lower;
108  m_Upper = upper;
109  m_DLower = m_Lower;
110  m_DUpper = m_Upper;
111  this->Modified();
112  }
113 }
114 
115 
119 template <class TInputImage, class TOutputImage>
120 void ClampVectorImageFilter<TInputImage, TOutputImage>::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
121 {
122  itkDebugMacro(<< "Actually executing");
123 
124  // Get the input and output pointers
125  InputImagePointer inputPtr = this->GetInput();
126  OutputImagePointer outputPtr = this->GetOutput(0);
127 
128  // Define/declare an iterator that will walk the output region for this
129  // thread.
130  typedef itk::ImageRegionConstIterator<TInputImage> InputIterator;
131  typedef itk::ImageRegionIterator<TOutputImage> OutputIterator;
132 
133  InputIterator inIt(inputPtr, outputRegionForThread);
134  OutputIterator outIt(outputPtr, outputRegionForThread);
135 
136  // support progress methods/callbacks
137  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
138 
139  // walk the regions, threshold each pixel
140  while (!outIt.IsAtEnd() && !inIt.IsAtEnd())
141  {
142  const InputImagePixelType inPix = inIt.Get();
143  unsigned int l_size = inPix.Size();
144  OutputImagePixelType outPix;
145  outPix.SetSize(l_size);
146  for (unsigned int i = 0; i < l_size; i++)
147  {
148  // Cast the value of the pixel to double in order to compare
149  // with the double version of the upper and the lower bounds of
150  // output image
151  const double value = static_cast<double>(inPix[i]);
152 
153  if (m_DLower <= value && value <= m_DUpper)
154  {
155  // pixel passes to output unchanged and is replaced by m_OutsideValue in
156  // the inverse output image
157  outPix[i] = static_cast<OutputImageInternalPixelType>(value);
158  }
159  else if (value < m_DLower)
160  {
161  outPix[i] = m_Lower;
162  }
163  else if (value > m_DUpper)
164  {
165  outPix[i] = m_Upper;
166  }
167  }
168  outIt.Set(outPix);
169 
170  ++inIt;
171  ++outIt;
172  progress.CompletedPixel();
173  }
174 }
175 
176 } // end namespace itk
177 
178 #endif
otb::ClampVectorImageFilter::ClampVectorImageFilter
ClampVectorImageFilter()
Definition: otbClampVectorImageFilter.hxx:38
otb::ClampVectorImageFilter::OutputImagePixelType
OutputImageType::PixelType OutputImagePixelType
Definition: otbClampVectorImageFilter.h:75
otb::ClampVectorImageFilter::OutputImageRegionType
OutputImageType::RegionType OutputImageRegionType
Definition: otbClampVectorImageFilter.h:74
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ClampVectorImageFilter::ClampBelow
void ClampBelow(const OutputImageInternalPixelType &thresh)
Definition: otbClampVectorImageFilter.hxx:79
otb::ClampVectorImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbClampVectorImageFilter.hxx:51
otbClampVectorImageFilter.h
otb::ClampVectorImageFilter::InputImagePointer
InputImageType::ConstPointer InputImagePointer
Definition: otbClampVectorImageFilter.h:66
otb::ClampVectorImageFilter::OutputImageInternalPixelType
OutputImageType::InternalPixelType OutputImageInternalPixelType
Definition: otbClampVectorImageFilter.h:76
otb::ClampVectorImageFilter::ClampAbove
void ClampAbove(const OutputImageInternalPixelType &thresh)
Definition: otbClampVectorImageFilter.hxx:63
otb::ClampVectorImageFilter::OutputImagePointer
OutputImageType::Pointer OutputImagePointer
Definition: otbClampVectorImageFilter.h:73
otb::ClampVectorImageFilter::InputImagePixelType
InputImageType::PixelType InputImagePixelType
Definition: otbClampVectorImageFilter.h:68
itk
Definition: otbNoDataHelper.h:31
otb::ClampVectorImageFilter::ClampOutside
void ClampOutside(const OutputImageInternalPixelType &lower, const OutputImageInternalPixelType &upper)
Definition: otbClampVectorImageFilter.hxx:96
otb::ClampVectorImageFilter::ThreadedGenerateData
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbClampVectorImageFilter.hxx:120