ITK  4.12.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 {
58 template< typename TInputImage, typename TOutputImage, typename TMaskImage = TInputImage >
59 class ITK_TEMPLATE_EXPORT ConnectedComponentImageFilter:
60  public ImageToImageFilter< TInputImage, TOutputImage >
61 {
62 public:
68 
72  typedef typename Superclass::InputImagePointer InputImagePointer;
73 
78  typedef typename TOutputImage::PixelType OutputPixelType;
79  typedef typename TOutputImage::InternalPixelType OutputInternalPixelType;
80  typedef typename TInputImage::PixelType InputPixelType;
81  typedef typename TInputImage::InternalPixelType InputInternalPixelType;
82  typedef typename TMaskImage::PixelType MaskPixelType;
83  itkStaticConstMacro(ImageDimension, unsigned int,
84  TOutputImage::ImageDimension);
85  itkStaticConstMacro(OutputImageDimension, unsigned int,
86  TOutputImage::ImageDimension);
87  itkStaticConstMacro(InputImageDimension, unsigned int,
88  TInputImage::ImageDimension);
90 
94  typedef TInputImage InputImageType;
95  typedef TMaskImage MaskImageType;
96  typedef typename TInputImage::IndexType IndexType;
97  typedef typename TInputImage::SizeType SizeType;
98  typedef typename TInputImage::OffsetType OffsetType;
99 
100  typedef TOutputImage OutputImageType;
101  typedef typename TOutputImage::RegionType RegionType;
102  typedef typename TOutputImage::IndexType OutputIndexType;
103  typedef typename TOutputImage::SizeType OutputSizeType;
104  typedef typename TOutputImage::OffsetType OutputOffsetType;
105  typedef typename TOutputImage::PixelType OutputImagePixelType;
106 
107  typedef std::list< IndexType > ListType;
109 
115 
120 
124  itkNewMacro(Self);
125 
132  itkSetMacro(FullyConnected, bool);
133  itkGetConstReferenceMacro(FullyConnected, bool);
134  itkBooleanMacro(FullyConnected);
136 
138  typedef IdentifierType LabelType;
139 
140  // only set after completion
141  itkGetConstReferenceMacro(ObjectCount, LabelType);
142 
143  // Concept checking -- input and output dimensions must be the same
144  itkConceptMacro( SameDimension,
145  ( Concept::SameDimension< itkGetStaticConstMacro(InputImageDimension),
146  itkGetStaticConstMacro(OutputImageDimension) > ) );
147  itkConceptMacro( OutputImagePixelTypeIsInteger, ( Concept::IsInteger< OutputImagePixelType > ) );
148 
149  void SetMaskImage(TMaskImage *mask)
150  {
151  this->SetNthInput( 1, const_cast< TMaskImage * >( mask ) );
152  }
153 
154  const TMaskImage * GetMaskImage() const
155  {
156  return ( static_cast< const TMaskImage * >( this->ProcessObject::GetInput(1) ) );
157  }
158 
164  itkSetMacro(BackgroundValue, OutputImagePixelType);
165  itkGetConstMacro(BackgroundValue, OutputImagePixelType);
167 
168 protected:
170  {
171  m_FullyConnected = false;
172  m_ObjectCount = 0;
174  }
175 
177  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
178 
182  void BeforeThreadedGenerateData() ITK_OVERRIDE;
183 
184  void AfterThreadedGenerateData() ITK_OVERRIDE;
185 
186  void ThreadedGenerateData(const RegionType & outputRegionForThread, ThreadIdType threadId) ITK_OVERRIDE;
187 
191  void GenerateInputRequestedRegion() ITK_OVERRIDE;
192 
197  void EnlargeOutputRequestedRegion( DataObject * itkNotUsed(output) ) ITK_OVERRIDE;
198 
199  bool m_FullyConnected;
200 
201 private:
202  ITK_DISALLOW_COPY_AND_ASSIGN(ConnectedComponentImageFilter);
203 
204  LabelType m_ObjectCount;
205  OutputImagePixelType m_BackgroundValue;
206 
207  // some additional types
208  typedef typename TOutputImage::RegionType::SizeType OutSizeType;
209 
210  // types to support the run length encoding of lines
211  class runLength
212  {
213 public:
214  // run length information - may be a more type safe way of doing this
216  typename TInputImage::IndexType where; // Index of the start of the run
217  LabelType label; // the initial label of the run
218  };
219 
220  typedef std::vector< runLength > lineEncoding;
221 
222  // the map storing lines
223  typedef std::vector< lineEncoding > LineMapType;
224 
225  typedef std::vector< typename TInputImage::OffsetValueType > OffsetVec;
226 
227  // the types to support union-find operations
228  typedef std::vector< LabelType > UnionFindType;
231 
232  // functions to support union-find operations
234  {
235  m_UnionFind = UnionFindType(size + 1);
236  }
237 
238  void InsertSet(const LabelType label);
239 
240  SizeValueType LookupSet(const LabelType label);
241 
242  void LinkLabels(const LabelType lab1, const LabelType lab2);
243 
244  SizeValueType CreateConsecutive();
245 
247  bool CheckNeighbors(const OutputIndexType & A,
248  const OutputIndexType & B);
249 
250  void CompareLines(lineEncoding & current, const lineEncoding & Neighbour);
251 
252  void FillOutput(const LineMapType & LineMap,
253  ProgressReporter & progress);
254 
255  void SetupLineOffsets(OffsetVec & LineOffsets);
256 
257  void Wait()
258  {
259  // use m_NumberOfLabels.size() to get the number of thread used
260  if ( m_NumberOfLabels.size() > 1 )
261  {
262  m_Barrier->Wait();
263  }
264  }
265 
266  typename std::vector< IdentifierType > m_NumberOfLabels;
267  typename std::vector< IdentifierType > m_FirstLineIdToJoin;
268 
270 
272 #if !defined( ITK_WRAPPING_PARSER )
274 #endif
275 };
276 } // end namespace itk
277 
278 #ifndef ITK_MANUAL_INSTANTIATION
279 #if !defined( ITK_WRAPPING_PARSER )
280 #include "itkConnectedComponentImageFilter.hxx"
281 #endif
282 #endif
283 
284 #endif
virtual void PrintSelf(std::ostream &os, Indent indent) const override
signed long OffsetValueType
Definition: itkIntTypes.h:154
std::vector< typename TInputImage::OffsetValueType > OffsetVec
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
SmartPointer< Self > Pointer
SizeValueType IdentifierType
Definition: itkIntTypes.h:147
TOutputImage::InternalPixelType OutputInternalPixelType
SmartPointer< const Self > ConstPointer
OutputImageType::PixelType OutputImagePixelType
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
Implements progress tracking for a filter.
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
Base class for all data objects in ITK.
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 itkConceptMacro(name, concept)
Label the objects in a binary image.