00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkThresholdLabelerImageFilter_h
00018 #define __itkThresholdLabelerImageFilter_h
00019
00020 #include "itkUnaryFunctorImageFilter.h"
00021 #include "itkConceptChecking.h"
00022 #include "itkNumericTraits.h"
00023
00024 namespace itk
00025 {
00026
00042 namespace Functor {
00043
00044 template< class TInput, class TOutput >
00045 class ThresholdLabeler
00046 {
00047 public:
00048 ThresholdLabeler() { m_LabelOffset = NumericTraits<TOutput>::One; }
00049 ~ThresholdLabeler() {};
00050
00051 typedef typename NumericTraits< TInput >::RealType RealThresholdType;
00052 typedef std::vector<RealThresholdType> RealThresholdVector;
00053
00055 void SetThresholds( const RealThresholdVector & thresholds )
00056 { m_Thresholds = thresholds; }
00057
00059 void SetLabelOffset( const TOutput & labelOffset )
00060 { m_LabelOffset = labelOffset; }
00061
00062 bool operator!=( const ThresholdLabeler & other ) const
00063 {
00064 if( m_Thresholds != other.m_Thresholds ||
00065 m_LabelOffset != other.m_LabelOffset )
00066 {
00067 return true;
00068 }
00069 return false;
00070 }
00071 bool operator==( const ThresholdLabeler & other ) const
00072 {
00073 return !(*this != other);
00074 }
00075
00076 inline TOutput operator()( const TInput & A )
00077 {
00078 unsigned int size = m_Thresholds.size();
00079 if (size == 0)
00080 {
00081 return m_LabelOffset;
00082 }
00083 if (A <= m_Thresholds[0])
00084 {
00085 return m_LabelOffset;
00086 }
00087 for (unsigned int i=0; i<size-1; i++)
00088 {
00089
00090 if (m_Thresholds[i] < A && A <= m_Thresholds[i+1])
00091 {
00092 return static_cast<TOutput>(i+1) + m_LabelOffset;
00093 }
00094 }
00095 return static_cast<TOutput>(size) + m_LabelOffset;
00096 }
00097
00098 private:
00099
00100 RealThresholdVector m_Thresholds;
00101 TOutput m_LabelOffset;
00102 };
00103 }
00104
00105 template <class TInputImage,class TOutputImage>
00106 class ThresholdLabelerImageFilter :
00107 public
00108 UnaryFunctorImageFilter<TInputImage,TOutputImage,
00109 Functor::ThresholdLabeler<
00110 typename TInputImage::PixelType,
00111 typename TOutputImage::PixelType> >
00112 {
00113 public:
00115 typedef ThresholdLabelerImageFilter Self;
00116 typedef UnaryFunctorImageFilter<TInputImage,TOutputImage,
00117 Functor::ThresholdLabeler<
00118 typename TInputImage::PixelType,
00119 typename TOutputImage::PixelType>
00120 > Superclass;
00121 typedef SmartPointer<Self> Pointer;
00122 typedef SmartPointer<const Self> ConstPointer;
00123
00125 itkNewMacro(Self);
00126
00128 itkTypeMacro(ThresholdLabelerImageFilter, UnaryFunctorImageFilter);
00129
00131 typedef typename TInputImage::PixelType InputPixelType;
00132 typedef typename TOutputImage::PixelType OutputPixelType;
00133
00135 typedef std::vector<InputPixelType> ThresholdVector;
00136 typedef typename NumericTraits< InputPixelType >::RealType
00137 RealThresholdType;
00138 typedef std::vector<RealThresholdType> RealThresholdVector;
00139
00140
00142 #ifdef ITK_USE_CONCEPT_CHECKING
00143
00145 itkConceptMacro(PixelTypeComparable,
00146 (Concept::Comparable<InputPixelType>));
00147 itkConceptMacro(OutputPixelTypeComparable,
00148 (Concept::Comparable<OutputPixelType>));
00149 itkConceptMacro(OutputPixelTypeOStreamWritable,
00150 (Concept::OStreamWritable<OutputPixelType>));
00151
00153 #endif
00154
00156 void SetThresholds( const ThresholdVector & thresholds )
00157 {
00158 m_Thresholds = thresholds;
00159 m_RealThresholds.clear();
00160 typename ThresholdVector::const_iterator itr = m_Thresholds.begin();
00161 while( itr != m_Thresholds.end() )
00162 {
00163 m_RealThresholds.push_back( static_cast< RealThresholdType >( *itr ) );
00164 ++itr;
00165 }
00166 this->Modified();
00167 }
00169
00171 const ThresholdVector & GetThresholds() const
00172 { return m_Thresholds; }
00173
00175 void SetRealThresholds( const RealThresholdVector & thresholds )
00176 {
00177 m_RealThresholds = thresholds;
00178 m_Thresholds.clear();
00179 typename RealThresholdVector::const_iterator itr = m_RealThresholds.begin();
00180 while( itr != m_RealThresholds.end() )
00181 {
00182 m_Thresholds.push_back( static_cast< InputPixelType >( *itr ) );
00183 ++itr;
00184 }
00185 this->Modified();
00186 }
00188
00190 const RealThresholdVector & GetRealThresholds() const
00191 { return m_RealThresholds; }
00192
00193
00194
00196 itkSetClampMacro(LabelOffset,OutputPixelType, NumericTraits<OutputPixelType>::Zero,NumericTraits<OutputPixelType>::max() );
00197 itkGetMacro(LabelOffset,OutputPixelType);
00199
00200 protected:
00201 ThresholdLabelerImageFilter();
00202 virtual ~ThresholdLabelerImageFilter() {}
00203 void PrintSelf(std::ostream& os, Indent indent) const;
00204
00207 virtual void BeforeThreadedGenerateData();
00208
00209 private:
00210 ThresholdLabelerImageFilter(const Self&);
00211 void operator=(const Self&);
00212
00213 ThresholdVector m_Thresholds;
00214 RealThresholdVector m_RealThresholds;
00215 OutputPixelType m_LabelOffset;
00216 };
00217
00218 }
00219
00220 #ifndef ITK_MANUAL_INSTANTIATION
00221 #include "itkThresholdLabelerImageFilter.txx"
00222 #endif
00223
00224 #endif
00225