9 #ifndef TomatoLIB_ITKREADFILELISTFILTER_HXX 10 #define TomatoLIB_ITKREADFILELISTFILTER_HXX 12 #include "CmakeConfigForTomato.h" 13 #include "gdcmTomatoReadTags.h" 19 #include "itkGDCMImageIO.h" 21 #include "itkStatisticsImageFilter.h" 23 #include "itkReadFileListFilter.h" 24 #include "itkDirectory.h" 25 #include "gdcmBase64.h" 26 #include "gdcmTomatoReadTags.h" 30 template<
typename TImage >
31 ReadFileListFilter<TImage>::
36 template<
typename TImage >
38 ReadFileListFilter<TImage>
39 ::SetFileList(
const std::vector<std::string> fileList){
40 m_FileList = fileList;
43 template<
typename TImage>
44 void ReadFileListFilter<TImage>::SetDirName(
const std::string dirName) {
47 if (m_DirName.empty()) {
51 typename itk::Directory::Pointer directory = itk::Directory::New();
52 directory->Load(m_DirName.c_str());
54 for (
size_t i = 0; i < directory->GetNumberOfFiles(); i++){
55 std::string fileName = directory->GetFile(i);
56 if (fileName ==
"." || fileName ==
".." || fileName ==
".DS_Store"){
59 m_FileList.push_back(m_DirName + KWUtil::PathSeparator() + fileName);
63 template<
typename TImage >
64 template<
typename TYPE >
66 ReadFileListFilter<TImage>
67 ::GetVnlVectorFromStdVector(std::vector<TYPE> stdVector){
68 if (stdVector.size() == 0){
69 return vnl_vector<TYPE>();
71 return vnl_vector<TYPE>(&stdVector.front(), stdVector.size());
75 template<
typename TImage >
77 ReadFileListFilter<TImage>
79 if (m_FileList.empty())
return vnl_vector<double>();
80 vnl_vector<double> invTimesVnl = GetVnlVectorFromStdVector(m_InvTimes);
81 vnl_vector<double> invTimesVnl20051572 = GetVnlVectorFromStdVector(m_InvTimes20051572);
82 vnl_vector<double> invTimesVnl00211189 = GetVnlVectorFromStdVector(m_InvTimes00211189);
83 vnl_vector<double> invTimesVnlFromImageCommentsVnl = GetVnlVectorFromStdVector(m_InvTimesFromImageComments);
84 vnl_vector<double> triggerTimesVnl = GetVnlVectorFromStdVector(m_TriggerTimes);
85 if (invTimesVnl.min_value() != invTimesVnl.max_value()) {
87 }
else if (invTimesVnl20051572.min_value() != invTimesVnl20051572.max_value()) {
88 return invTimesVnl20051572;
89 }
else if (invTimesVnl00211189.min_value() != invTimesVnl00211189.max_value()) {
90 return invTimesVnl00211189;
91 }
else if (invTimesVnlFromImageCommentsVnl.min_value() != invTimesVnlFromImageCommentsVnl.max_value()) {
92 return invTimesVnlFromImageCommentsVnl;
93 }
else if (triggerTimesVnl.min_value() != triggerTimesVnl.max_value()) {
94 return triggerTimesVnl;
100 template<
typename TImage >
102 ReadFileListFilter<TImage>
104 return GetVnlVectorFromStdVector(m_RepTimes);
107 template<
typename TImage >
109 ReadFileListFilter<TImage>
111 vnl_vector<double> echoTimesVnl = GetVnlVectorFromStdVector(m_EchoTimes);
112 vnl_vector<double> echoTimes00191016Vnl = GetVnlVectorFromStdVector(m_EchoTimes00191016);
113 vnl_vector<double> echoTimes00209158Vnl = GetVnlVectorFromStdVector(m_EchoTimes00209158);
114 vnl_vector<double> echoTimesFromImageCommentsVnl = GetVnlVectorFromStdVector(m_EchoTimesFromImageComments);
115 if (echoTimesVnl.min_value() != echoTimesVnl.max_value()) {
117 }
else if (echoTimesFromImageCommentsVnl.min_value() != echoTimesFromImageCommentsVnl.max_value()) {
118 return echoTimesFromImageCommentsVnl;
121 }
else if (echoTimes00209158Vnl.min_value() != echoTimes00209158Vnl.max_value()) {
122 return echoTimes00209158Vnl;
123 }
else if (m_EchoTimes.size() == 3 ){
125 vnl_vector<double> temp(3);
135 template<
typename TImage >
137 ReadFileListFilter<TImage>
139 return GetVnlVectorFromStdVector(m_TriggerTimes);
142 template<
typename TImage >
144 ReadFileListFilter<TImage>
146 return GetVnlVectorFromStdVector(m_AcqTimes);
149 template<
typename TImage >
151 ReadFileListFilter<TImage>
153 vnl_vector<double> temp = GetAcqTimes();
154 double mymin = temp.min_value();
155 for (
unsigned int i = 0; i < temp.size(); ++i){
156 temp[i] = temp[i] - mymin;
161 template<
class TImage >
163 ReadFileListFilter< TImage >
166 typename ImageType3D::Pointer outputImage;
168 m_DicomIO = GDCMImageIO::New();
169 m_DicomIO->LoadPrivateTagsOn();
171 itk::FixedArray< PixelType, 3 > layout;
174 layout[2] = m_FileList.size();
177 typename TileImageType::Pointer tiler = TileImageType::New();
178 tiler->SetLayout( layout );
180 typename ReaderType::Pointer reader;
183 unsigned int inputImageNumber = 0;
184 for (
unsigned int i = 0; i < m_FileList.size(); ++i ){
187 reader = ReaderType::New();
188 reader->SetImageIO(m_DicomIO);
189 reader->SetFileName(m_FileList[i]);
190 m_DicomIO->SetFileName(m_FileList[i]);
191 m_DicomIO->ReadImageInformation();
194 tiler->SetInput(i, reader->GetOutput());
199 m_MetaDataDictionaryArray.push_back(reader->GetMetaDataDictionary());
200 m_InvTimes.push_back(FindInversionTime(reader));
201 m_InvTimes20051572.push_back(FindInversionTime20051572(reader));
202 m_InvTimes00211189.push_back(FindInversionTime00211189(reader));
203 m_InvTimesFromImageComments.push_back(FindInversionTimeFromImageComments(reader));
204 m_RepTimes.push_back(FindRepetitionTime(reader));
205 m_EchoTimes.push_back(FindEchoTime(reader));
206 m_EchoTimes00191016.push_back(FindEchoTime00191016(reader));
207 m_EchoTimes00209158.push_back(FindEchoTime00209158(reader));
208 m_EchoTimesFromImageComments.push_back(FindEchoTimeFromImageComments(reader));
209 m_TriggerTimes.push_back(FindTriggerTime(reader));
210 m_AcqTimes.push_back(FindAcqTime(reader));
212 }
catch( itk::ExceptionObject & err ) {
213 std::cerr <<
"Unable to read file: " << m_FileList[i] << std::endl;
214 std::cerr <<
"ExceptionObject caught!" << std::endl << err << std::endl;
218 if (inputImageNumber == 0)
return;
220 tiler->UpdateLargestPossibleRegion();
222 outputImage = tiler->GetOutput();
227 outputImage->Update();
228 }
catch( itk::ExceptionObject & err ) {
229 std::cerr <<
"ExceptionObject caught!" << std::endl;
230 std::cerr << err << std::endl;
235 typename ImageType3D::PixelType rescaleIntercept = m_DicomIO->GetRescaleIntercept();
236 typename ImageType3D::PixelType rescaleSlope = m_DicomIO->GetRescaleSlope();
238 typedef itk::StatisticsImageFilter<ImageType3D> StatisticsImageFilterType;
239 typename StatisticsImageFilterType::Pointer statisticsImageFilter = StatisticsImageFilterType::New();
240 statisticsImageFilter->SetInput(outputImage);
241 statisticsImageFilter->Update();
243 std::cout <<
"Mean: " << statisticsImageFilter->GetMean();
244 std::cout <<
", Std.: " << statisticsImageFilter->GetSigma();
245 std::cout <<
", Min: " << statisticsImageFilter->GetMinimum();
246 std::cout <<
", Max: " << statisticsImageFilter->GetMaximum();
248 std::cout <<
", RescaleIntercept: " << rescaleIntercept <<
", RescaleSlope: " << rescaleSlope << std::endl;
250 vcl_cout <<
"Inversion Times: " << GetInvTimes() << vcl_endl;
251 vcl_cout <<
"Repetition Times: " << GetRepTimes() << vcl_endl;
252 vcl_cout <<
"Echo Times: " << GetEchoTimes() << vcl_endl;
253 vcl_cout <<
"Trigger Times: " << GetTriggerTimes() << vcl_endl;
254 vcl_cout <<
"Rel Acq Times: " << GetRelAcqTimes() << vcl_endl;
257 this->GraftOutput(outputImage);
260 template<
class TImage>
262 ReadFileListFilter< TImage>
263 ::FindInversionTime(ReaderType* reader) {
267 const DictionaryType & dictionary = reader->GetMetaDataDictionary();
268 DictionaryType::ConstIterator end = dictionary.End();
270 std::string entryId =
"0018|0082";
271 DictionaryType::ConstIterator tagItr = dictionary.Find(entryId);
273 if ( tagItr != end ) {
274 MetaDataStringType::ConstPointer entryvalueStr =
275 dynamic_cast<const MetaDataStringType *
>(tagItr->second.GetPointer());
278 std::string tagvalue = entryvalueStr->GetMetaDataObjectValue();
279 invTime = KWUtil::StringToNumber<double>(tagvalue);
285 template<
class TImage>
287 ReadFileListFilter< TImage>
288 ::FindInversionTime20051572(ReaderType* reader) {
292 const DictionaryType & dictionary = reader->GetMetaDataDictionary();
293 DictionaryType::ConstIterator end = dictionary.End();
295 std::string entryId =
"2005|1572";
296 DictionaryType::ConstIterator tagItr = dictionary.Find(entryId);
298 if ( tagItr != end ) {
299 MetaDataStringType::ConstPointer entryvalueStr =
300 dynamic_cast<const MetaDataStringType *
>(tagItr->second.GetPointer());
312 std::string tagvalue = entryvalueStr->GetMetaDataObjectValue();
313 invTime = KWUtil::StringToNumber<double>(tagvalue);
319 template<
class TImage>
321 ReadFileListFilter< TImage>
322 ::FindInversionTime00211189(ReaderType *reader) {
326 std::vector<std::pair<int, int> > tags;
327 tags.push_back(std::pair<int, int>(0x5200, 0x9230));
328 tags.push_back(std::pair<int, int>(0x0021, 0x11fe));
329 tags.push_back(std::pair<int, int>(0x0021, 0x1189));
331 std::string tagvalue;
332 if (gdcmTomatoReadTags(tags, reader->GetFileName(), tagvalue,
false) == 0) {
333 invTime = *(
double*)&tagvalue[0];
340 template<
class TImage>
342 ReadFileListFilter< TImage>
343 ::FindInversionTimeFromImageComments(ReaderType* reader) {
347 const DictionaryType & dictionary = reader->GetMetaDataDictionary();
348 DictionaryType::ConstIterator end = dictionary.End();
350 std::string entryId =
"0020|4000";
351 DictionaryType::ConstIterator tagItr = dictionary.Find(entryId);
353 if ( tagItr != end ) {
354 MetaDataStringType::ConstPointer entryvalueStr =
dynamic_cast<const MetaDataStringType *
>(tagItr->second.GetPointer());
357 std::string tagvalue = entryvalueStr->GetMetaDataObjectValue();
358 std::sscanf(tagvalue.c_str(),
"TIeff %i ms ", &invTime);
361 return (
double)invTime;
364 template<
class TImage>
366 ReadFileListFilter< TImage>
367 ::FindRepetitionTime(ReaderType* reader) {
371 const DictionaryType & dictionary = reader->GetMetaDataDictionary();
372 DictionaryType::ConstIterator end = dictionary.End();
374 std::string entryId =
"0018|0080";
375 DictionaryType::ConstIterator tagItr = dictionary.Find(entryId);
376 if ( tagItr != end ) {
377 MetaDataStringType::ConstPointer entryvalueStr =
378 dynamic_cast<const MetaDataStringType *
>(tagItr->second.GetPointer());
381 std::string tagvalue = entryvalueStr->GetMetaDataObjectValue();
382 repTime = KWUtil::StringToNumber<double>(tagvalue);
388 template<
class TImage>
390 ReadFileListFilter< TImage>
391 ::FindEchoTime(ReaderType* reader) {
395 const DictionaryType & dictionary = reader->GetMetaDataDictionary();
396 DictionaryType::ConstIterator end = dictionary.End();
398 std::string entryId =
"0018|0081";
399 DictionaryType::ConstIterator tagItr = dictionary.Find(entryId);
400 if ( tagItr != end ) {
401 MetaDataStringType::ConstPointer entryvalueStr =
402 dynamic_cast<const MetaDataStringType *
>(tagItr->second.GetPointer());
405 std::string tagvalue = entryvalueStr->GetMetaDataObjectValue();
406 echoTime = KWUtil::StringToNumber<double>(tagvalue);
412 template<
class TImage>
414 ReadFileListFilter< TImage>
415 ::FindEchoTime00191016(ReaderType* reader) {
419 const DictionaryType & dictionary = reader->GetMetaDataDictionary();
420 DictionaryType::ConstIterator end = dictionary.End();
422 std::string entryId =
"0019|1016";
423 DictionaryType::ConstIterator tagItr = dictionary.Find(entryId);
424 if ( tagItr != end ) {
425 MetaDataStringType::ConstPointer entryvalueStr =
426 dynamic_cast<const MetaDataStringType *
>(tagItr->second.GetPointer());
429 std::string tagvalue = entryvalueStr->GetMetaDataObjectValue();
430 echoTime = KWUtil::StringToNumber<double>(tagvalue);
436 template<
class TImage>
438 ReadFileListFilter< TImage>
439 ::FindEchoTime00209158(ReaderType* reader) {
443 std::vector<std::pair<int, int> > tags;
444 tags.push_back(std::pair<int, int>(0x5200, 0x9230));
445 tags.push_back(std::pair<int, int>(0x0020, 0x9111));
446 tags.push_back(std::pair<int, int>(0x0020, 0x9158));
448 std::string tagvalue;
449 if ( gdcmTomatoReadTags(tags, reader->GetFileName(), tagvalue,
false) == 0) {
450 std::sscanf(tagvalue.c_str(),
"T2 prep. duration = %i ms", &echoTime);
453 return (
double)echoTime;
456 template<
class TImage>
458 ReadFileListFilter< TImage>
459 ::FindEchoTimeFromImageComments(ReaderType* reader) {
463 const DictionaryType & dictionary = reader->GetMetaDataDictionary();
464 DictionaryType::ConstIterator end = dictionary.End();
466 std::string entryId =
"0020|4000";
467 DictionaryType::ConstIterator tagItr = dictionary.Find(entryId);
469 if ( tagItr != end ) {
470 MetaDataStringType::ConstPointer entryvalueStr =
dynamic_cast<const MetaDataStringType *
>(tagItr->second.GetPointer());
473 std::string tagvalue = entryvalueStr->GetMetaDataObjectValue();
474 std::sscanf(tagvalue.c_str(),
"T2 prep. duration = %i ms ", &echoTime);
477 return (
double)echoTime;
480 template<
class TImage>
482 ReadFileListFilter< TImage>
483 ::FindTriggerTime(ReaderType* reader) {
485 double triggerTime = 0;
487 const DictionaryType & dictionary = reader->GetMetaDataDictionary();
488 DictionaryType::ConstIterator end = dictionary.End();
490 std::string entryId =
"0018|1060";
491 DictionaryType::ConstIterator tagItr = dictionary.Find(entryId);
492 if ( tagItr != end ) {
493 MetaDataStringType::ConstPointer entryvalueStr =
494 dynamic_cast<const MetaDataStringType *
>(tagItr->second.GetPointer());
497 std::string tagvalue = entryvalueStr->GetMetaDataObjectValue();
498 triggerTime = KWUtil::StringToNumber<double>(tagvalue);
504 template<
class TImage>
506 ReadFileListFilter< TImage>
507 ::FindAcqTime(ReaderType* reader) {
511 const DictionaryType & dictionary = reader->GetMetaDataDictionary();
512 DictionaryType::ConstIterator end = dictionary.End();
514 std::string entryId =
"0008|0032";
515 DictionaryType::ConstIterator tagItr = dictionary.Find(entryId);
516 if ( tagItr != end ) {
517 MetaDataStringType::ConstPointer entryvalueStr =
518 dynamic_cast<const MetaDataStringType *
>(tagItr->second.GetPointer());
521 std::string tagvalue = entryvalueStr->GetMetaDataObjectValue();
522 acqTime = KWUtil::dicomTime2Seconds<double>(tagvalue);
528 template<
typename TImage>
529 const std::vector<std::string> &ReadFileListFilter<TImage>::GetFileList()
const {
533 template<
typename TImage>
534 const std::string &ReadFileListFilter<TImage>::GetDirName()
const {
542 #endif //TomatoLIB_ITKREADFILELISTFILTER_HXX Definition: itkImageFileReaderKW.h:31