18 #ifndef __itkDWIWriter_hxx 19 #define __itkDWIWriter_hxx 27 template <
class TPixelType,
unsigned int VImageDimension>
31 m_UseRelativePath =
true;
32 m_OutputEachShell =
false;
34 m_MaskImage = B0ImageType::New();
35 m_B0Image = MaskImageType::New();
37 m_SamplingSchemeQSpace = SamplingSchemeQSpaceType::New();
40 template <
class TPixelType,
unsigned int VImageDimension>
46 this->ProcessObject::SetNthInput( 0, const_cast< DWIImageType * >( input ) );
49 template <
class TPixelType,
unsigned int VImageDimension>
54 if (this->GetNumberOfInputs() < 1)
60 (this->ProcessObject::GetInput(0));
63 template <
class TPixelType,
unsigned int VImageDimension>
70 out.open ( file.c_str() );
71 utlException (!out,
"\nERROR : failed to open output file " << file );
73 typename DWIImageType::Pointer dwi =
const_cast<DWIImageType*
>(this->GetInput());
74 std::string folderPath =
"", fileNoPath;
75 if (!m_UseRelativePath)
80 MatrixPointer orientationsCartesian = m_SamplingSchemeQSpace->GetOrientationsCartesian();
81 if (m_OutputEachShell)
83 std::string BFile_ext, BFile_noext;
85 std::string DWIFile_ext, DWIFile_noext, DWIFile_final;
87 std::string OrientationFile_ext, OrientationFile_noext, OrientationFile_final;
90 std::vector<STDVectorType> bVectors = m_SamplingSchemeQSpace->GroupBValues();
93 for (
int j = 0; j < bVectors.size(); j += 1 )
96 for (
int k = 0; k < bVectors[j].size(); k += 1 )
97 bMean += bVectors[j][k];
98 bMean /= bVectors[j].size();
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;
108 for (
int k = 0; k < bVectors[j].size(); k += 1 )
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);
114 gradTemp.Save(OrientationFile_final);
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());
128 maskIt = ImageRegionIteratorWithIndex<MaskImageType>(this->m_MaskImage, m_MaskImage->GetLargestPossibleRegion());
129 for (dwiTempIt.GoToBegin(), dwiIt.GoToBegin(), maskIt.GoToBegin();
131 ++dwiTempIt, ++dwiIt, ++maskIt)
135 dwiTempIt.Set(zeroPixel);
138 dwiPixel = dwiIt.Get();
139 for (
int k = 0; k < bVectors[j].size(); k += 1 )
141 dwiTempPixel[k] = dwiPixel[ (*bIndices)[j][k] ];
143 dwiTempIt.Set(dwiTempPixel);
152 std::cout <<
"Write b vector to " << folderPath + m_BFile << std::endl << std::flush;
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;
164 template <
class TPixelType,
unsigned int VImageDimension>
169 if (m_ConfigurationFile!=
"")
170 WriteToConfigurationFile(m_ConfigurationFile);
175 template <
class TPixelType,
unsigned int VImageDimension>
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);
void SaveVector(const VectorType &vv, const int NSize, const std::string &vectorStr, const bool is_save_number=false)
VectorImage< TPixelType, VImageDimension > DWIImageType
helper functions specifically used in dmritool
bool IsImageEmpty(const SmartPointer< ImageType > &image)
#define utlException(cond, expout)
bool SaveImage(const SmartPointer< ImageType > &image, const std::string &filename, const std::string &printInfo="Writing Image:")
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
#define utlGlobalException(cond, expout)
DWIImageType::PixelType PixelType
int RoundNumber(const T x)
void SetInput(const DWIImageType *input)
virtual void GenerateData() ITK_OVERRIDE
const DWIImageType * GetInput()
utl_shared_ptr< MatrixType > MatrixPointer
void GetPath(const std::string &fileNameAbsolute, std::string &path, std::string &file)
Superclass::Index2DVectorPointer Index2DVectorPointer
void GetFileExtension(const std::string &fileNameAbsolute, std::string &ext, std::string &fileNoExt)
#define PrintVar4(cond, var1, var2, var3, var4, os)
#define PrintVar2(cond, var1, var2, os)
void WriteToConfigurationFile(const std::string &file)
utl_shared_ptr< STDVectorType > STDVectorPointer