OTB  10.0.0
Orfeo Toolbox
otbThresholdVectorImageFilter.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 otbThresholdVectorImageFilter_hxx
23 #define otbThresholdVectorImageFilter_hxx
24 
25 #include "otbMacro.h" //for
27 #include "itkImageRegionIterator.h"
28 #include "itkNumericTraits.h"
29 #include "itkObjectFactory.h"
30 #include "itkProgressReporter.h"
31 
32 namespace otb
33 {
34 
38 template <class TInputImage, class TOutputImage>
40  :
41  m_OutsideValue(itk::NumericTraits<OutputImageInternalPixelType>::Zero),
42  m_Lower(itk::NumericTraits<InputImageInternalPixelType>::NonpositiveMin()),
43  m_Upper(itk::NumericTraits<InputImageInternalPixelType>::max())
44 {this->DynamicMultiThreadingOn();}
45 
46 
50 template <class TInputImage, class TOutputImage>
51 void ThresholdVectorImageFilter<TInputImage, TOutputImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
52 {
53  Superclass::PrintSelf(os, indent);
54 
55  os << indent << "OutsideValue: " << static_cast<typename itk::NumericTraits<InputImageInternalPixelType>::PrintType>(m_OutsideValue) << std::endl;
56  os << indent << "Lower: " << static_cast<typename itk::NumericTraits<InputImageInternalPixelType>::PrintType>(m_Lower) << std::endl;
57  os << indent << "Upper: " << static_cast<typename itk::NumericTraits<InputImageInternalPixelType>::PrintType>(m_Upper) << std::endl;
58 }
59 
63 template <class TInputImage, class TOutputImage>
65 {
66  if (m_Upper != thresh || m_Lower > itk::NumericTraits<InputImageInternalPixelType>::NonpositiveMin())
67  {
68  m_Lower = itk::NumericTraits<InputImageInternalPixelType>::NonpositiveMin();
69  m_Upper = thresh;
70  this->Modified();
71  }
72 }
74 
78 template <class TInputImage, class TOutputImage>
80 {
81  if (m_Lower != thresh || m_Upper < itk::NumericTraits<InputImageInternalPixelType>::max())
82  {
83  m_Lower = thresh;
84  m_Upper = itk::NumericTraits<InputImageInternalPixelType>::max();
85  this->Modified();
86  }
87 }
89 
90 
94 template <class TInputImage, class TOutputImage>
96 {
97  if (lower > upper)
98  {
99  itkExceptionMacro(<< "Lower threshold cannot be greater than upper threshold.");
100  return;
101  }
103 
104  if (m_Lower != lower || m_Upper != upper)
105  {
106  m_Lower = lower;
107  m_Upper = upper;
108  this->Modified();
109  }
110 }
111 
112 
116 template <class TInputImage, class TOutputImage>
118 {
119  itkDebugMacro(<< "Actually executing");
120 
121  // Get the input and output pointers
122  InputImagePointer inputPtr = this->GetInput();
123  OutputImagePointer outputPtr = this->GetOutput(0);
124 
125  // Define/declare an iterator that will walk the output region for this
126  // thread.
127  typedef itk::ImageRegionConstIterator<TInputImage> InputIterator;
128  typedef itk::ImageRegionIterator<TOutputImage> OutputIterator;
129 
130  InputIterator inIt(inputPtr, outputRegionForThread);
131  OutputIterator outIt(outputPtr, outputRegionForThread);
132 
133  // walk the regions, threshold each pixel
134  while (!outIt.IsAtEnd() && !inIt.IsAtEnd())
135  {
136  const InputImagePixelType inPix = inIt.Get();
137  unsigned int l_size = inPix.Size();
138  OutputImagePixelType outPix;
139  outPix.SetSize(l_size);
140  for (unsigned int i = 0; i < l_size; i++)
141  {
142  const InputImageInternalPixelType value = inPix[i];
143 
144  if (m_Lower <= value && value <= m_Upper)
145  {
146  // pixel passes to output unchanged and is replaced by m_OutsideValue in
147  // the inverse output image
148  outPix[i] = static_cast<OutputImageInternalPixelType>(value);
149  }
150  else
151  {
152  outPix[i] = static_cast<OutputImageInternalPixelType>(m_OutsideValue);
153  }
154  }
155 
156  outIt.Set(outPix);
157 
158  ++inIt;
159  ++outIt;
160  }
161 }
162 
163 } // end namespace itk
164 
165 #endif
InputImageType::InternalPixelType InputImageInternalPixelType
OutputImageType::PixelType OutputImagePixelType
OutputImageType::InternalPixelType OutputImageInternalPixelType
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) override
void PrintSelf(std::ostream &os, itk::Indent indent) const override
InputImageType::ConstPointer InputImagePointer
void ThresholdOutside(const InputImageInternalPixelType &lower, const InputImageInternalPixelType &upper)
OutputImageType::RegionType OutputImageRegionType
void ThresholdAbove(const InputImageInternalPixelType &thresh)
void ThresholdBelow(const InputImageInternalPixelType &thresh)
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.