ITK  4.3.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 
25 namespace itk
26 {
43 template< class TImage >
44 class ITK_EXPORT ShapeRelabelLabelMapFilter:
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 
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:
110 
111  void GenerateData();
112 
113  template< class 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;
173 
175 
177 
178 private:
179  ShapeRelabelLabelMapFilter(const Self &); //purposely not implemented
180  void operator=(const Self &); //purposely not implemented
181 }; // end of class
182 } // end namespace itk
183 
184 #ifndef ITK_MANUAL_INSTANTIATION
185 #include "itkShapeRelabelLabelMapFilter.hxx"
186 #endif
187 
188 #endif
189