DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
DWIMultipleShellSplit.cxx
Go to the documentation of this file.
1 
19 #include "utl.h"
20 #include "DWIMultipleShellSplitCLP.h"
21 
25 int
26 main (int argc, char const* argv[])
27 {
28 
29  PARSE_ARGS;
30 
31  typedef float PixelType;
32  typedef itk::Image<PixelType, 4> InputImageType;
33  typedef InputImageType OutputImageType;
34  typedef itk::Image<PixelType, 3> B0ImageType;
35  typedef itk::Image<PixelType, 3> MaskImageType;
36 
37  InputImageType::Pointer inputImage;
38  B0ImageType::Pointer b0Image;
39  MaskImageType::Pointer mask;
40  OutputImageType::Pointer outputImage = OutputImageType::New();
41 
42  if (_MaskArg.isSet())
43  {
44  itk::ReadImage<MaskImageType>(_Mask, mask);
45  }
46 
47  std::vector<PixelType> bVector, bVectorSeparated;
48  utl::ReadVector(_BValueFile, bVector);
49 
50  std::vector<std::vector<int> > separateIndex;
51  separateIndex = utl::SeparateVector(bVector, bVectorSeparated, (PixelType)_Threshold);
52  int b0Index=-1;
53  for ( int i = 0; i < bVectorSeparated.size(); i += 1 )
54  {
55  if (bVectorSeparated[i]<_Threshold)
56  b0Index = i;
57  }
58 
59  std::string fileNoExt, ext, file;
60 
61  if (_OutputBValueFileArg.isSet())
62  {
63  utl::GetFileExtension(_OutputBValueFile, ext, fileNoExt);
64  for ( int m = 0; m < separateIndex.size(); m += 1 )
65  {
66  std::vector<PixelType> bVectorSingleShell = utl::SelectVector(bVector, separateIndex[m]);
67  file = fileNoExt + "_b" + utl::ConvertNumberToString(bVectorSeparated[m]) + "." + ext;
68  utl::SaveVector(bVectorSingleShell, file);
69  }
70  }
71 
72  utlException(!_GradientFileArg.isSet() && _OutputGradientFileArg.isSet(), "Need to set the gradients file");
73  utlException(_GradientFileArg.isSet() && !_OutputGradientFileArg.isSet(), "Need to set the gradients file");
74  if (_GradientFileArg.isSet())
75  {
76  std::vector<std::vector<std::string> > gradVector, gradVectorSingleShell;
77  utl::ReadLinesFirstlineCheck(_GradientFile, gradVector);
78  utlException(gradVector.size()!=bVector.size(), "wrong size for the gradient file");
79  utl::GetFileExtension(_OutputGradientFile, ext, fileNoExt);
80  for ( int m = 0; m < separateIndex.size(); m += 1 )
81  {
82  gradVectorSingleShell = utl::SelectVector(gradVector, separateIndex[m]);
83  file = fileNoExt + "_b" + utl::ConvertNumberToString(bVectorSeparated[m]) + "." + ext;
84  utl::Save2DVector(gradVectorSingleShell, file);
85  }
86  }
87 
88 
89  itk::ReadImage<InputImageType>(_InputFile, inputImage);
90 
91  InputImageType::PixelType inputImagePixelValue;
92  InputImageType::IndexType inputImagePixelIndex;
93  InputImageType::RegionType inRegion = inputImage->GetLargestPossibleRegion();
94  InputImageType::SizeType inputImageSize = inRegion.GetSize();
95  unsigned int numberOfVolumes = inputImageSize[3];
96  utlException(b0Index<0 && numberOfVolumes!=bVector.size()+1, "the first volume should be b0 image if there is no 0 in b values");
97  utlException(b0Index>=0 && numberOfVolumes!=bVector.size(), "wrong size!, bVector.size()="<<bVector.size()<<", numberOfVolumes="<<numberOfVolumes);
98  // utlException(numberOfVolumes!=bVector.size() && numberOfVolumes!=bVector.size()+1, "wrong size!, bVector.size()="<<bVector.size()<<", numberOfVolumes="<<numberOfVolumes);
99 
100  B0ImageType::IndexType b0ImagePixelIndex;
101  MaskImageType::IndexType maskPixelIndex;
102  utlException(_Setb0InEachShellArg.isSet() && _B0NormalizationArg.isSet(), "--b0InEachShell and --b0normalization can not be set simultaneously");
103  if (_Setb0InEachShellArg.isSet() || _B0NormalizationArg.isSet() || _B0OutputFileArg.isSet())
104  {
105  b0Image = B0ImageType::New();
106 
107  B0ImageType::PixelType b0ImagePixelValue;
108  itk::CopyImageInformation<InputImageType, B0ImageType>(inputImage, b0Image);
109  b0Image->Allocate();
110  b0Image->FillBuffer(0);
111 
112  for (unsigned int j = 0; j < inputImageSize[1]; j++)
113  for (unsigned int i = 0; i < inputImageSize[0]; i++)
114  for (unsigned int k = 0; k < inputImageSize[2]; k++)
115  {
116  inputImagePixelIndex[0] = i;
117  inputImagePixelIndex[1] = j;
118  inputImagePixelIndex[2] = k;
119 
120  b0ImagePixelIndex[0] = i;
121  b0ImagePixelIndex[1] = j;
122  b0ImagePixelIndex[2] = k;
123 
124  if (b0Index>=0)
125  {
126  b0ImagePixelValue = 0.0;
127  for ( int t = 0; t < separateIndex[b0Index].size(); t += 1 )
128  {
129  inputImagePixelIndex[3] = separateIndex[b0Index][t];
130  b0ImagePixelValue += inputImage->GetPixel(inputImagePixelIndex);
131  }
132  b0Image->SetPixel(b0ImagePixelIndex, b0ImagePixelValue/(PixelType)separateIndex[b0Index].size());
133  }
134  else
135  {
136  inputImagePixelIndex[3] = 0;
137  b0Image->SetPixel(b0ImagePixelIndex, inputImage->GetPixel(inputImagePixelIndex));
138  }
139  }
140  }
141 
142  if (_B0OutputFileArg.isSet())
143  itk::SaveImage<B0ImageType>(b0Image, _B0OutputFile, "Write b0 Image to");
144 
145  OutputImageType::RegionType outputImageRegion;
146  OutputImageType::SizeType outputImageSize;
147  OutputImageType::PixelType outputImagePixelValue;
148 // OutputImageType::PixelType OutputImageZeroPixelValue;
149  OutputImageType::IndexType outputImagePixelIndex;
150 // OutputImageType::SpacingType OutputImageSpacing;
151 
152  outputImage->CopyInformation(inputImage);
153  outputImageSize[0] = inputImageSize[0];
154  outputImageSize[1] = inputImageSize[1];
155  outputImageSize[2] = inputImageSize[2];
156  // outputImageSize[3] = NumberOfDWIVolumes;
157 
158  std::cout << "Number of Volumes: " << numberOfVolumes << std::endl;
159 
160  int offsetInput = numberOfVolumes==bVector.size() ? 0 : 1;
161  int offsetOutput = _Setb0InEachShell? 1 : 0;
162 
163 
164  for ( int m = 0; m < separateIndex.size(); m += 1 )
165  {
166  outputImageSize[3] = separateIndex[m].size() + offsetOutput;
167  outputImageRegion.SetSize(outputImageSize);
168  outputImage->SetRegions(outputImageRegion);
169  outputImage->Allocate();
170 
171  for (unsigned int i = 0; i < inputImageSize[0]; i++)
172  for (unsigned int j = 0; j < inputImageSize[1]; j++)
173  for (unsigned int k = 0; k < inputImageSize[2]; k++)
174  {
175  inputImagePixelIndex[0] = i;
176  inputImagePixelIndex[1] = j;
177  inputImagePixelIndex[2] = k;
178 
179  outputImagePixelIndex[0] = i;
180  outputImagePixelIndex[1] = j;
181  outputImagePixelIndex[2] = k;
182 
183  PixelType mm=1;
184  if (_MaskArg.isSet())
185  {
186  maskPixelIndex[0] = i;
187  maskPixelIndex[1] = j;
188  maskPixelIndex[2] = k;
189  mm = mask->GetPixel(maskPixelIndex);
190  }
191 
192  PixelType bb=0.0;
193  if ( mm>0 && (_B0NormalizationArg.isSet() || _Setb0InEachShellArg.isSet() ) )
194  {
195  b0ImagePixelIndex[0] = i;
196  b0ImagePixelIndex[1] = j;
197  b0ImagePixelIndex[2] = k;
198  bb = b0Image->GetPixel(b0ImagePixelIndex);
199  }
200 
201  for ( int n = 0; n < separateIndex[m].size(); n += 1 )
202  {
203  inputImagePixelIndex[3] = separateIndex[m][n] + offsetInput;
204  outputImagePixelIndex[3] = n + offsetOutput;
205 
206  if (_B0NormalizationArg.isSet())
207  {
208  if (bb>0 && mm>0)
209  outputImage->SetPixel(outputImagePixelIndex, inputImage->GetPixel(inputImagePixelIndex)/bb);
210  else
211  outputImage->SetPixel(outputImagePixelIndex, 0.0);
212  }
213  else if (mm>0)
214  outputImage->SetPixel(outputImagePixelIndex, inputImage->GetPixel(inputImagePixelIndex));
215  else
216  outputImage->SetPixel(outputImagePixelIndex, 0.0);
217  }
218 
219  if (_Setb0InEachShellArg.isSet())
220  {
221  outputImagePixelIndex[3] = 0;
222  outputImage->SetPixel(outputImagePixelIndex, bb);
223  }
224  }
225  utl::GetFileExtension(_OutputFile, ext, fileNoExt);
226  file = fileNoExt + "_b" + utl::ConvertNumberToString(bVectorSeparated[m]) + "." + ext;
227  itk::SaveImage<OutputImageType>(outputImage, file);
228  }
229 
230  return 0;
231 }
void SaveVector(const VectorType &vv, const int NSize, const std::string &vectorStr, const bool is_save_number=false)
Definition: utlCore.h:1213
helper functions specifically used in dmritool
std::string ConvertNumberToString(const T value, const int precision=6)
Definition: utlCore.h:740
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
std::vector< std::vector< int > > SeparateVector(const std::vector< T > &vec, std::vector< T > &vec_sep, const double gap=1e-9)
Definition: utlCore.h:1262
void ReadLinesFirstlineCheck(const std::string &filename, std::vector< std::vector< std::string > > &strVec, const char *cc=" ")
Definition: utlCore.h:890
void Save2DVector(const Vector2D &vv, std::ostream &out=std::cout)
Definition: utlCore.h:1850
void ReadVector(const std::string &vectorStr, std::vector< T > &vec, const char *cc=" ")
Definition: utlCore.h:1159
std::vector< T > SelectVector(const std::vector< T > &vec, const std::vector< int > &index)
Definition: utlCore.h:1312
int main(int argc, char const *argv[])
Split mutiple shell DWI data into several single shell data.
void GetFileExtension(const std::string &fileNameAbsolute, std::string &ext, std::string &fileNoExt)
Definition: utlCore.h:559