ITK  5.3.0
Insight Toolkit
itkWatershedSegmenter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 {
88 template <typename TInputImage>
89 class ITK_TEMPLATE_EXPORT Segmenter : public ProcessObject
90 {
91 public:
92 
94  using Self = Segmenter;
95 
97  using InputImageType = TInputImage;
98  static constexpr unsigned int ImageDimension = TInputImage::ImageDimension;
99 
102  using InputPixelType = typename InputImageType::PixelType;
108 
114  itkNewMacro(Self);
115  itkTypeMacro(WatershedSegmenter, ProcessObject);
117 
119  using InputImageTypePointer = typename InputImageType::Pointer;
123 
125  static constexpr IdentifierType NULL_LABEL = 0;
126 
128  static constexpr short NULL_FLOW = -1;
129 
133  {
134  return itkDynamicCastInDebugMode<InputImageType *>(this->ProcessObject::GetInput(0));
135  }
136 
137  void
139  {
140  this->ProcessObject::SetNthInput(0, img);
141  }
142 
145  OutputImageType *
147  {
148  return itkDynamicCastInDebugMode<OutputImageType *>(this->ProcessObject::GetOutput(0));
149  }
150 
151  void
153  {
154  this->ProcessObject::SetNthOutput(0, img);
155  }
156 
159  SegmentTableType *
161  {
162  return itkDynamicCastInDebugMode<SegmentTableType *>(this->ProcessObject::GetOutput(1));
163  }
164 
165  void
167  {
168  this->ProcessObject::SetNthOutput(1, s);
169  }
170 
173  BoundaryType *
175  {
176  return itkDynamicCastInDebugMode<BoundaryType *>(this->ProcessObject::GetOutput(2));
177  }
178 
179  void
181  {
182  this->ProcessObject::SetNthOutput(2, b);
183  }
184 
186  void
187  GenerateData() override;
188 
195  void
197  {
198  if (reg == m_LargestPossibleRegion)
199  {
200  return;
201  }
202  m_LargestPossibleRegion = reg;
203  this->Modified();
204  }
206 
207  ImageRegionType
209  {
210  return m_LargestPossibleRegion;
211  }
212 
215  static void RelabelImage(OutputImageTypePointer, ImageRegionType, EquivalencyTable::Pointer);
216 
219  using Superclass::MakeOutput;
221  MakeOutput(DataObjectPointerArraySizeType idx) override;
222 
225  itkSetMacro(CurrentLabel, IdentifierType);
226  itkGetConstMacro(CurrentLabel, IdentifierType);
228 
239  itkSetClampMacro(Threshold, double, 0.0, 1.0);
240  itkGetConstMacro(Threshold, double);
242 
246  itkSetMacro(DoBoundaryAnalysis, bool);
247  itkGetConstMacro(DoBoundaryAnalysis, bool);
249 
254  itkGetConstMacro(SortEdgeLists, bool);
255  itkSetMacro(SortEdgeLists, bool);
257 
258 protected:
262  {
265  // InputPixelType bounds_max; // <-- may not be necc.
267  bool is_on_boundary{ false };
268  flat_region_t() = default;
269  };
270 
272  using flat_region_table_t = std::unordered_map<IdentifierType, flat_region_t>;
273 
275  {
276  unsigned int size;
277  unsigned int * index;
278  typename InputImageType::OffsetType * direction;
279  };
280 
285  using edge_table_t = std::map<IdentifierType, InputPixelType>;
286 
287  using edge_table_hash_t = std::unordered_map<IdentifierType, edge_table_t>;
288 
289  Segmenter();
290  Segmenter(const Self &) {}
291  ~Segmenter() override;
292  void
293  PrintSelf(std::ostream & os, Indent indent) const override;
294 
295  void
296  operator=(const Self &)
297  {}
298 
301  virtual void
302  GenerateConnectivity();
303 
307  void
308  GenerateInputRequestedRegion() override;
309 
310  void
311  GenerateOutputRequestedRegion(DataObject * output) override;
312 
313  void
314  UpdateOutputInformation() override;
315 
318  void
319  InitializeBoundary();
320 
324  void
325  AnalyzeBoundaryFlow(InputImageTypePointer, flat_region_table_t &, InputPixelType);
326 
330  void BuildRetainingWall(InputImageTypePointer, ImageRegionType, InputPixelType);
331 
334  void
335  LabelMinima(InputImageTypePointer, ImageRegionType, flat_region_table_t &, InputPixelType);
336 
340  void GradientDescent(InputImageTypePointer, ImageRegionType);
341 
344  void
345  DescendFlatRegions(flat_region_table_t &, ImageRegionType);
346 
349  void UpdateSegmentTable(InputImageTypePointer, ImageRegionType);
350 
354  void
355  CollectBoundaryInformation(flat_region_table_t &);
356 
362  static void
363  Threshold(InputImageTypePointer destination,
364  InputImageTypePointer source,
365  const ImageRegionType source_region,
366  const ImageRegionType destination_region,
367  InputPixelType threshold);
368 
370  static void
371  MinMax(InputImageTypePointer img, ImageRegionType region, InputPixelType & min, InputPixelType & max);
372 
374  static void
375  MergeFlatRegions(flat_region_table_t &, EquivalencyTable::Pointer);
376 
378  static void
379  SetInputImageValues(InputImageTypePointer img, const ImageRegionType region, InputPixelType value);
380 
381  static void
382  SetOutputImageValues(OutputImageTypePointer img, const ImageRegionType region, IdentifierType value);
383 
385  // bool CheckLabeledBoundaries();
386 
389  connectivity_t m_Connectivity;
390 
391 private:
393  // void PrintFlatRegions(flat_region_table_t &t);
394 
399 
402  double m_Threshold;
405 };
406 } // end namespace watershed
407 } // end namespace itk
408 
409 #ifndef ITK_MANUAL_INSTANTIATION
410 # include "itkWatershedSegmenter.hxx"
411 #endif
412 
413 #endif
itk::watershed::Segmenter::m_DoBoundaryAnalysis
bool m_DoBoundaryAnalysis
Definition: itkWatershedSegmenter.h:401
itk::watershed::Segmenter< InputImageType >::edge_table_t
std::map< IdentifierType, InputPixelType > edge_table_t
Definition: itkWatershedSegmenter.h:285
itk::watershed::Segmenter< InputImageType >::edge_table_hash_t
std::unordered_map< IdentifierType, edge_table_t > edge_table_hash_t
Definition: itkWatershedSegmenter.h:287
itk::watershed::Segmenter::flat_region_t::value
InputPixelType value
Definition: itkWatershedSegmenter.h:266
itk::watershed::Segmenter::Segmenter
Segmenter(const Self &)
Definition: itkWatershedSegmenter.h:290
itk::watershed::Segmenter::SetLargestPossibleRegion
void SetLargestPossibleRegion(ImageRegionType reg)
Definition: itkWatershedSegmenter.h:196
itk::watershed::Segmenter< InputImageType >::flat_region_table_t
std::unordered_map< IdentifierType, flat_region_t > flat_region_table_t
Definition: itkWatershedSegmenter.h:272
itk::watershed::Segmenter::SetBoundary
void SetBoundary(BoundaryType *b)
Definition: itkWatershedSegmenter.h:180
itk::watershed::Segmenter::m_SortEdgeLists
bool m_SortEdgeLists
Definition: itkWatershedSegmenter.h:400
itkEquivalencyTable.h
itk::watershed::Segmenter::m_LargestPossibleRegion
ImageRegionType m_LargestPossibleRegion
Definition: itkWatershedSegmenter.h:398
itk::watershed::Segmenter::GetSegmentTable
SegmentTableType * GetSegmentTable()
Definition: itkWatershedSegmenter.h:160
itk::watershed::Segmenter::GetBoundary
BoundaryType * GetBoundary()
Definition: itkWatershedSegmenter.h:174
itk::ProcessObject::GetOutput
DataObject * GetOutput(const DataObjectIdentifierType &key)
itk::watershed::Segmenter::GetInputImage
InputImageType * GetInputImage()
Definition: itkWatershedSegmenter.h:132
itk::watershed::Segmenter::flat_region_t::min_label_ptr
IdentifierType * min_label_ptr
Definition: itkWatershedSegmenter.h:263
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::ProcessObject::DataObjectPointerArraySizeType
DataObjectPointerArray::size_type DataObjectPointerArraySizeType
Definition: itkProcessObject.h:193
itk::watershed::Segmenter::m_CurrentLabel
IdentifierType m_CurrentLabel
Definition: itkWatershedSegmenter.h:404
itk::watershed::Segmenter::flat_region_t::bounds_min
InputPixelType bounds_min
Definition: itkWatershedSegmenter.h:264
itk::watershed::Segmenter< InputImageType >::BoundaryTypePointer
typename BoundaryType::Pointer BoundaryTypePointer
Definition: itkWatershedSegmenter.h:122
itk::watershed::Segmenter::connectivity_t::index
unsigned int * index
Definition: itkWatershedSegmenter.h:277
itk::watershed::Segmenter< InputImageType >::SegmentTableTypePointer
typename SegmentTableType::Pointer SegmentTableTypePointer
Definition: itkWatershedSegmenter.h:121
itk::watershed::Segmenter::m_Connectivity
connectivity_t m_Connectivity
Definition: itkWatershedSegmenter.h:389
itk::watershed::Segmenter::m_MaximumFloodLevel
double m_MaximumFloodLevel
Definition: itkWatershedSegmenter.h:403
itkWatershedBoundary.h
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::watershed::Segmenter::GetOutputImage
OutputImageType * GetOutputImage()
Definition: itkWatershedSegmenter.h:146
itk::watershed::Segmenter::GetLargestPossibleRegion
ImageRegionType GetLargestPossibleRegion() const
Definition: itkWatershedSegmenter.h:208
itk::watershed::Segmenter
Definition: itkWatershedSegmenter.h:89
itk::watershed::Segmenter::SetOutputImage
void SetOutputImage(OutputImageType *img)
Definition: itkWatershedSegmenter.h:152
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::watershed::Segmenter< InputImageType >::InputImageType
InputImageType InputImageType
Definition: itkWatershedSegmenter.h:97
itk::watershed::Segmenter< InputImageType >::InputImageTypePointer
typename InputImageType::Pointer InputImageTypePointer
Definition: itkWatershedSegmenter.h:119
itk::watershed::Boundary
Definition: itkWatershedBoundary.h:54
itkWatershedSegmentTable.h
itk::watershed::Segmenter< InputImageType >::BoundaryIndexType
typename BoundaryType::IndexType BoundaryIndexType
Definition: itkWatershedSegmenter.h:104
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::watershed::Boundary::FlatHashValueType
typename flat_hash_t::value_type FlatHashValueType
Definition: itkWatershedBoundary.h:123
itk::watershed::Segmenter::SetSegmentTable
void SetSegmentTable(SegmentTableType *s)
Definition: itkWatershedSegmenter.h:166
itk::watershed::Segmenter::SetInputImage
void SetInputImage(InputImageType *img)
Definition: itkWatershedSegmenter.h:138
itk::watershed::Segmenter::operator=
void operator=(const Self &)
Definition: itkWatershedSegmenter.h:296
itk::watershed::Segmenter::connectivity_t
Definition: itkWatershedSegmenter.h:274
itk::watershed::Segmenter::connectivity_t::direction
InputImageType::OffsetType * direction
Definition: itkWatershedSegmenter.h:278
itk::watershed::Segmenter< InputImageType >::ImageRegionType
typename InputImageType::RegionType ImageRegionType
Definition: itkWatershedSegmenter.h:101
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ProcessObject::SetNthOutput
virtual void SetNthOutput(DataObjectPointerArraySizeType idx, DataObject *output)
itk::watershed::Segmenter::connectivity_t::size
unsigned int size
Definition: itkWatershedSegmenter.h:276
itk::ProcessObject::SetNthInput
virtual void SetNthInput(DataObjectPointerArraySizeType idx, DataObject *input)
itk::watershed::Segmenter< InputImageType >::BoundaryFlatHashValueType
typename BoundaryType::FlatHashValueType BoundaryFlatHashValueType
Definition: itkWatershedSegmenter.h:105
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:138
itk::ProcessObject
class ITK_FORWARD_EXPORT ProcessObject
Definition: itkDataObject.h:41
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
itk::watershed::Segmenter< InputImageType >::OutputImageTypePointer
typename OutputImageType::Pointer OutputImageTypePointer
Definition: itkWatershedSegmenter.h:120
itk::watershed::Segmenter::m_Threshold
double m_Threshold
Definition: itkWatershedSegmenter.h:402
itk::watershed::Segmenter< InputImageType >::InputPixelType
typename InputImageType::PixelType InputPixelType
Definition: itkWatershedSegmenter.h:102
itk::DataObject::Pointer
SmartPointer< Self > Pointer
Definition: itkDataObject.h:301
itk::IdentifierType
SizeValueType IdentifierType
Definition: itkIntTypes.h:87
itk::watershed::Segmenter::flat_region_t
Definition: itkWatershedSegmenter.h:261
itk::ProcessObject::GetInput
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293
itk::watershed::SegmentTable
Definition: itkWatershedSegmentTable.h:48