Medical Imaging Interaction Toolkit  2018.4.99-dcb22cd7
Medical Imaging Interaction Toolkit
CLN4.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 
13 #include "mitkCommandLineParser.h"
14 #include "mitkIOUtil.h"
15 #include <mitkImageCast.h>
16 #include "mitkCommandLineParser.h"
17 #include <itkN4BiasFieldCorrectionImageFilter.h>
18 
19 #include <itkSTAPLEImageFilter.h>
20 
21 int main(int argc, char* argv[])
22 {
23  typedef itk::Image<unsigned char, 3> MaskImageType;
24  typedef itk::Image<float, 3> ImageType;
25  typedef itk::N4BiasFieldCorrectionImageFilter < ImageType, MaskImageType, ImageType > FilterType;
26 
27  mitkCommandLineParser parser;
28  parser.setTitle("N4 Bias Field Correction");
29  parser.setCategory("Classification Command Tools");
30  parser.setDescription("");
31  parser.setContributor("German Cancer Research Center (DKFZ)");
32 
33  parser.setArgumentPrefix("--", "-");
34  // Add command line argument names
35  parser.addArgument("help", "h", mitkCommandLineParser::Bool, "Help:", "Show this help text");
36  parser.addArgument("input", "i", mitkCommandLineParser::Directory, "Input file:", "Input file", us::Any(), false, false, false, mitkCommandLineParser::Input);
37  parser.addArgument("mask", "m", mitkCommandLineParser::File, "Output file:", "Mask file", us::Any(), false, false, false, mitkCommandLineParser::Output);
38  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output file:", "Output file", us::Any(), false, false, false, mitkCommandLineParser::Output);
39 
40  parser.addArgument("number-of-controllpoints", "noc", mitkCommandLineParser::Int, "Parameter", "The noc for the point grid size defining the B-spline estimate (default 4)", us::Any(), true);
41  parser.addArgument("number-of-fitting-levels", "nofl", mitkCommandLineParser::Int, "Parameter", "Number of fitting levels for the multi-scale approach (default 1)", us::Any(), true);
42  parser.addArgument("number-of-histogram-bins", "nofl", mitkCommandLineParser::Int, "Parameter", "number of bins defining the log input intensity histogram (default 200)", us::Any(), true);
43  parser.addArgument("spline-order", "so", mitkCommandLineParser::Int, "Parameter", "Define the spline order (default 3)", us::Any(), true);
44  parser.addArgument("winer-filter-noise", "wfn", mitkCommandLineParser::Float, "Parameter", "Noise estimate defining the Wiener filter (default 0.01)", us::Any(), true);
45  parser.addArgument("number-of-maximum-iterations", "nomi", mitkCommandLineParser::Int, "Parameter", "Spezifies the maximum number of iterations per run", us::Any(), true);
46  // ToDo: Number Of Maximum Iterations durchschleifen
47 
48  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
49 
50  // Show a help message
51  if (parsedArgs.count("help") || parsedArgs.count("h"))
52  {
53  std::cout << parser.helpText();
54  return EXIT_SUCCESS;
55  }
56 
57  MaskImageType::Pointer itkMsk = MaskImageType::New();
58  mitk::Image::Pointer img = mitk::IOUtil::Load<mitk::Image>(parsedArgs["mask"].ToString());
59  mitk::CastToItkImage(img, itkMsk);
60 
61  ImageType::Pointer itkImage = ImageType::New();
62  mitk::Image::Pointer img2 = mitk::IOUtil::Load<mitk::Image>(parsedArgs["input"].ToString());
63  mitk::CastToItkImage(img2, itkImage);
64 
65  FilterType::Pointer filter = FilterType::New();
66  filter->SetInput(itkImage);
67  filter->SetMaskImage(itkMsk);
68 
69 
70 
71  if (parsedArgs.count("number-of-controllpoints") > 0)
72  {
73  int variable = us::any_cast<int>(parsedArgs["maximum-iterations"]);
74  MITK_INFO << "Number of controll points: " << variable;
75  filter->SetNumberOfControlPoints(variable);
76  }
77  if (parsedArgs.count("number-of-fitting-levels") > 0)
78  {
79  int variable = us::any_cast<int>(parsedArgs["number-of-fitting-levels"]);
80  MITK_INFO << "Number of fitting levels: " << variable;
81  filter->SetNumberOfFittingLevels(variable);
82  }
83  if (parsedArgs.count("number-of-histogram-bins") > 0)
84  {
85  int variable = us::any_cast<int>(parsedArgs["number-of-histogram-bins"]);
86  MITK_INFO << "Number of histogram bins: " << variable;
87  filter->SetNumberOfHistogramBins(variable);
88  }
89  if (parsedArgs.count("spline-order") > 0)
90  {
91  int variable = us::any_cast<int>(parsedArgs["spline-order"]);
92  MITK_INFO << "Spline Order " << variable;
93  filter->SetSplineOrder(variable);
94  }
95  if (parsedArgs.count("winer-filter-noise") > 0)
96  {
97  float variable = us::any_cast<float>(parsedArgs["winer-filter-noise"]);
98  MITK_INFO << "Number of histogram bins: " << variable;
99  filter->SetWienerFilterNoise(variable);
100  }
101  if (parsedArgs.count("number-of-maximum-iterations") > 0)
102  {
103  int variable = us::any_cast<int>(parsedArgs["number-of-maximum-iterations"]);
104  MITK_INFO << "Number of Maximum Iterations: " << variable;
105  auto list = filter->GetMaximumNumberOfIterations();
106  list.Fill(variable);
107  filter->SetMaximumNumberOfIterations(list);
108  }
109 
110  filter->Update();
111  auto out = filter->GetOutput();
113  mitk::CastToMitkImage(out, outImg);
114  mitk::IOUtil::Save(outImg, parsedArgs["output"].ToString());
115 
116  return EXIT_SUCCESS;
117 }
#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)
Definition: usAny.h:163
itk::Image< unsigned char, 3 > MaskImageType
Definition: CLBrainMask.cpp:32
std::string helpText() const
void setCategory(std::string category)
static Pointer New()
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
int main(int argc, char *argv[])
Definition: CLN4.cpp:21
void setTitle(std::string title)
void setDescription(std::string description)