ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkThresholdLabelerImageFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkThresholdLabelerImageFilter_h
19 #define itkThresholdLabelerImageFilter_h
20 
22 #include "itkConceptChecking.h"
23 
24 namespace itk
25 {
42 namespace Functor
43 {
44 template< typename TInput, typename TOutput >
45 class ITK_TEMPLATE_EXPORT ThresholdLabeler
46 {
47 public:
49  ~ThresholdLabeler() = default;
50 
52  using RealThresholdVector = std::vector< RealThresholdType >;
53 
55  void SetThresholds(const RealThresholdVector & thresholds)
56  { m_Thresholds = thresholds; }
57 
59  void SetLabelOffset(const TOutput & labelOffset)
60  { m_LabelOffset = labelOffset; }
61 
62  bool operator!=(const ThresholdLabeler & other) const
63  {
64  if ( m_Thresholds != other.m_Thresholds
65  || m_LabelOffset != other.m_LabelOffset )
66  {
67  return true;
68  }
69  return false;
70  }
71 
72  bool operator==(const ThresholdLabeler & other) const
73  {
74  return !( *this != other );
75  }
76 
77  inline TOutput operator()(const TInput & A) const
78  {
79  size_t size = m_Thresholds.size();
80 
81  if ( size == 0 )
82  {
83  return m_LabelOffset;
84  }
85  if ( A <= m_Thresholds[0] )
86  {
87  return m_LabelOffset;
88  }
89  for ( size_t i = 0; i < size - 1; i++ )
90  {
91  /* Value is in this class if it equals the upper bound. */
92  if ( m_Thresholds[i] < A && A <= m_Thresholds[i + 1] )
93  {
94  return static_cast< TOutput >( i + 1 ) + m_LabelOffset;
95  }
96  }
97  return static_cast< TOutput >( size ) + m_LabelOffset;
98  }
99 
100 private:
101 
103  TOutput m_LabelOffset;
104 };
105 }
106 
107 template< typename TInputImage, typename TOutputImage >
108 class ITK_TEMPLATE_EXPORT ThresholdLabelerImageFilter:
109  public
110  UnaryFunctorImageFilter< TInputImage, TOutputImage,
111  Functor::ThresholdLabeler<
112  typename TInputImage::PixelType,
113  typename TOutputImage::PixelType > >
114 {
115 public:
116  ITK_DISALLOW_COPY_AND_ASSIGN(ThresholdLabelerImageFilter);
117 
121  TInputImage, TOutputImage,
123  typename TInputImage::PixelType,
124  typename TOutputImage::PixelType >
125  >;
126 
129 
131  itkNewMacro(Self);
132 
135 
137  using InputPixelType = typename TInputImage::PixelType;
138  using OutputPixelType = typename TOutputImage::PixelType;
139 
141  using ThresholdVector = std::vector< InputPixelType >;
143  using RealThresholdVector = std::vector< RealThresholdType >;
144 
146 #ifdef ITK_USE_CONCEPT_CHECKING
147  // Begin concept checking
148  itkConceptMacro( PixelTypeComparable,
150  itkConceptMacro( OutputPixelTypeComparable,
152  itkConceptMacro( OutputPixelTypeOStreamWritable,
154  // End concept checking
155 #endif
156 
157 
159  void SetThresholds(const ThresholdVector & thresholds)
160  {
161  m_Thresholds = thresholds;
162  m_RealThresholds.clear();
163  typename ThresholdVector::const_iterator itr = m_Thresholds.begin();
164  while ( itr != m_Thresholds.end() )
165  {
166  m_RealThresholds.push_back( static_cast< RealThresholdType >( *itr ) );
167  ++itr;
168  }
169  this->Modified();
170  }
172 
175  { return m_Thresholds; }
176 
178  void SetRealThresholds(const RealThresholdVector & thresholds)
179  {
180  m_RealThresholds = thresholds;
181  m_Thresholds.clear();
182  typename RealThresholdVector::const_iterator itr = m_RealThresholds.begin();
183  while ( itr != m_RealThresholds.end() )
184  {
185  m_Thresholds.push_back( static_cast< InputPixelType >( *itr ) );
186  ++itr;
187  }
188  this->Modified();
189  }
191 
194  { return m_RealThresholds; }
195 
197  itkSetClampMacro( LabelOffset, OutputPixelType, NumericTraits< OutputPixelType >::ZeroValue(),
199  itkGetConstMacro(LabelOffset, OutputPixelType);
201 
202 protected:
204  ~ThresholdLabelerImageFilter() override = default;
205  void PrintSelf(std::ostream & os, Indent indent) const override;
206 
209  void BeforeThreadedGenerateData() override;
210 
211 private:
215 };
216 } // end namespace itk
217 
218 #ifndef ITK_MANUAL_INSTANTIATION
219 #include "itkThresholdLabelerImageFilter.hxx"
220 #endif
221 
222 #endif
void SetLabelOffset(const TOutput &labelOffset)
typename TOutputImage::PixelType OutputPixelType
const RealThresholdVector & GetRealThresholds() const
std::vector< InputPixelType > ThresholdVector
Define numeric traits for std::vector.
typename TInputImage::PixelType InputPixelType
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
typename NumericTraits< InputPixelType >::RealType RealThresholdType
TOutput operator()(const TInput &A) const
std::vector< RealThresholdType > RealThresholdVector
Base class for all process objects that output image data.
void SetThresholds(const RealThresholdVector &thresholds)
void SetThresholds(const ThresholdVector &thresholds)
Label an input image according to a set of thresholds.
bool operator!=(const ThresholdLabeler &other) const
Implements pixel-wise generic operation on one image.
const ThresholdVector & GetThresholds() const
Control indentation during Print() invocation.
Definition: itkIndent.h:49
bool operator==(const ThresholdLabeler &other) const
#define itkConceptMacro(name, concept)
void SetRealThresholds(const RealThresholdVector &thresholds)