ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkThresholdLabelerImageFilter.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkThresholdLabelerImageFilter_h
00019 #define __itkThresholdLabelerImageFilter_h
00020 
00021 #include "itkUnaryFunctorImageFilter.h"
00022 #include "itkConceptChecking.h"
00023 
00024 namespace itk
00025 {
00042 namespace Functor
00043 {
00044 template< class TInput, class TOutput >
00045 class ITK_EXPORT 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 
00072   bool operator==(const ThresholdLabeler & other) const
00073   {
00074     return !( *this != other );
00075   }
00076 
00077   inline TOutput operator()(const TInput & A) const
00078   {
00079     unsigned int size = m_Thresholds.size();
00080 
00081     if ( size == 0 )
00082       {
00083       return m_LabelOffset;
00084       }
00085     if ( A <= m_Thresholds[0] )
00086       {
00087       return m_LabelOffset;
00088       }
00089     for ( unsigned int i = 0; i < size - 1; i++ )
00090       {
00091       /* Value is in this class if it equals the upper bound. */
00092       if ( m_Thresholds[i] < A && A <= m_Thresholds[i + 1] )
00093         {
00094         return static_cast< TOutput >( i + 1 ) + m_LabelOffset;
00095         }
00096       }
00097     return static_cast< TOutput >( size ) + m_LabelOffset;
00098   }
00099 
00100 private:
00101 
00102   RealThresholdVector m_Thresholds;
00103   TOutput             m_LabelOffset;
00104 };
00105 }
00106 
00107 template< class TInputImage, class TOutputImage >
00108 class ThresholdLabelerImageFilter:
00109   public
00110   UnaryFunctorImageFilter< TInputImage, TOutputImage,
00111                            Functor::ThresholdLabeler<
00112                              typename TInputImage::PixelType,
00113                              typename TOutputImage::PixelType > >
00114 {
00115 public:
00117   typedef ThresholdLabelerImageFilter Self;
00118   typedef UnaryFunctorImageFilter<
00119     TInputImage, TOutputImage,
00120     Functor::ThresholdLabeler<
00121       typename TInputImage::PixelType,
00122       typename TOutputImage::PixelType >
00123     >                                  Superclass;
00124 
00125   typedef SmartPointer< Self >       Pointer;
00126   typedef SmartPointer< const Self > ConstPointer;
00127 
00129   itkNewMacro(Self);
00130 
00132   itkTypeMacro(ThresholdLabelerImageFilter, UnaryFunctorImageFilter);
00133 
00135   typedef typename TInputImage::PixelType  InputPixelType;
00136   typedef typename TOutputImage::PixelType OutputPixelType;
00137 
00139   typedef std::vector< InputPixelType >                      ThresholdVector;
00140   typedef typename NumericTraits< InputPixelType >::RealType RealThresholdType;
00141   typedef std::vector< RealThresholdType >                   RealThresholdVector;
00142 
00144 #ifdef ITK_USE_CONCEPT_CHECKING
00145 
00147   itkConceptMacro( PixelTypeComparable,
00148                    ( Concept::Comparable< InputPixelType > ) );
00149   itkConceptMacro( OutputPixelTypeComparable,
00150                    ( Concept::Comparable< OutputPixelType > ) );
00151   itkConceptMacro( OutputPixelTypeOStreamWritable,
00152                    ( Concept::OStreamWritable< OutputPixelType > ) );
00153 
00155 #endif
00156 
00158   void SetThresholds(const ThresholdVector & thresholds)
00159   {
00160     m_Thresholds = thresholds;
00161     m_RealThresholds.clear();
00162     typename ThresholdVector::const_iterator itr = m_Thresholds.begin();
00163     while ( itr != m_Thresholds.end() )
00164       {
00165       m_RealThresholds.push_back( static_cast< RealThresholdType >( *itr ) );
00166       ++itr;
00167       }
00168     this->Modified();
00169   }
00171 
00173   const ThresholdVector & GetThresholds() const
00174   { return m_Thresholds; }
00175 
00177   void SetRealThresholds(const RealThresholdVector & thresholds)
00178   {
00179     m_RealThresholds = thresholds;
00180     m_Thresholds.clear();
00181     typename RealThresholdVector::const_iterator itr = m_RealThresholds.begin();
00182     while ( itr != m_RealThresholds.end() )
00183       {
00184       m_Thresholds.push_back( static_cast< InputPixelType >( *itr ) );
00185       ++itr;
00186       }
00187     this->Modified();
00188   }
00190 
00192   const RealThresholdVector & GetRealThresholds() const
00193   { return m_RealThresholds; }
00194 
00196   itkSetClampMacro( LabelOffset, OutputPixelType, NumericTraits< OutputPixelType >::Zero,
00197                     NumericTraits< OutputPixelType >::max() );
00198   itkGetConstMacro(LabelOffset, OutputPixelType);
00199 protected:
00200   ThresholdLabelerImageFilter();
00201   virtual ~ThresholdLabelerImageFilter() {}
00202   void PrintSelf(std::ostream & os, Indent indent) const;
00204 
00207   virtual void BeforeThreadedGenerateData();
00208 
00209 private:
00210   ThresholdLabelerImageFilter(const Self &); //purposely not implemented
00211   void operator=(const Self &);              //purposely not implemented
00212 
00213   ThresholdVector     m_Thresholds;
00214   RealThresholdVector m_RealThresholds;
00215   OutputPixelType     m_LabelOffset;
00216 };
00217 } // end namespace itk
00218 
00219 #ifndef ITK_MANUAL_INSTANTIATION
00220 #include "itkThresholdLabelerImageFilter.hxx"
00221 #endif
00222 
00223 #endif
00224