Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ExtractImageStatistics.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3  The Medical Imaging Interaction Toolkit (MITK)
4 
5  Copyright (c) German Cancer Research Center,
6  Division of Medical and Biological Informatics.
7  All rights reserved.
8 
9  This software is distributed WITHOUT ANY WARRANTY; without
10  even the implied warranty of MERCHANTABILITY or FITNESS FOR
11  A PARTICULAR PURPOSE.
12 
13  See LICENSE.txt or http://www.mitk.org for details.
14 
15  ===================================================================*/
16 
17 //#include "MiniAppManager.h"
18 
19 //#include "ctkCommandLineParser.h"
21 #include "mitkImage.h"
22 #include "mitkImageStatisticsCalculator.h"
23 #include "mitkIOUtil.h"
24 #include <iostream>
25 #include <usAny.h>
26 #include <fstream>
27 
28 int ExtractImageStatistics(int argc, char* argv[])
29 {
30  mitkCommandLineParser parser;
31 
32  parser.setTitle("Extract Image Statistics");
33  parser.setCategory("Preprocessing Tools");
34  parser.setDescription("");
35  parser.setContributor("MBI");
36 
37  parser.setArgumentPrefix("--", "-");
38  parser.addArgument("help", "h", mitkCommandLineParser::String, "Help:", "Show this help text");
39  parser.addArgument("input", "i", mitkCommandLineParser::InputFile, "Input:", "input image", us::Any(),false);
40  parser.addArgument("mask", "m", mitkCommandLineParser::InputFile, "Mask:", "mask image / roi image denotin area on which statistics are calculated", us::Any(),false);
41  parser.addArgument("out", "o", mitkCommandLineParser::OutputFile, "Output", "output file (default: filenameOfRoi.nrrd_statistics.txt)", us::Any());
42 
43  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
44  if (parsedArgs.size()==0 || parsedArgs.count("help") || parsedArgs.count("h"))
45  {
46  std::cout << "\n\n MiniApp Description: \nCalculates statistics on the supplied image using given mask." << endl;
47  std::cout << "Output is written to the designated output file in this order:" << endl;
48  std::cout << "Mean, Standard Deviation, RMS, Max, Min, Number of Voxels, Volume [mm3]" << endl;
49  std::cout << "\n\n Parameters:"<< endl;
50  std::cout << parser.helpText();
51  return EXIT_SUCCESS;
52  }
53 
54 
55  // Parameters:
56  bool ignoreZeroValues = false;
57  unsigned int timeStep = 0;
58 
59  std::string inputImageFile = us::any_cast<string>(parsedArgs["input"]);
60  std::string maskImageFile = us::any_cast<string>(parsedArgs["mask"]);
61 
62  std::string outFile;
63  if (parsedArgs.count("out") || parsedArgs.count("o") )
64  outFile = us::any_cast<string>(parsedArgs["out"]);
65  else
66  outFile = inputImageFile + "_statistics.txt";
67 
68  // Load image and mask
69  mitk::Image::Pointer maskImage = mitk::IOUtil::LoadImage(maskImageFile);
70  mitk::Image::Pointer inputImage = mitk::IOUtil::LoadImage(inputImageFile);
71 
72  // Calculate statistics
75  try
76  {
77  calculator->SetImage(inputImage);
78  calculator->SetImageMask(maskImage);
79  calculator->SetMaskingModeToImage();
80  }
81  catch( const itk::ExceptionObject& e)
82  {
83  MITK_ERROR << "Statistic Calculation Failed - ITK Exception:" << e.what();
84  return -1;
85  }
86 
87 
88  calculator->SetDoIgnorePixelValue(ignoreZeroValues);
89  calculator->SetIgnorePixelValue(0);
90  try
91  {
92  calculator->ComputeStatistics(timeStep);
93  }
94  catch ( mitk::Exception& e)
95  {
96  MITK_ERROR<< "MITK Exception: " << e.what();
97  return -1;
98  }
99 
100 
101  statisticsStruct = calculator->GetStatistics(timeStep);
102 
103 
104  // Calculate Volume
105  double volume = 0;
106  const mitk::BaseGeometry *geometry = inputImage->GetGeometry();
107  if ( geometry != NULL )
108  {
109  const mitk::Vector3D &spacing = inputImage->GetGeometry()->GetSpacing();
110  volume = spacing[0] * spacing[1] * spacing[2] * (double) statisticsStruct.GetN();
111  }
112 
113  // Write Results to file
114  std::ofstream output;
115  output.open(outFile.c_str());
116  output << statisticsStruct.GetMean() << " , ";
117  output << statisticsStruct.GetSigma() << " , ";
118  output << statisticsStruct.GetRMS() << " , ";
119  output << statisticsStruct.GetMax() << " , ";
120  output << statisticsStruct.GetMin() << " , ";
121  output << statisticsStruct.GetN() << " , ";
122  output << volume << "\n";
123 
124  output.flush();
125  output.close();
126  return 0;
127 }
128 
#define MITK_ERROR
Definition: mitkLogMacros.h:24
int ExtractImageStatistics(int argc, char *argv[])
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
RegisterDiffusionMiniApp(ExtractImageStatistics)
Class for common statistics, includig hotspot properties.
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...
Definition: mitkException.h:49
Definition: usAny.h:163
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
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.
Definition: mitkIOUtil.cpp:597
BaseGeometry Describes the geometry of a data object.