Orfeo Toolbox  3.16
itkShapeKeepNObjectsLabelMapFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkShapeKeepNObjectsLabelMapFilter.h,v $
5  Language: C++
6  Date: $Date: 2009-08-05 16:08:10 $
7  Version: $Revision: 1.4 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkShapeKeepNObjectsLabelMapFilter_h
18 #define __itkShapeKeepNObjectsLabelMapFilter_h
19 
23 #include "itkProgressReporter.h"
24 
25 namespace itk {
41 template<class TImage>
43  public InPlaceLabelMapFilter<TImage>
44 {
45 public:
51 
53  typedef TImage ImageType;
54  typedef typename ImageType::Pointer ImagePointer;
55  typedef typename ImageType::ConstPointer ImageConstPointer;
56  typedef typename ImageType::PixelType PixelType;
57  typedef typename ImageType::IndexType IndexType;
58  typedef typename ImageType::LabelObjectType LabelObjectType;
59 
60  typedef typename LabelObjectType::AttributeType AttributeType;
61 
62  typedef typename Superclass::LabelObjectContainerType LabelObjectContainerType;
63 
65  itkStaticConstMacro(ImageDimension, unsigned int, TImage::ImageDimension);
66 
68  itkNewMacro(Self);
69 
72 
73 #ifdef ITK_USE_CONCEPT_CHECKING
74 
75 /* itkConceptMacro(InputEqualityComparableCheck,
76  (Concept::EqualityComparable<InputImagePixelType>));
77  itkConceptMacro(IntConvertibleToInputCheck,
78  (Concept::Convertible<int, InputImagePixelType>));
79  itkConceptMacro(InputOStreamWritableCheck,
80  (Concept::OStreamWritable<InputImagePixelType>));*/
82 #endif
83 
89  itkSetMacro( ReverseOrdering, bool );
90  itkGetConstReferenceMacro( ReverseOrdering, bool );
91  itkBooleanMacro( ReverseOrdering );
92 
96  itkSetMacro( NumberOfObjects, unsigned long );
97  itkGetConstReferenceMacro( NumberOfObjects, unsigned long );
98 
103  itkGetConstMacro( Attribute, AttributeType );
104  itkSetMacro( Attribute, AttributeType );
105 
106  void SetAttribute( const std::string & s )
107  {
108  this->SetAttribute( LabelObjectType::GetAttributeFromName( s ) );
109  }
110 
111 
112 protected:
115 
116  void GenerateData();
117 
118  template <class TAttributeAccessor>
119  void TemplatedGenerateData( const TAttributeAccessor & )
120  {
121  // Allocate the output
122  this->AllocateOutputs();
123 
124  ImageType * output = this->GetOutput();
125  ImageType * output2 = this->GetOutput( 1 );
126 
127  // set the background value for the second output - this is not done in the superclasses
128  output2->SetBackgroundValue( output->GetBackgroundValue() );
129 
130  const LabelObjectContainerType & labelObjectContainer = output->GetLabelObjectContainer();
131  typedef typename LabelObjectType::Pointer LabelObjectPointer;
132  typedef std::vector< LabelObjectPointer > VectorType;
133 
134  ProgressReporter progress( this, 0, 2 * labelObjectContainer.size() );
135 
136  // get the label objects in a vector, so they can be sorted
137  VectorType labelObjects;
138  labelObjects.reserve( labelObjectContainer.size() );
139  typename LabelObjectContainerType::const_iterator it = labelObjectContainer.begin();
140  while( it != labelObjectContainer.end() )
141  {
142  labelObjects.push_back( it->second );
143  progress.CompletedPixel();
144  it++;
145  }
146 
147  // instantiate the comparator and sort the vector
148  if( m_NumberOfObjects < labelObjectContainer.size() )
149  {
150  typename VectorType::iterator end = labelObjects.begin() + m_NumberOfObjects;
151  if( m_ReverseOrdering )
152  {
154  std::nth_element( labelObjects.begin(), end, labelObjects.end(), comparator );
155  }
156  else
157  {
159  std::nth_element( labelObjects.begin(), end, labelObjects.end(), comparator );
160  }
161  progress.CompletedPixel();
162 
163  // and remove the last objects of the map
164  for( typename VectorType::const_iterator it2 = end;
165  it2 != labelObjects.end();
166  it2++ )
167  {
168  output2->AddLabelObject( *it2 );
169  output->RemoveLabelObject( *it2 );
170  progress.CompletedPixel();
171  }
172  }
173  }
174 
175  void PrintSelf(std::ostream& os, Indent indent) const;
176 
178  unsigned long m_NumberOfObjects;
180 
181 private:
182  ShapeKeepNObjectsLabelMapFilter(const Self&); //purposely not implemented
183  void operator=(const Self&); //purposely not implemented
184 
185 }; // end of class
186 
187 } // end namespace itk
188 
189 #ifndef ITK_MANUAL_INSTANTIATION
191 #endif
192 
193 #endif

Generated at Sun Feb 3 2013 00:06:03 for Orfeo Toolbox with doxygen 1.8.1.1