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