21 #ifndef otbLabelMapWithAdjacency_h
22 #define otbLabelMapWithAdjacency_h
24 #include "itkLabelMap.h"
40 template <
class TLabelObject>
64 itkStaticConstMacro(ImageDimension,
unsigned int, LabelObjectType::ImageDimension);
67 typedef typename LabelObjectType::LabelType
LabelType;
87 m_AdjacencyMap = amap;
92 return m_AdjacencyMap;
98 if (m_AdjacencyMap.find(label1) != m_AdjacencyMap.end())
100 m_AdjacencyMap[label1].insert(label2);
105 newContainer.insert(label2);
106 m_AdjacencyMap[label1] = newContainer;
114 if (m_AdjacencyMap.find(label) != m_AdjacencyMap.end())
116 m_AdjacencyMap[label].clear();
124 if (m_AdjacencyMap.find(label1) != m_AdjacencyMap.end())
126 m_AdjacencyMap[label1].erase(label2);
134 typename AdjacencyMapType::const_iterator it = m_AdjacencyMap.find(label);
135 if (it != m_AdjacencyMap.end())
141 itkExceptionMacro(<<
"No Adjacency set for label " << label <<
".");
150 if (label1 == label2)
157 if (m_AdjacencyMap.find(label1) != m_AdjacencyMap.end() && !m_AdjacencyMap[label1].count(label2))
159 itkExceptionMacro(<<
"Labels " << label1 <<
" and " << label2 <<
" are not adjacent, can not merge.");
163 typename LabelObjectType::Pointer lo1 = this->GetLabelObject(label1);
164 typename LabelObjectType::Pointer lo2 = this->GetLabelObject(label2);
168 typename LabelObjectType::Pointer loOut = mergeFunctor(lo1, lo2);
171 for (
typename AdjacentLabelsContainerType::iterator it = m_AdjacencyMap[label2].begin(); it != m_AdjacencyMap[label2].end(); ++it)
173 m_AdjacencyMap[*it].erase(label2);
176 m_AdjacencyMap[*it].insert(label1);
177 m_AdjacencyMap[label1].insert(*it);
182 m_AdjacencyMap.erase(label2);
185 this->RemoveLabel(label2);
188 this->RemoveLabel(label1);
189 this->AddLabelObject(loOut);
198 typename LabelPairVectorType::iterator lpit1, lpit2;
200 for (lpit1 = internalLabelPairs.begin(); lpit1 != internalLabelPairs.end(); ++lpit1)
203 this->MergeLabels(lpit1->first, lpit1->second);
206 for (lpit2 = lpit1 + 1; lpit2 != internalLabelPairs.end(); ++lpit2)
208 if (lpit2->first == lpit1->second)
210 lpit2->first = lpit1->first;
212 if (lpit2->second == lpit1->second)
214 lpit2->second = lpit1->first;
232 void PrintSelf(std::ostream& os, itk::Indent indent)
const override
234 Superclass::PrintSelf(os, indent);
242 Superclass::CopyInformation(data);
245 const Self* selfData =
dynamic_cast<const Self*
>(data);
254 void operator=(
const Self&) =
delete;