ITK  4.13.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:
50 
52  typedef std::vector< RealThresholdType > RealThresholdVector;
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:
118  typedef UnaryFunctorImageFilter<
119  TInputImage, TOutputImage,
121  typename TInputImage::PixelType,
122  typename TOutputImage::PixelType >
124 
127 
129  itkNewMacro(Self);
130 
133 
135  typedef typename TInputImage::PixelType InputPixelType;
136  typedef typename TOutputImage::PixelType OutputPixelType;
137 
139  typedef std::vector< InputPixelType > ThresholdVector;
141  typedef std::vector< RealThresholdType > RealThresholdVector;
142 
144 #ifdef ITK_USE_CONCEPT_CHECKING
145  // Begin concept checking
146  itkConceptMacro( PixelTypeComparable,
148  itkConceptMacro( OutputPixelTypeComparable,
150  itkConceptMacro( OutputPixelTypeOStreamWritable,
152  // End concept checking
153 #endif
154 
155 
157  void SetThresholds(const ThresholdVector & thresholds)
158  {
159  m_Thresholds = thresholds;
160  m_RealThresholds.clear();
161  typename ThresholdVector::const_iterator itr = m_Thresholds.begin();
162  while ( itr != m_Thresholds.end() )
163  {
164  m_RealThresholds.push_back( static_cast< RealThresholdType >( *itr ) );
165  ++itr;
166  }
167  this->Modified();
168  }
170 
173  { return m_Thresholds; }
174 
176  void SetRealThresholds(const RealThresholdVector & thresholds)
177  {
178  m_RealThresholds = thresholds;
179  m_Thresholds.clear();
180  typename RealThresholdVector::const_iterator itr = m_RealThresholds.begin();
181  while ( itr != m_RealThresholds.end() )
182  {
183  m_Thresholds.push_back( static_cast< InputPixelType >( *itr ) );
184  ++itr;
185  }
186  this->Modified();
187  }
189 
192  { return m_RealThresholds; }
193 
195  itkSetClampMacro( LabelOffset, OutputPixelType, NumericTraits< OutputPixelType >::ZeroValue(),
197  itkGetConstMacro(LabelOffset, OutputPixelType);
199 
200 protected:
202  virtual ~ThresholdLabelerImageFilter() ITK_OVERRIDE {}
203  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
204 
207  virtual void BeforeThreadedGenerateData() ITK_OVERRIDE;
208 
209 private:
210  ITK_DISALLOW_COPY_AND_ASSIGN(ThresholdLabelerImageFilter);
211 
212  ThresholdVector m_Thresholds;
213  RealThresholdVector m_RealThresholds;
214  OutputPixelType m_LabelOffset;
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)
NumericTraits< TInput >::RealType RealThresholdType
const RealThresholdVector & GetRealThresholds() const
UnaryFunctorImageFilter< TInputImage, TOutputImage, Functor::ThresholdLabeler< typename TInputImage::PixelType, typename TOutputImage::PixelType > > Superclass
TOutput operator()(const TInput &A) const
Base class for all process objects that output image data.
std::vector< InputPixelType > ThresholdVector
std::vector< RealThresholdType > RealThresholdVector
void SetThresholds(const RealThresholdVector &thresholds)
void SetThresholds(const ThresholdVector &thresholds)
Label an input image according to a set of thresholds.
std::vector< RealThresholdType > RealThresholdVector
NumericTraits< InputPixelType >::RealType RealThresholdType
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 additional traits for native types such as int or float.
#define itkConceptMacro(name, concept)
void SetRealThresholds(const RealThresholdVector &thresholds)