ConnectedComponents.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_ConnectedComponents_H
21 #define MIRTK_ConnectedComponents_H
22 
23 #include "mirtk/ImageToImage.h"
24 
25 #include "mirtk/Array.h"
26 #include "mirtk/OrderedSet.h"
27 #include "mirtk/NeighborhoodOffsets.h"
28 
29 
30 namespace mirtk {
31 
32 
33 /// Enumeration of possible orderings of connected components
35 {
36  CC_NoOrdering, ///< No sorting of output components
37  CC_LargestFirst, ///< Sort by decreasing size
38  CC_SmallestFirst ///< Sort by increasing size
39 };
40 
41 
42 /**
43  * Label the connected components of a segmentation.
44  *
45  * The components are sorted by decreasing size, i.e., the first
46  * component is the largest connected component.
47  */
48 template <class TVoxel = GreyPixel>
49 class ConnectedComponents : public ImageToImage<TVoxel>
50 {
51  mirtkImageFilterMacro(ConnectedComponents, TVoxel);
52 
53  // ---------------------------------------------------------------------------
54  // Attributes
55 
56 protected:
57 
58  /// Ordering of output components
60 
61  /// What connectivity to assume when running the filter.
63 
64  /// List of voxel offsets of the neighborhood
66 
67  /// Number of connected components
68  mirtkReadOnlyAttributeMacro(int, NumberOfComponents);
69 
70  /// Sizes of connected components
72 
73  // ---------------------------------------------------------------------------
74  // Construction/Destruction
75 
76 public:
77 
78  /// Constructor
80 
81  /// Destructor
82  virtual ~ConnectedComponents();
83 
84  // ---------------------------------------------------------------------------
85  // Execution
86 
87  /// Run erosion
88  virtual void Run();
89 
90  /// Remove specified component from the output image
91  ///
92  /// This method must be called after Run(). Another execution of
93  /// Run() will add the component again to the output.
94  virtual void DeleteComponent(VoxelType);
95 
96  /// Size of the specified component
97  ///
98  /// \param[in] label Component label (1-based).
99  int ComponentSize(VoxelType label) const;
100 
101 protected:
102 
103  /// Initialize the filter execution
104  virtual void Initialize();
105 
106  /// Finalize the filter execution
107  virtual void Finalize();
108 
109 };
110 
111 // =============================================================================
112 // Ordering / string conversion
113 // =============================================================================
114 
115 // -----------------------------------------------------------------------------
116 /// Convert connected components ordering to string
117 template <>
118 inline string ToString(const ConnectedComponentsOrdering &value, int w, char c, bool left)
119 {
120  const char *str;
121  switch (value) {
122  case CC_NoOrdering: str = "none"; break;
123  case CC_LargestFirst: str = "largest"; break;
124  case CC_SmallestFirst: str = "smallest"; break;
125  }
126  return ToString(str, w, c, left);
127 }
128 
129 // -----------------------------------------------------------------------------
130 /// Convert string to connected components ordering
131 template <>
132 inline bool FromString(const char *str, ConnectedComponentsOrdering &value)
133 {
134  const string lstr = ToLower(str);
135  if (lstr == "none" || lstr == "no ordering") {
136  value = CC_NoOrdering;
137  } else if (lstr == "largest" || lstr == "largestfirst" || lstr == "largest first") {
138  value = CC_LargestFirst;
139  } else if (lstr == "smallest" || lstr == "smallestfirst" || lstr == "smallest first") {
140  value = CC_SmallestFirst;
141  } else return false;
142  return true;
143 }
144 
145 // =============================================================================
146 // Inline definitions
147 // =============================================================================
148 
149 // -----------------------------------------------------------------------------
150 template <class TVoxel>
152 {
153  return _ComponentSize[label-1];
154 }
155 
156 
157 } // namespace mirtk
158 
159 #endif // MIRTK_ConnectedComponents_H
ConnectedComponents(ConnectedComponentsOrdering=CC_LargestFirst, ConnectivityType=CONNECTIVITY_26)
Constructor.
virtual void Initialize()
Initialize the filter execution.
virtual ~ConnectedComponents()
Destructor.
No sorting of output components.
mirtkReadOnlyAttributeMacro(int, NumberOfComponents)
Number of connected components.
Sort by increasing size.
ConnectedComponentsOrdering
Enumeration of possible orderings of connected components.
string ToLower(const string &)
Convert string to lowercase letters.
Definition: IOConfig.h:41
mirtkPublicAttributeMacro(ConnectedComponentsOrdering, Ordering)
Ordering of output components.
mirtkAttributeMacro(NeighborhoodOffsets, Offsets)
List of voxel offsets of the neighborhood.
virtual void Finalize()
Finalize the filter execution.
TVoxel VoxelType
Input/output image voxel type.
Definition: ImageToImage.h:48
string ToString(const EnergyMeasure &value, int w, char c, bool left)
Convert energy measure enumeration value to string.
bool FromString(const char *str, EnergyMeasure &value)
Convert energy measure string to enumeration value.
virtual void Run()
Run erosion.
int ComponentSize(VoxelType label) const
ConnectivityType
Type of image connectivity, i.e., number of neighbors for each voxel.
virtual void DeleteComponent(VoxelType)
Sort by decreasing size.