diff --git a/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx b/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx index a9b4e4fc74efe1ef3c409fb3f2aef8acace9c286..d7391db9eebccb18c06715bed2ebd0375d58e8d5 100644 --- a/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx +++ b/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx @@ -125,20 +125,22 @@ private: labelStatsFilter->SetInputLabelImage(labelIn); AddProcess(labelStatsFilter->GetStreamer() , "Computing stats on input image ..."); labelStatsFilter->Update(); - - // Convert Map to Vector + + // Convert Map to Unordered map + auto labelPopulationMap = labelStatsFilter->GetLabelPopulationMap(); - std::vector<double> labelPopulation; - for (unsigned int i =0; i <= labelPopulationMap.rbegin()->first; i++) + std::unordered_map< unsigned int,double> labelPopulation; + for (population : labelPopulationMap) { - labelPopulation.push_back(labelPopulationMap[i]); + labelPopulation[population.first]=population.second; } auto meanValueMap = labelStatsFilter->GetMeanValueMap(); - std::vector<itk::VariableLengthVector<double> > meanValues; - for (unsigned int i =0; i <= meanValueMap.rbegin()->first; i++) + std::unordered_map< unsigned int, itk::VariableLengthVector<double> > meanValues; + for (mean : meanValueMap) { - meanValues.push_back(meanValueMap[i]); - } + meanValues[mean.first] = mean.second; + } + // Compute the LUT from the original label image to the merged output label image. auto regionMergingFilter = LabelImageSmallRegionMergingFilterType::New(); diff --git a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h index e13b8894ff8dcccae94e1e0e655cee14ad012376..15a91b8785ca35203643437015b7974deaf2d9f1 100644 --- a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h +++ b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h @@ -24,6 +24,8 @@ #include "otbPersistentImageFilter.h" #include "otbPersistentFilterStreamingDecorator.h" +#include <unordered_map> + namespace otb { @@ -76,9 +78,10 @@ public: typedef itk::VariableLengthVector<double> RealVectorPixelType; typedef std::map<InputLabelType, std::set<InputLabelType> > NeigboursMapType; - typedef std::vector<RealVectorPixelType > LabelStatisticType; - typedef std::vector<double> LabelPopulationType; - typedef std::vector<InputLabelType> LUTType; + + typedef std::unordered_map<InputLabelType , RealVectorPixelType > LabelStatisticType; + typedef std::unordered_map<InputLabelType , double> LabelPopulationType; + typedef std::unordered_map<InputLabelType , InputLabelType> LUTType; /** Set/Get size of segments to be merged */ itkGetMacro(Size , unsigned int); @@ -88,12 +91,12 @@ public: void SetLabelPopulation( LabelPopulationType const & labelPopulation ) { m_LabelPopulation = labelPopulation; + // Initialize m_CorrespondingMap to the identity (i.e. m[label] = label) - m_LUT.resize( labelPopulation.size() ); - for (unsigned int i =0; i <labelPopulation.size(); i++) + for (label : m_LabelPopulation) { - m_LUT[ i ] = i; - } + m_LUT[label.first] = label.first; + } } /** Get the Label population */ diff --git a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx index 7c152b8c003718d241c56026da7b91ae968510b7..29267904987cfeaca16b5d4b7425d07bccd2b7ab 100644 --- a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx +++ b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx @@ -210,7 +210,6 @@ PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage > { assert( !itN.IsAtEnd() ); int currentLabel = FindCorrespondingLabel(it.Get()); - if ( m_LabelPopulation[currentLabel] == m_Size ) { for (auto ci = itN.Begin() ; !ci.IsAtEnd(); ci++)