Tomato
Tomato_exportT1_magSignRecov.hxx
1 //
2 // Created by Konrad Werys on 19.12.19.
3 //
4 
5 #ifndef TOMATO_TOMATO_EXPORTT1_MAGSIGNRECOV_H
6 #define TOMATO_TOMATO_EXPORTT1_MAGSIGNRECOV_H
7 
8 #include "CmakeConfigForTomato.h"
9 #ifdef USE_ITK
10 
11 #include "gdcmUIDGenerator.h"
12 #include "itkGDCMImageIO.h"
13 #include "itkGDCMSeriesFileNames.h"
14 #include "itkMultiplyImageFilter.h"
15 #include "itkImageFileWriter.h"
16 #include "itkFileTools.h"
17 #include "itkExtractImageFilter.h"
18 
19 
20 namespace Ox {
21 
22  template< typename MeasureType >
23  int
24  Tomato<MeasureType>
25  ::exportT1MagSignRecovToDicom(){
26 
27  if (_opts->dir_output_magSignRecov.empty()) {
28  printf("No DICOM export, dir_output_magSignRecov not given.\n");
29  return 1; // EXIT_FAILURE
30  }
31 
32  typedef itk::AdaptImageFilter < ImageType2D, OutputImageType, CastPixelAccessor< InputPixelType, OutputPixelType > > ImageAdaptorType;
33  typedef itk::ImageFileWriter<OutputImageType> WriterType;
34  typedef itk::ExtractImageFilter<ImageType3D, ImageType2D> ExtractImageFilterType;
35 
36  // this one I want to reset
37  itk::EncapsulateMetaData<std::string>( _dictionaryInput, std::string("0028|1052"), "0"); // Rescale intercept
38  itk::EncapsulateMetaData<std::string>( _dictionaryInput, std::string("0028|1053"), "1"); // Rescale slope
39 
40 
41  DictionaryType dictionaryOutput_MagSignRecov(_dictionaryInput);
42 
43  // UIDs
44  gdcm::UIDGenerator sopuid;
45  gdcm::UIDGenerator suid;
46 
47  std::string seriesUID_MagSignRecov = suid.Generate();
48 
49  // seriesNumber
50  std::string seriesNumber;
51  itk::ExposeMetaData<std::string>(_dictionaryInput, "0020|0011", seriesNumber);
52  if (_opts->output_map_series_number == 0) {
53  _opts->output_map_series_number = KWUtil::StringToNumber<int>(seriesNumber) + 10004;
54  }
55  std::string newSeriesNumber_T1 = KWUtil::NumberToString(_opts->output_map_series_number);
56 
57  // get GDCMImageIO ready before export
58  typedef itk::GDCMImageIO ImageIOType;
59  ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
60  gdcmImageIO->KeepOriginalUIDOn();
61 
62  // to extract 2d image from 3d image
63  typename ExtractImageFilterType::Pointer extractor = ExtractImageFilterType::New();
64  extractor->SetInput(_imageCalculatorItk->GetMagSignRecovered());
65  extractor->SetDirectionCollapseToIdentity();
66  typename ImageType3D::SizeType size = _imageCalculatorItk->GetMagSignRecovered()->GetLargestPossibleRegion().GetSize();
67  size[2] = 0;
68  typename ImageType3D::IndexType extractorIndex;
69  extractorIndex.Fill(0);
70  typename ImageType3D::RegionType desiredRegion;
71  desiredRegion.SetSize(size);
72  desiredRegion.SetIndex(extractorIndex);
73  extractor->SetExtractionRegion(desiredRegion);
74 
75  // to cast
76  typename ImageAdaptorType::Pointer adaptor = ImageAdaptorType::New();
77 
78  // get the writer ready before export
79  typename WriterType::Pointer writer = WriterType::New();
80  writer->SetUseInputMetaDataDictionary(false);
81 
82  printf("Saving to: %s ", _opts->dir_output_magSignRecov.c_str());
83 
84  for (size_t iImage = 0; iImage < _opts->files_magnitude.size(); iImage++) {
85 
86  MeasureType invTime = this->_invTimes[iImage];
87 
88  std::string sopInstanceUID_MagSignRecov = sopuid.Generate(); // for every dicom
89 
90  itk::EncapsulateMetaData<std::string>(dictionaryOutput_MagSignRecov, std::string("0008|0018"), sopInstanceUID_MagSignRecov);
91  itk::EncapsulateMetaData<std::string>(dictionaryOutput_MagSignRecov, std::string("0002|0003"), sopInstanceUID_MagSignRecov);
92  itk::EncapsulateMetaData<std::string>(dictionaryOutput_MagSignRecov, std::string("0020|000e"), seriesUID_MagSignRecov);
93  itk::EncapsulateMetaData<std::string>(dictionaryOutput_MagSignRecov, std::string("0020|0011"), newSeriesNumber_T1); // series number
94  itk::EncapsulateMetaData<std::string>(dictionaryOutput_MagSignRecov, std::string("0020|0013"), KWUtil::NumberToString(iImage)); // instance number
95  itk::EncapsulateMetaData<std::string>(dictionaryOutput_MagSignRecov, std::string("0020|4000"), "Tomato magnitude image with sign"); // image comments
96  itk::EncapsulateMetaData<std::string>(dictionaryOutput_MagSignRecov, std::string("0018|0082"), KWUtil::NumberToString(invTime)); // inversion time
97 
98  itk::FileTools::CreateDirectory(_opts->dir_output_magSignRecov);
99 
100  // export T2 color
101  gdcmImageIO->SetMetaDataDictionary(dictionaryOutput_MagSignRecov);
102 
103  // export to dicom
104  extractorIndex[2] = iImage;
105  desiredRegion.SetIndex(extractorIndex);
106  extractor->SetExtractionRegion(desiredRegion);
107 // extractor->Update();
108 
109  adaptor->SetInput(extractor->GetOutput());
110  writer->SetFileName(_opts->dir_output_magSignRecov + KWUtil::PathSeparator() + KWUtil::NumberToString(iImage) + ".dcm");
111  writer->SetInput(adaptor->GetOutput());
112  writer->SetImageIO(gdcmImageIO);
113  try {
114  writer->Update();
115  } catch (itk::ExceptionObject &e) {
116  std::cerr << "Exception in file writer " << std::endl;
117  std::cerr << e << std::endl;
118  }
119  }
120 
121  std::cout << " Saved!" << std::endl;
122 
123  return 0; // EXIT_SUCCESS
124  }
125 
126 } // namespace Ox
127 
128 #endif // USE_ITK
129 
130 #endif //TOMATO_TOMATO_EXPORTT1_MAGSIGNRECOV_H
Definition: OxCalculator.h:19