Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
CLMRNormalization.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 #ifndef mitkCLPolyToNrrd_cpp
13 #define mitkCLPolyToNrrd_cpp
14 
15 #include "time.h"
16 #include <sstream>
17 #include <fstream>
18 
19 #include <mitkIOUtil.h>
20 #include "mitkCommandLineParser.h"
21 
22 #include "itkImageRegionIterator.h"
23 // MITK
24 #include <mitkITKImageImport.h>
25 #include <mitkImageCast.h>
26 #include <mitkImageAccessByItk.h>
27 
30 // ITK
31 #include <itkLabelStatisticsImageFilter.h>
32 #include <itkMinimumMaximumImageCalculator.h>
33 
34 typedef itk::Image< double, 3 > FloatImageType;
35 typedef itk::Image< unsigned char, 3 > MaskImageType;
36 
37 int main(int argc, char* argv[])
38 {
39  MITK_INFO << "Start";
40  mitkCommandLineParser parser;
41  parser.setArgumentPrefix("--", "-");
42  // required params
43  parser.addArgument("image", "i", mitkCommandLineParser::Image, "Input Image", "Path to the input VTK polydata", us::Any(), false, false, false, mitkCommandLineParser::Input);
44  parser.addArgument("mode", "mode", mitkCommandLineParser::Image, "Normalisation mode", "1,2,3: Single Area normalization to Mean, Median, Mode, 4,5,6: Mean, Median, Mode of two regions. ", us::Any(), false, false, false, mitkCommandLineParser::Input);
45  parser.addArgument("mask0", "m0", mitkCommandLineParser::Image, "Input Mask", "The median of the area covered by this mask will be set to 0", us::Any(), false, false, false, mitkCommandLineParser::Input);
46  parser.addArgument("mask1", "m1", mitkCommandLineParser::Image, "Input Mask", "The median of the area covered by this mask will be set to 1", us::Any(), true, false, false, mitkCommandLineParser::Input);
47  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output Image", "Target file. The output statistic is appended to this file.", us::Any(), false, false, false, mitkCommandLineParser::Output);
48  parser.addArgument("ignore-outlier", "outlier", mitkCommandLineParser::Bool, "Ignore Outlier", "Ignores the highest and lowest 2% during calculation. Only on single mask normalization.", us::Any(), true);
49  parser.addArgument("value", "v", mitkCommandLineParser::Float, "Target Value", "Target value, the target value (for example median) is set to this value.", us::Any(), true);
50  parser.addArgument("width", "w", mitkCommandLineParser::Float, "Target Width", "Ignores the highest and lowest 2% during calculation. Only on single mask normalization.", us::Any(), true);
51  parser.addArgument("float", "float", mitkCommandLineParser::Bool, "Target Width", "Ignores the highest and lowest 2% during calculation. Only on single mask normalization.", us::Any(), true);
52 
53  // Miniapp Infos
54  parser.setCategory("Classification Tools");
55  parser.setTitle("MR Normalization Tool");
56  parser.setDescription("Normalizes a MR image. Sets the Median of the tissue covered by mask 0 to 0 and the median of the area covered by mask 1 to 1.");
57  parser.setContributor("German Cancer Research Center (DKFZ)");
58 
59  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
60 
61  if (parsedArgs.size()==0)
62  {
63  return EXIT_FAILURE;
64  }
65  if ( parsedArgs.count("help") || parsedArgs.count("h"))
66  {
67  return EXIT_SUCCESS;
68  }
69 
70  bool ignore_outlier = false;
71  if (parsedArgs.count("ignore-outlier"))
72  {
73  ignore_outlier = us::any_cast<bool>(parsedArgs["ignore-outlier"]);
74  }
75 
76  MITK_INFO << "Mode access";
77  int mode =std::stoi(us::any_cast<std::string>(parsedArgs["mode"]));
78  MITK_INFO << "Mode: " << mode;
79 
80  MITK_INFO << "Read images";
82  mitk::Image::Pointer image = mitk::IOUtil::Load<mitk::Image>(parsedArgs["image"].ToString());
83 
84  if (parsedArgs.count("float"))
85  {
86  typedef itk::Image<float, 3> ImageType;
87  ImageType::Pointer img = ImageType::New();
88  mitk::CastToItkImage(image, img);
89  mitk::CastToMitkImage(img, image);
90  }
91 
92  mitk::Image::Pointer mask0 = mitk::IOUtil::Load<mitk::Image>(parsedArgs["mask0"].ToString());
93  if (mode > 3)
94  {
95  mask1 = mitk::IOUtil::Load<mitk::Image>(parsedArgs["mask1"].ToString());
96  }
99  mitk::Image::Pointer output;
100 
101  oneRegion->SetInput(image);
102  oneRegion->SetMask(mask0);
103  oneRegion->SetIgnoreOutlier(ignore_outlier);
104  twoRegion->SetInput(image);
105  twoRegion->SetMask1(mask0);
106  twoRegion->SetMask2(mask1);
107 
108  if (parsedArgs.count("value"))
109  {
110  double target = us::any_cast<float>(parsedArgs["value"]);
111  oneRegion->SetTargetValue(target);
112  }
113  if (parsedArgs.count("width"))
114  {
115  double width = us::any_cast<float>(parsedArgs["width"]);
116  oneRegion->SetTargetValue(width);
117  }
118 
119  switch (mode)
120  {
121  case 1:
122  oneRegion->SetCenterMode(mitk::MRNormLinearStatisticBasedFilter::MEAN);
123  oneRegion->Update();
124  output=oneRegion->GetOutput();
125  break;
126  case 2:
127  oneRegion->SetCenterMode(mitk::MRNormLinearStatisticBasedFilter::MEDIAN);
128  oneRegion->Update();
129  output=oneRegion->GetOutput();
130  break;
131  case 3:
132  oneRegion->SetCenterMode(mitk::MRNormLinearStatisticBasedFilter::MODE);
133  oneRegion->Update();
134  output=oneRegion->GetOutput();
135  break;
136  case 4:
137  twoRegion->SetArea1(mitk::MRNormTwoRegionsBasedFilter::MEAN);
138  twoRegion->SetArea2(mitk::MRNormTwoRegionsBasedFilter::MEAN);
139  twoRegion->Update();
140  output=twoRegion->GetOutput();
141  break;
142  case 5:
143  twoRegion->SetArea1(mitk::MRNormTwoRegionsBasedFilter::MEDIAN);
144  twoRegion->SetArea2(mitk::MRNormTwoRegionsBasedFilter::MEDIAN);
145  twoRegion->Update();
146  output=twoRegion->GetOutput();
147  break;
148  case 6:
149  twoRegion->SetArea1(mitk::MRNormTwoRegionsBasedFilter::MODE);
150  twoRegion->SetArea2(mitk::MRNormTwoRegionsBasedFilter::MODE);
151  twoRegion->Update();
152  output=twoRegion->GetOutput();
153  break;
154  }
155 
156  mitk::IOUtil::Save(output, parsedArgs["output"].ToString());
157 
158  return 0;
159 }
160 
161 #endif
#define MITK_INFO
Definition: mitkLogMacros.h:18
itk::Image< unsigned char, 3 > ImageType
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
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, mitkCommandLineParser::Channel channel=mitkCommandLineParser::Channel::None)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
itk::Image< unsigned char, 3 > MaskImageType
itk::Image< double, 3 > FloatImageType
Definition: usAny.h:163
void setCategory(std::string category)
mitk::Image::Pointer image
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
Definition: mitkImageCast.h:74
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
void setTitle(std::string title)
void setDescription(std::string description)
int main(int argc, char *argv[])