ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkRelabelComponentImageFilter.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 __itkRelabelComponentImageFilter_h
00019 #define __itkRelabelComponentImageFilter_h
00020 
00021 #include "itkInPlaceImageFilter.h"
00022 #include "itkImage.h"
00023 #include <vector>
00024 
00025 namespace itk
00026 {
00078 template< class TInputImage, class TOutputImage >
00079 class ITK_EXPORT RelabelComponentImageFilter:
00080   public InPlaceImageFilter< TInputImage, TOutputImage >
00081 {
00082 public:
00086   typedef RelabelComponentImageFilter                     Self;
00087   typedef InPlaceImageFilter< TInputImage, TOutputImage > Superclass;
00088 
00092   typedef typename Superclass::InputImagePointer InputImagePointer;
00093 
00098   typedef typename TOutputImage::PixelType         OutputPixelType;
00099   typedef typename TOutputImage::InternalPixelType OutputInternalPixelType;
00100   typedef typename TInputImage::PixelType          InputPixelType;
00101   typedef typename TInputImage::InternalPixelType  InputInternalPixelType;
00102   itkStaticConstMacro(ImageDimension, unsigned int,
00103                       TOutputImage::ImageDimension);
00104   itkStaticConstMacro(InputImageDimension, unsigned int,
00105                       TInputImage::ImageDimension);
00107 
00111   typedef TInputImage                         InputImageType;
00112   typedef TOutputImage                        OutputImageType;
00113   typedef   typename TInputImage::IndexType   IndexType;
00114   typedef   typename TInputImage::SizeType    SizeType;
00115   typedef   typename TOutputImage::RegionType RegionType;
00116 
00120   typedef SmartPointer< Self >       Pointer;
00121   typedef SmartPointer< const Self > ConstPointer;
00122 
00126   itkTypeMacro(RelabelComponentImageFilter, ImageToImageFilter);
00127 
00131   itkNewMacro(Self);
00132 
00134   typedef IdentifierType LabelType;
00135 
00137   typedef SizeValueType ObjectSizeType;
00138 
00141   itkGetConstMacro(NumberOfObjects, LabelType);
00142 
00143   typedef std::vector< ObjectSizeType >   ObjectSizeInPixelsContainerType;
00144   typedef std::vector< float >            ObjectSizeInPhysicalUnitsContainerType;
00145 
00151   itkGetConstMacro(OriginalNumberOfObjects, LabelType);
00152 
00155   itkSetMacro(NumberOfObjectsToPrint, LabelType);
00156   itkGetConstReferenceMacro(NumberOfObjectsToPrint, LabelType);
00158 
00165   itkSetMacro(MinimumObjectSize, ObjectSizeType);
00166 
00172   itkGetConstMacro(MinimumObjectSize, ObjectSizeType);
00173 
00179   const ObjectSizeInPixelsContainerType & GetSizeOfObjectsInPixels() const
00180     {
00181     // The GetConstReferenceMacro can't be used here becase this container
00182     // doesn't have an ostream<< operator overloaded.
00183     return this->m_SizeOfObjectsInPixels;
00184     }
00185 
00191   const ObjectSizeInPhysicalUnitsContainerType & GetSizeOfObjectsInPhysicalUnits() const
00192     {
00193     // The GetConstReferenceMacro can't be used here becase this container
00194     // doesn't have an ostream<< operator overloaded.
00195     return this->m_SizeOfObjectsInPhysicalUnits;
00196     }
00197 
00201   ObjectSizeType GetSizeOfObjectInPixels(LabelType obj) const
00202   {
00203     if ( obj > 0 && obj <= m_NumberOfObjects )
00204       {
00205       return m_SizeOfObjectsInPixels[obj - 1];
00206       }
00207     else
00208       {
00209       return 0;
00210       }
00211   }
00213 
00217   float GetSizeOfObjectInPhysicalUnits(LabelType obj) const
00218   {
00219     if ( obj > 0 && obj <= m_NumberOfObjects )
00220       {
00221       return m_SizeOfObjectsInPhysicalUnits[obj - 1];
00222       }
00223     else
00224       {
00225       return 0;
00226       }
00227   }
00229 
00230 #ifdef ITK_USE_CONCEPT_CHECKING
00231 
00232   itkConceptMacro( InputEqualityComparableCheck,
00233                    ( Concept::EqualityComparable< InputPixelType > ) );
00234   itkConceptMacro( UnsignedLongConvertibleToInputCheck,
00235                    ( Concept::Convertible< LabelType, InputPixelType > ) );
00236   itkConceptMacro( OutputLongConvertibleToUnsignedLongCheck,
00237                    ( Concept::Convertible< OutputPixelType, LabelType > ) );
00238   itkConceptMacro( InputConvertibleToOutputCheck,
00239                    ( Concept::Convertible< InputPixelType, OutputPixelType > ) );
00240   itkConceptMacro( SameDimensionCheck,
00241                    ( Concept::SameDimension< InputImageDimension, ImageDimension > ) );
00242 
00244 #endif
00245 protected:
00246 
00247   RelabelComponentImageFilter():
00248     m_NumberOfObjects(0), m_NumberOfObjectsToPrint(10),
00249     m_OriginalNumberOfObjects(0), m_MinimumObjectSize(0)
00250   { this->InPlaceOff(); }
00251   virtual ~RelabelComponentImageFilter() {}
00252 
00256   void GenerateData();
00257 
00261   void GenerateInputRequestedRegion();
00262 
00264   void PrintSelf(std::ostream & os, Indent indent) const;
00265 
00266   struct RelabelComponentObjectType {
00267     LabelType m_ObjectNumber;
00268     ObjectSizeType m_SizeInPixels;
00269     float m_SizeInPhysicalUnits;
00270   };
00271 
00272   // put the function objects here for sorting in descending order
00273   class RelabelComponentSizeInPixelsComparator
00274   {
00275 public:
00276     bool operator()(const RelabelComponentObjectType & a,
00277                     const RelabelComponentObjectType & b)
00278     {
00279       if ( a.m_SizeInPixels > b.m_SizeInPixels )
00280         {
00281         return true;
00282         }
00283       else if ( a.m_SizeInPixels < b.m_SizeInPixels )
00284         {
00285         return false;
00286         }
00287       // size in pixels and physical units are the same, sort based on
00288       // original object number
00289       else if ( a.m_ObjectNumber < b.m_ObjectNumber )
00290         {
00291         return true;
00292         }
00293       else
00294         {
00295         return false;
00296         }
00297     }
00298   };
00299 private:
00300   RelabelComponentImageFilter(const Self &); //purposely not implemented
00301   void operator=(const Self &); //purposely not implemented
00302 
00303   LabelType      m_NumberOfObjects;
00304   LabelType      m_NumberOfObjectsToPrint;
00305   LabelType      m_OriginalNumberOfObjects;
00306   ObjectSizeType m_MinimumObjectSize;
00307 
00308   ObjectSizeInPixelsContainerType         m_SizeOfObjectsInPixels;
00309   ObjectSizeInPhysicalUnitsContainerType  m_SizeOfObjectsInPhysicalUnits;
00310 };
00311 } // end namespace itk
00312 
00313 #ifndef ITK_MANUAL_INSTANTIATION
00314 #include "itkRelabelComponentImageFilter.hxx"
00315 #endif
00316 
00317 #endif
00318