Medical Imaging Interaction Toolkit  2018.4.99-e1268d66
Medical Imaging Interaction Toolkit
MaskRangeBasedFiltering.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 #include "mitkProperties.h"
13 
14 #include "mitkCommandLineParser.h"
15 #include "mitkIOUtil.h"
16 
18 
19 
20 int main(int argc, char* argv[])
21 {
22  mitkCommandLineParser parser;
23 
24  parser.setTitle("Mask Outlier filtering");
25  parser.setCategory("Basic Image Processing");
26  parser.setDescription("");
27  parser.setContributor("German Cancer Research Center (DKFZ)");
28 
29  parser.setArgumentPrefix("--", "-");
30  // Add command line argument names
31  parser.addArgument("help", "h", mitkCommandLineParser::Bool, "Help:", "Show this help text");
32  parser.addArgument("image", "i", mitkCommandLineParser::File, "Input image:", "Input Image", us::Any(), false, false, false, mitkCommandLineParser::Input);
33  parser.addArgument("mask", "m", mitkCommandLineParser::File, "Input mask:", "Input Mask", us::Any(), false, false, false, mitkCommandLineParser::Input);
34  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output file:", "Output Mask", us::Any(), false, false, false, mitkCommandLineParser::Output);
35 
36  parser.addArgument("lower-limit", "lL", mitkCommandLineParser::Float, "Lower Limit", "Lower Limit", us::Any(), true);
37  parser.addArgument("upper-limit", "ul", mitkCommandLineParser::Float, "Upper Limit", "Upper Limit", us::Any(), true);
38 
39  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
40 
41  if (parsedArgs.size() == 0)
42  return EXIT_FAILURE;
43 
44  // Show a help message
45  if (parsedArgs.count("help") || parsedArgs.count("h"))
46  {
47  std::cout << parser.helpText();
48  return EXIT_SUCCESS;
49  }
50 
51  std::string inputFilename = us::any_cast<std::string>(parsedArgs["image"]);
52  std::string maskFilename = us::any_cast<std::string>(parsedArgs["mask"]);
53  std::string outputFilename = us::any_cast<std::string>(parsedArgs["output"]);
54 
55  auto nodes = mitk::IOUtil::Load(inputFilename);
56  if (nodes.size() == 0)
57  {
58  MITK_INFO << "No Image Loaded";
59  return 0;
60  }
61  mitk::Image::Pointer image = dynamic_cast<mitk::Image*>(nodes[0].GetPointer());
62 
63  if (image.IsNull())
64  {
65  MITK_INFO << "Loaded data (image) is not of type image";
66  return 0;
67  }
68 
69 
70  auto maskNodes = mitk::IOUtil::Load(maskFilename);
71  if (maskNodes.size() == 0)
72  {
73  MITK_INFO << "No Mask Loaded";
74  return 0;
75  }
76  mitk::Image::Pointer mask = dynamic_cast<mitk::Image*>(maskNodes[0].GetPointer());
77 
78  if (image.IsNull())
79  {
80  MITK_INFO << "Loaded data (mask) is not of type image";
81  return 0;
82  }
83 
84  bool useUpperLimit = false;
85  bool useLowerLimit = false;
86  double lowerLimit = 0;
87  double upperLimit = 1;
88 
89  if (parsedArgs.count("lower-limit"))
90  {
91  useLowerLimit = true;
92  lowerLimit = us::any_cast<float>(parsedArgs["lower-limit"]);
93  }
94  if (parsedArgs.count("upper-limit"))
95  {
96  useUpperLimit = true;
97  upperLimit = us::any_cast<float>(parsedArgs["upper-limit"]);
98  }
99 
100  if (useLowerLimit || useUpperLimit)
101  {
102  mitk::Image::Pointer tmpImage = mitk::MaskCleaningOperation::RangeBasedMasking(image, mask, useLowerLimit, lowerLimit, useUpperLimit, upperLimit);
103  mitk::IOUtil::Save(tmpImage, outputFilename);
104  } else
105  {
106  MITK_INFO << "No limit specified. Specify either lower or upper limit";
107  }
108 
109  return EXIT_SUCCESS;
110 }
static Image::Pointer RangeBasedMasking(Image::Pointer &image, Image::Pointer &mask, bool lowerLimitOn=false, double lowerLimit=0, bool upperLimitOn=false, double upperLimit=1)
Limits a Mask to a given Range.
#define MITK_INFO
Definition: mitkLogMacros.h:18
std::string inputFilename
Definition: MitkMCxyz.cpp:625
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)
int main(int argc, char *argv[])
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
std::string outputFilename
Definition: MitkMCxyz.cpp:626
Image class for storing images.
Definition: mitkImage.h:72
Definition: usAny.h:163
std::string helpText() const
void setCategory(std::string category)
mitk::Image::Pointer image
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
mitk::Image::Pointer mask
void setTitle(std::string title)
void setDescription(std::string description)
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage, const ReaderOptionsFunctorBase *optionsCallback=nullptr)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:489