Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
TensorDerivedMapsExtraction.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 
18 #include <mitkIOUtil.h>
19 
20 #include "mitkImage.h"
21 #include <mitkImageCast.h>
22 #include "mitkITKImageImport.h"
23 #include <iostream>
24 #include <fstream>
25 #include <mitkTensorImage.h>
26 #include <mitkImage.h>
28 
30 #include "itkDiffusionTensor3DReconstructionImageFilter.h"
31 #include "mitkCommandLineParser.h"
32 
33 #include <itkImageFileWriter.h>
34 #include <itkNrrdImageIO.h>
35 #include <itkDiffusionTensor3D.h>
36 
37 using namespace std;
38 
39 typedef short DiffusionPixelType;
40 typedef double TTensorPixelType;
41 
42 static void ExtractMapsAndSave(mitk::TensorImage::Pointer tensorImage, std::string filename, std::string postfix = "")
43 {
44 
45  mitk::Image* image = dynamic_cast<mitk::Image*> (tensorImage.GetPointer());
46 
47  typedef itk::DiffusionTensor3D< TTensorPixelType > TensorPixelType;
48  typedef itk::Image< TensorPixelType, 3 > TensorImageType;
49 
51  mitk::CastToItkImage(image, itkvol);
52 
54 
55  MeasurementsType::Pointer measurementsCalculator = MeasurementsType::New();
56  measurementsCalculator->SetInput(itkvol.GetPointer() );
57 
59  // FA
60  measurementsCalculator->SetMeasure(MeasurementsType::FA);
61  measurementsCalculator->Update();
62  map->InitializeByItk( measurementsCalculator->GetOutput() );
63  map->SetVolume( measurementsCalculator->GetOutput()->GetBufferPointer() );
64  mitk::IOUtil::Save(map, filename + "_FA" + postfix + ".nrrd");
65 
66  // MD
67  measurementsCalculator->SetMeasure(MeasurementsType::MD);
68  measurementsCalculator->Update();
69  map->InitializeByItk( measurementsCalculator->GetOutput() );
70  map->SetVolume( measurementsCalculator->GetOutput()->GetBufferPointer() );
71  mitk::IOUtil::Save(map, filename + "_MD" + postfix + ".nrrd");
72 
73  // AD
74  measurementsCalculator->SetMeasure(MeasurementsType::AD);
75  measurementsCalculator->Update();
76  map->InitializeByItk( measurementsCalculator->GetOutput() );
77  map->SetVolume( measurementsCalculator->GetOutput()->GetBufferPointer() );
78  mitk::IOUtil::Save(map, filename + "_AD" + postfix + ".nrrd");
79 
80 
81  // CA
82  measurementsCalculator->SetMeasure(MeasurementsType::CA);
83  measurementsCalculator->Update();
84  map->InitializeByItk( measurementsCalculator->GetOutput() );
85  map->SetVolume( measurementsCalculator->GetOutput()->GetBufferPointer() );
86  mitk::IOUtil::Save(map, filename + "_CA" + postfix + ".nrrd");
87 
88  // RA
89  measurementsCalculator->SetMeasure(MeasurementsType::RA);
90  measurementsCalculator->Update();
91  map->InitializeByItk( measurementsCalculator->GetOutput() );
92  map->SetVolume( measurementsCalculator->GetOutput()->GetBufferPointer() );
93  mitk::IOUtil::Save(map, filename + "_RA" + postfix + ".nrrd");
94 
95  // RD
96  measurementsCalculator->SetMeasure(MeasurementsType::RD);
97  measurementsCalculator->Update();
98  map->InitializeByItk( measurementsCalculator->GetOutput() );
99  map->SetVolume( measurementsCalculator->GetOutput()->GetBufferPointer() );
100  mitk::IOUtil::Save(map, filename + "_RD" + postfix + ".nrrd");
101 
102 }
103 
104 
105 int main(int argc, char* argv[])
106 {
107  mitkCommandLineParser parser;
108  parser.setArgumentPrefix("--", "-");
109  parser.addArgument("help", "h", mitkCommandLineParser::String, "Help", "Show this help text");
110  parser.addArgument("input", "i", mitkCommandLineParser::InputFile, "Input file", "input dwi file", us::Any(),false);
111  parser.addArgument("out", "o", mitkCommandLineParser::OutputDirectory, "Output folder", "output folder and base name, e.g. /tmp/outPatient1 ", us::Any(),false);
112 
113  parser.setTitle("Tensor Derived Maps Extraction");
114  parser.setCategory("Diffusion Related Measures");
115  parser.setDescription("");
116  parser.setContributor("MBI");
117 
118  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
119  if (parsedArgs.size()==0)
120  {
121  std::cout << parser.helpText();
122  return EXIT_SUCCESS;
123  }
124 
125 
126  std::string inputFile = us::any_cast<string>(parsedArgs["input"]);
127  std::string baseFileName = us::any_cast<string>(parsedArgs["out"]);
128 
129  std::string dtiFileName = "_dti.dti";
130 
131  mitk::Image::Pointer diffusionImage = mitk::IOUtil::LoadImage(inputFile);
132 
133  if (diffusionImage.IsNull() || !mitk::DiffusionPropertyHelper::IsDiffusionWeightedImage(diffusionImage)) // does NULL pointer check make sense after static cast ?
134  {
135  MITK_ERROR << "Invalid Input Image. Must be DWI. Aborting.";
136  return -1;
137  }
138 
139  typedef itk::DiffusionTensor3DReconstructionImageFilter< DiffusionPixelType, DiffusionPixelType, TTensorPixelType > TensorReconstructionImageFilterType;
141 
143  for( mitk::DiffusionPropertyHelper::GradientDirectionsContainerType::ConstIterator it = mitk::DiffusionPropertyHelper::GetGradientContainer(diffusionImage)->Begin(); it != mitk::DiffusionPropertyHelper::GetGradientContainer(diffusionImage)->End(); it++)
144  {
145  gradientContainerCopy->push_back(it.Value());
146  }
147 
149  mitk::CastToItkImage(diffusionImage, itkVectorImagePointer);
150 
151  tensorReconstructionFilter->SetGradientImage( gradientContainerCopy, itkVectorImagePointer );
152  tensorReconstructionFilter->SetBValue( mitk::DiffusionPropertyHelper::GetReferenceBValue( diffusionImage ) );
153  tensorReconstructionFilter->SetThreshold(50);
154  tensorReconstructionFilter->Update();
155 
156  typedef itk::Image<itk::DiffusionTensor3D<TTensorPixelType>, 3> TensorImageType;
157  TensorImageType::Pointer tensorImage = tensorReconstructionFilter->GetOutput();
158  tensorImage->SetDirection( itkVectorImagePointer->GetDirection() );
159 
161  tensorImageMitk->InitializeByItk(tensorImage.GetPointer());
162  tensorImageMitk->SetVolume( tensorImage->GetBufferPointer() );
163 
164 
165 
167  io->SetFileType( itk::ImageIOBase::Binary );
168  io->UseCompressionOn();
169 
170  itk::ImageFileWriter< itk::Image< itk::DiffusionTensor3D< double >, 3 > >::Pointer writer = itk::ImageFileWriter< itk::Image< itk::DiffusionTensor3D< double >, 3 > >::New();
171  writer->SetInput(tensorReconstructionFilter->GetOutput());
172  writer->SetFileName(baseFileName + dtiFileName);
173  writer->SetImageIO(io);
174  writer->UseCompressionOn();
175  writer->Update();
176 
177  ExtractMapsAndSave(tensorImageMitk,baseFileName);
178 
179  return EXIT_SUCCESS;
180 
181 }
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:824
int main(int argc, char *argv[])
itk::SmartPointer< Self > Pointer
#define MITK_ERROR
Definition: mitkLogMacros.h:24
void setContributor(std::string contributor)
STL namespace.
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
static Pointer New()
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)
static const std::string filename
Image class for storing images.
Definition: mitkImage.h:76
Definition: usAny.h:163
double TTensorPixelType
void setCategory(std::string category)
static Pointer New()
static void ExtractMapsAndSave(mitk::TensorImage::Pointer tensorImage, std::string filename, std::string postfix="")
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
short DiffusionPixelType
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
std::string helpText() const
GradientDirectionsContainerType::Pointer GetGradientContainer() const
void setTitle(std::string title)
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.