ITK  6.0.0
Insight Toolkit
itkConnectedComponentAlgorithm.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 * https://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 itkConnectedComponentAlgorithm_h
19#define itkConnectedComponentAlgorithm_h
20
21#include "itkImage.h"
23
24namespace itk
25{
26template <typename TIterator>
27TIterator *
28setConnectivity(TIterator * it, bool fullyConnected = false)
29{
30
31 it->ClearActiveList();
32 if (!fullyConnected)
33 {
34 // only activate the neighbors that are face connected
35 // to the current pixel. do not include the center pixel
36 typename TIterator::OffsetType offset{};
37 for (unsigned int d = 0; d < TIterator::Dimension; ++d)
38 {
39 offset[d] = -1;
40 it->ActivateOffset(offset);
41 offset[d] = 1;
42 it->ActivateOffset(offset);
43 offset[d] = 0;
44 }
45 }
46 else
47 {
48 // activate all neighbors that are face+edge+vertex
49 // connected to the current pixel. do not include the center pixel
50 const unsigned int centerIndex = it->GetCenterNeighborhoodIndex();
51 for (unsigned int d = 0; d < centerIndex * 2 + 1; ++d)
52 {
53 const typename TIterator::OffsetType offset = it->GetOffset(d);
54 it->ActivateOffset(offset);
55 }
56 const typename TIterator::OffsetType offset_zeros{};
57 it->DeactivateOffset(offset_zeros);
58 }
59 return it;
60}
61
62template <typename TIterator>
63TIterator *
64setConnectivityPrevious(TIterator * it, bool fullyConnected = false)
65{
66 // activate the "previous" neighbours
67 it->ClearActiveList();
68 if (!fullyConnected)
69 {
70 // only activate the neighbors that are face connected
71 // to the current pixel. do not include the center pixel
72 typename TIterator::OffsetType offset{};
73 for (unsigned int d = 0; d < TIterator::Dimension; ++d)
74 {
75 offset[d] = -1;
76 it->ActivateOffset(offset);
77 offset[d] = 0;
78 }
79 }
80 else
81 {
82 // activate all neighbors that are face+edge+vertex
83 // connected to the current pixel. do not include the center pixel
84 const unsigned int centerIndex = it->GetCenterNeighborhoodIndex();
85 for (unsigned int d = 0; d < centerIndex; ++d)
86 {
87 const typename TIterator::OffsetType offset = it->GetOffset(d);
88 it->ActivateOffset(offset);
89 }
90 const typename TIterator::OffsetType offset_zeros{};
91 it->DeactivateOffset(offset_zeros);
92 }
93 return it;
94}
95
96template <typename TIterator>
97TIterator *
98setConnectivityLater(TIterator * it, bool fullyConnected = false)
99{
100 // activate the "later" neighbours
101
102 it->ClearActiveList();
103 if (!fullyConnected)
104 {
105 // only activate the neighbors that are face connected
106 // to the current pixel. do not include the center pixel
107 typename TIterator::OffsetType offset{};
108 for (unsigned int d = 0; d < TIterator::Dimension; ++d)
109 {
110 offset[d] = 1;
111 it->ActivateOffset(offset);
112 offset[d] = 0;
113 }
114 }
115 else
116 {
117 // activate all neighbors that are face+edge+vertex
118 // connected to the current pixel. do not include the center pixel
119 const unsigned int centerIndex = it->GetCenterNeighborhoodIndex();
120 for (unsigned int d = centerIndex + 1; d < 2 * centerIndex + 1; ++d)
121 {
122 const typename TIterator::OffsetType offset = it->GetOffset(d);
123 it->ActivateOffset(offset);
124 }
125 const typename TIterator::OffsetType offset_zeros{};
126 it->DeactivateOffset(offset_zeros);
127 }
128 return it;
129}
130} // namespace itk
131
132#endif
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
TIterator * setConnectivityLater(TIterator *it, bool fullyConnected=false)
TIterator * setConnectivity(TIterator *it, bool fullyConnected=false)
TIterator * setConnectivityPrevious(TIterator *it, bool fullyConnected=false)