|
| 1 | +#ifndef __COMBINEDWISWITHBMATRIX_H |
| 2 | +#define __COMBINEDWISWITHBMATRIX_H |
| 3 | + |
| 4 | + |
| 5 | +#include "defines.h" |
| 6 | +#include "../utilities/write_3D_image_to_4D_file.h" |
| 7 | +#include "../utilities/extract_3Dvolume_from_4D.h" |
| 8 | +#include "../utilities/read_bmatrix_file.h" |
| 9 | +#include "../tools/TORTOISEBmatrixToFSLBVecs/tortoise_bmatrix_to_fsl_bvecs.h" |
| 10 | + |
| 11 | +void CombineDWIsWithBMatrix(std::vector<std::string> nii_names, std::string output_name) |
| 12 | +{ |
| 13 | + |
| 14 | + int Nimgs= nii_names.size(); |
| 15 | + |
| 16 | + int tot_Nvols=0; |
| 17 | + for(int ni=0;ni<Nimgs;ni++) |
| 18 | + { |
| 19 | + std::string nii_name = nii_names[ni]; |
| 20 | + itk::NiftiImageIO::Pointer myio = itk::NiftiImageIO::New(); |
| 21 | + myio->SetFileName(nii_name); |
| 22 | + myio->ReadImageInformation(); |
| 23 | + int Nvols= myio->GetDimensions(3); |
| 24 | + tot_Nvols+=Nvols; |
| 25 | + } |
| 26 | + |
| 27 | + vnl_matrix<double> tot_Bmatrix(tot_Nvols,6); |
| 28 | + std::cout<<"Total volumes: "<< tot_Nvols<<std::endl; |
| 29 | + |
| 30 | + int vols_so_far=0; |
| 31 | + for(int ni=0;ni<Nimgs;ni++) |
| 32 | + { |
| 33 | + std::string nii_name = nii_names[ni]; |
| 34 | + ImageType4D::Pointer img = readImageD<ImageType4D>(nii_name) ; |
| 35 | + int Nvols = img->GetLargestPossibleRegion().GetSize()[3]; |
| 36 | + |
| 37 | + std::string bmtxt_name = nii_name.substr(0,nii_name.rfind(".nii"))+".bmtxt"; |
| 38 | + |
| 39 | + vnl_matrix<double> Bmatrix= read_bmatrix_file(bmtxt_name); |
| 40 | + tot_Bmatrix.update(Bmatrix,vols_so_far,0); |
| 41 | + |
| 42 | + for(int v=0;v<Nvols;v++) |
| 43 | + { |
| 44 | + ImageType3D::Pointer vol = extract_3D_volume_from_4D(img,v); |
| 45 | + write_3D_image_to_4D_file<float>(vol,output_name,vols_so_far+v,tot_Nvols); |
| 46 | + } |
| 47 | + vols_so_far+=Nvols; |
| 48 | + } |
| 49 | + |
| 50 | + std::string bmat_name= output_name.substr(0,output_name.rfind(".nii")) + ".bmtxt"; |
| 51 | + std::string bvals_fname= output_name.substr(0,output_name.rfind(".nii")) + ".bvals"; |
| 52 | + std::string bvecs_fname= output_name.substr(0,output_name.rfind(".nii")) + ".bvecs"; |
| 53 | + std::ofstream outfile(bmat_name); |
| 54 | + outfile<<tot_Bmatrix; |
| 55 | + outfile.close(); |
| 56 | + |
| 57 | + vnl_matrix<double> bvecs(3,tot_Nvols); |
| 58 | + vnl_matrix<double> bvals= tortoise_bmatrix_to_fsl_bvecs(tot_Bmatrix, bvecs); |
| 59 | + |
| 60 | + |
| 61 | + std::ofstream bvecs_file(bvecs_fname.c_str()); |
| 62 | + for(int i=0;i<3;i++) |
| 63 | + { |
| 64 | + for(int j=0;j<tot_Nvols;j++) |
| 65 | + { |
| 66 | + if(bvals(0,j)==0) |
| 67 | + bvecs_file<<"0 "; |
| 68 | + else |
| 69 | + bvecs_file<< bvecs(i,j)<< " "; |
| 70 | + } |
| 71 | + bvecs_file<<std::endl; |
| 72 | + } |
| 73 | + bvecs_file.close(); |
| 74 | + |
| 75 | + |
| 76 | + std::ofstream bvals_file(bvals_fname.c_str()); |
| 77 | + |
| 78 | + |
| 79 | + for(int j=0;j<tot_Nvols;j++) |
| 80 | + { |
| 81 | + bvals_file<< bvals(0,j)<< " "; |
| 82 | + } |
| 83 | + bvals_file.close(); |
| 84 | + |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +#endif |
0 commit comments