ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkShapeRelabelLabelMapFilter.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 __itkShapeRelabelLabelMapFilter_h
00019 #define __itkShapeRelabelLabelMapFilter_h
00020 
00021 #include "itkInPlaceLabelMapFilter.h"
00022 #include "itkLabelObject.h"
00023 #include "itkShapeLabelObjectAccessors.h"
00024 
00025 namespace itk
00026 {
00043 template< class TImage >
00044 class ITK_EXPORT ShapeRelabelLabelMapFilter:
00045   public InPlaceLabelMapFilter< TImage >
00046 {
00047 public:
00049   typedef ShapeRelabelLabelMapFilter      Self;
00050   typedef InPlaceLabelMapFilter< TImage > Superclass;
00051   typedef SmartPointer< Self >            Pointer;
00052   typedef SmartPointer< const Self >      ConstPointer;
00053 
00055   typedef TImage                              ImageType;
00056   typedef typename ImageType::Pointer         ImagePointer;
00057   typedef typename ImageType::ConstPointer    ImageConstPointer;
00058   typedef typename ImageType::PixelType       PixelType;
00059   typedef typename ImageType::IndexType       IndexType;
00060   typedef typename ImageType::LabelObjectType LabelObjectType;
00061 
00062   typedef typename LabelObjectType::AttributeType AttributeType;
00063 
00065   itkStaticConstMacro(ImageDimension, unsigned int, TImage::ImageDimension);
00066 
00068   itkNewMacro(Self);
00069 
00071   itkTypeMacro(ShapeRelabelLabelMapFilter, 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 
00099   itkGetConstMacro(Attribute, AttributeType);
00100   itkSetMacro(Attribute, AttributeType);
00101   void SetAttribute(const std::string & s)
00102   {
00103     this->SetAttribute( LabelObjectType::GetAttributeFromName(s) );
00104   }
00106 
00107 protected:
00108   ShapeRelabelLabelMapFilter();
00109   ~ShapeRelabelLabelMapFilter() {}
00110 
00111   void GenerateData();
00112 
00113   template< class TAttributeAccessor >
00114   void TemplatedGenerateData(const TAttributeAccessor &)
00115   {
00116     // Allocate the output
00117     this->AllocateOutputs();
00118 
00119     ImageType *output = this->GetOutput();
00120 
00121     typedef typename LabelObjectType::Pointer LabelObjectPointer;
00122     typedef std::vector< LabelObjectPointer > VectorType;
00123 
00124     ProgressReporter progress( this, 0, 2 * output->GetNumberOfLabelObjects() );
00125 
00126     // Get the label objects in a vector, so they can be sorted
00127     VectorType labelObjects;
00128     labelObjects.reserve( output->GetNumberOfLabelObjects() );
00129     for ( typename ImageType::Iterator it( output );
00130           ! it.IsAtEnd();
00131           ++it )
00132       {
00133       labelObjects.push_back(it.GetLabelObject());
00134       progress.CompletedPixel();
00135       }
00136 
00137     // Instantiate the comparator and sort the vector
00138     if ( m_ReverseOrdering )
00139       {
00140       std::sort( labelObjects.begin(), labelObjects.end(),
00141                  Functor::LabelObjectReverseComparator< LabelObjectType, TAttributeAccessor >() );
00142       }
00143     else
00144       {
00145       std::sort( labelObjects.begin(), labelObjects.end(),
00146                  Functor::LabelObjectComparator< LabelObjectType, TAttributeAccessor >() );
00147       }
00148     //   progress.CompletedPixel();
00149 
00150     // and put back the objects in the map
00151     output->ClearLabels();
00152     PixelType label = NumericTraits<PixelType>::ZeroValue();
00153     typename VectorType::const_iterator it2 = labelObjects.begin();
00154     while ( it2 != labelObjects.end() )
00155       {
00156       // Avoid the background label if it is used
00157       if ( label == output->GetBackgroundValue() )
00158         {
00159         label++;
00160         }
00161       ( *it2 )->SetLabel(label);
00162       output->AddLabelObject(*it2);
00163 
00164       // Go to the next label
00165       label++;
00166       progress.CompletedPixel();
00167 
00168       ++it2;
00169       }
00170   }
00171 
00172   void PrintSelf(std::ostream & os, Indent indent) const;
00173 
00174   bool m_ReverseOrdering;
00175 
00176   AttributeType m_Attribute;
00177 private:
00178   ShapeRelabelLabelMapFilter(const Self &); //purposely not implemented
00179   void operator=(const Self &);             //purposely not implemented
00180 };                                          // end of class
00181 } // end namespace itk
00182 
00183 #ifndef ITK_MANUAL_INSTANTIATION
00184 #include "itkShapeRelabelLabelMapFilter.hxx"
00185 #endif
00186 
00187 #endif
00188