Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
DwiDenoising.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 <mitkImageCast.h>
18 #include "mitkCommandLineParser.h"
19 #include <boost/algorithm/string.hpp>
20 #include <mitkImage.h>
22 #include <itkImage.h>
23 #include <mitkIOUtil.h>
25 #include <mitkImageCast.h>
26 #include <mitkITKImageImport.h>
27 #include <mitkProperties.h>
28 
29 using namespace std;
30 
32 typedef itk::Image<short, 3> ImageType;
33 
37 int main(int argc, char* argv[])
38 {
39  mitkCommandLineParser parser;
40 
41  parser.setTitle("DWI Denoising");
42  parser.setCategory("Preprocessing Tools");
43  parser.setContributor("MBI");
44  parser.setDescription("Denoising for diffusion weighted images using a non-local means algorithm.");
45 
46  parser.setArgumentPrefix("--", "-");
47  parser.addArgument("input", "i", mitkCommandLineParser::InputFile, "Input:", "input image (DWI)", us::Any(), false);
48  parser.addArgument("variance", "v", mitkCommandLineParser::Float, "Variance:", "noise variance", us::Any(), false);
49 
50  parser.addArgument("mask", "m", mitkCommandLineParser::InputFile, "Mask:", "brainmask for input image", us::Any(), true);
51  parser.addArgument("search", "s", mitkCommandLineParser::Int, "Search radius:", "search radius", us::Any(), true);
52  parser.addArgument("compare", "c", mitkCommandLineParser::Int, "Comparison radius:", "comparison radius", us::Any(), true);
53  parser.addArgument("joint", "j", mitkCommandLineParser::Bool, "Joint information:", "use joint information");
54  parser.addArgument("rician", "r", mitkCommandLineParser::Bool, "Rician adaption:", "use rician adaption");
55 
56  parser.changeParameterGroup("Output", "Output of this miniapp");
57 
58  parser.addArgument("output", "o", mitkCommandLineParser::OutputFile, "Output:", "output image (DWI)", us::Any(), false);
59 
60  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
61  if (parsedArgs.size()==0)
62  return EXIT_FAILURE;
63 
64  string inFileName = us::any_cast<string>(parsedArgs["input"]);
65  double variance = static_cast<double>(us::any_cast<float>(parsedArgs["variance"]));
66  string maskName;
67  if (parsedArgs.count("mask"))
68  maskName = us::any_cast<string>(parsedArgs["mask"]);
69  string outFileName = us::any_cast<string>(parsedArgs["output"]);
70 // boost::algorithm::erase_all(outFileName, ".dwi");
71  int search = 4;
72  if (parsedArgs.count("search"))
73  search = us::any_cast<int>(parsedArgs["search"]);
74  int compare = 1;
75  if (parsedArgs.count("compare"))
76  compare = us::any_cast<int>(parsedArgs["compare"]);
77  bool joint = false;
78  if (parsedArgs.count("joint"))
79  joint = true;
80  bool rician = false;
81  if (parsedArgs.count("rician"))
82  rician = true;
83 
84  try
85  {
86 
87  if( boost::algorithm::ends_with(inFileName, ".dwi"))
88  {
89 
91 
93  mitk::CastToItkImage(dwi, itkVectorImagePointer);
94 
96  filter->SetNumberOfThreads(12);
97  filter->SetInputImage( itkVectorImagePointer );
98 
99  if (!maskName.empty())
100  {
103  mitk::CastToItkImage(mask, itkMask);
104  filter->SetInputMask(itkMask);
105  }
106 
107  filter->SetUseJointInformation(joint);
108  filter->SetUseRicianAdaption(rician);
109  filter->SetSearchRadius(search);
110  filter->SetComparisonRadius(compare);
111  filter->SetVariance(variance);
112  filter->Update();
113 
114  DiffusionImageType::Pointer output = mitk::GrabItkImageMemory( filter->GetOutput() );
117  mitk::DiffusionPropertyHelper propertyHelper( output );
118  propertyHelper.InitializeImage();
119 
120 // std::stringstream name;
121 // name << outFileName << "_NLM_" << search << "-" << compare << "-" << variance << ".dwi";
122 
123  mitk::IOUtil::Save(output, outFileName.c_str());
124  }
125  else
126  {
127  std::cout << "Only supported for .dwi!";
128  }
129  }
130  catch (itk::ExceptionObject e)
131  {
132  std::cout << e;
133  return EXIT_FAILURE;
134  }
135  catch (std::exception e)
136  {
137  std::cout << e.what();
138  return EXIT_FAILURE;
139  }
140  catch (...)
141  {
142  std::cout << "ERROR!?!";
143  return EXIT_FAILURE;
144  }
145  return EXIT_SUCCESS;
146 }
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:824
static const std::string REFERENCEBVALUEPROPERTYNAME
itk::SmartPointer< Self > Pointer
int main(int argc, char *argv[])
void changeParameterGroup(std::string name, std::string tooltip)
void setContributor(std::string contributor)
STL namespace.
Helper class for mitk::Images containing diffusion weighted data.
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grabs the memory of an itk::Image (with a specific type) and puts it into an mitk::Image.The memory is managed by the mitk::Image after calling this function. The itk::Image remains valid until the mitk::Image decides to free the memory.
void InitializeImage()
Make certain the owned image is up to date with all necessary properties.
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)
bool compare(std::pair< double, int > i, std::pair< double, int > j)
Image class for storing images.
Definition: mitkImage.h:76
Definition: usAny.h:163
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
static Pointer New()
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
GradientDirectionsContainerType::Pointer GetGradientContainer() const
mitk::Image DiffusionImageType
static const std::string GRADIENTCONTAINERPROPERTYNAME
void setTitle(std::string title)
itk::Image< short, 3 > ImageType
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
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.