ITK  4.4.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< class 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 
75 /* itkConceptMacro(InputEqualityComparableCheck,
76  (Concept::EqualityComparable<InputImagePixelType>));
77  itkConceptMacro(IntConvertibleToInputCheck,
78  (Concept::Convertible<int, InputImagePixelType>));
79  itkConceptMacro(InputOStreamWritableCheck,
80  (Concept::OStreamWritable<InputImagePixelType>));*/
81 
83 #endif
84 
90  itkSetMacro(ReverseOrdering, bool);
91  itkGetConstReferenceMacro(ReverseOrdering, bool);
92  itkBooleanMacro(ReverseOrdering);
94 
98  itkSetMacro(NumberOfObjects, SizeValueType);
99  itkGetConstReferenceMacro(NumberOfObjects, SizeValueType);
101 
106  itkGetConstMacro(Attribute, AttributeType);
107  itkSetMacro(Attribute, AttributeType);
109 
110  void SetAttribute(const std::string & s)
111  {
112  this->SetAttribute( LabelObjectType::GetAttributeFromName(s) );
113  }
114 
115 protected:
118 
119  void GenerateData();
120 
121  template< class TAttributeAccessor >
122  void TemplatedGenerateData(const TAttributeAccessor &)
123  {
124  // Allocate the output
125  this->AllocateOutputs();
126 
127  ImageType *output = this->GetOutput();
128  ImageType *output2 = this->GetOutput(1);
129 
130  // set the background value for the second output - this is not done in the
131  // superclasses
132  output2->SetBackgroundValue( output->GetBackgroundValue() );
133 
134  typedef typename LabelObjectType::Pointer LabelObjectPointer;
135  typedef std::vector< LabelObjectPointer > VectorType;
136 
137  ProgressReporter progress( this, 0, 2 * output->GetNumberOfLabelObjects() );
138 
139  // get the label objects in a vector, so they can be sorted
140  VectorType labelObjects;
141  labelObjects.reserve( output->GetNumberOfLabelObjects() );
142  typename ImageType::Iterator it( output );
143  while ( ! it.IsAtEnd() )
144  {
145  labelObjects.push_back( it.GetLabelObject() );
146  progress.CompletedPixel();
147  ++it;
148  }
149 
150  // instantiate the comparator and sort the vector
151  if ( m_NumberOfObjects < output->GetNumberOfLabelObjects() )
152  {
153  typename VectorType::iterator end = labelObjects.begin() + m_NumberOfObjects;
154  if ( m_ReverseOrdering )
155  {
157  std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
158  }
159  else
160  {
162  std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
163  }
164  progress.CompletedPixel();
165 
166  // and remove the last objects of the map
167  for ( typename VectorType::const_iterator it2 = end;
168  it2 != labelObjects.end();
169  it2++ )
170  {
171  output2->AddLabelObject(*it2);
172  output->RemoveLabelObject(*it2);
173  progress.CompletedPixel();
174  }
175  }
176  }
177 
178  void PrintSelf(std::ostream & os, Indent indent) const;
179 
181 
184 
185 private:
186  ShapeKeepNObjectsLabelMapFilter(const Self &); //purposely not implemented
187  void operator=(const Self &); //purposely not implemented
188 }; // end of class
189 } // end namespace itk
190 
191 #ifndef ITK_MANUAL_INSTANTIATION
192 #include "itkShapeKeepNObjectsLabelMapFilter.hxx"
193 #endif
194 
195 #endif
196