Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _itk_TensorImageToDiffusionImageFilter_txx_
00018 #define _itk_TensorImageToDiffusionImageFilter_txx_
00019 #endif
00020
00021 #include "itkTensorImageToDiffusionImageFilter.h"
00022 #include "itkTensorToL2NormImageFilter.h"
00023 #include "itkRescaleIntensityImageFilter.h"
00024 #include <itkImageRegionIterator.h>
00025 #include <itkImageRegionConstIterator.h>
00026
00027 namespace itk
00028 {
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 template <class TInputScalarType, class TOutputScalarType>
00061 void
00062 TensorImageToDiffusionImageFilter<TInputScalarType, TOutputScalarType>
00063 ::BeforeThreadedGenerateData()
00064 {
00065
00066 if( m_GradientList.size()==0 )
00067 {
00068 throw itk::ExceptionObject (__FILE__,__LINE__,"Error: gradient list is empty, cannot generate DWI.");
00069 }
00070
00071
00072 typedef itk::TensorToL2NormImageFilter<InputImageType, itk::Image<InputScalarType,3> >
00073 TensorToL2NormFilterType;
00074
00075 typename TensorToL2NormFilterType::Pointer myFilter1 = TensorToL2NormFilterType::New();
00076 myFilter1->SetInput (this->GetInput());
00077
00078 try
00079 {
00080 myFilter1->Update();
00081 }
00082 catch (itk::ExceptionObject &e)
00083 {
00084 std::cerr << e;
00085 return;
00086 }
00087
00088 typename itk::RescaleIntensityImageFilter< itk::Image<InputScalarType,3>, BaselineImageType>::Pointer rescaler=
00089 itk::RescaleIntensityImageFilter<itk::Image<InputScalarType,3>, BaselineImageType>::New();
00090
00091 rescaler->SetOutputMinimum ( 0 );
00092 rescaler->SetOutputMaximum ( 32767 );
00093 rescaler->SetInput ( myFilter1->GetOutput() );
00094 try
00095 {
00096 rescaler->Update();
00097 }
00098 catch (itk::ExceptionObject &e)
00099 {
00100 std::cerr << e;
00101 return;
00102 }
00103
00104 m_BaselineImage = rescaler->GetOutput();
00105
00106 typename OutputImageType::Pointer outImage = OutputImageType::New();
00107 outImage->SetSpacing( this->GetInput()->GetSpacing() );
00108 outImage->SetOrigin( this->GetInput()->GetOrigin() );
00109 outImage->SetDirection( this->GetInput()->GetDirection() );
00110 outImage->SetLargestPossibleRegion( this->GetInput()->GetLargestPossibleRegion());
00111 outImage->SetBufferedRegion( this->GetInput()->GetLargestPossibleRegion() );
00112 outImage->SetRequestedRegion( this->GetInput()->GetLargestPossibleRegion() );
00113 outImage->SetVectorLength(m_GradientList.size()+1);
00114 outImage->Allocate();
00115
00116 this->SetNumberOfRequiredOutputs (1);
00117 this->SetNthOutput (0, outImage);
00118
00119 }
00120
00121 template <class TInputScalarType, class TOutputScalarType>
00122 void
00123 TensorImageToDiffusionImageFilter<TInputScalarType, TOutputScalarType>
00124 ::ThreadedGenerateData ( const OutputImageRegionType &outputRegionForThread, int threadId )
00125 {
00126
00127 typedef ImageRegionIterator<OutputImageType> IteratorOutputType;
00128 typedef ImageRegionConstIterator<InputImageType> IteratorInputType;
00129 typedef ImageRegionConstIterator<BaselineImageType> IteratorBaselineType;
00130
00131 unsigned long numPixels = outputRegionForThread.GetNumberOfPixels();
00132 unsigned long step = numPixels/100;
00133 unsigned long progress = 0;
00134
00135 IteratorOutputType itOut (this->GetOutput(0), outputRegionForThread);
00136 IteratorInputType itIn (this->GetInput(), outputRegionForThread);
00137 IteratorBaselineType itB0 (m_BaselineImage, outputRegionForThread);
00138
00139 if( threadId==0 )
00140 {
00141 this->UpdateProgress (0.0);
00142 }
00143
00144
00145 while(!itIn.IsAtEnd())
00146 {
00147
00148 if( this->GetAbortGenerateData() )
00149 {
00150 throw itk::ProcessAborted(__FILE__,__LINE__);
00151 }
00152
00153 InputPixelType T = itIn.Get();
00154
00155 BaselinePixelType b0 = itB0.Get();
00156
00157 OutputPixelType out;
00158 out.SetSize(m_GradientList.size()+1);
00159
00160 for( unsigned int i=0; i<m_GradientList.size(); i++)
00161 {
00162
00163 if( b0 > 0)
00164 {
00165 GradientType g = m_GradientList[i];
00166
00167 InputPixelType S;
00168 S[0] = g[0]*g[0];
00169 S[1] = g[1]*g[0];
00170 S[2] = g[2]*g[0];
00171 S[3] = g[1]*g[1];
00172 S[4] = g[2]*g[1];
00173 S[5] = g[2]*g[2];
00174
00175 double res =
00176 T[0]*S[0] + T[1]*S[1] + T[2]*S[2] +
00177 T[1]*S[1] + T[3]*S[3] + T[4]*S[4] +
00178 T[2]*S[2] + T[4]*S[4] + T[5]*S[5];
00179
00180 out[i] = static_cast<OutputScalarType>( 1.0*b0*exp ( -1.0 * m_BValue * res ) );
00181 }
00182
00183 }
00184
00185 out[m_GradientList.size()] = b0;
00186
00187 itOut.Set(out);
00188
00189 if( threadId==0 && step>0)
00190 {
00191 if( (progress%step)==0 )
00192 {
00193 this->UpdateProgress ( double(progress)/double(numPixels) );
00194 }
00195 }
00196
00197 ++progress;
00198 ++itB0;
00199 ++itIn;
00200 ++itOut;
00201
00202 }
00203
00204 if( threadId==0 )
00205 {
00206 this->UpdateProgress (1.0);
00207 }
00208 }
00209
00210
00211
00212 }