Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkTeemDiffusionTensor3DReconstructionImageFilter.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 <cstdlib>
18 #include <ctime>
19 
20 #include <iostream>
21 #include <fstream>
22 
23 #include <itksys/SystemTools.hxx>
24 #include "mitkPixelType.h"
25 #include "itkImageRegionIterator.h"
26 #include "itkImageFileReader.h"
27 #include <mitkIOUtil.h>
28 
29 template< class D, class T >
32 m_EstimateErrorImage(false),m_Sigma(-19191919),
33 m_EstimationMethod(TeemTensorEstimationMethodsLLS),
34 m_NumIterations(1),m_ConfidenceThreshold(-19191919.0),
35 m_ConfidenceFuzzyness(0.0),m_MinPlausibleValue(1.0)
36 {
37 }
38 
39 template< class D, class T >
42 {
43 }
44 
45 void file_replace(std::string filename, std::string what, std::string with)
46 {
47  ofstream myfile2;
48 
49  std::locale C("C");
50  std::locale originalLocale2 = myfile2.getloc();
51  myfile2.imbue(C);
52 
53  char filename2[512];
54  sprintf(filename2, "%s2",filename.c_str());
55  myfile2.open (filename2);
56 
57  std::string line;
58  ifstream myfile (filename.c_str());
59 
60  std::locale originalLocale = myfile.getloc();
61  myfile.imbue(C);
62 
63  if (myfile.is_open())
64  {
65  while (! myfile.eof() )
66  {
67  getline (myfile,line);
68  itksys::SystemTools::ReplaceString(line,what.c_str(),with.c_str());
69  myfile2 << line << std::endl;
70  }
71  myfile.close();
72  }
73  myfile2.close();
74  itksys::SystemTools::RemoveFile(filename.c_str());
75  rename(filename2,filename.c_str());
76 
77  myfile.imbue( originalLocale );
78  myfile2.imbue( originalLocale2 );
79 }
80 
81 // do the work
82 template< class D, class T >
83 void
86 {
87 
88  // save input image to nrrd file in temp-folder
89  char filename[512];
90  srand((unsigned)time(0));
91  int random_integer = rand();
92  sprintf( filename, "dwi_%d.nhdr",random_integer);
93 
94  try
95  {
96  mitk::IOUtil::Save(m_Input, filename);
97  }
98  catch (itk::ExceptionObject e)
99  {
100  std::cout << e << std::endl;
101  }
102 
103  file_replace(filename,"vector","list");
104 
105  // build up correct command from input params
106  char command[4096];
107  sprintf( command, "tend estim -i %s -B kvp -o tensors_%d.nhdr -knownB0 true",
108  filename, random_integer);
109 
110  //m_DiffusionImages;
111  if(m_EstimateErrorImage)
112  {
113  sprintf( command, "%s -ee error_image_%d.nhdr", command, random_integer);
114  }
115 
116  if(m_Sigma != -19191919)
117  {
118  sprintf( command, "%s -sigma %f", command, m_Sigma);
119  }
120 
121  switch(m_EstimationMethod)
122  {
124  sprintf( command, "%s -est lls", command);
125  break;
127  sprintf( command, "%s -est mle", command);
128  break;
130  sprintf( command, "%s -est nls", command);
131  break;
133  sprintf( command, "%s -est wls", command);
134  break;
135  }
136 
137  sprintf( command, "%s -wlsi %d", command, m_NumIterations);
138 
139  if(m_ConfidenceThreshold != -19191919.0)
140  {
141  sprintf( command, "%s -t %f", command, m_ConfidenceThreshold);
142  }
143 
144  sprintf( command, "%s -soft %f", command, m_ConfidenceFuzzyness);
145  sprintf( command, "%s -mv %f", command, m_MinPlausibleValue);
146 
147  // call tend estim command
148  std::cout << "Calling <" << command << ">" << std::endl;
149  int success = system(command);
150  if(!success)
151  {
152  MITK_ERROR << "system command could not be called!";
153  }
154 
155  remove(filename);
156  sprintf( filename, "dwi_%d.raw", random_integer);
157  remove(filename);
158 
159  // change kind from tensor to vector
160  sprintf( filename, "tensors_%d.nhdr", random_integer);
161  file_replace(filename,"3D-masked-symmetric-matrix","vector");
162 
163  // read result as mitk::Image and provide it in m_Output
164  typedef itk::ImageFileReader<VectorImageType> FileReaderType;
165  typename FileReaderType::Pointer reader = FileReaderType::New();
166  reader->SetFileName(filename);
167  reader->Update();
168  typename VectorImageType::Pointer vecImage = reader->GetOutput();
169 
170  remove(filename);
171  sprintf( filename, "tensors_%d.raw", random_integer);
172  remove(filename);
173 
174  typename ItkTensorImageType::Pointer itkTensorImage = ItkTensorImageType::New();
175  itkTensorImage->SetSpacing( vecImage->GetSpacing() ); // Set the image spacing
176  itkTensorImage->SetOrigin( vecImage->GetOrigin() ); // Set the image origin
177  itkTensorImage->SetDirection( vecImage->GetDirection() ); // Set the image direction
178  itkTensorImage->SetLargestPossibleRegion( vecImage->GetLargestPossibleRegion() );
179  itkTensorImage->SetBufferedRegion( vecImage->GetLargestPossibleRegion() );
180  itkTensorImage->SetRequestedRegion( vecImage->GetLargestPossibleRegion() );
181  itkTensorImage->Allocate();
182 
183  itk::ImageRegionIterator<VectorImageType> it(vecImage,
184  vecImage->GetLargestPossibleRegion());
185 
186  itk::ImageRegionIterator<ItkTensorImageType> it2(itkTensorImage,
187  itkTensorImage->GetLargestPossibleRegion());
188  it2 = it2.Begin();
189 
190  //#pragma omp parallel private (it)
191  {
192  for(it=it.Begin();!it.IsAtEnd(); ++it, ++it2)
193  {
194  //#pragma omp single nowait
195  {
196  VectorType vec = it.Get();
197  TensorType tensor;
198  for(int i=1;i<7;i++)
199  tensor[i-1] = vec[i] * vec[0];
200  it2.Set( tensor );
201  }
202  } // end for
203  } // end ompparallel
204 
205  m_OutputItk = mitk::TensorImage::New();
206  m_OutputItk->InitializeByItk(itkTensorImage.GetPointer());
207  m_OutputItk->SetVolume( itkTensorImage->GetBufferPointer() );
208 
209  // in case: read resulting error-image and provide it in m_ErrorImage
210  if(m_EstimateErrorImage)
211  {
212  // open error image here
213  }
214 
215 }
216 
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:824
static char * line
Definition: svm.cpp:2884
itk::SmartPointer< Self > Pointer
#define MITK_ERROR
Definition: mitkLogMacros.h:24
static Pointer New()
void file_replace(std::string filename, std::string what, std::string with)
static const std::string filename
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.