21 #ifndef otbHooverInstanceFilter_hxx
22 #define otbHooverInstanceFilter_hxx
31 template <
class TLabelMap>
34 this->SetNumberOfRequiredInputs(2);
35 this->SetNumberOfRequiredOutputs(2);
36 typename LabelMapType::Pointer secondOutput = LabelMapType::New();
37 this->AddOutput(secondOutput);
53 template <
class TLabelMap>
56 this->SetInput(0, gt);
60 template <
class TLabelMap>
63 this->SetInput(1, ms);
67 template <
class TLabelMap>
70 return this->GetInput(0);
74 template <
class TLabelMap>
77 return const_cast<TLabelMap*
>(this->GetInput(1));
81 template <
class TLabelMap>
84 return this->GetOutput(0);
88 template <
class TLabelMap>
91 return this->GetOutput(1);
94 template <
class TLabelMap>
97 Superclass::AllocateOutputs();
99 if (this->GetInPlace() && this->CanRunInPlace())
101 LabelMapPointer secondInput =
const_cast<TLabelMap*
>(this->GetMachineSegmentationLabelMap());
105 ImageRegionType region = this->GetOutput(1)->GetLargestPossibleRegion();
106 this->GraftNthOutput(1, secondInput);
107 this->GetOutput(1)->SetRegions(region);
113 const TLabelMap* inputMS = this->GetInput(1);
114 TLabelMap* outputMS = this->GetOutput(1);
115 assert(inputMS != NULL);
116 assert(outputMS != NULL);
118 outputMS->SetBackgroundValue(inputMS->GetBackgroundValue());
122 while (!it.IsAtEnd())
126 assert(labeObject != NULL);
127 assert(labeObject->GetLabel() == it.GetLabel());
129 typename LabelObjectType::Pointer newLabelObject = LabelObjectType::New();
130 newLabelObject->CopyAllFrom(labeObject);
132 outputMS->AddLabelObject(newLabelObject);
138 template <
class TLabelMap>
141 Superclass::ReleaseInputs();
143 if (this->GetInPlace())
146 TLabelMap* ptr =
const_cast<TLabelMap*
>(this->GetInput(1));
154 template <
class TLabelMap>
158 Superclass::BeforeThreadedGenerateData();
160 m_NumberOfRegionsGT = this->GetGroundTruthLabelMap()->GetNumberOfLabelObjects();
161 m_NumberOfRegionsMS = this->GetMachineSegmentationLabelMap()->GetNumberOfLabelObjects();
163 if (m_NumberOfRegionsGT == 0 || m_NumberOfRegionsMS == 0)
165 itkExceptionMacro(
"Empty label map");
169 if (m_NumberOfRegionsGT != m_HooverMatrix.Rows() || m_NumberOfRegionsMS != m_HooverMatrix.Cols())
171 itkExceptionMacro(
"The given Hoover confusion matrix (" << m_HooverMatrix.Rows() <<
" x " << m_HooverMatrix.Cols()
172 <<
") doesn't match with the input label maps (" << m_NumberOfRegionsGT <<
" x "
173 << m_NumberOfRegionsMS <<
")");
177 m_CardRegGT.SetSize(m_NumberOfRegionsGT);
180 m_CardRegMS.SetSize(m_NumberOfRegionsMS);
188 typename LabelObjectType::Pointer blankRegion;
190 while (!iter.IsAtEnd())
193 m_CardRegMS[i] = regionMS->Size();
194 if (m_CardRegMS[i] == 0)
196 otbWarningMacro(
"Region " << i <<
" in machine segmentation label map is empty");
199 regionMS->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RC), 0.0);
200 regionMS->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RF), 0.0);
201 regionMS->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RA), 0.0);
202 regionMS->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RN), 0.0);
204 if (m_UseExtendedAttributes)
206 blankRegion = LabelObjectType::New();
207 blankRegion->SetLabel(regionMS->GetLabel());
208 std::vector<std::string> attKeys = regionMS->GetAvailableAttributes();
209 for (
unsigned int k = 0; k < attKeys.size(); k++)
211 if (attKeys[k].
find(
"HooverInstance_Ext_") == 0)
217 blankRegion->SetAttribute(attKeys[k].c_str(), regionMS->GetAttribute(attKeys[k].c_str()));
220 regionMS->CopyAttributesFrom(blankRegion);
226 m_LabelsGT = this->GetGroundTruthLabelMap()->GetLabels();
229 template <
class TLabelMap>
233 unsigned long currentRegionGT = 0;
234 LabelType currentLabelGT = labelObject->GetLabel();
235 for (
unsigned long k = 0; k < m_NumberOfRegionsGT; k++)
237 if (currentLabelGT == m_LabelsGT[k])
244 m_CardRegGT[currentRegionGT] = labelObject->Size();
245 if (m_CardRegGT[currentRegionGT] == 0)
247 otbWarningMacro(
"Region " << currentRegionGT <<
" in ground truth label map is empty");
251 labelObject->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RC), 0.0);
252 labelObject->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RF), 0.0);
253 labelObject->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RA), 0.0);
254 labelObject->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RM), 0.0);
256 if (m_UseExtendedAttributes)
258 typename LabelObjectType::Pointer blankRegion;
259 blankRegion = LabelObjectType::New();
260 blankRegion->SetLabel(labelObject->GetLabel());
261 std::vector<std::string> attKeys = labelObject->GetAvailableAttributes();
262 for (
unsigned int k = 0; k < attKeys.size(); k++)
264 if (attKeys[k].
find(
"HooverInstance_Ext_") == 0)
270 blankRegion->SetAttribute(attKeys[k].c_str(), labelObject->GetAttribute(attKeys[k].c_str()));
273 labelObject->CopyAttributesFrom(blankRegion);
277 template <
class TLabelMap>
296 double bufferRC = 0.0;
297 double bufferRF = 0.0;
298 double bufferRA = 0.0;
299 double bufferRM = 0.0;
300 double bufferRN = 0.0;
305 for (
unsigned int row = 0; row < m_NumberOfRegionsGT; row++, iterGT++)
308 double sumScoreRF = 0.0;
312 double tGT =
static_cast<double>(m_CardRegGT[row]) * m_Threshold;
315 for (
unsigned int col = 0; col < m_NumberOfRegionsMS; col++, iterMS++)
318 double coefT =
static_cast<double>(m_HooverMatrix(row, col));
329 double tMS =
static_cast<double>(m_CardRegMS[col]) * m_Threshold;
331 otbDebugMacro(<<
"* coef[" << row <<
"," << col <<
"]=" << coefT <<
" #tGT=" << tGT <<
" #tMS=" << tMS);
338 otbDebugMacro(<<
"1 coef[" << row <<
"," << col <<
"]=" << coefT <<
" #tGT=" << tGT <<
" #tMS=" << tMS <<
" -> CD");
342 double scoreRC = m_Threshold * (std::min(coefT / tGT, coefT / tMS));
343 bufferRC += scoreRC *
static_cast<double>(m_CardRegGT[row]);
345 regionGT->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RC),
static_cast<AttributesValueType>(scoreRC));
346 regionMS->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RC),
static_cast<AttributesValueType>(scoreRC));
348 if (m_UseExtendedAttributes)
350 regionGT->SetAttribute(GetNameFromAttribute(ATTRIBUTE_CD),
static_cast<AttributesValueType>(regionMS->GetLabel()));
351 regionMS->SetAttribute(GetNameFromAttribute(ATTRIBUTE_CD),
static_cast<AttributesValueType>(regionGT->GetLabel()));
354 GTindices.insert(row);
355 MSindices.insert(col);
359 otbDebugMacro(<<
"2 coef[" << row <<
"," << col <<
"]=" << coefT <<
" #tGT=" << tGT <<
" #tMS=" << tMS <<
" -> OSmaybe");
361 objectsOfMS.push_back(iterMS.GetLabelObject());
362 regionsOfMS.insert(col);
364 sumScoreRF += coefT * (coefT - 1.0);
368 otbDebugMacro(<<
"end of line " << row <<
"; sumOS=" << sumOS <<
" " << regionsOfMS.size() <<
" of MS region");
369 if (sumOS >= tGT && sumOS > 0)
372 if (regionsOfMS.size() == 1)
377 else if (regionsOfMS.size() > 1)
382 double cardRegGT =
static_cast<double>(m_CardRegGT[row]);
383 double scoreRF = 1.0 - sumScoreRF / (cardRegGT * (cardRegGT - 1.0));
384 bufferRF += scoreRF * cardRegGT;
386 regionGT->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RF),
static_cast<AttributesValueType>(scoreRF));
388 unsigned int indexOS = 1;
389 for (
typename ObjectVectorType::iterator it = objectsOfMS.begin(); it != objectsOfMS.end(); ++it)
392 std::ostringstream attribute;
393 attribute << ATTRIBUTE_OS <<
"_" << indexOS;
395 regionMS->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RF),
static_cast<AttributesValueType>(scoreRF));
397 if (m_UseExtendedAttributes)
399 regionGT->SetAttribute(attribute.str().c_str(),
static_cast<AttributesValueType>(regionMS->GetLabel()));
400 regionMS->SetAttribute(GetNameFromAttribute(ATTRIBUTE_OS),
static_cast<AttributesValueType>(regionGT->GetLabel()));
406 GTindices.insert(row);
407 for (RegionSetType::iterator it = regionsOfMS.begin(); it != regionsOfMS.end(); ++it)
409 MSindices.insert(*it);
415 otbDebugMacro(<<
"No MS region present in potential OS instance");
421 GTindices.insert(row);
425 areaGT +=
static_cast<double>(m_CardRegGT[row]);
431 for (
unsigned int col = 0; col < m_NumberOfRegionsMS; col++, iterMS++)
434 double sumScoreUS = 0.0;
435 double sumCardUS = 0.0;
440 double tMS =
static_cast<double>(m_CardRegMS[col]) * m_Threshold;
443 for (
unsigned int row = 0; row < m_NumberOfRegionsGT; row++, iterGT++)
445 double coefT =
static_cast<double>(m_HooverMatrix(row, col));
456 double tGT =
static_cast<double>(m_CardRegGT[row]) * m_Threshold;
460 otbDebugMacro(<<
"3 coef[" << row <<
"," << col <<
"]=" << coefT <<
" #tGT=" << tGT <<
" #tMS=" << tMS <<
" -> USmaybe");
461 regionsOfGT.insert(row);
462 objectsOfGT.push_back(iterGT.GetLabelObject());
464 sumScoreUS += coefT * (coefT - 1.0);
465 sumCardUS +=
static_cast<double>(m_CardRegGT[row]);
472 if (regionsOfGT.size() == 1)
476 else if (regionsOfGT.size() > 1)
479 double scoreRA = 1.0 - sumScoreUS / (sumCardUS * (sumCardUS - 1.0));
480 bufferRA += scoreRA * sumCardUS;
482 regionMS->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RA),
static_cast<AttributesValueType>(scoreRA));
484 unsigned int indexUS = 1;
485 for (
typename ObjectVectorType::iterator it = objectsOfGT.begin(); it != objectsOfGT.end(); ++it)
488 std::ostringstream attribute;
489 attribute << ATTRIBUTE_US <<
"_" << indexUS;
491 regionGT->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RA),
static_cast<AttributesValueType>(scoreRA));
493 if (m_UseExtendedAttributes)
495 regionMS->SetAttribute(attribute.str(),
static_cast<AttributesValueType>(regionGT->GetLabel()));
496 regionGT->SetAttribute(GetNameFromAttribute(ATTRIBUTE_US),
static_cast<AttributesValueType>(regionMS->GetLabel()));
502 MSindices.insert(col);
503 for (RegionSetType::iterator it = regionsOfGT.begin(); it != regionsOfGT.end(); ++it)
505 GTindices.insert(*it);
512 otbDebugMacro(<<
"No GT region present in potential US instance.");
518 MSindices.insert(col);
522 areaMS +=
static_cast<double>(m_CardRegMS[col]);
528 for (
unsigned int i = 0; i < m_NumberOfRegionsGT; ++i, ++iterGT)
530 if (GTindices.count(i) == 0)
535 bufferRM +=
static_cast<double>(m_CardRegGT[i]);
537 regionGT->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RM), 1.0);
539 if (m_UseExtendedAttributes)
541 regionGT->SetAttribute(GetNameFromAttribute(ATTRIBUTE_M),
static_cast<AttributesValueType>(regionGT->GetLabel()));
548 for (
unsigned int i = 0; i < m_NumberOfRegionsMS; ++i, ++iterMS)
550 if (MSindices.count(i) == 0)
554 bufferRN +=
static_cast<double>(m_CardRegMS[i]);
556 regionMS->SetAttribute(GetNameFromAttribute(ATTRIBUTE_RN), 1.0);
558 if (m_UseExtendedAttributes)
560 regionMS->SetAttribute(GetNameFromAttribute(ATTRIBUTE_N),
static_cast<AttributesValueType>(regionMS->GetLabel()));