diff --git a/Modules/DiffusionImaging/MiniApps/ImageStatisticsMiniapp_v2.cpp b/Modules/DiffusionImaging/MiniApps/ImageStatisticsMiniapp_v2.cpp index 63395ef046..6d33c71c56 100644 --- a/Modules/DiffusionImaging/MiniApps/ImageStatisticsMiniapp_v2.cpp +++ b/Modules/DiffusionImaging/MiniApps/ImageStatisticsMiniapp_v2.cpp @@ -1,193 +1,203 @@ /*=================================================================== 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 "mitkCommandLineParser.h" #include "mitkImage.h" #include #include #include #include #include #include "mitkIOUtil.h" #include #include #include #include #include "mitkImageAccessByItk.h" #include #include #include #include #include #include #include #include #include #include +#include struct statistics_res{ double mean, variance, min, max, count, moment; }; void printstats(statistics_res s) { std::cout << "mean: " << s.mean << std::endl << "variance: " << s.variance << std::endl << "min: " << s.min << std::endl << "max: " << s.max << std::endl << "count: " << s.count << std::endl << "moment: " << s.moment << std::endl; } template void printMap(std::map input) { for (auto it = input.begin(); it != input.end(); ++it) { std::cout << it->first<< ": " << it->second<< std::endl; } std::cout << std::endl; } template < typename TPixel, unsigned int VImageDimension > void get_statistics_boost(itk::Image* itkImage, statistics_res& res){ typedef itk::Image ImageType; itk::ImageRegionConstIterator it(itkImage, itkImage->GetLargestPossibleRegion()); int ctr=0; double sum=0; boost::accumulators::accumulator_set> > acc; for (it.GoToBegin(); !it.IsAtEnd(); ++it) { acc(it.Get()); // currentPixel = it.Get(); // sum+=currentPixel; // ctr+=1; } // res.mean=(double)sum/(double)ctr; res.mean = boost::accumulators::mean(acc); res.variance = boost::accumulators::variance(acc); res.min = boost::accumulators::min(acc); res.max = boost::accumulators::max(acc); res.count = boost::accumulators::count(acc); res.moment = boost::accumulators::moment<2>(acc); std::cout << "sum: " << sum << " N: " << ctr << " mean: " << res.mean << std::endl; } int main( int argc, char* argv[] ) { unsigned int timeStep = 0; std::string inputImageFile; - inputImageFile = "/home/fabian/MITK/MITK_platform_project/bin/MITK-superbuild/MITK-Data/Pic2DplusT.nrrd"; + //inputImageFile = "/home/fabian/MITK/MITK_platform_project/bin/MITK-superbuild/MITK-Data/Pic2DplusT.nrrd"; + inputImageFile = "/home/fabian/MITK/MITK_platform_project/bin/MITK-superbuild/MITK-Data/Pic3D.nrrd"; // Load image mitk::Image::Pointer inputImage = mitk::IOUtil::LoadImage(inputImageFile); mitk::Image::Pointer maskImage; std::string maskImageFile = "/home/fabian/MITK/MITK_platform_project/bin/MITK-superbuild/MITK-Data/Pic3D_someSegmentation.nrrd"; maskImage = mitk::IOUtil::LoadImage(maskImageFile); mitk::ImageMaskGenerator::Pointer binaryImageMaskGen = mitk::ImageMaskGenerator::New(); binaryImageMaskGen->SetImageMask(maskImage); mitk::HotspotMaskGenerator::Pointer hotSpotMaskGen = mitk::HotspotMaskGenerator::New(); hotSpotMaskGen->SetImage(inputImage); hotSpotMaskGen->SetHotspotRadiusInMM(10); hotSpotMaskGen->SetTimeStep(0); std::string pFfile = "/home/fabian/MITK/MITK_platform_project/bin/MITK-superbuild/MITK-Data/Pic2DplusT_ellipse.pf"; std::vector loadedObjects; loadedObjects = mitk::IOUtil::Load(pFfile); mitk::BaseData::Pointer pf = loadedObjects[0]; mitk::PlanarFigure::Pointer planarFigure = dynamic_cast(pf.GetPointer()); mitk::PlanarFigureMaskGenerator::Pointer planarFigMaskExtr = mitk::PlanarFigureMaskGenerator::New(); planarFigMaskExtr->SetPlanarFigure(planarFigure); planarFigMaskExtr->SetImage(inputImage); // Calculate statistics mitk::ImageStatisticsCalculator_v2::Pointer calculator = mitk::ImageStatisticsCalculator_v2::New(); std::cout << "calculating statistics (unmasked) itk: " << std::endl; mitk::ImageStatisticsCalculator_v2::StatisticsContainer::Pointer result; calculator->SetInputImage(inputImage); - calculator->SetNBinsForHistogramStatistics(5); + calculator->SetNBinsForHistogramStatistics(100); std::cout << std::endl; - std::cout << "unmasked: " << std::endl; - calculator->SetMask(nullptr); - result = calculator->GetStatistics(timeStep); - result->PrintSelf(); - std::cout << std::endl; +// std::cout << "unmasked: " << std::endl; +// calculator->SetMask(nullptr); +// result = calculator->GetStatistics(timeStep); +// result->PrintSelf(); +// std::cout << std::endl; - std::cout << "planarfigure: " << std::endl; - calculator->SetMask(planarFigMaskExtr.GetPointer()); +// std::cout << "planarfigure: " << std::endl; +// calculator->SetMask(planarFigMaskExtr.GetPointer()); +// result = calculator->GetStatistics(timeStep, 1); +// result->PrintSelf(); +// std::cout << std::endl; + + std::cout << "ignore pixel value mask: " << std::endl; + mitk::IgnorePixelMaskGenerator::Pointer ignPixVal = mitk::IgnorePixelMaskGenerator::New(); + ignPixVal->SetImage(inputImage); + ignPixVal->SetIgnoredPixelValue(0); + calculator->SetMask(ignPixVal.GetPointer()); result = calculator->GetStatistics(timeStep, 1); result->PrintSelf(); std::cout << std::endl; - - + mitk::IOUtil::SaveImage(ignPixVal->GetMask(), "/home/fabian/MITK/MITK_platform_project/bin/MITK-superbuild/MITK-Data/Pic3D_zeroValuedPixelsMask.nrrd"); // std::cout << "image mask: " << std::endl; // calculator->SetMask(binaryImageMaskGen.GetPointer()); // result = calculator->GetStatistics(timeStep, 1); // result->PrintSelf(); // std::cout << "no mask: " << std::endl; // calculator->SetMask(nullptr); // result = calculator->GetStatistics(timeStep); // result->PrintSelf(); // calculator->SetMask(hotSpotMaskGen.GetPointer()); // result = calculator->GetStatistics(timeStep, 1); // result->PrintSelf(); // std::cout << std::endl << std::endl << "calculating statistics old imgstatcalc: " << std::endl; // mitk::ImageStatisticsCalculator::Statistics statisticsStruct; // mitk::ImageStatisticsCalculator::Pointer calculator_old = mitk::ImageStatisticsCalculator::New(); // calculator_old->SetImage(inputImage); // calculator_old->SetImageMask(maskImage); // //calculator_old->SetMaskingModeToImage(); // calculator_old->SetHistogramBinSize(100); // calculator_old->SetPlanarFigure(planarFigure); // calculator_old->SetMaskingModeToPlanarFigure(); // calculator_old->SetMaskingModeToNone(); // calculator_old->SetDoIgnorePixelValue(false); // calculator_old->ComputeStatistics(timeStep); // statisticsStruct = calculator_old->GetStatistics(timeStep); // std::cout << "calculating statistics boost: " << std::endl; // statistics_res res; // AccessByItk_n(inputImage, get_statistics_boost, (res)); // printstats(res); return EXIT_SUCCESS; } diff --git a/Modules/ImageStatistics/files.cmake b/Modules/ImageStatistics/files.cmake index cefbe9ea3c..ffca98b49e 100644 --- a/Modules/ImageStatistics/files.cmake +++ b/Modules/ImageStatistics/files.cmake @@ -1,31 +1,33 @@ set(CPP_FILES mitkImageStatisticsCalculator.cpp mitkImageStatisticsCalculator2.cpp mitkPointSetStatisticsCalculator.cpp mitkPointSetDifferenceStatisticsCalculator.cpp mitkIntensityProfile.cpp mitkHotspotMaskGenerator.cpp mitkMaskGenerator.cpp mitkPlanarFigureMaskGenerator.cpp mitkMultiLabelMaskGenerator.cpp mitkImageMaskGenerator.cpp mitkHistogramStatisticsCalculator.cpp mitkMaskUtilities.cpp + mitkIgnorePixelMaskGenerator.cpp ) set(H_FILES mitkImageStatisticsCalculator.h mitkImageStatisticsCalculator2.h mitkPointSetDifferenceStatisticsCalculator.h mitkPointSetStatisticsCalculator.h mitkExtendedStatisticsImageFilter.h mitkExtendedLabelStatisticsImageFilter.h mitkHotspotMaskGenerator.h mitkMaskGenerator.h mitkPlanarFigureMaskGenerator.h mitkMultiLabelMaskGenerator.h mitkImageMaskGenerator.h mitkHistogramStatisticsCalculator.h mitkMaskUtilities.h mitkitkMaskImageFilter.h + mitkIgnorePixelMaskGenerator.h ) diff --git a/Modules/ImageStatistics/mitkIgnorePixelMaskGenerator.cpp b/Modules/ImageStatistics/mitkIgnorePixelMaskGenerator.cpp new file mode 100644 index 0000000000..a709d90a8c --- /dev/null +++ b/Modules/ImageStatistics/mitkIgnorePixelMaskGenerator.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include + +namespace mitk +{ +void IgnorePixelMaskGenerator::SetImage(Image::Pointer image) +{ + if (m_Image != image) + { + m_Image = image; + m_Modified = true; + } +} + +void IgnorePixelMaskGenerator::SetIgnoredPixelValue(RealType pixelValue) +{ + if (pixelValue != m_IgnoredPixelValue) + { + m_IgnoredPixelValue = pixelValue; + m_Modified = true; + } +} + +mitk::Image::Pointer IgnorePixelMaskGenerator::GetMask() +{ + if (m_Modified) + { + if (m_Image.IsNull()) + { + MITK_ERROR << "Image not set!"; + } + + if (m_IgnoredPixelValue == std::numeric_limits::min()) + { + MITK_ERROR << "IgnotePixelValue not set!"; + } + + if (m_TimeStep > (m_Image->GetTimeSteps() - 1)) + { + MITK_ERROR << "Invalid time step: " << m_TimeStep << ". The image has " << m_Image->GetTimeSteps() << " timeSteps!"; + } + + // extractimage time slice + ImageTimeSelector::Pointer imgTimeSel = ImageTimeSelector::New(); + imgTimeSel->SetInput(m_Image); + imgTimeSel->SetTimeNr(m_TimeStep); + imgTimeSel->UpdateLargestPossibleRegion(); + + mitk::Image::Pointer timeSliceImage = imgTimeSel->GetOutput(); + + // update m_InternalMask + AccessByItk(timeSliceImage, InternalCalculateMask); + m_InternalMask->SetGeometry(timeSliceImage->GetGeometry()); + + m_Modified = false; + } + + return m_InternalMask; +} + +template +void IgnorePixelMaskGenerator::InternalCalculateMask(typename itk::Image* image) +{ + typedef itk::Image ImageType; + typedef itk::Image MaskType; + + typename MaskType::Pointer mask = MaskType::New(); + mask->SetOrigin(image->GetOrigin()); + mask->SetSpacing(image->GetSpacing()); + mask->SetLargestPossibleRegion(image->GetLargestPossibleRegion()); + mask->SetBufferedRegion(image->GetBufferedRegion()); + mask->SetDirection(image->GetDirection()); + mask->SetNumberOfComponentsPerPixel(image->GetNumberOfComponentsPerPixel()); + mask->Allocate(); + mask->FillBuffer(1); + + // iterate over image and mask and set mask=1 if image=m_IgnorePixelValue + itk::ImageRegionConstIterator imageIterator(image, image->GetLargestPossibleRegion()); + itk::ImageRegionIterator maskIterator(mask, mask->GetLargestPossibleRegion()); + + + for (imageIterator.GoToBegin(); !imageIterator.IsAtEnd(); ++imageIterator, ++maskIterator) + { + if (imageIterator.Value() == static_cast(m_IgnoredPixelValue)) + { + maskIterator.Set(0); + } + } + + m_InternalMask = GrabItkImageMemory(mask); +} + +} // end namespace diff --git a/Modules/ImageStatistics/mitkIgnorePixelMaskGenerator.h b/Modules/ImageStatistics/mitkIgnorePixelMaskGenerator.h new file mode 100644 index 0000000000..e060ebb244 --- /dev/null +++ b/Modules/ImageStatistics/mitkIgnorePixelMaskGenerator.h @@ -0,0 +1,53 @@ +#ifndef MITKIGNOREPIXELMASKGEN_ +#define MITKIGNOREPIXELMASKGEN_ + +#include +#include +#include +#include +#include + + +namespace mitk +{ +class MITKIMAGESTATISTICS_EXPORT IgnorePixelMaskGenerator: public MaskGenerator +{ +public: + /** Standard Self typedef */ + typedef IgnorePixelMaskGenerator Self; + typedef MaskGenerator Superclass; + typedef itk::SmartPointer< Self > Pointer; + typedef itk::SmartPointer< const Self > ConstPointer; + typedef double RealType; + + /** Method for creation through the object factory. */ + itkNewMacro(Self) + + /** Runtime information support. */ + itkTypeMacro(IgnorePixelMaskGenerator, MaskGenerator) + + void SetImage(mitk::Image::Pointer image); + + void SetIgnoredPixelValue(RealType pixelValue); + + mitk::Image::Pointer GetMask(); + +protected: + IgnorePixelMaskGenerator(): + m_IgnoredPixelValue(std::numeric_limits::min()) + {} + + ~IgnorePixelMaskGenerator(){} + + template + void InternalCalculateMask(typename itk::Image* image); + +private: + mitk::Image::Pointer m_Image; + RealType m_IgnoredPixelValue; + +}; + +} + +#endif