diff --git a/Modules/ImageStatistics/mitkImageStatisticsContainer.cpp b/Modules/ImageStatistics/mitkImageStatisticsContainer.cpp index a38c652768..9544b76a22 100644 --- a/Modules/ImageStatistics/mitkImageStatisticsContainer.cpp +++ b/Modules/ImageStatistics/mitkImageStatisticsContainer.cpp @@ -1,96 +1,158 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include namespace mitk { StatisticsContainer::StatisticsContainer() { this->Reset(); } + StatisticsContainer::StatisticsObject::StatisticsObject() { + this->Reset(); + } + + StatisticsContainer::RealType StatisticsContainer::StatisticsObject::GetVariance() const { + return m_Std * m_Std; + } + + StatisticsContainer::statisticsOrderedVectorType StatisticsContainer::StatisticsObject::GetStatisticsAsOrderedVector() const { + statisticsOrderedVectorType statisticsAsVector; + statisticsAsVector.emplace_back(std::make_pair("Mean", std::to_string(m_Mean))); + statisticsAsVector.emplace_back(std::make_pair("Median", std::to_string(m_Median))); + statisticsAsVector.emplace_back(std::make_pair("StandardDeviation", std::to_string(m_Std))); + statisticsAsVector.emplace_back(std::make_pair("RMS", std::to_string(m_RMS))); + statisticsAsVector.emplace_back(std::make_pair("Max", std::to_string(m_Max))); + statisticsAsVector.emplace_back(std::make_pair("MaxPosition", convertToString(m_MaxIndex))); + statisticsAsVector.emplace_back(std::make_pair("Min", std::to_string(m_Min))); + statisticsAsVector.emplace_back(std::make_pair("MinPosition", convertToString(m_MinIndex))); + statisticsAsVector.emplace_back(std::make_pair("#Voxel", std::to_string(m_N))); + statisticsAsVector.emplace_back(std::make_pair("Volume [mm^3]", std::to_string(m_Volume))); + statisticsAsVector.emplace_back(std::make_pair("Skewness", std::to_string(m_Skewness))); + statisticsAsVector.emplace_back(std::make_pair("Kurtosis", std::to_string(m_Kurtosis))); + statisticsAsVector.emplace_back(std::make_pair("Uniformity", std::to_string(m_Uniformity))); + statisticsAsVector.emplace_back(std::make_pair("Entropy", std::to_string(m_Entropy))); + statisticsAsVector.emplace_back(std::make_pair("MPP", std::to_string(m_MPP))); + statisticsAsVector.emplace_back(std::make_pair("UPP", std::to_string(m_UPP))); + return statisticsAsVector; + } + + std::string StatisticsContainer::StatisticsObject::convertToString(const vnl_vector& index) const { + std::string result; + for (const auto& aValue : index) { + if (!result.empty()) { + result += "/"; + } + result += std::to_string(aValue); + } + return result; + } + + void StatisticsContainer::StatisticsObject::Reset() { + m_N = 0; + m_Volume = nan(""); + m_Mean = nan(""); + m_Min = nan(""); + m_Max = nan(""); + m_Std = nan(""); + m_Skewness = nan(""); + m_Kurtosis = nan(""); + m_RMS = nan(""); + m_MPP = nan(""); + m_Median = nan(""); + m_Uniformity = nan(""); + m_UPP = nan(""); + m_Entropy = nan(""); + + m_MinIndex.set_size(0); + m_MaxIndex.set_size(0); + m_Label = 0; + m_Histogram = HistogramType::New(); + } + bool StatisticsContainer::TimeStepExists(TimeStepType timeStep) const { return m_TimeStepMap.find(timeStep) != m_TimeStepMap.end(); } StatisticsContainer::StatisticsObject StatisticsContainer::GetStatisticsForTimeStep(TimeStepType timeStep) const { auto it = m_TimeStepMap.find(timeStep); if (it != m_TimeStepMap.end()) { return it->second; } mitkThrow() << "StatisticsObject for timeStep " << timeStep << " not found!"; } void StatisticsContainer::SetStatisticsForTimeStep(TimeStepType timeStep, StatisticsObject statistics) { if (timeStep < this->GetTimeSteps()) { m_TimeStepMap[timeStep] = statistics; } else { mitkThrow() << "Given timeStep " << timeStep << " out of timeStep geometry bounds. TimeSteps in geometry: " << this->GetTimeSteps(); } } void StatisticsContainer::PrintSelf(std::ostream &os, itk::Indent indent) const { Superclass::PrintSelf(os, indent); for (unsigned int i = 0; i < this->GetTimeSteps(); i++) { auto statistics = GetStatisticsAsOrderedVector(i); os << std::endl << indent << "Statistics instance for timeStep " << i << ":"; for (const auto& aStatisticValue : statistics) { os << std::endl << indent.GetNextIndent() << aStatisticValue.first << ": " << aStatisticValue.second; } } } StatisticsContainer::statisticsOrderedVectorType StatisticsContainer::GetStatisticsAsOrderedVector(TimeStepType timeStep) const { return GetStatisticsForTimeStep(timeStep).GetStatisticsAsOrderedVector(); } unsigned int StatisticsContainer::GetNumberOfTimeSteps()const { return this->GetTimeSteps(); } void StatisticsContainer::Reset() { for (auto iter = m_TimeStepMap.begin(); iter != m_TimeStepMap.end(); iter++) { iter->second.Reset(); } } itk::LightObject::Pointer StatisticsContainer::InternalClone() const { itk::LightObject::Pointer ioPtr = Superclass::InternalClone(); Self::Pointer rval = dynamic_cast(ioPtr.GetPointer()); if (rval.IsNull()) { itkExceptionMacro(<< "downcast to type " << "StatisticsContainer" << " failed."); } rval->SetTimeStepMap(m_TimeStepMap); rval->SetTimeGeometry(this->GetTimeGeometry()->Clone()); return ioPtr; } void StatisticsContainer::SetTimeStepMap(TimeStepMapType map) { m_TimeStepMap = map; } } diff --git a/Modules/ImageStatistics/mitkImageStatisticsContainer.h b/Modules/ImageStatistics/mitkImageStatisticsContainer.h index 2432427704..86c1b49ad3 100644 --- a/Modules/ImageStatistics/mitkImageStatisticsContainer.h +++ b/Modules/ImageStatistics/mitkImageStatisticsContainer.h @@ -1,193 +1,135 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef MITKIMAGESTATISTICSCONTAINER #define MITKIMAGESTATISTICSCONTAINER #include #include #include namespace mitk { /**Documentation @brief Container class for storing the computed image statistics. Stored statistics are: - N: number of voxels - Mean - MPP (Mean of positive pixels) - Median - Skewness - Kurtosis - Uniformity - UPP (Uniformity of positive pixels) - Std (Standard Deviation) - Min - Max - RMS (Root Mean Square) - Label (if applicable, the label (unsigned short) of the mask the statistics belong to) - Entropy - MinIndex (Index of Image where the Minimum is located) - MaxIndex (Index of Image where the Maximum is located) - Histogram of Pixel Values*/ class MITKIMAGESTATISTICS_EXPORT StatisticsContainer : public mitk::BaseData { public: mitkClassMacro(StatisticsContainer, mitk::BaseData) itkFactorylessNewMacro(Self) itkCloneMacro(Self) using HistogramType = itk::Statistics::Histogram; using RealType = double; using statisticsOrderedVectorType = std::vector < std::pair >; using LabelIndex = unsigned int; virtual void SetRequestedRegionToLargestPossibleRegion() override {}; virtual bool RequestedRegionIsOutsideOfTheBufferedRegion() override { return false; }; virtual bool VerifyRequestedRegion() override { return true; }; virtual void SetRequestedRegion(const itk::DataObject*) override {}; struct StatisticsObject { - StatisticsObject() { - this->Reset(); - } + StatisticsObject(); long m_N; RealType m_Volume; RealType m_Mean, m_Min, m_Max, m_Std; RealType m_Skewness; RealType m_Kurtosis; RealType m_RMS; RealType m_MPP; vnl_vector m_MinIndex, m_MaxIndex; RealType m_Median; RealType m_Uniformity; RealType m_UPP; RealType m_Entropy; LabelIndex m_Label; HistogramType::Pointer m_Histogram; - RealType GetVariance() const { - return m_Std * m_Std; - } - - statisticsOrderedVectorType GetStatisticsAsOrderedVector() { - statisticsOrderedVectorType statisticsAsVector; - statisticsAsVector.emplace_back(std::make_pair("Mean", std::to_string(m_Mean))); - statisticsAsVector.emplace_back(std::make_pair("Median", std::to_string(m_Median))); - statisticsAsVector.emplace_back(std::make_pair("StandardDeviation", std::to_string(m_Std))); - statisticsAsVector.emplace_back(std::make_pair("RMS", std::to_string(m_RMS))); - statisticsAsVector.emplace_back(std::make_pair("Max", std::to_string(m_Max))); - statisticsAsVector.emplace_back(std::make_pair("MaxPosition", convertToString(m_MaxIndex))); - statisticsAsVector.emplace_back(std::make_pair("Min", std::to_string(m_Min))); - statisticsAsVector.emplace_back(std::make_pair("MinPosition", convertToString(m_MinIndex))); - statisticsAsVector.emplace_back(std::make_pair("#Voxel", std::to_string(m_N))); - statisticsAsVector.emplace_back(std::make_pair("Volume [mm^3]", std::to_string(m_Volume))); - statisticsAsVector.emplace_back(std::make_pair("Skewness", std::to_string(m_Skewness))); - statisticsAsVector.emplace_back(std::make_pair("Kurtosis", std::to_string(m_Kurtosis))); - statisticsAsVector.emplace_back(std::make_pair("Uniformity", std::to_string(m_Uniformity))); - statisticsAsVector.emplace_back(std::make_pair("Entropy", std::to_string(m_Entropy))); - statisticsAsVector.emplace_back(std::make_pair("MPP", std::to_string(m_MPP))); - statisticsAsVector.emplace_back(std::make_pair("UPP", std::to_string(m_UPP))); - return statisticsAsVector; - } - - std::string convertToString(const vnl_vector& index) { - std::string result; - for (const auto& aValue : index) { - if (!result.empty()) { - result += "/"; - } - result += std::to_string(aValue); - } - return result; - } - - void Reset() { - m_N = 0; - m_Volume = nan(""); - m_Mean = nan(""); - m_Min = nan(""); - m_Max = nan(""); - m_Std = nan(""); - m_Skewness = nan(""); - m_Kurtosis = nan(""); - m_RMS = nan(""); - m_MPP = nan(""); - m_Median = nan(""); - m_Uniformity = nan(""); - m_UPP = nan(""); - m_Entropy = nan(""); - - m_MinIndex.set_size(0); - m_MaxIndex.set_size(0); - m_Label = 0; - m_Histogram = HistogramType::New(); - } - - + RealType GetVariance() const; + statisticsOrderedVectorType GetStatisticsAsOrderedVector() const; + std::string convertToString(const vnl_vector& index) const; + void Reset(); }; typedef std::map TimeStepMapType; /**Documentation @brief Returns a std::vector