18 #ifndef __otbImageMultiSegmentationToRCC8GraphFilter_txx
19 #define __otbImageMultiSegmentationToRCC8GraphFilter_txx
36 template <
class TInputImage,
class TOutputGraph>
40 m_Optimisation =
false;
45 template <
class TInputImage,
class TOutputGraph>
50 template <
class TInputImage,
class TOutputGraph>
55 return m_Accumulator[val];
58 template <
class TInputImage,
class TOutputGraph>
63 unsigned int result = 0;
64 for (
unsigned int i = 0; i < 8; ++i)
66 result += m_Accumulator[i];
71 template <
class TInputImage,
class TOutputGraph>
88 const int knowledge[8][8]
90 { {-3, -2, -2, -2, 0, -2, 0, 0}, {-1, -3, -2, -3, -1, -3, 0, 1}, {-1, -1, -3, -3, -1, -3, -1, 2}, { 0, -1, -2, -3, -3, 5, -1, 3}, {-1, -1, -1, -3, -1, -3, 6, 4}, { 0, 0, -2, 5, -2, 5, -3, 5}, {-1, -1, -1, -1, 6, -3, 6, 6}, { 0, 1, 2, 3, 4, 5, 6, 7}
102 int value = knowledge[r1][r2];
109 else if (value == -1)
114 else if (value == -2)
128 template <
class TInputImage,
class TOutputGraph>
146 typedef typename SimplifyPathFilterType::InputListType PathListType;
155 std::vector<PixelType> maxLabelVector;
158 unsigned int vertexIndex = 0;
159 unsigned int segmentationImageIndex = 0;
160 unsigned int nbVertices = 0;
166 typename MinMaxCalculatorType::Pointer minMax = MinMaxCalculatorType::New();
167 minMax->SetImage(it.Get());
168 minMax->ComputeMaximum();
169 maxLabelVector.push_back(minMax->GetMaximum());
170 otbMsgDebugMacro(<<
"Number of objects in image " << segmentationImageIndex <<
": "
171 << minMax->GetMaximum());
174 for (
PixelType label = 1; label <= maxLabelVector.back(); ++label)
176 typename PathListType::Pointer region = PathListType::New();
177 typename EdgeExtractionFilterType::Pointer extraction = EdgeExtractionFilterType::New();
178 extraction->SetInput(it.Get());
179 extraction->SetForegroundValue(label);
180 extraction->Update();
181 region->PushBack(extraction->GetOutput());
182 typename SimplifyPathFilterType::Pointer simplifier = SimplifyPathFilterType::New();
183 simplifier->SetInput(region);
184 simplifier->GetFunctor().SetTolerance(0.1);
185 simplifier->Update();
190 vertex->SetPath(simplifier->GetOutput()->GetNthElement(0));
191 vertex->SetSegmentationLevel(segmentationImageIndex / 2);
192 vertex->SetSegmentationType(segmentationImageIndex % 2);
194 graph->SetVertex(vertexIndex, vertex);
198 ++segmentationImageIndex;
203 VertexIteratorType vIt1(graph);
204 VertexIteratorType vIt2(graph);
207 for (vIt1.GoToBegin(); !vIt1.IsAtEnd(); ++vIt1)
209 for (vIt2.GoToBegin(); !vIt2.IsAtEnd(); ++vIt2)
212 if (vIt1.GetIndex() < vIt2.GetIndex())
216 typename RCC8CalculatorType::Pointer calc = RCC8CalculatorType::New();
217 calc->SetPolygon1(vIt1.Get()->GetPath());
218 calc->SetPolygon2(vIt2.Get()->GetPath());
225 InEdgeIteratorType inIt1(vIt1.GetIndex(), graph);
226 InEdgeIteratorType inIt2(vIt2.GetIndex(), graph);
233 while (!inIt1.IsAtEnd() && (know.first != FULL))
235 betweenIndex = inIt1.GetSourceIndex();
237 bool edgeFound =
false;
238 while (!inIt2.IsAtEnd() && (know.first != FULL))
242 if (inIt2.GetSourceIndex() == betweenIndex)
248 know = GetKnowledge(invert[inIt1.GetValue()], inIt2.GetValue());
249 calc->SetLevel1APrioriKnowledge(know.first == LEVEL_1);
250 calc->SetLevel3APrioriKnowledge(know.first == LEVEL_3);
260 know = GetKnowledge(invert[inIt1.GetValue()],
OTB_RCC8_DC);
261 calc->SetLevel1APrioriKnowledge(know.first == LEVEL_1);
262 calc->SetLevel3APrioriKnowledge(know.first == LEVEL_3);
268 if (know.first == FULL)
278 value = calc->GetValue();
286 value = calc->GetValue();
288 m_Accumulator[value] += 1;
289 m_Accumulator[invert[value]] += 1;
294 otbMsgDevMacro(<<
"Adding edge: " << vIt1.GetIndex() <<
" -> " << vIt2.GetIndex() <<
": " << value);
295 graph->AddEdge(vIt1.GetIndex(), vIt2.GetIndex(), value);
304 template <
class TInputImage,
class TOutputGraph>
309 Superclass::PrintSelf(os, indent);