Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
MultishellMethods.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 #include <vector>
17 #include <iostream>
18 #include <fstream>
19 #include <algorithm>
20 #include <string>
21 
22 #include <itkImage.h>
23 #include <itkImageFileReader.h>
24 #include <itkExceptionObject.h>
25 #include <itkImageFileWriter.h>
26 #include <itkMetaDataObject.h>
27 #include <itkVectorImage.h>
28 #include <itkResampleImageFilter.h>
29 
30 #include <mitkImage.h>
31 #include <mitkQBallImage.h>
32 #include <mitkBaseData.h>
33 #include <mitkFiberBundle.h>
34 #include "mitkCommandLineParser.h"
35 #include <boost/lexical_cast.hpp>
36 
38 #include <itkADCAverageFunctor.h>
39 #include <itkBiExpFitFunctor.h>
40 #include <itkKurtosisFitFunctor.h>
42 #include <mitkIOUtil.h>
44 #include <mitkProperties.h>
45 #include <mitkImageCast.h>
46 #include <mitkITKImageImport.h>
47 
48 using namespace std;
49 
50 int main(int argc, char* argv[])
51 {
52  mitkCommandLineParser parser;
53 
54  parser.setTitle("Multishell Methods");
55  parser.setCategory("Fiber Tracking and Processing Methods");
56  parser.setDescription("");
57  parser.setContributor("MBI");
58 
59  parser.setArgumentPrefix("--", "-");
60  parser.addArgument("in", "i", mitkCommandLineParser::InputFile, "Input:", "input file", us::Any(), false);
61  parser.addArgument("out", "o", mitkCommandLineParser::OutputFile, "Output:", "output file", us::Any(), false);
62  parser.addArgument("adc", "D", mitkCommandLineParser::Bool, "ADC:", "ADC Average", us::Any(), false);
63  parser.addArgument("akc", "K", mitkCommandLineParser::Bool, "Kurtosis fit:", "Kurtosis Fit", us::Any(), false);
64  parser.addArgument("biexp", "B", mitkCommandLineParser::Bool, "BiExp fit:", "BiExp fit", us::Any(), false);
65  parser.addArgument("targetbvalue", "b", mitkCommandLineParser::String, "b Value:", "target bValue (mean, min, max)", us::Any(), false);
66 
67  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
68  if (parsedArgs.size()==0)
69  return EXIT_FAILURE;
70 
71  // mandatory arguments
72  string inName = us::any_cast<string>(parsedArgs["in"]);
73  string outName = us::any_cast<string>(parsedArgs["out"]);
74  bool applyADC = us::any_cast<bool>(parsedArgs["adc"]);
75  bool applyAKC = us::any_cast<bool>(parsedArgs["akc"]);
76  bool applyBiExp = us::any_cast<bool>(parsedArgs["biexp"]);
77  string targetType = us::any_cast<string>(parsedArgs["targetbvalue"]);
78 
79  try
80  {
81  std::cout << "Loading " << inName;
82 
84 
86  {
88 
89  typedef itk::DwiGradientLengthCorrectionFilter CorrectionFilterType;
90 
92  roundfilter->SetRoundingValue( 1000 );
93  roundfilter->SetReferenceBValue(mitk::DiffusionPropertyHelper::GetReferenceBValue( dwi ));
94  roundfilter->SetReferenceGradientDirectionContainer(mitk::DiffusionPropertyHelper::GetGradientContainer(dwi));
95  roundfilter->Update();
96 
97  dwi->SetProperty( mitk::DiffusionPropertyHelper::REFERENCEBVALUEPROPERTYNAME.c_str(), mitk::FloatProperty::New( roundfilter->GetNewBValue() ) );
98  dwi->SetProperty( mitk::DiffusionPropertyHelper::GRADIENTCONTAINERPROPERTYNAME.c_str(), mitk::GradientDirectionsProperty::New( roundfilter->GetOutputGradientDirectionContainer() ) );
99 
100  // filter input parameter
102  &originalShellMap = mitk::DiffusionPropertyHelper::GetBValueMap(dwi);
103 
105  mitk::CastToItkImage(dwi, vectorImage);
106 
109 
110  const unsigned int
112 
113  // filter call
114 
115 
116  vnl_vector<double> bValueList(originalShellMap.size()-1);
117  double targetBValue = bValueList.mean();
118 
119  mitk::DiffusionPropertyHelper::BValueMapType::const_iterator it = originalShellMap.begin();
120  ++it; int i = 0 ;
121  for(; it != originalShellMap.end(); ++it)
122  bValueList.put(i++,it->first);
123 
124  if( targetType == "mean" )
125  targetBValue = bValueList.mean();
126  else if( targetType == "min" )
127  targetBValue = bValueList.min_value();
128  else if( targetType == "max" )
129  targetBValue = bValueList.max_value();
130 
131  if(applyADC)
132  {
134  filter->SetInput(vectorImage);
135  filter->SetOriginalGradientDirections(gradientContainer);
136  filter->SetOriginalBValueMap(originalShellMap);
137  filter->SetOriginalBValue(bValue);
138 
140  functor->setListOfBValues(bValueList);
141  functor->setTargetBValue(targetBValue);
142 
143  filter->SetFunctor(functor);
144  filter->Update();
145  // create new DWI image
146  mitk::Image::Pointer outImage = mitk::GrabItkImageMemory( filter->GetOutput() );
147  outImage->SetProperty( mitk::DiffusionPropertyHelper::REFERENCEBVALUEPROPERTYNAME.c_str(), mitk::FloatProperty::New( targetBValue ) );
148  outImage->SetProperty( mitk::DiffusionPropertyHelper::GRADIENTCONTAINERPROPERTYNAME.c_str(), mitk::GradientDirectionsProperty::New( filter->GetTargetGradientDirections() ) );
149  mitk::DiffusionPropertyHelper propertyHelper( outImage );
150  propertyHelper.InitializeImage();
151 
152  mitk::IOUtil::Save(outImage, (outName + "_ADC.dwi").c_str());
153  }
154  if(applyAKC)
155  {
157  filter->SetInput(vectorImage);
158  filter->SetOriginalGradientDirections(gradientContainer);
159  filter->SetOriginalBValueMap(originalShellMap);
160  filter->SetOriginalBValue(bValue);
161 
163  functor->setListOfBValues(bValueList);
164  functor->setTargetBValue(targetBValue);
165 
166  filter->SetFunctor(functor);
167  filter->Update();
168  // create new DWI image
169  mitk::Image::Pointer outImage = mitk::GrabItkImageMemory( filter->GetOutput() );
170  outImage->SetProperty( mitk::DiffusionPropertyHelper::REFERENCEBVALUEPROPERTYNAME.c_str(), mitk::FloatProperty::New( targetBValue ) );
171  outImage->SetProperty( mitk::DiffusionPropertyHelper::GRADIENTCONTAINERPROPERTYNAME.c_str(), mitk::GradientDirectionsProperty::New( filter->GetTargetGradientDirections() ) );
172  mitk::DiffusionPropertyHelper propertyHelper( outImage );
173  propertyHelper.InitializeImage();
174 
175  mitk::IOUtil::Save(outImage, (string(outName) + "_AKC.dwi").c_str());
176  }
177  if(applyBiExp)
178  {
180  filter->SetInput(vectorImage);
181  filter->SetOriginalGradientDirections(gradientContainer);
182  filter->SetOriginalBValueMap(originalShellMap);
183  filter->SetOriginalBValue(bValue);
184 
186  functor->setListOfBValues(bValueList);
187  functor->setTargetBValue(targetBValue);
188 
189  filter->SetFunctor(functor);
190  filter->Update();
191  // create new DWI image
192  mitk::Image::Pointer outImage = mitk::GrabItkImageMemory( filter->GetOutput() );
193  outImage->SetProperty( mitk::DiffusionPropertyHelper::REFERENCEBVALUEPROPERTYNAME.c_str(), mitk::FloatProperty::New( targetBValue ) );
194  outImage->SetProperty( mitk::DiffusionPropertyHelper::GRADIENTCONTAINERPROPERTYNAME.c_str(), mitk::GradientDirectionsProperty::New( filter->GetTargetGradientDirections() ) );
195  mitk::DiffusionPropertyHelper propertyHelper( outImage );
196  propertyHelper.InitializeImage();
197 
198  mitk::IOUtil::Save(outImage, (string(outName) + "_BiExp.dwi").c_str());
199  }
200  }
201  }
202  catch (itk::ExceptionObject e)
203  {
204  std::cout << e;
205  return EXIT_FAILURE;
206  }
207  catch (std::exception e)
208  {
209  std::cout << e.what();
210  return EXIT_FAILURE;
211  }
212  catch (...)
213  {
214  std::cout << "ERROR!?!";
215  return EXIT_FAILURE;
216  }
217  return EXIT_SUCCESS;
218 }
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
static Pointer New()
itk::SmartPointer< Self > Pointer
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
mitk::BValueMapProperty::BValueMap BValueMapType
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.
const BValueMapType & GetBValueMap() const
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)
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.
static Pointer New()
GradientDirectionsContainerType::Pointer GetGradientContainer() const
static const std::string GRADIENTCONTAINERPROPERTYNAME
void setTitle(std::string title)
static Pointer New()
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
int main(int argc, char *argv[])
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.