ITK  4.4.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< class TInput, class TOutput >
45 class ITK_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  unsigned int 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 ( unsigned int 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< class TInputImage, class TOutputImage >
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 
147  itkConceptMacro( PixelTypeComparable,
149  itkConceptMacro( OutputPixelTypeComparable,
151  itkConceptMacro( OutputPixelTypeOStreamWritable,
153 
155 #endif
156 
158  void SetThresholds(const ThresholdVector & thresholds)
159  {
160  m_Thresholds = thresholds;
161  m_RealThresholds.clear();
162  typename ThresholdVector::const_iterator itr = m_Thresholds.begin();
163  while ( itr != m_Thresholds.end() )
164  {
165  m_RealThresholds.push_back( static_cast< RealThresholdType >( *itr ) );
166  ++itr;
167  }
168  this->Modified();
169  }
171 
174  { return m_Thresholds; }
175 
177  void SetRealThresholds(const RealThresholdVector & thresholds)
178  {
179  m_RealThresholds = thresholds;
180  m_Thresholds.clear();
181  typename RealThresholdVector::const_iterator itr = m_RealThresholds.begin();
182  while ( itr != m_RealThresholds.end() )
183  {
184  m_Thresholds.push_back( static_cast< InputPixelType >( *itr ) );
185  ++itr;
186  }
187  this->Modified();
188  }
190 
193  { return m_RealThresholds; }
194 
196  itkSetClampMacro( LabelOffset, OutputPixelType, NumericTraits< OutputPixelType >::Zero,
198  itkGetConstMacro(LabelOffset, OutputPixelType);
200 
201 protected:
204  void PrintSelf(std::ostream & os, Indent indent) const;
205 
208  virtual void BeforeThreadedGenerateData();
209 
210 private:
211  ThresholdLabelerImageFilter(const Self &); //purposely not implemented
212  void operator=(const Self &); //purposely not implemented
213 
217 };
218 } // end namespace itk
219 
220 #ifndef ITK_MANUAL_INSTANTIATION
221 #include "itkThresholdLabelerImageFilter.hxx"
222 #endif
223 
224 #endif
225