From 5f68d2cc08641c75a1af9da279ca06636cfe9e11 Mon Sep 17 00:00:00 2001 From: Jonathan Guinet <jonathan.guinet@c-s.fr> Date: Fri, 29 Jun 2012 14:43:29 +0200 Subject: [PATCH] std::map loop using for is wrong in GetMeanIntensity() when dealing wth labelimage which contains non contiguous labels. Mantis #ref564 --- Applications/Utils/otbColorMapping.cxx | 40 +++++++++++++++++++------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/Applications/Utils/otbColorMapping.cxx b/Applications/Utils/otbColorMapping.cxx index 84ef09e8a7..c89a18114c 100644 --- a/Applications/Utils/otbColorMapping.cxx +++ b/Applications/Utils/otbColorMapping.cxx @@ -199,6 +199,7 @@ public: void AddNewLabel(TLabel label, TValue value) { + TValue newValue; newValue.SetSize(value.Size()); for (unsigned int index = 0; index < value.Size(); index++) @@ -251,17 +252,34 @@ public: { MeanValueMapType MeanMap; - for(unsigned int i=0; i<m_WeigthingMap.size(); i++) + typename std::map<TLabel, unsigned int>::iterator mapIt = m_WeigthingMap.begin(); + typename std::map<TLabel, TValue>::iterator sumIt = m_LabelToImageIntensityMap.begin(); + + unsigned int pixelSize = m_MinVal.Size(); + while (mapIt != m_WeigthingMap.end()) { - TValue value = m_LabelToImageIntensityMap[i]; - for (unsigned int index = 0; index < value.Size(); index++) - { - value[index]=value[index]/static_cast<float>(m_WeigthingMap[i]); - } - MeanMap[i]=value; - } + TLabel i = mapIt->first; + float weight = static_cast<float> (mapIt->second); + TValue sum = sumIt->second; + TValue value; + value.SetSize(pixelSize); + if (sum.Size() == 0) + { + value.Fill(0.0); + } + else + { + for (unsigned int index = 0; index < sum.Size(); index++) + { + value[index] = sum[index] / weight; + } + } + MeanMap[i] = value; + ++mapIt; + ++sumIt; + } return MeanMap; } @@ -757,17 +775,17 @@ private: { LabelType clabel = mapIt->first; - if (clabel == 0) + meanValue = mapIt->second; //meanValue.Size() is null if label is not present in label image + if (clabel == 0 || meanValue.Size()==0) { color.Fill(0.0); } else { - - meanValue = mapIt->second; for (int RGB = 0; RGB < 3; RGB++) { unsigned int dispIndex = RGBIndex[RGB]; + color[RGB] = ((meanValue.GetElement(dispIndex) - minVal.GetElement(dispIndex)) / ( maxVal.GetElement(dispIndex) - minVal.GetElement(dispIndex))) * 255.0; } -- GitLab