Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkShapeKeepNObjectsLabelMapFilter.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkShapeKeepNObjectsLabelMapFilter.h,v $
00005   Language:  C++
00006   Date:      $Date: 2009-08-05 16:08:10 $
00007   Version:   $Revision: 1.4 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 #ifndef __itkShapeKeepNObjectsLabelMapFilter_h
00018 #define __itkShapeKeepNObjectsLabelMapFilter_h
00019 
00020 #include "itkInPlaceLabelMapFilter.h"
00021 #include "itkLabelObjectAccessors.h"
00022 #include "itkShapeLabelObjectAccessors.h"
00023 #include "itkProgressReporter.h"
00024 
00025 namespace itk {
00041 template<class TImage>
00042 class ITK_EXPORT ShapeKeepNObjectsLabelMapFilter : 
00043     public InPlaceLabelMapFilter<TImage>
00044 {
00045 public:
00047   typedef ShapeKeepNObjectsLabelMapFilter Self;
00048   typedef InPlaceLabelMapFilter<TImage>   Superclass;
00049   typedef SmartPointer<Self>              Pointer;
00050   typedef SmartPointer<const Self>        ConstPointer;
00051 
00053   typedef TImage                              ImageType;
00054   typedef typename ImageType::Pointer         ImagePointer;
00055   typedef typename ImageType::ConstPointer    ImageConstPointer;
00056   typedef typename ImageType::PixelType       PixelType;
00057   typedef typename ImageType::IndexType       IndexType;
00058   typedef typename ImageType::LabelObjectType LabelObjectType;
00059 
00060   typedef typename LabelObjectType::AttributeType AttributeType;
00061   
00062   typedef typename Superclass::LabelObjectContainerType  LabelObjectContainerType;
00063 
00065   itkStaticConstMacro(ImageDimension, unsigned int, TImage::ImageDimension);
00066 
00068   itkNewMacro(Self);
00069 
00071   itkTypeMacro(ShapeKeepNObjectsLabelMapFilter, InPlaceLabelMapFilter);
00072 
00073 #ifdef ITK_USE_CONCEPT_CHECKING
00074 
00075 /*  itkConceptMacro(InputEqualityComparableCheck,
00076     (Concept::EqualityComparable<InputImagePixelType>));
00077   itkConceptMacro(IntConvertibleToInputCheck,
00078     (Concept::Convertible<int, InputImagePixelType>));
00079   itkConceptMacro(InputOStreamWritableCheck,
00080     (Concept::OStreamWritable<InputImagePixelType>));*/
00081 
00083 #endif
00084 
00090   itkSetMacro( ReverseOrdering, bool );
00091   itkGetConstReferenceMacro( ReverseOrdering, bool );
00092   itkBooleanMacro( ReverseOrdering );
00094 
00098   itkSetMacro( NumberOfObjects, unsigned long );
00099   itkGetConstReferenceMacro( NumberOfObjects, unsigned long );
00101 
00106   itkGetConstMacro( Attribute, AttributeType );
00107   itkSetMacro( Attribute, AttributeType );
00109 
00110   void SetAttribute( const std::string & s )
00111     {
00112     this->SetAttribute( LabelObjectType::GetAttributeFromName( s ) );
00113     }
00114 
00115 
00116 protected:
00117   ShapeKeepNObjectsLabelMapFilter();
00118   ~ShapeKeepNObjectsLabelMapFilter() {};
00119 
00120   void GenerateData();
00121 
00122   template <class TAttributeAccessor> 
00123   void TemplatedGenerateData( const TAttributeAccessor & )
00124     {
00125     // Allocate the output
00126     this->AllocateOutputs();
00127 
00128     ImageType * output = this->GetOutput();
00129     ImageType * output2 = this->GetOutput( 1 );
00130 
00131     // set the background value for the second output - this is not done in the superclasses
00132     output2->SetBackgroundValue( output->GetBackgroundValue() );
00133 
00134     const LabelObjectContainerType & labelObjectContainer = output->GetLabelObjectContainer();
00135     typedef typename LabelObjectType::Pointer   LabelObjectPointer;
00136     typedef std::vector< LabelObjectPointer >   VectorType;
00137 
00138     ProgressReporter progress( this, 0, 2 * labelObjectContainer.size() );
00139 
00140     // get the label objects in a vector, so they can be sorted
00141     VectorType labelObjects;
00142     labelObjects.reserve( labelObjectContainer.size() );
00143     typename LabelObjectContainerType::const_iterator it = labelObjectContainer.begin();
00144     while( it != labelObjectContainer.end() )
00145       {
00146       labelObjects.push_back( it->second );
00147       progress.CompletedPixel();
00148       it++;
00149       }
00150 
00151     // instantiate the comparator and sort the vector
00152     if( m_NumberOfObjects < labelObjectContainer.size() )
00153       {
00154       typename VectorType::iterator end = labelObjects.begin() + m_NumberOfObjects;
00155       if( m_ReverseOrdering )
00156         {
00157         Functor::LabelObjectReverseComparator< LabelObjectType, TAttributeAccessor > comparator;
00158         std::nth_element( labelObjects.begin(), end, labelObjects.end(), comparator );
00159         }
00160       else
00161         {
00162         Functor::LabelObjectComparator< LabelObjectType, TAttributeAccessor > comparator;
00163         std::nth_element( labelObjects.begin(), end, labelObjects.end(), comparator );
00164         }
00165       progress.CompletedPixel();
00166     
00167       // and remove the last objects of the map
00168       for( typename VectorType::const_iterator it2 = end;
00169         it2 != labelObjects.end();
00170         it2++ )
00171         {
00172         output2->AddLabelObject( *it2 );
00173         output->RemoveLabelObject( *it2 );
00174         progress.CompletedPixel();
00175         }
00176       }
00177     }
00178 
00179   void PrintSelf(std::ostream& os, Indent indent) const;
00180 
00181   bool          m_ReverseOrdering;
00182   unsigned long m_NumberOfObjects;
00183   AttributeType m_Attribute;
00184 
00185 private:
00186   ShapeKeepNObjectsLabelMapFilter(const Self&); //purposely not implemented
00187   void operator=(const Self&); //purposely not implemented
00188 
00189 }; // end of class
00190 
00191 } // end namespace itk
00192   
00193 #ifndef ITK_MANUAL_INSTANTIATION
00194 #include "itkShapeKeepNObjectsLabelMapFilter.txx"
00195 #endif
00196 
00197 #endif
00198 

Generated at Fri Apr 16 19:39:25 2010 for ITK by doxygen 1.6.1 written by Dimitri van Heesch, © 1997-2000