ITK  4.9.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 <list>
23 #include <map>
24 #include <vector>
25 #include "itkProgressReporter.h"
26 #include "itkBarrier.h"
27 #include "itkLabelMap.h"
28 #include "itkLabelObject.h"
30 
31 namespace itk
32 {
60 template< typename TInputImage,
61  typename TOutputImage =
62  LabelMap< LabelObject< SizeValueType, TInputImage::ImageDimension > > >
64  public ImageToImageFilter< TInputImage, TOutputImage >
65 {
66 public:
74 
78  itkNewMacro(Self);
79 
84 
89 
94  typedef typename TOutputImage::PixelType OutputPixelType;
95  typedef typename TInputImage::PixelType InputPixelType;
98  itkStaticConstMacro(ImageDimension, unsigned int, TOutputImage::ImageDimension);
99  itkStaticConstMacro(OutputImageDimension, unsigned int, TOutputImage::ImageDimension);
100  itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension);
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 ITK_OVERRIDE;
161 
163 
167  void BeforeThreadedGenerateData() ITK_OVERRIDE;
168 
169  void AfterThreadedGenerateData() ITK_OVERRIDE;
170 
171  void ThreadedGenerateData(const RegionType & outputRegionForThread, ThreadIdType threadId) ITK_OVERRIDE;
172 
176  void GenerateInputRequestedRegion() ITK_OVERRIDE;
177 
182  void EnlargeOutputRequestedRegion( DataObject *itkNotUsed(output) ) ITK_OVERRIDE;
183 
187  virtual const ImageRegionSplitterBase* GetImageRegionSplitter() const ITK_OVERRIDE;
188 
189 private:
190  BinaryImageToLabelMapFilter(const Self &) ITK_DELETE_FUNCTION;
191  void operator=(const Self &) ITK_DELETE_FUNCTION;
192 
193  // some additional types
194  typedef typename TOutputImage::RegionType::SizeType OutSizeType;
195 
196  // types to support the run length encoding of lines
197  class runLength
198  {
199  public:
200  // run length information - may be a more type safe way of doing this
202  typename InputImageType::IndexType where; // Index of the start of the run
203  InternalLabelType label; // the initial label of the run
204  };
205 
206  typedef std::vector< runLength > lineEncoding;
207 
208  // the map storing lines
209  typedef std::vector< lineEncoding > LineMapType;
210 
211  typedef std::vector< OffsetValueType > OffsetVectorType;
212 
213  // the types to support union-find operations
214  typedef std::vector< InternalLabelType > UnionFindType;
216 
217  typedef std::vector< OutputPixelType > ConsecutiveVectorType;
219 
220  // functions to support union-find operations
221  void InitUnion(const InternalLabelType size);
222 
223  void InsertSet(const InternalLabelType label);
224 
226 
227  void LinkLabels(const InternalLabelType lab1, const InternalLabelType lab2);
228 
230 
232  bool CheckNeighbors(const OutputIndexType & A,
233  const OutputIndexType & B);
234 
235  void CompareLines(lineEncoding & current, const lineEncoding & Neighbour);
236 
237  void FillOutput(const LineMapType & LineMap,
238  ProgressReporter & progress);
239 
240  void SetupLineOffsets(OffsetVectorType & LineOffsets);
241 
242  void Wait();
243 
246 
248 
250 
251  std::vector< SizeValueType > m_NumberOfLabels;
252  std::vector< SizeValueType > m_FirstLineIdToJoin;
253 
255 
257 
258 #if !defined( ITK_WRAPPING_PARSER )
260 #endif
261 };
262 } // end namespace itk
263 
264 #ifndef ITK_MANUAL_INSTANTIATION
265 #if !defined( ITK_WRAPPING_PARSER )
266 #include "itkBinaryImageToLabelMapFilter.hxx"
267 #endif
268 #endif
269 
270 #endif
void AfterThreadedGenerateData() override
void SetupLineOffsets(OffsetVectorType &LineOffsets)
signed long OffsetValueType
Definition: itkIntTypes.h:154
std::vector< OutputPixelType > ConsecutiveVectorType
void GenerateInputRequestedRegion() override
std::vector< SizeValueType > m_FirstLineIdToJoin
InputImageType::Pointer InputImagePointer
TInputImage::OffsetValueType OffsetValueType
ImageToImageFilter< TInputImage, TOutputImage > Superclass
void CompareLines(lineEncoding &current, const lineEncoding &Neighbour)
void InsertSet(const InternalLabelType label)
void EnlargeOutputRequestedRegion(DataObject *) override
Base class for all process objects that output image data.
void PrintSelf(std::ostream &os, Indent indent) const override
unsigned long SizeValueType
Definition: itkIntTypes.h:143
std::vector< InternalLabelType > UnionFindType
void InitUnion(const InternalLabelType size)
typedef(Concept::SameDimension< itkGetStaticConstMacro(InputImageDimension), itkGetStaticConstMacro(OutputImageDimension) >) SameDimension
Superclass::InputImagePointer InputImagePointer
TOutputImage::RegionType::SizeType OutSizeType
Divide an image region into several pieces.
virtual const ImageRegionSplitterBase * GetImageRegionSplitter() const override
ImageRegionSplitterDirection::Pointer m_ImageRegionSplitter
void LinkLabels(const InternalLabelType lab1, const InternalLabelType lab2)
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
Implements progress tracking for a filter.
void BeforeThreadedGenerateData() override
Label the connected components in a binary image and produce a collection of label objects...
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
InternalLabelType LookupSet(const InternalLabelType label)
#define itkConceptMacro(name, concept)
void ThreadedGenerateData(const RegionType &outputRegionForThread, ThreadIdType threadId) override
Base class for all data objects in ITK.