DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkDWIWriter.hxx
Go to the documentation of this file.
1 
18 #ifndef __itkDWIWriter_hxx
19 #define __itkDWIWriter_hxx
20 
21 #include "utl.h"
22 
23 
24 namespace itk
25 {
26 
27 template <class TPixelType, unsigned int VImageDimension>
30 {
31  m_UseRelativePath = true;
32  m_OutputEachShell = false;
33 
34  m_MaskImage = B0ImageType::New();
35  m_B0Image = MaskImageType::New();
36 
37  m_SamplingSchemeQSpace = SamplingSchemeQSpaceType::New();
38 }
39 
40 template <class TPixelType, unsigned int VImageDimension>
41 void
43 ::SetInput(const DWIImageType *input)
44 {
45  // ProcessObject is not const_correct so this cast is required here.
46  this->ProcessObject::SetNthInput( 0, const_cast< DWIImageType * >( input ) );
47 }
48 
49 template <class TPixelType, unsigned int VImageDimension>
53 {
54  if (this->GetNumberOfInputs() < 1)
55  {
56  return 0;
57  }
58 
59  return static_cast<DWIImageType*>
60  (this->ProcessObject::GetInput(0));
61 }
62 
63 template <class TPixelType, unsigned int VImageDimension>
64 void
66 ::WriteToConfigurationFile(const std::string& file)
67 {
68  std::ofstream out; // create ofstream object
69 
70  out.open ( file.c_str() ); // open ofstream
71  utlException (!out, "\nERROR : failed to open output file " << file );
72 
73  typename DWIImageType::Pointer dwi = const_cast<DWIImageType*>(this->GetInput());
74  std::string folderPath = "", fileNoPath;
75  if (!m_UseRelativePath)
76  {
77  utl::GetPath(file, folderPath, fileNoPath);
78  }
79 
80  MatrixPointer orientationsCartesian = m_SamplingSchemeQSpace->GetOrientationsCartesian();
81  if (m_OutputEachShell)
82  {
83  std::string BFile_ext, BFile_noext;
84  utl::GetFileExtension(m_BFile, BFile_ext, BFile_noext);
85  std::string DWIFile_ext, DWIFile_noext, DWIFile_final;
86  utl::GetFileExtension(m_DWIFile, DWIFile_ext, DWIFile_noext);
87  std::string OrientationFile_ext, OrientationFile_noext, OrientationFile_final;
88  utl::GetFileExtension(m_OrientationFile, OrientationFile_ext, OrientationFile_noext);
89 
90  std::vector<STDVectorType> bVectors = m_SamplingSchemeQSpace->GroupBValues();
91  typename SamplingSchemeQSpaceType::Index2DVectorPointer bIndices = m_SamplingSchemeQSpace->GetIndicesInShells();
92 
93  for ( int j = 0; j < bVectors.size(); j += 1 )
94  {
95  double bMean=0;
96  for ( int k = 0; k < bVectors[j].size(); k += 1 )
97  bMean += bVectors[j][k];
98  bMean /= bVectors[j].size();
99 
100  char outTemp[1024];
101  sprintf(outTemp, "_b%d.", utl::RoundNumber(bMean));
102  DWIFile_final = folderPath+ DWIFile_noext + outTemp + DWIFile_ext;
103  OrientationFile_final = folderPath + OrientationFile_noext + outTemp + OrientationFile_ext;
104  out << bMean << " " << OrientationFile_final << " " << DWIFile_final << std::endl;
105  std::cout << bMean << " " << OrientationFile_final << " " << DWIFile_final << ", " << bVectors[j].size() << " dwis" << std::endl;
106 
107  MatrixType gradTemp(bVectors[j].size(),3);
108  for ( int k = 0; k < bVectors[j].size(); k += 1 )
109  {
110  gradTemp(k,0) = (*orientationsCartesian)((*bIndices)[j][k],0);
111  gradTemp(k,1) = (*orientationsCartesian)((*bIndices)[j][k],1);
112  gradTemp(k,2) = (*orientationsCartesian)((*bIndices)[j][k],2);
113  }
114  gradTemp.Save(OrientationFile_final);
115 
116  typename DWIImageType::Pointer dwiTempImage = DWIImageType::New();
117  itk::CopyImageInformation<DWIImageType, DWIImageType>( dwi, dwiTempImage);
118  dwiTempImage->SetNumberOfComponentsPerPixel(bVectors[j].size());
119  dwiTempImage->Allocate();
120  ImageRegionIteratorWithIndex<DWIImageType> dwiIt(dwi, dwi->GetLargestPossibleRegion());
121  ImageRegionIteratorWithIndex<DWIImageType> dwiTempIt(dwiTempImage, dwiTempImage->GetLargestPossibleRegion());
122  ImageRegionIteratorWithIndex<MaskImageType> maskIt;
123  PixelType dwiPixel, dwiTempPixel, zeroPixel;
124  dwiTempPixel.SetSize(bVectors[j].size());
125  zeroPixel.SetSize(bVectors[j].size());
126  zeroPixel.Fill(0.0);
127  if (!IsImageEmpty(m_MaskImage))
128  maskIt = ImageRegionIteratorWithIndex<MaskImageType>(this->m_MaskImage, m_MaskImage->GetLargestPossibleRegion());
129  for (dwiTempIt.GoToBegin(), dwiIt.GoToBegin(), maskIt.GoToBegin();
130  !dwiIt.IsAtEnd();
131  ++dwiTempIt, ++dwiIt, ++maskIt)
132  {
133  if (!IsImageEmpty(m_MaskImage) && maskIt.Get()<=1e-10)
134  {
135  dwiTempIt.Set(zeroPixel);
136  continue;
137  }
138  dwiPixel = dwiIt.Get();
139  for ( int k = 0; k < bVectors[j].size(); k += 1 )
140  {
141  dwiTempPixel[k] = dwiPixel[ (*bIndices)[j][k] ];
142  }
143  dwiTempIt.Set(dwiTempPixel);
144  }
145  itk::SaveImage(dwiTempImage, DWIFile_final);
146  }
147 
148  }
149  else
150  {
151  STDVectorPointer bVector = m_SamplingSchemeQSpace->GetBVector();
152  std::cout << "Write b vector to " << folderPath + m_BFile << std::endl << std::flush;
153  utl::SaveVector(*bVector, folderPath + m_BFile);
154  std::cout << "Write orientation file to " << folderPath + m_OrientationFile << std::endl << std::flush;
155  orientationsCartesian->Save(folderPath + m_OrientationFile);
156  itk::SaveImage(dwi, folderPath + m_DWIFile, "Write DWI image to");
157  out << m_BFile << " " << m_OrientationFile << " " << m_DWIFile << std::endl;
158  }
159 
160  out.close();
161  return;
162 }
163 
164 template <class TPixelType, unsigned int VImageDimension>
165 void
168 {
169  if (m_ConfigurationFile!="")
170  WriteToConfigurationFile(m_ConfigurationFile);
171  else
172  utlGlobalException(true, "unknown format! ");
173 }
174 
175 template <class TPixelType, unsigned int VImageDimension>
176 void
178 ::PrintSelf(std::ostream& os, Indent indent) const
179 {
180  Superclass::PrintSelf(os, indent);
181  os << indent << "m_SamplingSchemeQSpace = " << m_SamplingSchemeQSpace << std::endl << std::flush;
182  PrintVar4(true, m_ConfigurationFile, m_DWIFile, m_BFile, m_OrientationFile, os<<indent);
183  PrintVar2(true, m_UseRelativePath, m_OutputEachShell, os<<indent);
184 }
185 
186 }
187 
188 
189 #endif
void SaveVector(const VectorType &vv, const int NSize, const std::string &vectorStr, const bool is_save_number=false)
Definition: utlCore.h:1213
VectorImage< TPixelType, VImageDimension > DWIImageType
Definition: itkDWIWriter.h:76
helper functions specifically used in dmritool
bool IsImageEmpty(const SmartPointer< ImageType > &image)
Definition: utlITK.h:435
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
bool SaveImage(const SmartPointer< ImageType > &image, const std::string &filename, const std::string &printInfo="Writing Image:")
Definition: utlITK.h:157
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
DWIImageType::PixelType PixelType
Definition: itkDWIWriter.h:87
int RoundNumber(const T x)
Definition: utlCore.h:785
void SetInput(const DWIImageType *input)
virtual void GenerateData() ITK_OVERRIDE
const DWIImageType * GetInput()
utl_shared_ptr< MatrixType > MatrixPointer
Definition: itkDWIWriter.h:80
void GetPath(const std::string &fileNameAbsolute, std::string &path, std::string &file)
Definition: utlCore.h:550
Superclass::Index2DVectorPointer Index2DVectorPointer
void GetFileExtension(const std::string &fileNameAbsolute, std::string &ext, std::string &fileNoExt)
Definition: utlCore.h:559
#define PrintVar4(cond, var1, var2, var3, var4, os)
Definition: utlCoreMacro.h:470
#define PrintVar2(cond, var1, var2, os)
Definition: utlCoreMacro.h:454
void WriteToConfigurationFile(const std::string &file)
utl_shared_ptr< STDVectorType > STDVectorPointer
Definition: itkDWIWriter.h:82