ITK  4.9.0
Insight Segmentation and Registration Toolkit
itkShapeKeepNObjectsLabelMapFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkShapeKeepNObjectsLabelMapFilter_h
19 #define itkShapeKeepNObjectsLabelMapFilter_h
20 
23 #include "itkProgressReporter.h"
24 
25 namespace itk
26 {
43 template< typename TImage >
45  public InPlaceLabelMapFilter< TImage >
46 {
47 public:
53 
55  typedef TImage ImageType;
56  typedef typename ImageType::Pointer ImagePointer;
57  typedef typename ImageType::ConstPointer ImageConstPointer;
58  typedef typename ImageType::PixelType PixelType;
59  typedef typename ImageType::IndexType IndexType;
60  typedef typename ImageType::LabelObjectType LabelObjectType;
61 
62  typedef typename LabelObjectType::AttributeType AttributeType;
63 
65  itkStaticConstMacro(ImageDimension, unsigned int, TImage::ImageDimension);
66 
68  itkNewMacro(Self);
69 
72 
73 #ifdef ITK_USE_CONCEPT_CHECKING
74  // Begin concept checking
75 /* itkConceptMacro(InputEqualityComparableCheck,
76  (Concept::EqualityComparable<InputImagePixelType>));
77  itkConceptMacro(IntConvertibleToInputCheck,
78  (Concept::Convertible<int, InputImagePixelType>));
79  itkConceptMacro(InputOStreamWritableCheck,
80  (Concept::OStreamWritable<InputImagePixelType>));*/
81 // End concept checking
82 #endif
83 
89  itkSetMacro(ReverseOrdering, bool);
90  itkGetConstReferenceMacro(ReverseOrdering, bool);
91  itkBooleanMacro(ReverseOrdering);
93 
97  itkSetMacro(NumberOfObjects, SizeValueType);
98  itkGetConstReferenceMacro(NumberOfObjects, SizeValueType);
100 
105  itkGetConstMacro(Attribute, AttributeType);
106  itkSetMacro(Attribute, AttributeType);
108 
109  void SetAttribute(const std::string & s)
110  {
111  this->SetAttribute( LabelObjectType::GetAttributeFromName(s) );
112  }
113 
114 protected:
117 
118  void GenerateData() ITK_OVERRIDE;
119 
120  template< typename TAttributeAccessor >
121  void TemplatedGenerateData(const TAttributeAccessor &)
122  {
123  // Allocate the output
124  this->AllocateOutputs();
125 
126  ImageType *output = this->GetOutput();
127  ImageType *output2 = this->GetOutput(1);
128 
129  // set the background value for the second output - this is not done in the
130  // superclasses
131  output2->SetBackgroundValue( output->GetBackgroundValue() );
132 
133  typedef typename LabelObjectType::Pointer LabelObjectPointer;
134  typedef std::vector< LabelObjectPointer > VectorType;
135 
136  ProgressReporter progress( this, 0, 2 * output->GetNumberOfLabelObjects() );
137 
138  // get the label objects in a vector, so they can be sorted
139  VectorType labelObjects;
140  labelObjects.reserve( output->GetNumberOfLabelObjects() );
141  typename ImageType::Iterator it( output );
142  while ( ! it.IsAtEnd() )
143  {
144  labelObjects.push_back( it.GetLabelObject() );
145  progress.CompletedPixel();
146  ++it;
147  }
148 
149  // instantiate the comparator and sort the vector
150  if ( m_NumberOfObjects < output->GetNumberOfLabelObjects() )
151  {
152  typename VectorType::iterator end = labelObjects.begin() + m_NumberOfObjects;
153  if ( m_ReverseOrdering )
154  {
156  std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
157  }
158  else
159  {
161  std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
162  }
163  progress.CompletedPixel();
164 
165  // and remove the last objects of the map
166  for ( typename VectorType::const_iterator it2 = end;
167  it2 != labelObjects.end();
168  it2++ )
169  {
170  output2->AddLabelObject(*it2);
171  output->RemoveLabelObject(*it2);
172  progress.CompletedPixel();
173  }
174  }
175  }
176 
177  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
178 
180 
183 
184 private:
185  ShapeKeepNObjectsLabelMapFilter(const Self &) ITK_DELETE_FUNCTION;
186  void operator=(const Self &) ITK_DELETE_FUNCTION;
187 }; // end of class
188 } // end namespace itk
189 
190 #ifndef ITK_MANUAL_INSTANTIATION
191 #include "itkShapeKeepNObjectsLabelMapFilter.hxx"
192 #endif
193 
194 #endif
Light weight base class for most itk classes.
virtual void SetAttribute(AttributeType _arg)
void PrintSelf(std::ostream &os, Indent indent) const override
virtual void AllocateOutputs() override
unsigned long SizeValueType
Definition: itkIntTypes.h:143
void TemplatedGenerateData(const TAttributeAccessor &)
Base class for filters that takes an image as input and overwrites that image as the output...
Implements progress tracking for a filter.
Keep N objects according to their shape attributes.
OutputImageType * GetOutput()
Control indentation during Print() invocation.
Definition: itkIndent.h:49