20 #include "DWIMultipleShellSplitCLP.h" 26 main (
int argc,
char const* argv[])
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;
37 InputImageType::Pointer inputImage;
38 B0ImageType::Pointer b0Image;
39 MaskImageType::Pointer mask;
40 OutputImageType::Pointer outputImage = OutputImageType::New();
44 itk::ReadImage<MaskImageType>(_Mask, mask);
47 std::vector<PixelType> bVector, bVectorSeparated;
50 std::vector<std::vector<int> > separateIndex;
53 for (
int i = 0; i < bVectorSeparated.size(); i += 1 )
55 if (bVectorSeparated[i]<_Threshold)
59 std::string fileNoExt, ext, file;
61 if (_OutputBValueFileArg.isSet())
64 for (
int m = 0; m < separateIndex.size(); m += 1 )
66 std::vector<PixelType> bVectorSingleShell =
utl::SelectVector(bVector, separateIndex[m]);
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())
76 std::vector<std::vector<std::string> > gradVector, gradVectorSingleShell;
78 utlException(gradVector.size()!=bVector.size(),
"wrong size for the gradient file");
80 for (
int m = 0; m < separateIndex.size(); m += 1 )
89 itk::ReadImage<InputImageType>(_InputFile, inputImage);
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);
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())
105 b0Image = B0ImageType::New();
107 B0ImageType::PixelType b0ImagePixelValue;
108 itk::CopyImageInformation<InputImageType, B0ImageType>(inputImage, b0Image);
110 b0Image->FillBuffer(0);
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++)
116 inputImagePixelIndex[0] = i;
117 inputImagePixelIndex[1] = j;
118 inputImagePixelIndex[2] = k;
120 b0ImagePixelIndex[0] = i;
121 b0ImagePixelIndex[1] = j;
122 b0ImagePixelIndex[2] = k;
126 b0ImagePixelValue = 0.0;
127 for (
int t = 0; t < separateIndex[b0Index].size(); t += 1 )
129 inputImagePixelIndex[3] = separateIndex[b0Index][t];
130 b0ImagePixelValue += inputImage->GetPixel(inputImagePixelIndex);
132 b0Image->SetPixel(b0ImagePixelIndex, b0ImagePixelValue/(PixelType)separateIndex[b0Index].size());
136 inputImagePixelIndex[3] = 0;
137 b0Image->SetPixel(b0ImagePixelIndex, inputImage->GetPixel(inputImagePixelIndex));
142 if (_B0OutputFileArg.isSet())
143 itk::SaveImage<B0ImageType>(b0Image, _B0OutputFile,
"Write b0 Image to");
145 OutputImageType::RegionType outputImageRegion;
146 OutputImageType::SizeType outputImageSize;
147 OutputImageType::PixelType outputImagePixelValue;
149 OutputImageType::IndexType outputImagePixelIndex;
152 outputImage->CopyInformation(inputImage);
153 outputImageSize[0] = inputImageSize[0];
154 outputImageSize[1] = inputImageSize[1];
155 outputImageSize[2] = inputImageSize[2];
158 std::cout <<
"Number of Volumes: " << numberOfVolumes << std::endl;
160 int offsetInput = numberOfVolumes==bVector.size() ? 0 : 1;
161 int offsetOutput = _Setb0InEachShell? 1 : 0;
164 for (
int m = 0; m < separateIndex.size(); m += 1 )
166 outputImageSize[3] = separateIndex[m].size() + offsetOutput;
167 outputImageRegion.SetSize(outputImageSize);
168 outputImage->SetRegions(outputImageRegion);
169 outputImage->Allocate();
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++)
175 inputImagePixelIndex[0] = i;
176 inputImagePixelIndex[1] = j;
177 inputImagePixelIndex[2] = k;
179 outputImagePixelIndex[0] = i;
180 outputImagePixelIndex[1] = j;
181 outputImagePixelIndex[2] = k;
184 if (_MaskArg.isSet())
186 maskPixelIndex[0] = i;
187 maskPixelIndex[1] = j;
188 maskPixelIndex[2] = k;
189 mm = mask->GetPixel(maskPixelIndex);
193 if ( mm>0 && (_B0NormalizationArg.isSet() || _Setb0InEachShellArg.isSet() ) )
195 b0ImagePixelIndex[0] = i;
196 b0ImagePixelIndex[1] = j;
197 b0ImagePixelIndex[2] = k;
198 bb = b0Image->GetPixel(b0ImagePixelIndex);
201 for (
int n = 0; n < separateIndex[m].size(); n += 1 )
203 inputImagePixelIndex[3] = separateIndex[m][n] + offsetInput;
204 outputImagePixelIndex[3] = n + offsetOutput;
206 if (_B0NormalizationArg.isSet())
209 outputImage->SetPixel(outputImagePixelIndex, inputImage->GetPixel(inputImagePixelIndex)/bb);
211 outputImage->SetPixel(outputImagePixelIndex, 0.0);
214 outputImage->SetPixel(outputImagePixelIndex, inputImage->GetPixel(inputImagePixelIndex));
216 outputImage->SetPixel(outputImagePixelIndex, 0.0);
219 if (_Setb0InEachShellArg.isSet())
221 outputImagePixelIndex[3] = 0;
222 outputImage->SetPixel(outputImagePixelIndex, bb);
227 itk::SaveImage<OutputImageType>(outputImage, file);
void SaveVector(const VectorType &vv, const int NSize, const std::string &vectorStr, const bool is_save_number=false)
helper functions specifically used in dmritool
std::string ConvertNumberToString(const T value, const int precision=6)
#define utlException(cond, expout)
std::vector< std::vector< int > > SeparateVector(const std::vector< T > &vec, std::vector< T > &vec_sep, const double gap=1e-9)
void ReadLinesFirstlineCheck(const std::string &filename, std::vector< std::vector< std::string > > &strVec, const char *cc=" ")
void Save2DVector(const Vector2D &vv, std::ostream &out=std::cout)
void ReadVector(const std::string &vectorStr, std::vector< T > &vec, const char *cc=" ")
std::vector< T > SelectVector(const std::vector< T > &vec, const std::vector< int > &index)
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)