ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkWatershedSegmenter.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 itkWatershedSegmenter_h
19 #define itkWatershedSegmenter_h
20 
21 
22 #include "itkWatershedBoundary.h"
24 #include "itkEquivalencyTable.h"
25 
26 namespace itk
27 {
28 namespace watershed
29 {
87 template< typename TInputImage >
88 class ITK_TEMPLATE_EXPORT Segmenter:
89  public ProcessObject
90 {
91 public:
93  using Self = Segmenter;
94 
96  using InputImageType = TInputImage;
97  static constexpr unsigned int ImageDimension = TInputImage::ImageDimension;
98 
99  using OutputImageType =
102  using InputPixelType = typename InputImageType::PixelType;
103  using BoundaryType =
109 
115  itkNewMacro(Self);
116  itkTypeMacro(WatershedSegmenter, ProcessObject);
118 
120  using InputImageTypePointer = typename InputImageType::Pointer;
124 
126  static constexpr IdentifierType NULL_LABEL = 0;
127 
129  static constexpr short NULL_FLOW = -1;
130 
133  {
134  return itkDynamicCastInDebugMode< InputImageType * >
135  ( this->ProcessObject::GetInput(0) );
136  }
137 
139  { this->ProcessObject::SetNthInput(0, img); }
140 
144  {
145  return itkDynamicCastInDebugMode< OutputImageType * >
146  ( this->ProcessObject::GetOutput(0) );
147  }
148 
150  { this->ProcessObject::SetNthOutput(0, img); }
151 
155  {
156  return itkDynamicCastInDebugMode< SegmentTableType * >
157  ( this->ProcessObject::GetOutput(1) );
158  }
159 
161  { this->ProcessObject::SetNthOutput(1, s); }
162 
166  {
167  return itkDynamicCastInDebugMode< BoundaryType * >
168  ( this->ProcessObject::GetOutput(2) );
169  }
170 
172  { this->ProcessObject::SetNthOutput(2, b); }
173 
175  void GenerateData() override;
176 
184  {
185  if ( reg == m_LargestPossibleRegion ) { return; }
186  m_LargestPossibleRegion = reg;
187  this->Modified();
188  }
190 
192  { return m_LargestPossibleRegion; }
193 
196  static void RelabelImage(OutputImageTypePointer,
197  ImageRegionType,
199 
202  using Superclass::MakeOutput;
203  DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) override;
204 
207  itkSetMacro(CurrentLabel, IdentifierType);
208  itkGetConstMacro(CurrentLabel, IdentifierType);
210 
221  itkSetClampMacro(Threshold, double, 0.0, 1.0);
222  itkGetConstMacro(Threshold, double);
224 
228  itkSetMacro(DoBoundaryAnalysis, bool);
229  itkGetConstMacro(DoBoundaryAnalysis, bool);
231 
236  itkGetConstMacro(SortEdgeLists, bool);
237  itkSetMacro(SortEdgeLists, bool);
239 
240 protected:
243  struct flat_region_t {
246  // InputPixelType bounds_max; // <-- may not be necc.
248  bool is_on_boundary{false};
250  };
251 
253  using flat_region_table_t = itksys::hash_map< IdentifierType, flat_region_t, itksys::hash< IdentifierType > >;
254 
255  struct connectivity_t {
256  unsigned int size;
257  unsigned int *index;
258  typename InputImageType::OffsetType * direction;
259  };
260 
265  using edge_table_t = itksys::hash_map< IdentifierType, InputPixelType, itksys::hash< IdentifierType > >;
266 
267  using edge_table_hash_t = itksys::hash_map< IdentifierType, edge_table_t, itksys::hash< IdentifierType > >;
268 
269  Segmenter();
270  Segmenter(const Self &) {}
271  ~Segmenter() override;
272  void PrintSelf(std::ostream & os, Indent indent) const override;
273 
274  void operator=(const Self &) {}
275 
278  virtual void GenerateConnectivity();
279 
283  void GenerateInputRequestedRegion() override;
284 
285  void GenerateOutputRequestedRegion(DataObject *output) override;
286 
287  void UpdateOutputInformation() override;
288 
291  void InitializeBoundary();
292 
296  void AnalyzeBoundaryFlow(InputImageTypePointer,
297  flat_region_table_t &,
298  InputPixelType);
299 
303  void BuildRetainingWall(InputImageTypePointer,
304  ImageRegionType, InputPixelType);
305 
308  void LabelMinima(InputImageTypePointer,
309  ImageRegionType, flat_region_table_t &,
310  InputPixelType);
311 
315  void GradientDescent(InputImageTypePointer, ImageRegionType);
316 
319  void DescendFlatRegions(flat_region_table_t &, ImageRegionType);
320 
323  void UpdateSegmentTable(InputImageTypePointer, ImageRegionType);
324 
328  void CollectBoundaryInformation(flat_region_table_t &);
329 
335  static void Threshold(InputImageTypePointer destination,
336  InputImageTypePointer source,
337  const ImageRegionType source_region,
338  const ImageRegionType destination_region,
339  InputPixelType threshold);
340 
342  static void MinMax(InputImageTypePointer img,
343  ImageRegionType region,
344  InputPixelType & min,
345  InputPixelType & max);
346 
348  static void MergeFlatRegions(flat_region_table_t &, EquivalencyTable::Pointer);
349 
351  static void SetInputImageValues(InputImageTypePointer img,
352  const ImageRegionType region,
353  InputPixelType value);
354 
355  static void SetOutputImageValues(OutputImageTypePointer img,
356  const ImageRegionType region,
357  IdentifierType value);
358 
360  // bool CheckLabeledBoundaries();
361 
364  connectivity_t m_Connectivity;
365 
366 private:
368  // void PrintFlatRegions(flat_region_table_t &t);
369 
374 
377  double m_Threshold;
380 };
381 } // end namespace watershed
382 } // end namespace itk
383 
384 #ifndef ITK_MANUAL_INSTANTIATION
385 #include "itkWatershedSegmenter.hxx"
386 #endif
387 
388 #endif
SegmentTableType * GetSegmentTable()
typename flat_hash_t::value_type FlatHashValueType
OutputImageType * GetOutputImage()
Light weight base class for most itk classes.
DataObjectPointerArray::size_type DataObjectPointerArraySizeType
ImageRegionType GetLargestPossibleRegion() const
itksys::hash_map< IdentifierType, InputPixelType, itksys::hash< IdentifierType > > edge_table_t
typename InputImageType::RegionType ImageRegionType
typename OutputImageType::Pointer OutputImageTypePointer
typename SegmentTableType::Pointer SegmentTableTypePointer
itksys::hash_map< IdentifierType, edge_table_t, itksys::hash< IdentifierType > > edge_table_hash_t
itksys::hash_map< IdentifierType, flat_region_t, itksys::hash< IdentifierType > > flat_region_table_t
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
void SetOutputImage(OutputImageType *img)
class ITK_FORWARD_EXPORT ProcessObject
Definition: itkDataObject.h:40
typename BoundaryType::FlatHashValueType BoundaryFlatHashValueType
void SetLargestPossibleRegion(ImageRegionType reg)
SizeValueType IdentifierType
Definition: itkIntTypes.h:87
typename InputImageType::PixelType InputPixelType
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
typename BoundaryType::Pointer BoundaryTypePointer
void SetSegmentTable(SegmentTableType *s)
Control indentation during Print() invocation.
Definition: itkIndent.h:49
virtual void SetNthInput(DataObjectPointerArraySizeType num, DataObject *input)
typename InputImageType::Pointer InputImageTypePointer
typename BoundaryType::IndexType BoundaryIndexType
virtual void SetNthOutput(DataObjectPointerArraySizeType num, DataObject *output)
SmartPointer< Self > Pointer
void SetBoundary(BoundaryType *b)
Base class for all data objects in ITK.
Templated n-dimensional image class.
Definition: itkImage.h:75
void SetInputImage(InputImageType *img)
DataObject * GetOutput(const DataObjectIdentifierType &key)