ITK  5.1.0
Insight Toolkit
itkShapeKeepNObjectsLabelMapFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 {
44 template <typename TImage>
45 class ITK_TEMPLATE_EXPORT ShapeKeepNObjectsLabelMapFilter : public InPlaceLabelMapFilter<TImage>
46 {
47 public:
48  ITK_DISALLOW_COPY_AND_ASSIGN(ShapeKeepNObjectsLabelMapFilter);
49 
55 
57  using ImageType = TImage;
58  using ImagePointer = typename ImageType::Pointer;
59  using ImageConstPointer = typename ImageType::ConstPointer;
60  using PixelType = typename ImageType::PixelType;
61  using IndexType = typename ImageType::IndexType;
62  using LabelObjectType = typename ImageType::LabelObjectType;
63 
64  using AttributeType = typename LabelObjectType::AttributeType;
65 
67  static constexpr unsigned int ImageDimension = TImage::ImageDimension;
68 
70  itkNewMacro(Self);
71 
74 
75 #ifdef ITK_USE_CONCEPT_CHECKING
76  // Begin concept checking
77 /* itkConceptMacro(InputEqualityComparableCheck,
78  (Concept::EqualityComparable<InputImagePixelType>));
79  itkConceptMacro(IntConvertibleToInputCheck,
80  (Concept::Convertible<int, InputImagePixelType>));
81  itkConceptMacro(InputOStreamWritableCheck,
82  (Concept::OStreamWritable<InputImagePixelType>));*/
83 // End concept checking
84 #endif
85 
91  itkSetMacro(ReverseOrdering, bool);
92  itkGetConstReferenceMacro(ReverseOrdering, bool);
93  itkBooleanMacro(ReverseOrdering);
95 
99  itkSetMacro(NumberOfObjects, SizeValueType);
100  itkGetConstReferenceMacro(NumberOfObjects, SizeValueType);
102 
107  itkGetConstMacro(Attribute, AttributeType);
108  itkSetMacro(Attribute, AttributeType);
110 
111  void
112  SetAttribute(const std::string & s)
113  {
114  this->SetAttribute(LabelObjectType::GetAttributeFromName(s));
115  }
116 
117 protected:
119  ~ShapeKeepNObjectsLabelMapFilter() override = default;
120 
121  void
122  GenerateData() override;
123 
124  template <typename TAttributeAccessor>
125  void
126  TemplatedGenerateData(const TAttributeAccessor &)
127  {
128  // Allocate the output
129  this->AllocateOutputs();
130 
131  ImageType * output = this->GetOutput();
132  ImageType * output2 = this->GetOutput(1);
133 
134  // set the background value for the second output - this is not done in the
135  // superclasses
136  output2->SetBackgroundValue(output->GetBackgroundValue());
137 
138  using LabelObjectPointer = typename LabelObjectType::Pointer;
139  using VectorType = std::vector<LabelObjectPointer>;
140 
141  ProgressReporter progress(this, 0, 2 * output->GetNumberOfLabelObjects());
142 
143  // get the label objects in a vector, so they can be sorted
144  VectorType labelObjects;
145  labelObjects.reserve(output->GetNumberOfLabelObjects());
146  typename ImageType::Iterator it(output);
147  while (!it.IsAtEnd())
148  {
149  labelObjects.push_back(it.GetLabelObject());
150  progress.CompletedPixel();
151  ++it;
152  }
153 
154  // instantiate the comparator and sort the vector
155  if (m_NumberOfObjects < output->GetNumberOfLabelObjects())
156  {
157  auto end = labelObjects.begin() + m_NumberOfObjects;
158  if (m_ReverseOrdering)
159  {
161  std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
162  }
163  else
164  {
166  std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
167  }
168  progress.CompletedPixel();
169 
170  // and remove the last objects of the map
171  for (typename VectorType::const_iterator it2 = end; it2 != labelObjects.end(); it2++)
172  {
173  output2->AddLabelObject(*it2);
174  output->RemoveLabelObject(*it2);
175  progress.CompletedPixel();
176  }
177  }
178  }
179 
180  void
181  PrintSelf(std::ostream & os, Indent indent) const override;
182 
184 
187 }; // end of class
188 } // end namespace itk
189 
190 #ifndef ITK_MANUAL_INSTANTIATION
191 # include "itkShapeKeepNObjectsLabelMapFilter.hxx"
192 #endif
193 
194 #endif
itk::InPlaceLabelMapFilter
Base class for filters that takes an image as input and overwrites that image as the output.
Definition: itkInPlaceLabelMapFilter.h:84
itk::GTest::TypedefsAndConstructors::Dimension2::VectorType
ImageBaseType::SpacingType VectorType
Definition: itkGTestTypedefsAndConstructors.h:53
itkProgressReporter.h
itk::ShapeKeepNObjectsLabelMapFilter
Keep N objects according to their shape attributes.
Definition: itkShapeKeepNObjectsLabelMapFilter.h:45
itk::Functor::LabelObjectComparator
Definition: itkLabelObjectAccessors.h:66
itk::ShapeKeepNObjectsLabelMapFilter::PixelType
typename ImageType::PixelType PixelType
Definition: itkShapeKeepNObjectsLabelMapFilter.h:60
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itkShapeLabelObjectAccessors.h
itk::Functor::LabelObjectReverseComparator
Definition: itkLabelObjectAccessors.h:85
itk::ShapeKeepNObjectsLabelMapFilter::ImageConstPointer
typename ImageType::ConstPointer ImageConstPointer
Definition: itkShapeKeepNObjectsLabelMapFilter.h:59
itk::ShapeKeepNObjectsLabelMapFilter::m_NumberOfObjects
SizeValueType m_NumberOfObjects
Definition: itkShapeKeepNObjectsLabelMapFilter.h:185
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::ShapeKeepNObjectsLabelMapFilter::TemplatedGenerateData
void TemplatedGenerateData(const TAttributeAccessor &)
Definition: itkShapeKeepNObjectsLabelMapFilter.h:126
itk::ShapeKeepNObjectsLabelMapFilter::AttributeType
typename LabelObjectType::AttributeType AttributeType
Definition: itkShapeKeepNObjectsLabelMapFilter.h:64
itk::ShapeKeepNObjectsLabelMapFilter::m_ReverseOrdering
bool m_ReverseOrdering
Definition: itkShapeKeepNObjectsLabelMapFilter.h:183
itkInPlaceLabelMapFilter.h
itk::ShapeKeepNObjectsLabelMapFilter::ImageType
TImage ImageType
Definition: itkShapeKeepNObjectsLabelMapFilter.h:57
itk::ShapeKeepNObjectsLabelMapFilter::SetAttribute
void SetAttribute(const std::string &s)
Definition: itkShapeKeepNObjectsLabelMapFilter.h:112
itk::ShapeKeepNObjectsLabelMapFilter::IndexType
typename ImageType::IndexType IndexType
Definition: itkShapeKeepNObjectsLabelMapFilter.h:61
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::ShapeKeepNObjectsLabelMapFilter::ImagePointer
typename ImageType::Pointer ImagePointer
Definition: itkShapeKeepNObjectsLabelMapFilter.h:58
itk::ShapeKeepNObjectsLabelMapFilter::m_Attribute
AttributeType m_Attribute
Definition: itkShapeKeepNObjectsLabelMapFilter.h:186
itk::ShapeKeepNObjectsLabelMapFilter::LabelObjectType
typename ImageType::LabelObjectType LabelObjectType
Definition: itkShapeKeepNObjectsLabelMapFilter.h:62
itk::ProgressReporter
Implements progress tracking for a filter.
Definition: itkProgressReporter.h:60
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83