00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <cstdlib>
00019 #include <ctime>
00020
00021 #include <iostream>
00022 #include <fstream>
00023
00024 #include <itksys/SystemTools.hxx>
00025 #include "mitkPixelType.h"
00026 #include "itkImageRegionIterator.h"
00027 #include "itkImageFileReader.h"
00028
00029 #include "mitkNrrdDiffusionImageWriter.h"
00030
00031 template< class D, class T >
00032 mitk::TeemDiffusionTensor3DReconstructionImageFilter<D,T>
00033 ::TeemDiffusionTensor3DReconstructionImageFilter():
00034 m_EstimateErrorImage(false),m_Sigma(-19191919),
00035 m_EstimationMethod(TeemTensorEstimationMethodsLLS),
00036 m_NumIterations(1),m_ConfidenceThreshold(-19191919.0),
00037 m_ConfidenceFuzzyness(0.0),m_MinPlausibleValue(1.0)
00038 {
00039 }
00040
00041 template< class D, class T >
00042 mitk::TeemDiffusionTensor3DReconstructionImageFilter<D,T>
00043 ::~TeemDiffusionTensor3DReconstructionImageFilter()
00044 {
00045 }
00046
00047 void file_replace(std::string filename, std::string what, std::string with)
00048 {
00049 ofstream myfile2;
00050 char filename2[512];
00051 sprintf(filename2, "%s2",filename.c_str());
00052 myfile2.open (filename2);
00053
00054 std::string line;
00055 ifstream myfile (filename.c_str());
00056 if (myfile.is_open())
00057 {
00058 while (! myfile.eof() )
00059 {
00060 getline (myfile,line);
00061 itksys::SystemTools::ReplaceString(line,what.c_str(),with.c_str());
00062 myfile2 << line << std::endl;
00063 }
00064 myfile.close();
00065 }
00066 myfile2.close();
00067 itksys::SystemTools::RemoveFile(filename.c_str());
00068 rename(filename2,filename.c_str());
00069
00070 }
00071
00072
00073 template< class D, class T >
00074 void
00075 mitk::TeemDiffusionTensor3DReconstructionImageFilter<D,T>
00076 ::Update()
00077 {
00078
00079
00080 char filename[512];
00081 srand((unsigned)time(0));
00082 int random_integer = rand();
00083 sprintf( filename, "dwi_%d.nhdr",random_integer);
00084
00085 typedef mitk::NrrdDiffusionImageWriter<D> WriterType;
00086 typename WriterType::Pointer nrrdWriter = WriterType::New();
00087 nrrdWriter->SetInput( m_Input );
00088
00089
00090 nrrdWriter->SetFileName(filename);
00091 try
00092 {
00093 nrrdWriter->Update();
00094 }
00095 catch (itk::ExceptionObject e)
00096 {
00097 std::cout << e << std::endl;
00098 }
00099
00100 file_replace(filename,"vector","list");
00101
00102
00103 char command[4096];
00104 sprintf( command, "tend estim -i %s -B kvp -o tensors_%d.nhdr -knownB0 true",
00105 filename, random_integer);
00106
00107
00108 if(m_EstimateErrorImage)
00109 {
00110 sprintf( command, "%s -ee error_image_%d.nhdr", command, random_integer);
00111 }
00112
00113 if(m_Sigma != -19191919)
00114 {
00115 sprintf( command, "%s -sigma %f", command, m_Sigma);
00116 }
00117
00118 switch(m_EstimationMethod)
00119 {
00120 case TeemTensorEstimationMethodsLLS:
00121 sprintf( command, "%s -est lls", command);
00122 break;
00123 case TeemTensorEstimationMethodsMLE:
00124 sprintf( command, "%s -est mle", command);
00125 break;
00126 case TeemTensorEstimationMethodsNLS:
00127 sprintf( command, "%s -est nls", command);
00128 break;
00129 case TeemTensorEstimationMethodsWLS:
00130 sprintf( command, "%s -est wls", command);
00131 break;
00132 }
00133
00134 sprintf( command, "%s -wlsi %d", command, m_NumIterations);
00135
00136 if(m_ConfidenceThreshold != -19191919.0)
00137 {
00138 sprintf( command, "%s -t %f", command, m_ConfidenceThreshold);
00139 }
00140
00141 sprintf( command, "%s -soft %f", command, m_ConfidenceFuzzyness);
00142 sprintf( command, "%s -mv %f", command, m_MinPlausibleValue);
00143
00144
00145 std::cout << "Calling <" << command << ">" << std::endl;
00146 int success = system(command);
00147 if(!success)
00148 {
00149 MITK_ERROR << "system command could not be called!";
00150 }
00151
00152 remove(filename);
00153 sprintf( filename, "dwi_%d.raw", random_integer);
00154 remove(filename);
00155
00156
00157 sprintf( filename, "tensors_%d.nhdr", random_integer);
00158 file_replace(filename,"3D-masked-symmetric-matrix","vector");
00159
00160
00161
00162
00163
00164
00165 typedef itk::ImageFileReader<VectorImageType> FileReaderType;
00166 typename FileReaderType::Pointer reader = FileReaderType::New();
00167 reader->SetFileName(filename);
00168 reader->Update();
00169 typename VectorImageType::Pointer vecImage = reader->GetOutput();
00170
00171 remove(filename);
00172 sprintf( filename, "tensors_%d.raw", random_integer);
00173 remove(filename);
00174
00175 typename ItkTensorImageType::Pointer itkTensorImage = ItkTensorImageType::New();
00176 itkTensorImage->SetSpacing( vecImage->GetSpacing() );
00177 itkTensorImage->SetOrigin( vecImage->GetOrigin() );
00178 itkTensorImage->SetDirection( vecImage->GetDirection() );
00179 itkTensorImage->SetLargestPossibleRegion( vecImage->GetLargestPossibleRegion() );
00180 itkTensorImage->SetBufferedRegion( vecImage->GetLargestPossibleRegion() );
00181 itkTensorImage->SetRequestedRegion( vecImage->GetLargestPossibleRegion() );
00182 itkTensorImage->Allocate();
00183
00184 itk::ImageRegionIterator<VectorImageType> it(vecImage,
00185 vecImage->GetLargestPossibleRegion());
00186
00187 itk::ImageRegionIterator<ItkTensorImageType> it2(itkTensorImage,
00188 itkTensorImage->GetLargestPossibleRegion());
00189 it2 = it2.Begin();
00190
00191
00192 {
00193 for(it=it.Begin();!it.IsAtEnd(); ++it, ++it2)
00194 {
00195
00196 {
00197 VectorType vec = it.Get();
00198 TensorType tensor;
00199 for(int i=1;i<7;i++)
00200 tensor[i-1] = vec[i] * vec[0];
00201 it2.Set( tensor );
00202 }
00203 }
00204 }
00205
00206 m_OutputItk = mitk::TensorImage::New();
00207 m_OutputItk->InitializeByItk(itkTensorImage.GetPointer());
00208 m_OutputItk->SetVolume( itkTensorImage->GetBufferPointer() );
00209
00210
00211 if(m_EstimateErrorImage)
00212 {
00213
00214 }
00215
00216 }
00217