ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkShapeRelabelLabelMapFilter.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 itkShapeRelabelLabelMapFilter_h
19 #define itkShapeRelabelLabelMapFilter_h
20 
22 #include "itkLabelObject.h"
24 #include "itkProgressReporter.h"
25 
26 namespace itk
27 {
44 template< typename TImage >
45 class ITK_TEMPLATE_EXPORT ShapeRelabelLabelMapFilter:
46  public InPlaceLabelMapFilter< TImage >
47 {
48 public:
49  ITK_DISALLOW_COPY_AND_ASSIGN(ShapeRelabelLabelMapFilter);
50 
56 
58  using ImageType = TImage;
59  using ImagePointer = typename ImageType::Pointer;
60  using ImageConstPointer = typename ImageType::ConstPointer;
61  using PixelType = typename ImageType::PixelType;
62  using IndexType = typename ImageType::IndexType;
63  using LabelObjectType = typename ImageType::LabelObjectType;
64 
65  using AttributeType = typename LabelObjectType::AttributeType;
66 
68  static constexpr unsigned int ImageDimension = TImage::ImageDimension;
69 
71  itkNewMacro(Self);
72 
75 
76 #ifdef ITK_USE_CONCEPT_CHECKING
77  // Begin concept checking
78 /* itkConceptMacro(InputEqualityComparableCheck,
79  (Concept::EqualityComparable<InputImagePixelType>));
80  itkConceptMacro(IntConvertibleToInputCheck,
81  (Concept::Convertible<int, InputImagePixelType>));
82  itkConceptMacro(InputOStreamWritableCheck,
83  (Concept::OStreamWritable<InputImagePixelType>));*/
84 // End concept checking
85 #endif
86 
92  itkSetMacro(ReverseOrdering, bool);
93  itkGetConstReferenceMacro(ReverseOrdering, bool);
94  itkBooleanMacro(ReverseOrdering);
96 
101  itkGetConstMacro(Attribute, AttributeType);
102  itkSetMacro(Attribute, AttributeType);
103  void SetAttribute(const std::string & s)
104  {
105  this->SetAttribute( LabelObjectType::GetAttributeFromName(s) );
106  }
108 
109 protected:
111  ~ShapeRelabelLabelMapFilter() override = default;
112 
113  void GenerateData() override;
114 
115  template< typename TAttributeAccessor >
116  void TemplatedGenerateData(const TAttributeAccessor &)
117  {
118  // Allocate the output
119  this->AllocateOutputs();
120 
121  ImageType *output = this->GetOutput();
122 
123  using LabelObjectPointer = typename LabelObjectType::Pointer;
124  using VectorType = std::vector< LabelObjectPointer >;
125 
126  ProgressReporter progress( this, 0, 2 * output->GetNumberOfLabelObjects() );
127 
128  // Get the label objects in a vector, so they can be sorted
129  VectorType labelObjects;
130  labelObjects.reserve( output->GetNumberOfLabelObjects() );
131  for ( typename ImageType::Iterator it( output );
132  ! it.IsAtEnd();
133  ++it )
134  {
135  labelObjects.push_back(it.GetLabelObject());
136  progress.CompletedPixel();
137  }
138 
139  // Instantiate the comparator and sort the vector
140  if ( m_ReverseOrdering )
141  {
142  std::sort( labelObjects.begin(), labelObjects.end(),
144  }
145  else
146  {
147  std::sort( labelObjects.begin(), labelObjects.end(),
149  }
150  // progress.CompletedPixel();
151 
152  // and put back the objects in the map
153  output->ClearLabels();
155  typename VectorType::const_iterator it2 = labelObjects.begin();
156  while ( it2 != labelObjects.end() )
157  {
158  // Avoid the background label if it is used
159  if ( label == output->GetBackgroundValue() )
160  {
161  label++;
162  }
163  ( *it2 )->SetLabel(label);
164  output->AddLabelObject(*it2);
165 
166  // Go to the next label
167  label++;
168  progress.CompletedPixel();
169 
170  ++it2;
171  }
172  }
173 
174  void PrintSelf(std::ostream & os, Indent indent) const override;
175 
177 
179 }; // end of class
180 } // end namespace itk
181 
182 #ifndef ITK_MANUAL_INSTANTIATION
183 #include "itkShapeRelabelLabelMapFilter.hxx"
184 #endif
185 
186 #endif
Relabels objects according to their shape attributes.
Light weight base class for most itk classes.
typename ImageType::ConstPointer ImageConstPointer
Define numeric traits for std::vector.
typename LabelObjectType::AttributeType AttributeType
Base class for filters that takes an image as input and overwrites that image as the output...
Implements progress tracking for a filter.
Control indentation during Print() invocation.
Definition: itkIndent.h:49
typename ImageType::IndexType IndexType
void TemplatedGenerateData(const TAttributeAccessor &)
typename ImageType::PixelType PixelType
typename ImageType::LabelObjectType LabelObjectType