Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __mitkNrrdDiffusionImageReader_cpp
00019 #define __mitkNrrdDiffusionImageReader_cpp
00020
00021 #include "mitkNrrdDiffusionImageReader.h"
00022
00023 #include "itkImageFileReader.h"
00024 #include "itkMetaDataObject.h"
00025 #include "itkNrrdImageIO.h"
00026
00027 namespace mitk
00028 {
00029
00030 template <class TPixelType>
00031 void NrrdDiffusionImageReader<TPixelType>
00032 ::GenerateData()
00033 {
00034
00035
00036
00037
00038
00039
00040
00041 if ( ( ! m_OutputCache ) || ( this->GetMTime( ) > m_CacheTime.GetMTime( ) ) )
00042 {
00043 this->GenerateOutputInformation();
00044 itkWarningMacro("Cache regenerated!");
00045 }
00046
00047 if (!m_OutputCache)
00048 {
00049 itkWarningMacro("Tree cache is empty!");
00050 }
00051
00052 static_cast<OutputType*>(this->GetOutput())
00053 ->SetVectorImage(m_OutputCache->GetVectorImage());
00054 static_cast<OutputType*>(this->GetOutput())
00055 ->SetB_Value(m_OutputCache->GetB_Value());
00056 static_cast<OutputType*>(this->GetOutput())
00057 ->SetDirections(m_OutputCache->GetDirections());
00058 static_cast<OutputType*>(this->GetOutput())
00059 ->InitializeFromVectorImage();
00060 }
00061
00062 template <class TPixelType>
00063 void NrrdDiffusionImageReader<TPixelType>::GenerateOutputInformation()
00064 {
00065 typename OutputType::Pointer outputForCache = OutputType::New();
00066 if ( m_FileName == "")
00067 {
00068 throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, the filename to be read is empty!");
00069 }
00070 else
00071 {
00072 try
00073 {
00074 itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New();
00075 typedef itk::ImageFileReader<ImageType> FileReaderType;
00076 typename FileReaderType::Pointer reader = FileReaderType::New();
00077 reader->SetImageIO(io);
00078 reader->SetFileName(this->m_FileName);
00079 reader->Update();
00080
00081 typename ImageType::Pointer img = reader->GetOutput();
00082
00083 itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary();
00084 std::vector<std::string> imgMetaKeys = imgMetaDictionary.GetKeys();
00085 std::vector<std::string>::const_iterator itKey = imgMetaKeys.begin();
00086 std::string metaString;
00087
00088 GradientDirectionType vect3d;
00089 m_DiffusionVectors = GradientDirectionContainerType::New();
00090
00091 int numberOfImages = 0;
00092 int numberOfGradientImages = 0;
00093 bool readb0 = false;
00094
00095 for (; itKey != imgMetaKeys.end(); itKey ++)
00096 {
00097 double x,y,z;
00098
00099 itk::ExposeMetaData<std::string> (imgMetaDictionary, *itKey, metaString);
00100 if (itKey->find("DWMRI_gradient") != std::string::npos)
00101 {
00102 std::cout << *itKey << " ---> " << metaString << std::endl;
00103 sscanf(metaString.c_str(), "%lf %lf %lf\n", &x, &y, &z);
00104 vect3d[0] = x; vect3d[1] = y; vect3d[2] = z;
00105 m_DiffusionVectors->InsertElement( numberOfImages, vect3d );
00106 ++numberOfImages;
00107
00108 if (vect3d[0] == 0.0 &&
00109 vect3d[1] == 0.0 &&
00110 vect3d[2] == 0.0)
00111 {
00112 continue;
00113 }
00114 ++numberOfGradientImages;;
00115 }
00116 else if (itKey->find("DWMRI_b-value") != std::string::npos)
00117 {
00118 std::cout << *itKey << " ---> " << metaString << std::endl;
00119 readb0 = true;
00120 m_B_Value = atof(metaString.c_str());
00121 }
00122 }
00123
00124 std::cout << "Number of gradient images: "
00125 << numberOfGradientImages
00126 << " and Number of reference images: "
00127 << numberOfImages - numberOfGradientImages
00128 << std::endl;
00129
00130 if(!readb0)
00131 {
00132 std::cerr << "BValue not specified in header file" << std::endl;
00133 }
00134
00135
00136 outputForCache->SetVectorImage(img);
00137 outputForCache->SetB_Value(m_B_Value);
00138 outputForCache->SetDirections(m_DiffusionVectors);
00139
00140
00141
00142 m_OutputCache = outputForCache;
00143 m_CacheTime.Modified();
00144 }
00145 catch(std::exception& e)
00146 {
00147 throw itk::ImageFileReaderException(__FILE__, __LINE__, e.what());
00148 }
00149 catch(...)
00150 {
00151 throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, an error occurred while reading the requested vessel tree file!");
00152 }
00153 }
00154 }
00155
00156
00157 template <class TPixelType>
00158 const char* NrrdDiffusionImageReader<TPixelType>
00159 ::GetFileName() const
00160 {
00161 return m_FileName.c_str();
00162 }
00163
00164 template <class TPixelType>
00165 void NrrdDiffusionImageReader<TPixelType>
00166 ::SetFileName(const char* aFileName)
00167 {
00168 m_FileName = aFileName;
00169 }
00170
00171 template <class TPixelType>
00172 const char* NrrdDiffusionImageReader<TPixelType>
00173 ::GetFilePrefix() const
00174 {
00175 return m_FilePrefix.c_str();
00176 }
00177
00178 template <class TPixelType>
00179 void NrrdDiffusionImageReader<TPixelType>
00180 ::SetFilePrefix(const char* aFilePrefix)
00181 {
00182 m_FilePrefix = aFilePrefix;
00183 }
00184
00185 template <class TPixelType>
00186 const char* NrrdDiffusionImageReader<TPixelType>
00187 ::GetFilePattern() const
00188 {
00189 return m_FilePattern.c_str();
00190 }
00191
00192 template <class TPixelType>
00193 void NrrdDiffusionImageReader<TPixelType>
00194 ::SetFilePattern(const char* aFilePattern)
00195 {
00196 m_FilePattern = aFilePattern;
00197 }
00198
00199 template <class TPixelType>
00200 bool NrrdDiffusionImageReader<TPixelType>
00201 ::CanReadFile(const std::string filename, const std::string , const std::string )
00202 {
00203
00204 if( filename == "" )
00205 {
00206 return false;
00207 }
00208 std::string ext = itksys::SystemTools::GetFilenameLastExtension(filename);
00209 ext = itksys::SystemTools::LowerCase(ext);
00210
00211 if (ext == ".hdwi" || ext == ".dwi")
00212 {
00213 itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New();
00214
00215 typedef itk::ImageFileReader<ImageType> FileReaderType;
00216 typename FileReaderType::Pointer reader = FileReaderType::New();
00217 reader->SetImageIO(io);
00218 reader->SetFileName(filename);
00219 reader->Update();
00220 typename ImageType::Pointer img = reader->GetOutput();
00221 itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary();
00222 std::vector<std::string> imgMetaKeys = imgMetaDictionary.GetKeys();
00223 std::vector<std::string>::const_iterator itKey = imgMetaKeys.begin();
00224 std::string metaString;
00225
00226 for (; itKey != imgMetaKeys.end(); itKey ++)
00227 {
00228 itk::ExposeMetaData<std::string> (imgMetaDictionary, *itKey, metaString);
00229 if (itKey->find("modality") != std::string::npos)
00230 {
00231 if (metaString.find("DWMRI") != std::string::npos)
00232 {
00233 return true;
00234 }
00235 }
00236 }
00237 }
00238 return false;
00239 }
00240
00241 }
00242
00243 #endif