OTB  9.0.0
Orfeo Toolbox
otbClampImageFilter.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 otbClampImageFilter_hxx
23 #define otbClampImageFilter_hxx
24 
25 #include "otbClampImageFilter.h"
26 #include "itkImageScanlineIterator.h"
27 #include "itkImageScanlineConstIterator.h"
28 #include "itkImageRegionIterator.h"
29 #include "itkNumericTraits.h"
30 #include "itkObjectFactory.h"
31 #include "itkProgressReporter.h"
32 #include <limits>
33 
34 namespace otb
35 {
36 
40 template <class TInputImage, class TOutputImage>
42  :
43  m_Lower(std::numeric_limits<OutputPixelValueType>::lowest()),
44  m_Upper(std::numeric_limits<OutputPixelValueType>::max())
45 {}
46 
47 template <class TInputImage, class TOutputImage>
49 {
50  assert( lowerVal <= upperVal );
51  if ((m_Lower != lowerVal) || (m_Upper != upperVal))
52  {
53  m_Lower = lowerVal;
54  m_Upper = upperVal;
55  this->GetFunctor().SetThresholds(m_Lower, m_Upper);
56  this->Modified();
57  }
58 }
59 
63 template <class TInputImage, class TOutputImage>
64 void ClampImageFilter<TInputImage, TOutputImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
65 {
66  Superclass::PrintSelf(os, indent);
67 
68  os << indent << "Lower: " << static_cast<typename itk::NumericTraits<OutputImagePixelType>::PrintType>(m_Lower) << std::endl;
69  os << indent << "Upper: " << static_cast<typename itk::NumericTraits<OutputImagePixelType>::PrintType>(m_Upper) << std::endl;
70 }
71 
75 template <class TInputImage, class TOutputImage>
77 {
78  if (m_Upper != thresh || m_Lower > std::numeric_limits<OutputPixelValueType>::lowest())
79  {
80  m_Lower = std::numeric_limits<OutputPixelValueType>::lowest();
81  m_Upper = thresh;
82  this->GetFunctor().SetThresholds(m_Lower, m_Upper);
83  this->Modified();
84  }
85 }
87 
91 template <class TInputImage, class TOutputImage>
93 {
94  if (m_Lower != thresh || m_Upper < std::numeric_limits<OutputPixelValueType>::max())
95  {
96  m_Upper = std::numeric_limits<OutputPixelValueType>::max();
97  m_Lower = thresh;
98  this->GetFunctor().SetThresholds(m_Lower, m_Upper);
99  this->Modified();
100  }
101 }
103 
104 
108 template <class TInputImage, class TOutputImage>
110 {
111  if (lower > upper)
112  {
113  itkExceptionMacro(<< "Lower threshold cannot be greater than upper threshold.");
114  return;
115  }
117 
118  if (m_Lower != lower || m_Upper != upper)
119  {
120  m_Lower = lower;
121  m_Upper = upper;
122  this->GetFunctor().SetThresholds(m_Lower, m_Upper);
123  this->Modified();
124  }
125 }
126 
127 template <class TInputImage, class TOutputImage>
128 void
130 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
131 {
132  const auto& regionSize = outputRegionForThread.GetSize();
133 
134  if (regionSize[0] == 0)
135  {
136  return;
137  }
138  const auto numberOfLinesToProcess = outputRegionForThread.GetNumberOfPixels() / regionSize[0];
139  itk::ProgressReporter p(this, threadId, numberOfLinesToProcess);
140 
141  // Build output iterator
142  itk::ImageScanlineConstIterator<InputImageType> inIt(this->GetInput(), outputRegionForThread);
143  itk::ImageScanlineIterator<OutputImageType> outIt(this->GetOutput(), outputRegionForThread);
144 
145  // Build a default value
146  typename OutputImageType::PixelType outputValueHolder;
147  // Luc: I'm not sure this will be the correct size given the convoluted
148  // size computations done in the ConvertTypeFunctor
149  itk::NumericTraits<typename OutputImageType::PixelType>::SetLength(outputValueHolder, this->GetOutput()->GetNumberOfComponentsPerPixel());
150 
151  while (!outIt.IsAtEnd())
152  {
153  // MoveIterartors will ++ all iterators in the tuple
154  for (; !outIt.IsAtEndOfLine(); ++outIt, ++inIt)
155  {
156  // This will call the operator with inputIterators Get() results
157  // and fill outputValueHolder with the result.
158  m_Functor(outputValueHolder, inIt.Get());
159  outIt.Set(outputValueHolder);
160  }
161  outIt.NextLine();
162  inIt.NextLine();
163  p.CompletedPixel(); // may throw
164  }
165 }
166 
167 } // end namespace otb
168 
169 #endif
otb::ClampImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbClampImageFilter.hxx:64
otb::ClampImageFilter::OutputImageRegionType
typename OutputImageType::RegionType OutputImageRegionType
Definition: otbClampImageFilter.h:73
otb::ClampImageFilter::ClampOutside
void ClampOutside(const OutputPixelValueType &lower, const OutputPixelValueType &upper)
Definition: otbClampImageFilter.hxx:109
otb::ClampImageFilter::OutputPixelValueType
typename itk::NumericTraits< OutputInternalPixelType >::ValueType OutputPixelValueType
Definition: otbClampImageFilter.h:76
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ClampImageFilter::SetThresholds
void SetThresholds(OutputPixelValueType lowerVal, OutputPixelValueType upperVal)
Definition: otbClampImageFilter.hxx:48
otb::ClampImageFilter::ClampBelow
void ClampBelow(const OutputPixelValueType &thresh)
Definition: otbClampImageFilter.hxx:92
otb::ClampImageFilter::ClampImageFilter
ClampImageFilter()
Definition: otbClampImageFilter.hxx:41
otbClampImageFilter.h
otb::ClampImageFilter::ClampAbove
void ClampAbove(const OutputPixelValueType &thresh)
Definition: otbClampImageFilter.hxx:76
otb::ClampImageFilter::ThreadedGenerateData
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbClampImageFilter.hxx:130