ITK  4.6.0
Insight Segmentation and Registration Toolkit
itkBinaryImageToLabelMapFilter.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 __itkBinaryImageToLabelMapFilter_h
19 #define __itkBinaryImageToLabelMapFilter_h
20 
21 #include "itkImageToImageFilter.h"
22 #include <vector>
23 #include <map>
24 #include "itkProgressReporter.h"
25 #include "itkBarrier.h"
26 #include "itkLabelMap.h"
27 #include "itkLabelObject.h"
28 
29 namespace itk
30 {
58 template< typename TInputImage,
59  typename TOutputImage =
60  LabelMap< LabelObject< SizeValueType, TInputImage::ImageDimension > > >
62  public ImageToImageFilter< TInputImage, TOutputImage >
63 {
64 public:
72 
76  itkNewMacro(Self);
77 
82 
87 
92  typedef typename TOutputImage::PixelType OutputPixelType;
93  typedef typename TInputImage::PixelType InputPixelType;
96  itkStaticConstMacro(ImageDimension, unsigned int, TOutputImage::ImageDimension);
97  itkStaticConstMacro(OutputImageDimension, unsigned int, TOutputImage::ImageDimension);
98  itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension);
100 
102 
106  typedef TInputImage InputImageType;
107  typedef typename TInputImage::IndexType IndexType;
108  typedef typename TInputImage::SizeType SizeType;
109  typedef typename TInputImage::OffsetType OffsetType;
110 
111  typedef TOutputImage OutputImageType;
112  typedef typename TOutputImage::RegionType RegionType;
113  typedef typename TOutputImage::IndexType OutputIndexType;
114  typedef typename TOutputImage::SizeType OutputSizeType;
115  typedef typename TOutputImage::OffsetType OutputOffsetType;
116  typedef typename TOutputImage::PixelType OutputImagePixelType;
117 
118  typedef std::list< IndexType > ListType;
119 
126  itkSetMacro(FullyConnected, bool);
127  itkGetConstReferenceMacro(FullyConnected, bool);
128  itkBooleanMacro(FullyConnected);
130 
131  // only set after completion
132  itkGetConstReferenceMacro(NumberOfObjects, SizeValueType);
133 
138  itkSetMacro(OutputBackgroundValue, OutputPixelType);
139  itkGetConstMacro(OutputBackgroundValue, OutputPixelType);
141 
146  itkSetMacro(InputForegroundValue, InputPixelType);
147  itkGetConstMacro(InputForegroundValue, InputPixelType);
149 
150 #ifdef ITK_USE_CONCEPT_CHECKING
151  // Concept checking -- input and output dimensions must be the same
152  itkConceptMacro( SameDimension,
153  ( Concept::SameDimension< itkGetStaticConstMacro(InputImageDimension),
154  itkGetStaticConstMacro(OutputImageDimension) > ) );
155 #endif
156 
157 protected:
160  void PrintSelf(std::ostream & os, Indent indent) const;
161 
166 
168 
169  void ThreadedGenerateData(const RegionType & outputRegionForThread, ThreadIdType threadId);
170 
175 
180  void EnlargeOutputRequestedRegion( DataObject *itkNotUsed(output) );
181 
182 private:
183  BinaryImageToLabelMapFilter(const Self &); //purposely not implemented
184  void operator=(const Self &); //purposely not implemented
185 
186  // some additional types
187  typedef typename TOutputImage::RegionType::SizeType OutSizeType;
188 
189  // types to support the run length encoding of lines
190  class runLength
191  {
192  public:
193  // run length information - may be a more type safe way of doing this
195  typename InputImageType::IndexType where; // Index of the start of the run
196  LabelType label; // the initial label of the run
197  };
198 
199  typedef std::vector< runLength > lineEncoding;
200 
201  // the map storing lines
202  typedef std::vector< lineEncoding > LineMapType;
203 
204  typedef std::vector< OffsetValueType > OffsetVectorType;
205 
206  // the types to support union-find operations
207  typedef std::vector< LabelType > UnionFindType;
210  // functions to support union-find operations
211  void InitUnion(const LabelType size)
212  {
213  m_UnionFind = UnionFindType(size + 1);
214  }
215 
216  void InsertSet(const LabelType label);
217 
218  LabelType LookupSet(const LabelType label);
219 
220  void LinkLabels(const LabelType lab1, const LabelType lab2);
221 
223 
225  bool CheckNeighbors(const OutputIndexType & A,
226  const OutputIndexType & B);
227 
228  void CompareLines(lineEncoding & current, const lineEncoding & Neighbour);
229 
230  void FillOutput(const LineMapType & LineMap,
231  ProgressReporter & progress);
232 
233  void SetupLineOffsets(OffsetVectorType & LineOffsets);
234 
235  void Wait()
236  {
237  // use m_NumberOfLabels.size() to get the number of thread used
238  if ( m_NumberOfLabels.size() > 1 )
239  {
240  m_Barrier->Wait();
241  }
242  }
243 
246 
248 
250 
251  typename std::vector< SizeValueType > m_NumberOfLabels;
252  typename std::vector< SizeValueType > m_FirstLineIdToJoin;
253 
255 
256 #if !defined( CABLE_CONFIGURATION )
258 #endif
259 };
260 } // end namespace itk
261 
262 #ifndef ITK_MANUAL_INSTANTIATION
263 #if !defined( CABLE_CONFIGURATION )
264 #include "itkBinaryImageToLabelMapFilter.hxx"
265 #endif
266 #endif
267 
268 #endif
void SetupLineOffsets(OffsetVectorType &LineOffsets)
signed long OffsetValueType
Definition: itkIntTypes.h:154
void InsertSet(const LabelType label)
std::vector< SizeValueType > m_FirstLineIdToJoin
InputImageType::Pointer InputImagePointer
void EnlargeOutputRequestedRegion(DataObject *)
TInputImage::OffsetValueType OffsetValueType
ImageToImageFilter< TInputImage, TOutputImage > Superclass
void CompareLines(lineEncoding &current, const lineEncoding &Neighbour)
Base class for all process objects that output image data.
unsigned long SizeValueType
Definition: itkIntTypes.h:143
void PrintSelf(std::ostream &os, Indent indent) const
Superclass::InputImagePointer InputImagePointer
TOutputImage::RegionType::SizeType OutSizeType
Implements progress tracking for a filter.
void LinkLabels(const LabelType lab1, const LabelType lab2)
Label the connected components in a binary image and produce a collection of label objects...
void ThreadedGenerateData(const RegionType &outputRegionForThread, ThreadIdType threadId)
Base class for filters that take an image as input and produce an image as output.
Control indentation during Print() invocation.
Definition: itkIndent.h:49
void FillOutput(const LineMapType &LineMap, ProgressReporter &progress)
bool CheckNeighbors(const OutputIndexType &A, const OutputIndexType &B)
std::vector< OffsetValueType > OffsetVectorType
LabelType LookupSet(const LabelType label)
#define itkConceptMacro(name, concept)
Base class for all data objects in ITK.
unsigned int ThreadIdType
Definition: itkIntTypes.h:159