Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "itkImageRegionIterator.h"
00019 #include "itkImageRegionConstIterator.h"
00020 #include "mitkImageCast.h"
00021
00022 template<typename TPixelType>
00023 mitk::DiffusionImage<TPixelType>::DiffusionImage()
00024 : m_VectorImage(0), m_Directions(0), m_B_Value(-1.0), m_VectorImageAdaptor(0)
00025 {
00026 }
00027
00028 template<typename TPixelType>
00029 mitk::DiffusionImage<TPixelType>::~DiffusionImage()
00030 {
00031
00032 }
00033
00034 template<typename TPixelType>
00035 void mitk::DiffusionImage<TPixelType>
00036 ::InitializeFromVectorImage()
00037 {
00038 if(!m_VectorImage || !m_Directions || m_B_Value==-1.0)
00039 {
00040 MITK_INFO << "DiffusionImage could not be initialized. Set all members first!" << std::endl;
00041 return;
00042 }
00043
00044
00045 int firstZeroIndex = -1;
00046 for(GradientDirectionContainerType::ConstIterator it = m_Directions->Begin();
00047 it != m_Directions->End(); ++it)
00048 {
00049 firstZeroIndex++;
00050 GradientDirectionType g = it.Value();
00051 if(g[0] == 0 && g[1] == 0 && g[2] == 0 )
00052 break;
00053 }
00054
00055 typedef itk::Image<TPixelType,3> ImgType;
00056 typename ImgType::Pointer img = ImgType::New();
00057 img->SetSpacing( m_VectorImage->GetSpacing() );
00058 img->SetOrigin( m_VectorImage->GetOrigin() );
00059 img->SetDirection( m_VectorImage->GetDirection() );
00060 img->SetLargestPossibleRegion( m_VectorImage->GetLargestPossibleRegion());
00061 img->SetBufferedRegion( m_VectorImage->GetLargestPossibleRegion() );
00062 img->Allocate();
00063
00064 int vecLength = m_VectorImage->GetVectorLength();
00065 InitializeByItk( img.GetPointer(), 1, vecLength );
00066
00067
00068
00069 itk::ImageRegionIterator<ImgType> itw (img, img->GetLargestPossibleRegion() );
00070 itw = itw.Begin();
00071
00072 itk::ImageRegionConstIterator<ImageType> itr (m_VectorImage, m_VectorImage->GetLargestPossibleRegion() );
00073 itr = itr.Begin();
00074
00075 while(!itr.IsAtEnd())
00076 {
00077 itw.Set(itr.Get().GetElement(firstZeroIndex));
00078 ++itr;
00079 ++itw;
00080 }
00081
00082
00083 SetImportVolume(img->GetBufferPointer());
00084
00085
00086
00087 m_DisplayIndex = firstZeroIndex;
00088 MITK_INFO << "Diffusion-Image successfully initialized.";
00089
00090 }
00091
00092 template<typename TPixelType>
00093 void mitk::DiffusionImage<TPixelType>
00094 ::SetDisplayIndexForRendering(int displayIndex)
00095 {
00096
00097 int index = displayIndex;
00098 int vecLength = m_VectorImage->GetVectorLength();
00099 index = index > vecLength-1 ? vecLength-1 : index;
00100 if( m_DisplayIndex != index )
00101 {
00102 typedef itk::Image<TPixelType,3> ImgType;
00103 typename ImgType::Pointer img = ImgType::New();
00104 CastToItkImage<ImgType>(this, img);
00105
00106 itk::ImageRegionIterator<ImgType> itw (img, img->GetLargestPossibleRegion() );
00107 itw = itw.Begin();
00108
00109 itk::ImageRegionConstIterator<ImageType> itr (m_VectorImage, m_VectorImage->GetLargestPossibleRegion() );
00110 itr = itr.Begin();
00111
00112 while(!itr.IsAtEnd())
00113 {
00114 itw.Set(itr.Get().GetElement(index));
00115 ++itr;
00116 ++itw;
00117 }
00118 }
00119
00120 m_DisplayIndex = index;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 template<typename TPixelType>
00185 bool mitk::DiffusionImage<TPixelType>::AreAlike(GradientDirectionType g1,
00186 GradientDirectionType g2,
00187 double precision)
00188 {
00189 GradientDirectionType diff = g1 - g2;
00190 return diff.two_norm() < precision;
00191 }
00192
00193 template<typename TPixelType>
00194 void mitk::DiffusionImage<TPixelType>::CorrectDKFZBrokenGradientScheme(double precision)
00195 {
00196 GradientDirectionContainerType::Pointer directionSet = CalcAveragedDirectionSet(precision);
00197 if(directionSet->size() < 7)
00198 {
00199 MITK_INFO << "Too few directions, assuming and correcting DKFZ-bogus sequence details.";
00200
00201 double v [7][3] =
00202 {{ 0, 0, 0 },
00203 {-0.707057, 0, 0.707057 },
00204 { 0.707057, 0, 0.707057 },
00205 { 0, 0.707057, 0.707057 },
00206 { 0, 0.707057, -0.707057 },
00207 {-0.707057, 0.707057, 0 },
00208 { 0.707057, 0.707057, 0 } };
00209
00210 int i=0;
00211 for(GradientDirectionContainerType::Iterator it = m_Directions->Begin();
00212 it != m_Directions->End(); ++it)
00213 {
00214 it.Value().set(v[i++%7]);
00215 }
00216
00217 }
00218 }
00219
00220 template<typename TPixelType>
00221 mitk::DiffusionImage<TPixelType>::GradientDirectionContainerType::Pointer
00222 mitk::DiffusionImage<TPixelType>::CalcAveragedDirectionSet(double precision)
00223 {
00224
00225 GradientDirectionContainerType::Pointer newDirections = GradientDirectionContainerType::New();
00226
00227
00228 for(GradientDirectionContainerType::ConstIterator gdcitOld = m_Directions->Begin();
00229 gdcitOld != m_Directions->End(); ++gdcitOld)
00230 {
00231
00232 bool found = false;
00233 for(GradientDirectionContainerType::ConstIterator gdcitNew = newDirections->Begin();
00234 gdcitNew != newDirections->End(); ++gdcitNew)
00235 {
00236 if(AreAlike(gdcitNew.Value(), gdcitOld.Value(), precision))
00237 {
00238 found = true;
00239 break;
00240 }
00241 }
00242
00243
00244 if(!found)
00245 {
00246 newDirections->push_back(gdcitOld.Value());
00247 }
00248 }
00249
00250 return newDirections;
00251 }
00252
00253 template<typename TPixelType>
00254 void mitk::DiffusionImage<TPixelType>::AverageRedundantGradients(double precision)
00255 {
00256
00257 GradientDirectionContainerType::Pointer newDirs =
00258 CalcAveragedDirectionSet(precision);
00259
00260
00261 if(m_Directions->size() == newDirs->size())
00262 return;
00263
00264 GradientDirectionContainerType::Pointer oldDirections = m_Directions;
00265 m_Directions = newDirs;
00266
00267
00268 typename ImageType::Pointer oldImage = m_VectorImage;
00269 m_VectorImage = ImageType::New();
00270 m_VectorImage->SetSpacing( oldImage->GetSpacing() );
00271 m_VectorImage->SetOrigin( oldImage->GetOrigin() );
00272 m_VectorImage->SetDirection( oldImage->GetDirection() );
00273 m_VectorImage->SetLargestPossibleRegion( oldImage->GetLargestPossibleRegion() );
00274 m_VectorImage->SetVectorLength( m_Directions->size() );
00275 m_VectorImage->SetBufferedRegion( oldImage->GetLargestPossibleRegion() );
00276 m_VectorImage->Allocate();
00277
00278
00279 itk::ImageRegionIterator< ImageType > newIt(m_VectorImage, m_VectorImage->GetLargestPossibleRegion());
00280 newIt.GoToBegin();
00281 itk::ImageRegionIterator< ImageType > oldIt(oldImage, oldImage->GetLargestPossibleRegion());
00282 oldIt.GoToBegin();
00283
00284
00285 typename ImageType::PixelType newVec;
00286 newVec.SetSize(m_Directions->size());
00287 newVec.AllocateElements(m_Directions->size());
00288
00289 std::vector<std::vector<int> > dirIndices;
00290 for(GradientDirectionContainerType::ConstIterator gdcitNew = m_Directions->Begin();
00291 gdcitNew != m_Directions->End(); ++gdcitNew)
00292 {
00293 dirIndices.push_back(std::vector<int>(0));
00294 for(GradientDirectionContainerType::ConstIterator gdcitOld = oldDirections->Begin();
00295 gdcitOld != oldDirections->End(); ++gdcitOld)
00296 {
00297 if(AreAlike(gdcitNew.Value(), gdcitOld.Value(), precision))
00298 {
00299 dirIndices[gdcitNew.Index()].push_back(gdcitOld.Index());
00300 }
00301 }
00302 }
00303
00304 int ind1 = -1;
00305 while(!newIt.IsAtEnd())
00306 {
00307
00308
00309 typename ImageType::IndexType ind = newIt.GetIndex();
00310 ind1 = ind.m_Index[2];
00311
00312
00313 newVec.Fill(0.0);
00314
00315
00316 typename ImageType::PixelType oldVec = oldIt.Get();
00317
00318 for(unsigned int i=0; i<dirIndices.size(); i++)
00319 {
00320
00321 int numavg = dirIndices[i].size();
00322 for(int j=0; j<numavg; j++)
00323 {
00324 newVec[i] += oldVec[dirIndices[i].at(j)];
00325 }
00326 newVec[i] /= numavg;
00327 }
00328
00329 newIt.Set(newVec);
00330
00331 ++newIt;
00332 ++oldIt;
00333 }
00334 }
00335
00336