diff --git a/Modules/ImageStatistics/mitkImageStatisticsContainer.cpp b/Modules/ImageStatistics/mitkImageStatisticsContainer.cpp index 09061ff03d..1077da5200 100644 --- a/Modules/ImageStatistics/mitkImageStatisticsContainer.cpp +++ b/Modules/ImageStatistics/mitkImageStatisticsContainer.cpp @@ -1,97 +1,149 @@ /*=================================================================== 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 { ImageStatisticsContainer::ImageStatisticsContainer() { this->Reset(); } + ImageStatisticsContainer::StatisticsObject::StatisticsObject() { + Reset(); + } + void ImageStatisticsContainer::StatisticsObject::AddStatistic(const std::string& key, StatisticsVariantType value) { statistics.emplace(key, value); } + std::vector ImageStatisticsContainer::StatisticsObject::GetAllStatisticNames() + { + std::vector statisticKeys = { + ImageStatisticsConstants::MEAN(), + ImageStatisticsConstants::MEDIAN(), + ImageStatisticsConstants::STANDARDDEVIATION(), + ImageStatisticsConstants::RMS(), + ImageStatisticsConstants::MAXIMUM(), + ImageStatisticsConstants::MAXIMUMPOSITION(), + ImageStatisticsConstants::MINIMUM(), + ImageStatisticsConstants::MINIMUMPOSITION(), + ImageStatisticsConstants::NUMBEROFVOXELS(), + ImageStatisticsConstants::VOLUME(), + ImageStatisticsConstants::SKEWNESS(), + ImageStatisticsConstants::KURTOSIS(), + ImageStatisticsConstants::UNIFORMITY(), + ImageStatisticsConstants::ENTROPY(), + ImageStatisticsConstants::MPP(), + ImageStatisticsConstants::UPP() }; + return statisticKeys; + } + + bool ImageStatisticsContainer::StatisticsObject::HasStatistic(const std::string& name) const + { + auto valueIterator = statistics.find(name); + if (valueIterator != statistics.end()) { + return true; + } + else { + return false; + } + } + + ImageStatisticsContainer::StatisticsVariantType ImageStatisticsContainer::StatisticsObject::GetValueNonConverted(const std::string& name) const + { + if (HasStatistic(name)) { + return statistics.find(name)->second; + } + else { + mitkThrow() << "invalid statistic key, could not find"; + } + } + + void ImageStatisticsContainer::StatisticsObject::Reset() + { + statistics.clear(); + } + bool ImageStatisticsContainer::TimeStepExists(TimeStepType timeStep) const { return m_TimeStepMap.find(timeStep) != m_TimeStepMap.end(); } ImageStatisticsContainer::StatisticsObject ImageStatisticsContainer::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 ImageStatisticsContainer::SetStatisticsForTimeStep(TimeStepType timeStep, StatisticsObject statistics) { if (timeStep < this->GetTimeSteps()) { m_TimeStepMap.emplace(timeStep, statistics); } else { mitkThrow() << "Given timeStep " << timeStep << " out of timeStep geometry bounds. TimeSteps in geometry: " << this->GetTimeSteps(); } } void ImageStatisticsContainer::PrintSelf(std::ostream &os, itk::Indent indent) const { Superclass::PrintSelf(os, indent); auto statisticKeys = ImageStatisticsContainer::StatisticsObject::GetAllStatisticNames(); for (unsigned int i = 0; i < this->GetTimeSteps(); i++) { auto statisticsValues = GetStatisticsForTimeStep(i); os << std::endl << indent << "Statistics instance for timeStep " << i << ":"; for (const auto& aKey : statisticKeys) { os << std::endl << indent.GetNextIndent() << aKey << ": " << statisticsValues.GetValueNonConverted(aKey); } } } unsigned int ImageStatisticsContainer::GetNumberOfTimeSteps()const { return this->GetTimeSteps(); } void ImageStatisticsContainer::Reset() { for (auto iter = m_TimeStepMap.begin(); iter != m_TimeStepMap.end(); iter++) { iter->second.Reset(); } } itk::LightObject::Pointer ImageStatisticsContainer::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 ImageStatisticsContainer::SetTimeStepMap(TimeStepMapType map) { m_TimeStepMap = map; } } diff --git a/Modules/ImageStatistics/mitkImageStatisticsContainer.h b/Modules/ImageStatistics/mitkImageStatisticsContainer.h index 5fa5bba325..9c6d22ec64 100644 --- a/Modules/ImageStatistics/mitkImageStatisticsContainer.h +++ b/Modules/ImageStatistics/mitkImageStatisticsContainer.h @@ -1,183 +1,146 @@ /*=================================================================== 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 #include #include namespace mitk { /**Documentation @brief Container class for storing a StatisticsObject for each timestep. Stored statistics are: - for the defined statistics, see GetAllStatisticNames - Label (if applicable, the label (unsigned short) of the mask the statistics belong to) - Histogram of Pixel Values*/ class MITKIMAGESTATISTICS_EXPORT ImageStatisticsContainer : public mitk::BaseData { public: mitkClassMacro(ImageStatisticsContainer, mitk::BaseData) itkFactorylessNewMacro(Self) itkCloneMacro(Self) using HistogramType = itk::Statistics::Histogram; using RealType = double; using LabelIndex = unsigned int; using IndexType = vnl_vector; using VoxelCountType = unsigned long; using StatisticsVariantType = boost::variant; using StatisticsMapType = std::map < std::string, StatisticsVariantType>; using StatisticsKeyType = std::string; virtual void SetRequestedRegionToLargestPossibleRegion() override {}; virtual bool RequestedRegionIsOutsideOfTheBufferedRegion() override { return false; }; virtual bool VerifyRequestedRegion() override { return true; }; virtual void SetRequestedRegion(const itk::DataObject*) override {}; /**Documentation @brief Container class for storing the computed image statistics. @details The statistics are stored in a map with value as boost::variant. The type used to create the boost::variant is important as only this type can be recovered lateron. */ - class StatisticsObject { + class MITKIMAGESTATISTICS_EXPORT StatisticsObject { public: - StatisticsObject() { - Reset(); - } + StatisticsObject(); /**Documentation @brief Adds a statistic to the statistics object @details if already a statistic with that name is included, it is overwritten */ void AddStatistic(const std::string& key, StatisticsVariantType value); /**Documentation @brief Returns the names of all statistics @details The order is derived from the image statistics plugin */ - static std::vector GetAllStatisticNames() { - std::vector statisticKeys = { - ImageStatisticsConstants::MEAN(), - ImageStatisticsConstants::MEDIAN(), - ImageStatisticsConstants::STANDARDDEVIATION(), - ImageStatisticsConstants::RMS(), - ImageStatisticsConstants::MAXIMUM(), - ImageStatisticsConstants::MAXIMUMPOSITION(), - ImageStatisticsConstants::MINIMUM(), - ImageStatisticsConstants::MINIMUMPOSITION(), - ImageStatisticsConstants::NUMBEROFVOXELS(), - ImageStatisticsConstants::VOLUME(), - ImageStatisticsConstants::SKEWNESS(), - ImageStatisticsConstants::KURTOSIS(), - ImageStatisticsConstants::UNIFORMITY(), - ImageStatisticsConstants::ENTROPY(), - ImageStatisticsConstants::MPP(), - ImageStatisticsConstants::UPP() }; - return statisticKeys; }; - - bool HasStatistic(const std::string& name) const { - auto valueIterator = statistics.find(name); - if (valueIterator != statistics.end()) { - return true; - } - else { - return false; - } - } + static std::vector GetAllStatisticNames(); + + bool HasStatistic(const std::string& name) const; /**Documentation @brief Converts the requested value to the defined type @param name defined string on creation (AddStatistic) @exception if no statistics with key name was found. */ template TType GetValueConverted(const std::string& name) const { auto value = GetValueNonConverted(name); return boost::get(value); }; /**Documentation @brief Returns the requested value @exception if no statistics with key name was found. */ - StatisticsVariantType GetValueNonConverted(const std::string& name) const { - if (HasStatistic(name)) { - return statistics.find(name)->second; - } - else { - mitkThrow() << "invalid statistic key, could not find"; - } - } - - void Reset() { - statistics.clear(); - }; + StatisticsVariantType GetValueNonConverted(const std::string& name) const; + + void Reset(); LabelIndex m_Label; HistogramType::ConstPointer m_Histogram=nullptr; private: StatisticsMapType statistics; }; typedef std::map TimeStepMapType; unsigned int GetNumberOfTimeSteps() const; /**Documentation @brief Deletes all stored values*/ void Reset(); /**Documentation @brief Returns the statisticObject for the given Timestep @pre timeStep must be valid */ StatisticsObject GetStatisticsForTimeStep(TimeStepType timeStep) const; /**Documentation @brief Sets the statisticObject for the given Timestep @pre timeStep must be valid */ void SetStatisticsForTimeStep(TimeStepType timeStep, StatisticsObject statistics); /**Documentation @brief Checks if the Time step exists @pre timeStep must be valid */ bool TimeStepExists(TimeStepType timeStep) const; protected: ImageStatisticsContainer(); virtual void PrintSelf(std::ostream &os, itk::Indent indent) const override; private: itk::LightObject::Pointer InternalClone() const override; void SetTimeStepMap(TimeStepMapType map); TimeStepMapType m_TimeStepMap; }; } #endif // MITKIMAGESTATISTICSCONTAINER