Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00076
00077
00078
00079
00080
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
00126 this->AllocateOutputs();
00127
00128 ImageType * output = this->GetOutput();
00129 ImageType * output2 = this->GetOutput( 1 );
00130
00131
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
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
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
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&);
00187 void operator=(const Self&);
00188
00189 };
00190
00191 }
00192
00193 #ifndef ITK_MANUAL_INSTANTIATION
00194 #include "itkShapeKeepNObjectsLabelMapFilter.txx"
00195 #endif
00196
00197 #endif
00198