OTB  10.0.0
Orfeo Toolbox
otbClampVectorImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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  this->DynamicMultiThreadingOn();
46 }
47 
48 
52 template <class TInputImage, class TOutputImage>
53 void ClampVectorImageFilter<TInputImage, TOutputImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
54 {
55  Superclass::PrintSelf(os, indent);
56 
57  os << indent << "Lower: " << static_cast<typename itk::NumericTraits<OutputImageInternalPixelType>::PrintType>(m_Lower) << std::endl;
58  os << indent << "Upper: " << static_cast<typename itk::NumericTraits<OutputImageInternalPixelType>::PrintType>(m_Upper) << std::endl;
59 }
60 
64 template <class TInputImage, class TOutputImage>
66 {
67  if (m_Upper != thresh || m_Lower > itk::NumericTraits<OutputImageInternalPixelType>::NonpositiveMin())
68  {
69  m_Lower = itk::NumericTraits<OutputImageInternalPixelType>::NonpositiveMin();
70  m_Upper = thresh;
71  m_DUpper = static_cast<double>(m_Upper);
72  this->Modified();
73  }
74 }
76 
80 template <class TInputImage, class TOutputImage>
82 {
83  if (m_Lower != thresh || m_Upper < itk::NumericTraits<OutputImageInternalPixelType>::max())
84  {
85  m_Lower = thresh;
86  m_DLower = m_Lower;
87  m_Upper = itk::NumericTraits<OutputImageInternalPixelType>::max();
88  this->Modified();
89  }
90 }
92 
93 
97 template <class TInputImage, class TOutputImage>
99 {
100  if (lower > upper)
101  {
102  itkExceptionMacro(<< "Lower threshold cannot be greater than upper threshold.");
103  return;
104  }
106 
107  if (m_Lower != lower || m_Upper != upper)
108  {
109  m_Lower = lower;
110  m_Upper = upper;
111  m_DLower = m_Lower;
112  m_DUpper = m_Upper;
113  this->Modified();
114  }
115 }
116 
117 
121 template <class TInputImage, class TOutputImage>
123 {
124  itkDebugMacro(<< "Actually executing");
125 
126  // Get the input and output pointers
127  InputImagePointer inputPtr = this->GetInput();
128  OutputImagePointer outputPtr = this->GetOutput(0);
129 
130  // Define/declare an iterator that will walk the output region for this
131  // thread.
132  typedef itk::ImageRegionConstIterator<TInputImage> InputIterator;
133  typedef itk::ImageRegionIterator<TOutputImage> OutputIterator;
134 
135  InputIterator inIt(inputPtr, outputRegionForThread);
136  OutputIterator outIt(outputPtr, outputRegionForThread);
137 
138  // walk the regions, threshold each pixel
139  while (!outIt.IsAtEnd() && !inIt.IsAtEnd())
140  {
141  const InputImagePixelType inPix = inIt.Get();
142  unsigned int l_size = inPix.Size();
143  OutputImagePixelType outPix;
144  outPix.SetSize(l_size);
145  for (unsigned int i = 0; i < l_size; i++)
146  {
147  // Cast the value of the pixel to double in order to compare
148  // with the double version of the upper and the lower bounds of
149  // output image
150  const double value = static_cast<double>(inPix[i]);
151 
152  if (m_DLower <= value && value <= m_DUpper)
153  {
154  // pixel passes to output unchanged and is replaced by m_OutsideValue in
155  // the inverse output image
156  outPix[i] = static_cast<OutputImageInternalPixelType>(value);
157  }
158  else if (value < m_DLower)
159  {
160  outPix[i] = m_Lower;
161  }
162  else if (value > m_DUpper)
163  {
164  outPix[i] = m_Upper;
165  }
166  }
167  outIt.Set(outPix);
168 
169  ++inIt;
170  ++outIt;
171  }
172 }
173 
174 } // end namespace itk
175 
176 #endif
OutputImageType::RegionType OutputImageRegionType
void PrintSelf(std::ostream &os, itk::Indent indent) const override
OutputImageType::InternalPixelType OutputImageInternalPixelType
OutputImageType::Pointer OutputImagePointer
OutputImageType::PixelType OutputImagePixelType
void ClampAbove(const OutputImageInternalPixelType &thresh)
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) override
void ClampBelow(const OutputImageInternalPixelType &thresh)
InputImageType::ConstPointer InputImagePointer
void ClampOutside(const OutputImageInternalPixelType &lower, const OutputImageInternalPixelType &upper)
InputImageType::PixelType InputImagePixelType
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.