19 #include "mitkImageStatisticsCalculator.h"
24 #include <itkImageRegionConstIterator.h>
26 #include <boost/accumulators/accumulators.hpp>
27 #include <boost/accumulators/statistics/stats.hpp>
28 #include <boost/accumulators/statistics/mean.hpp>
29 #include <boost/accumulators/statistics/variance.hpp>
30 #include <boost/accumulators/statistics/min.hpp>
31 #include <boost/accumulators/statistics/max.hpp>
32 #include <boost/accumulators/statistics/count.hpp>
33 #include <boost/accumulators/statistics/moment.hpp>
37 struct statistics_res{
38 double mean, variance,
min,
max, count, moment;
43 std::cout <<
"mean: " << s.mean << std::endl
44 <<
"variance: " << s.variance << std::endl
45 <<
"min: " << s.min << std::endl
46 <<
"max: " << s.max << std::endl
47 <<
"count: " << s.count << std::endl
48 <<
"moment: " << s.moment << std::endl;
51 template <
typename TPixel,
unsigned int VImageDimension >
53 typedef itk::Image<TPixel, VImageDimension>
ImageType;
55 itk::ImageRegionConstIterator<ImageType> it(itkImage, itkImage->GetLargestPossibleRegion());
61 boost::accumulators::accumulator_set<double, boost::accumulators::stats<
62 boost::accumulators::tag::mean,
63 boost::accumulators::tag::variance,
66 boost::accumulators::tag::count,
67 boost::accumulators::tag::moment<2>> > acc;
69 for (it.GoToBegin(); !it.IsAtEnd(); ++it)
78 res.mean = boost::accumulators::mean(acc);
79 res.variance = boost::accumulators::variance(acc);
82 res.count = boost::accumulators::count(acc);
83 res.moment = boost::accumulators::moment<2>(acc);
85 std::cout <<
"sum: " << sum <<
" N: " << ctr <<
" mean: " << res.mean << std::endl;
88 int main(
int argc,
char* argv[] )
92 parser.
setTitle(
"Extract Image Statistics");
103 std::cout <<
"test...." << std::endl;
105 std::map<std::string, us::Any> parsedArgs = parser.
parseArguments(argc, argv);
106 std::cout <<
"parsedArgs.size()= " << parsedArgs.size() << std::endl;
107 if (parsedArgs.size()==0 || parsedArgs.count(
"help") || parsedArgs.count(
"h"))
109 std::cout <<
"\n\n MiniApp Description: \nCalculates statistics on the supplied image using given mask." << endl;
110 std::cout <<
"Output is written to the designated output file in this order:" << endl;
111 std::cout <<
"Mean, Standard Deviation, RMS, Max, Min, Number of Voxels, Volume [mm3]" << endl;
112 std::cout <<
"\n\n Parameters:"<< endl;
119 bool ignoreZeroValues =
false;
120 unsigned int timeStep = 0;
122 std::string inputImageFile =
us::any_cast<std::string>(parsedArgs[
"input"]);
124 if (parsedArgs.count(
"mask") || parsedArgs.count(
"m"))
126 std::string maskImageFile =
us::any_cast<std::string>(parsedArgs[
"mask"]);
131 if (parsedArgs.count(
"out") || parsedArgs.count(
"o") )
132 outFile = us::any_cast<std::string>(parsedArgs[
"out"]);
134 outFile = inputImageFile +
"_statistics.txt";
144 calculator->SetInputImage(inputImage);
145 if (parsedArgs.count(
"mask") || parsedArgs.count(
"m"))
148 imgMask->SetImageMask(maskImage);
149 imgMask->SetTimeStep(timeStep);
150 calculator->SetMask(imgMask.GetPointer());
154 calculator->SetMask(
nullptr);
158 catch(
const itk::ExceptionObject& e)
160 MITK_ERROR <<
"Statistic Calculation Failed - ITK Exception:" << e.what();
165 if (ignoreZeroValues)
169 ignorePixelMask->SetInputImage(inputImage);
170 ignorePixelMask->SetTimeStep(timeStep);
171 ignorePixelMask->SetIgnoredPixelValue(0);
172 calculator->SetMask(ignorePixelMask.GetPointer());
175 std::cout <<
"calculating statistics itk: " << std::endl;
178 statisticsStruct = calculator->GetStatistics(timeStep);
190 if ( geometry != NULL )
192 const mitk::Vector3D &spacing = inputImage->GetGeometry()->GetSpacing();
193 volume = spacing[0] * spacing[1] * spacing[2] * (double) statisticsStruct->GetN();
197 std::ofstream output;
198 output.open(outFile.c_str());
199 output << statisticsStruct->GetMean() <<
" , ";
200 output << statisticsStruct->GetStd() <<
" , ";
201 output << statisticsStruct->GetRMS() <<
" , ";
202 output << statisticsStruct->GetMax() <<
" , ";
203 output << statisticsStruct->GetMin() <<
" , ";
204 output << statisticsStruct->GetN() <<
" , ";
205 output << volume <<
"\n";
210 std::cout <<
"calculating statistics boost: " << std::endl;
void get_statistics_boost(itk::Image< TPixel, VImageDimension > *itkImage, statistics_res &res)
itk::Image< unsigned char, 3 > ImageType
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
int main(int argc, char *argv[])
itk::SmartPointer< Self > Pointer
#define AccessByItk_n(mitkImage, itkImageTypeFunction, va_tuple)
Access a MITK image by an ITK image with one or more parameters.
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
void addArgument(const std::string &longarg, const std::string &shortarg, Type type, const std::string &argLabel, const std::string &argHelp=std::string(), const us::Any &defaultValue=us::Any(), bool optional=true, bool ignoreRest=false, bool deprecated=false)
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
void printstats(statistics_res s)
std::string helpText() const
void setTitle(std::string title)
void setDescription(std::string description)
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
BaseGeometry Describes the geometry of a data object.