ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkShapeKeepNObjectsLabelMapFilter.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 __itkShapeKeepNObjectsLabelMapFilter_h
00019 #define __itkShapeKeepNObjectsLabelMapFilter_h
00020 
00021 #include "itkInPlaceLabelMapFilter.h"
00022 #include "itkShapeLabelObjectAccessors.h"
00023 #include "itkProgressReporter.h"
00024 
00025 namespace itk
00026 {
00043 template< class TImage >
00044 class ITK_EXPORT ShapeKeepNObjectsLabelMapFilter:
00045   public InPlaceLabelMapFilter< TImage >
00046 {
00047 public:
00049   typedef ShapeKeepNObjectsLabelMapFilter 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(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, SizeValueType);
00099   itkGetConstReferenceMacro(NumberOfObjects, SizeValueType);
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 protected:
00116   ShapeKeepNObjectsLabelMapFilter();
00117   ~ShapeKeepNObjectsLabelMapFilter() {}
00118 
00119   void GenerateData();
00120 
00121   template< class TAttributeAccessor >
00122   void TemplatedGenerateData(const TAttributeAccessor &)
00123   {
00124     // Allocate the output
00125     this->AllocateOutputs();
00126 
00127     ImageType *output = this->GetOutput();
00128     ImageType *output2 = this->GetOutput(1);
00129 
00130     // set the background value for the second output - this is not done in the
00131     // superclasses
00132     output2->SetBackgroundValue( output->GetBackgroundValue() );
00133 
00134     typedef typename LabelObjectType::Pointer LabelObjectPointer;
00135     typedef std::vector< LabelObjectPointer > VectorType;
00136 
00137     ProgressReporter progress( this, 0, 2 * output->GetNumberOfLabelObjects() );
00138 
00139     // get the label objects in a vector, so they can be sorted
00140     VectorType labelObjects;
00141     labelObjects.reserve( output->GetNumberOfLabelObjects() );
00142     typename ImageType::Iterator it( output );
00143     while ( ! it.IsAtEnd() )
00144       {
00145       labelObjects.push_back( it.GetLabelObject() );
00146       progress.CompletedPixel();
00147       ++it;
00148       }
00149 
00150     // instantiate the comparator and sort the vector
00151     if ( m_NumberOfObjects < output->GetNumberOfLabelObjects() )
00152       {
00153       typename VectorType::iterator end = labelObjects.begin() + m_NumberOfObjects;
00154       if ( m_ReverseOrdering )
00155         {
00156         Functor::LabelObjectReverseComparator< LabelObjectType, TAttributeAccessor > comparator;
00157         std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
00158         }
00159       else
00160         {
00161         Functor::LabelObjectComparator< LabelObjectType, TAttributeAccessor > comparator;
00162         std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
00163         }
00164       progress.CompletedPixel();
00165 
00166       // and remove the last objects of the map
00167       for ( typename VectorType::const_iterator it2 = end;
00168             it2 != labelObjects.end();
00169             it2++ )
00170         {
00171         output2->AddLabelObject(*it2);
00172         output->RemoveLabelObject(*it2);
00173         progress.CompletedPixel();
00174         }
00175       }
00176   }
00177 
00178   void PrintSelf(std::ostream & os, Indent indent) const;
00179 
00180   bool m_ReverseOrdering;
00181 
00182   SizeValueType m_NumberOfObjects;
00183   AttributeType m_Attribute;
00184 private:
00185   ShapeKeepNObjectsLabelMapFilter(const Self &); //purposely not implemented
00186   void operator=(const Self &);                  //purposely not implemented
00187 };                                               // end of class
00188 } // end namespace itk
00189 
00190 #ifndef ITK_MANUAL_INSTANTIATION
00191 #include "itkShapeKeepNObjectsLabelMapFilter.hxx"
00192 #endif
00193 
00194 #endif
00195