ITK  4.2.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< class TInputImage >
88 class ITK_EXPORT Segmenter:
89  public ProcessObject
90 {
91 public:
93  typedef Segmenter Self;
94 
96  typedef TInputImage InputImageType;
97  itkStaticConstMacro(ImageDimension, unsigned int,
98  TInputImage::ImageDimension);
99 
102  typedef typename InputImageType::RegionType ImageRegionType;
103  typedef typename InputImageType::PixelType InputPixelType;
110 
116  itkNewMacro(Self);
117  itkTypeMacro(WatershedSegmenter, ProcessObject);
119 
121  typedef typename InputImageType::Pointer InputImageTypePointer;
125 
128 
130  static short NULL_FLOW;
131 
133  InputImageType * GetInputImage(void)
134  {
135  return static_cast< InputImageType * >
136  ( this->ProcessObject::GetInput(0) );
137  }
138 
139  void SetInputImage(InputImageType *img)
140  { this->ProcessObject::SetNthInput(0, img); }
141 
144  OutputImageType * GetOutputImage(void)
145  {
146  return static_cast< OutputImageType * >
147  ( this->ProcessObject::GetOutput(0) );
148  }
149 
150  void SetOutputImage(OutputImageType *img)
151  { this->ProcessObject::SetNthOutput(0, img); }
152 
155  SegmentTableType * GetSegmentTable(void)
156  {
157  return static_cast< SegmentTableType * >
158  ( this->ProcessObject::GetOutput(1) );
159  }
160 
161  void SetSegmentTable(SegmentTableType *s)
162  { this->ProcessObject::SetNthOutput(1, s); }
163 
166  BoundaryType * GetBoundary(void)
167  {
168  return static_cast< BoundaryType * >
169  ( this->ProcessObject::GetOutput(2) );
170  }
171 
172  void SetBoundary(BoundaryType *b)
173  { this->ProcessObject::SetNthOutput(2, b); }
174 
176  void GenerateData();
177 
184  void SetLargestPossibleRegion(ImageRegionType reg)
185  {
186  if ( reg == m_LargestPossibleRegion ) { return; }
187  m_LargestPossibleRegion = reg;
188  this->Modified();
189  }
191 
192  ImageRegionType GetLargestPossibleRegion() const
193  { return m_LargestPossibleRegion; }
194 
197  static void RelabelImage(OutputImageTypePointer,
198  ImageRegionType,
200 
203  using Superclass::MakeOutput;
204  virtual DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx);
205 
208  itkSetMacro(CurrentLabel, IdentifierType);
209  itkGetConstMacro(CurrentLabel, IdentifierType);
211 
222  itkSetClampMacro(Threshold, double, 0.0, 1.0);
223  itkGetConstMacro(Threshold, double);
225 
229  itkSetMacro(DoBoundaryAnalysis, bool);
230  itkGetConstMacro(DoBoundaryAnalysis, bool);
232 
237  itkGetConstMacro(SortEdgeLists, bool);
238  itkSetMacro(SortEdgeLists, bool);
239 protected:
240 
243  struct flat_region_t {
246  // InputPixelType bounds_max; // <-- may not be necc.
249  flat_region_t():is_on_boundary(false) {}
250  };
251 
253  typedef itksys::hash_map< IdentifierType, flat_region_t, itksys::hash< IdentifierType > >
255 
256  struct connectivity_t {
257  unsigned int size;
258  unsigned int *index;
259  typename InputImageType::OffsetType * direction;
260  };
261 
266  typedef itksys::hash_map< IdentifierType, InputPixelType, itksys::hash< IdentifierType >
268 
269  typedef itksys::hash_map< IdentifierType, edge_table_t, itksys::hash< IdentifierType >
271 
272  Segmenter();
273  Segmenter(const Self &) {}
274  virtual ~Segmenter();
275  void PrintSelf(std::ostream & os, Indent indent) const;
276 
277  void operator=(const Self &) {}
278 
281  virtual void GenerateConnectivity();
282 
286  void GenerateInputRequestedRegion();
287 
288  void GenerateOutputRequestedRegion(DataObject *output);
289 
290  void UpdateOutputInformation();
291 
294  void InitializeBoundary();
295 
299  void AnalyzeBoundaryFlow(InputImageTypePointer,
300  flat_region_table_t &,
301  InputPixelType);
302 
306  void BuildRetainingWall(InputImageTypePointer,
307  ImageRegionType, InputPixelType);
308 
311  void LabelMinima(InputImageTypePointer,
312  ImageRegionType, flat_region_table_t &,
313  InputPixelType);
314 
318  void GradientDescent(InputImageTypePointer, ImageRegionType);
319 
322  void DescendFlatRegions(flat_region_table_t &, ImageRegionType);
323 
326  void UpdateSegmentTable(InputImageTypePointer, ImageRegionType);
327 
331  void CollectBoundaryInformation(flat_region_table_t &);
332 
338  static void Threshold(InputImageTypePointer destination,
339  InputImageTypePointer source,
340  const ImageRegionType source_region,
341  const ImageRegionType destination_region,
342  InputPixelType threshold);
343 
345  static void MinMax(InputImageTypePointer img,
346  ImageRegionType region,
347  InputPixelType & min,
348  InputPixelType & max);
349 
351  static void MergeFlatRegions(flat_region_table_t &, EquivalencyTable::Pointer);
352 
354  static void SetInputImageValues(InputImageTypePointer img,
355  const ImageRegionType region,
356  InputPixelType value);
357 
358  static void SetOutputImageValues(OutputImageTypePointer img,
359  const ImageRegionType region,
360  IdentifierType value);
361 
363  // bool CheckLabeledBoundaries();
364 
367  connectivity_t m_Connectivity;
368 private:
369 
371  // void PrintFlatRegions(flat_region_table_t &t);
372 
377 
380  double m_Threshold;
383 };
384 } // end namespace watershed
385 } // end namespace itk
386 
387 #ifndef ITK_MANUAL_INSTANTIATION
388 #include "itkWatershedSegmenter.hxx"
389 #endif
390 
391 #endif
392