18 #ifndef itkShapeKeepNObjectsLabelMapFilter_h
19 #define itkShapeKeepNObjectsLabelMapFilter_h
43 template <
typename TImage>
66 static constexpr
unsigned int ImageDimension = TImage::ImageDimension;
74 #ifdef ITK_USE_CONCEPT_CHECKING
90 itkSetMacro(ReverseOrdering,
bool);
91 itkGetConstReferenceMacro(ReverseOrdering,
bool);
92 itkBooleanMacro(ReverseOrdering);
113 this->SetAttribute(LabelObjectType::GetAttributeFromName(s));
121 GenerateData()
override;
123 template <
typename TAttributeAccessor>
128 this->AllocateOutputs();
131 ImageType * output2 = this->GetOutput(1);
135 output2->SetBackgroundValue(output->GetBackgroundValue());
138 using VectorType = std::vector<LabelObjectPointer>;
144 labelObjects.reserve(output->GetNumberOfLabelObjects());
145 typename ImageType::Iterator it(output);
146 while (!it.IsAtEnd())
148 labelObjects.push_back(it.GetLabelObject());
149 progress.CompletedPixel();
154 if (m_NumberOfObjects < output->GetNumberOfLabelObjects())
156 auto end = labelObjects.begin() + m_NumberOfObjects;
157 if (m_ReverseOrdering)
160 std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
165 std::nth_element(labelObjects.begin(), end, labelObjects.end(), comparator);
167 progress.CompletedPixel();
170 for (
typename VectorType::const_iterator it2 = end; it2 != labelObjects.end(); ++it2)
172 output2->AddLabelObject(*it2);
173 output->RemoveLabelObject(*it2);
174 progress.CompletedPixel();
180 PrintSelf(std::ostream & os,
Indent indent)
const override;
182 bool m_ReverseOrdering{};
189 #ifndef ITK_MANUAL_INSTANTIATION
190 # include "itkShapeKeepNObjectsLabelMapFilter.hxx"