diff --git a/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx b/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx index 6e49430d0848b4ce0fb725f0daf3c8f529880c49..e1b46e572ebc2d73d2fd92192b4ef81140815f18 100644 --- a/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx +++ b/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx @@ -63,7 +63,7 @@ private: { SetName("SmallRegionsMerging"); SetDescription("This application merges small regions of a segmentation " - "result to connected region."); + "result."); SetDocName("Small Region Merging"); SetDocLongDescription("Given a segmentation result and the original image," @@ -173,7 +173,7 @@ private: const auto & LUT = regionMergingFilter->GetLUT(); - for (auto label : LUT) + for (auto const & label : LUT) { if (label.first != label.second) { diff --git a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx index 7a72781957eec63ab9a975ac610b75996bfa4202..1a8851564b0604f4a2e57771e89c7335f61b8ad2 100644 --- a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx +++ b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx @@ -69,10 +69,11 @@ PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage > double proximity = itk::NumericTraits<double>::max(); InputLabelType label = neighbours.first; InputLabelType closestNeighbour = label; + auto const& statsLabel = m_LabelStatistic[ label ]; + for (auto neighbour : neighbours.second) { - auto statsLabel = m_LabelStatistic[ label ]; - auto statsNeighbour = m_LabelStatistic[ neighbour ]; + auto const & statsNeighbour = m_LabelStatistic[ neighbour ]; assert( statsLabel.Size() == statsNeighbour.Size() ); double distance = (statsLabel - statsNeighbour).GetSquaredNorm(); @@ -106,16 +107,22 @@ PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage > // Update statistics : for each newly merged segments, sum the population, and // recompute the mean. - for (auto label : m_LUT) + for (auto const & label : m_LUT) { - if((m_LabelPopulation[label.first]!=0) && (label.second != label.first)) + if ((label.second != label.first) && (m_LabelPopulation[label.first]!=0)) { + // Cache values to reduce number of lookups + auto const & populationFirst = m_LabelPopulation[label.first]; + auto const & populationSecond = m_LabelPopulation[label.second]; + auto const & statisticFirst = m_LabelStatistic[label.first]; + auto const & statisticSecond = m_LabelStatistic[label.second]; + m_LabelStatistic[ label.second ] = - ( m_LabelStatistic[label.first]*m_LabelPopulation[label.first] - + m_LabelStatistic[label.second]*m_LabelPopulation[label.second] ) - / (m_LabelPopulation[label.first]+m_LabelPopulation[label.second]); - - m_LabelPopulation[ label.second ] += m_LabelPopulation[ label.first ] ; + ( (statisticFirst * populationFirst) + + (statisticSecond * populationSecond) ) + / (populationFirst + populationSecond); + + m_LabelPopulation[ label.second ] += populationFirst; // Do not use this label anymore m_LabelPopulation[ label.first ] = 0;