ITK  4.6.0
Insight Segmentation and Registration Toolkit
itkConnectedComponentImageFilter.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 __itkConnectedComponentImageFilter_h
19 #define __itkConnectedComponentImageFilter_h
20 
21 #include "itkImageToImageFilter.h"
22 #include "itkImage.h"
23 #include <vector>
24 #include <map>
25 #include "itkProgressReporter.h"
26 #include "itkBarrier.h"
27 
28 namespace itk
29 {
56 template< typename TInputImage, typename TOutputImage, typename TMaskImage = TInputImage >
58  public ImageToImageFilter< TInputImage, TOutputImage >
59 {
60 public:
66 
71 
76  typedef typename TOutputImage::PixelType OutputPixelType;
77  typedef typename TOutputImage::InternalPixelType OutputInternalPixelType;
78  typedef typename TInputImage::PixelType InputPixelType;
79  typedef typename TInputImage::InternalPixelType InputInternalPixelType;
80  typedef typename TMaskImage::PixelType MaskPixelType;
81  itkStaticConstMacro(ImageDimension, unsigned int,
82  TOutputImage::ImageDimension);
83  itkStaticConstMacro(OutputImageDimension, unsigned int,
84  TOutputImage::ImageDimension);
85  itkStaticConstMacro(InputImageDimension, unsigned int,
86  TInputImage::ImageDimension);
88 
92  typedef TInputImage InputImageType;
93  typedef TMaskImage MaskImageType;
94  typedef typename TInputImage::IndexType IndexType;
95  typedef typename TInputImage::SizeType SizeType;
96  typedef typename TInputImage::OffsetType OffsetType;
97 
98  typedef TOutputImage OutputImageType;
99  typedef typename TOutputImage::RegionType RegionType;
100  typedef typename TOutputImage::IndexType OutputIndexType;
101  typedef typename TOutputImage::SizeType OutputSizeType;
102  typedef typename TOutputImage::OffsetType OutputOffsetType;
103  typedef typename TOutputImage::PixelType OutputImagePixelType;
104 
105  typedef std::list< IndexType > ListType;
106  typedef typename MaskImageType::Pointer MaskImagePointer;
107 
113 
118 
122  itkNewMacro(Self);
123 
130  itkSetMacro(FullyConnected, bool);
131  itkGetConstReferenceMacro(FullyConnected, bool);
132  itkBooleanMacro(FullyConnected);
134 
136  typedef IdentifierType LabelType;
137 
138  // only set after completion
139  itkGetConstReferenceMacro(ObjectCount, LabelType);
140 
141  // Concept checking -- input and output dimensions must be the same
142  itkConceptMacro( SameDimension,
143  ( Concept::SameDimension< itkGetStaticConstMacro(InputImageDimension),
144  itkGetStaticConstMacro(OutputImageDimension) > ) );
145  itkConceptMacro( OutputImagePixelTypeIsInteger, ( Concept::IsInteger< OutputImagePixelType > ) );
146 
147  void SetMaskImage(TMaskImage *mask)
148  {
149  this->SetNthInput( 1, const_cast< TMaskImage * >( mask ) );
150  }
151 
152  const TMaskImage * GetMaskImage() const
153  {
154  return ( static_cast< const TMaskImage * >( this->ProcessObject::GetInput(1) ) );
155  }
156 
162  itkSetMacro(BackgroundValue, OutputImagePixelType);
163  itkGetConstMacro(BackgroundValue, OutputImagePixelType);
165 
166 protected:
168  {
169  m_FullyConnected = false;
170  m_ObjectCount = 0;
172  }
173 
175  void PrintSelf(std::ostream & os, Indent indent) const;
176 
181 
183 
184  void ThreadedGenerateData(const RegionType & outputRegionForThread, ThreadIdType threadId);
185 
190 
195  void EnlargeOutputRequestedRegion( DataObject * itkNotUsed(output) );
196 
198 
199 private:
200  ConnectedComponentImageFilter(const Self &); //purposely not implemented
201  void operator=(const Self &); //purposely not implemented
202 
205 
206  // some additional types
207  typedef typename TOutputImage::RegionType::SizeType OutSizeType;
208 
209  // types to support the run length encoding of lines
210  class runLength
211  {
212 public:
213  // run length information - may be a more type safe way of doing this
215  typename TInputImage::IndexType where; // Index of the start of the run
216  LabelType label; // the initial label of the run
217  };
218 
219  typedef std::vector< runLength > lineEncoding;
220 
221  // the map storing lines
222  typedef std::vector< lineEncoding > LineMapType;
223 
224  typedef std::vector< typename TInputImage::OffsetValueType > OffsetVec;
225 
226  // the types to support union-find operations
227  typedef std::vector< LabelType > UnionFindType;
230 
231  // functions to support union-find operations
233  {
234  m_UnionFind = UnionFindType(size + 1);
235  }
236 
237  void InsertSet(const LabelType label);
238 
239  SizeValueType LookupSet(const LabelType label);
240 
241  void LinkLabels(const LabelType lab1, const LabelType lab2);
242 
244 
246  bool CheckNeighbors(const OutputIndexType & A,
247  const OutputIndexType & B);
248 
249  void CompareLines(lineEncoding & current, const lineEncoding & Neighbour);
250 
251  void FillOutput(const LineMapType & LineMap,
252  ProgressReporter & progress);
253 
254  void SetupLineOffsets(OffsetVec & LineOffsets);
255 
256  void Wait()
257  {
258  // use m_NumberOfLabels.size() to get the number of thread used
259  if ( m_NumberOfLabels.size() > 1 )
260  {
261  m_Barrier->Wait();
262  }
263  }
264 
265  typename std::vector< IdentifierType > m_NumberOfLabels;
266  typename std::vector< IdentifierType > m_FirstLineIdToJoin;
267 
269 
270  typename TInputImage::ConstPointer m_Input;
271 #if !defined( CABLE_CONFIGURATION )
273 #endif
274 };
275 } // end namespace itk
276 
277 #ifndef ITK_MANUAL_INSTANTIATION
278 #if !defined( CABLE_CONFIGURATION )
279 #include "itkConnectedComponentImageFilter.hxx"
280 #endif
281 #endif
282 
283 #endif
void ThreadedGenerateData(const RegionType &outputRegionForThread, ThreadIdType threadId)
void LinkLabels(const LabelType lab1, const LabelType lab2)
void InsertSet(const LabelType label)
signed long OffsetValueType
Definition: itkIntTypes.h:154
std::vector< typename TInputImage::OffsetValueType > OffsetVec
InputImageType::Pointer InputImagePointer
ImageToImageFilter< TInputImage, TOutputImage > Superclass
TOutputImage::RegionType::SizeType OutSizeType
TInputImage::InternalPixelType InputInternalPixelType
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
void FillOutput(const LineMapType &LineMap, ProgressReporter &progress)
SizeValueType IdentifierType
Definition: itkIntTypes.h:147
void EnlargeOutputRequestedRegion(DataObject *)
TOutputImage::InternalPixelType OutputInternalPixelType
bool CheckNeighbors(const OutputIndexType &A, const OutputIndexType &B)
OutputImageType::PixelType OutputImagePixelType
void SetupLineOffsets(OffsetVec &LineOffsets)
SizeValueType LookupSet(const LabelType label)
Implements progress tracking for a filter.
DataObject * GetInput(const DataObjectIdentifierType &key)
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
Define additional traits for native types such as int or float.
virtual void SetNthInput(DataObjectPointerArraySizeType num, DataObject *input)
#define itkConceptMacro(name, concept)
Base class for all data objects in ITK.
void CompareLines(lineEncoding &current, const lineEncoding &Neighbour)
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
Label the objects in a binary image.