CBICA Toolkit  1.0.0
DicomImageReader.h
1 ///////////////////////////////////////////////////////////////////////////////////////
2 // DicomImageReader.h
3 //
4 // Copyright (c) 2018. All rights reserved.
5 // Section of Biomedical Image Analysis
6 // Center for Biomedical Image Computing and Analytics
7 // Department of Radiology
8 // Perelman School of Medicine
9 // University of Pennsylvania
10 //
11 // Contact details: software@cbica.upenn.edu
12 //
13 // License Agreement: https://www.med.upenn.edu/cbica/software-agreement.html
14 ///////////////////////////////////////////////////////////////////////////////////////
15 
16 #ifndef DicomImageReader_H
17 #define DicomImageReader_H
18 
19 #include "itkImageFileReader.h"
20 #include "itkGDCMImageIO.h"
21 #include "itkGDCMSeriesFileNames.h"
22 #include "itkImageFileWriter.h"
23 #include "itkCastImageFilter.h"
24 #include <itkMapContainer.h>
25 
27 {
28 public:
29 
30  typedef itk::Image< float, 3 > ImageType3DFloat;
31  typedef itk::GDCMSeriesFileNames NamesGeneratorType;
32  typedef itk::GDCMImageIO ImageIOType;
33  typedef std::vector<std::string> FileNamesContainer;
34 
37 
38 // //! set the input directory containing dicom series
39  void SetDirectoryPath(std::string path);
40 
41 // //! get the read dicom data as 3D float ITK image
42 // DicomImageReader::ImageType3DFloat::Pointer GetITKImage();
43 
44 // //! load dicom data
45 // bool LoadDicom();
46 
47  //! Read dicom series
48  template <class TInputImage>
49  typename TInputImage::Pointer ReadDicomImage(bool &readStatus);
50 
51 // //! helper to write itk image
52 // template <class TInputImage>
53 // void WriteITKImage(typename TInputImage::Pointer image, std::string filename);
54 
55 // //! convert itk image to float 3D itk image
56 // template <class TInputImage>
57 // DicomImageReader::ImageType3DFloat::Pointer ConvertImage3DToFloatImage3D(typename TInputImage::Pointer image);
58 
59 private:
60 
61  ImageType3DFloat::Pointer m_image3dfloat; //! image 3D as float
62  std::string m_dir; //! input directory path
63 };
64 
65 template<class TInputImage>
66 inline typename TInputImage::Pointer DicomImageReader::ReadDicomImage(bool &readStatus)
67 {
68  readStatus = false;
69  typedef itk::ImageFileReader< TInputImage> DicomReaderType;
70  auto reader = DicomReaderType::New();
71  auto dicomIO = ImageIOType::New();
72  auto nameGenerator = NamesGeneratorType::New();
73 
74  nameGenerator->SetInputDirectory(this->m_dir);
75  reader->SetImageIO(dicomIO);
76  std::vector<std::string> fileNames = nameGenerator->GetInputFileNames();
77  reader->SetFileName(fileNames.at(0)); //assuming there is only 1 image, since this is a single dicom image reader
78 
79  try
80  {
81  reader->Update();
82  }
83  catch (itk::ExceptionObject &ex)
84  {
85  readStatus = false;
86  std::cout << "unsupported dicom" << std::endl;
87  std::cout << ex << std::endl;
88  return nullptr;
89  }
90 
91  readStatus = true;
92  return reader->GetOutput();
93 
94 }
95 
96 //template<class TInputImage>
97 //inline void DicomImageReader::WriteITKImage(typename TInputImage::Pointer image, std::string filename)
98 //{
99 // typedef itk::ImageFileWriter< TInputImage > WriterType;
100 // auto writer = WriterType::New();
101 // writer->SetFileName(filename);
102 // writer->SetInput(image);
103 // writer->Update();
104 //}
105 
106 //template<class TInputImage>
107 //inline DicomImageReader::ImageType3DFloat::Pointer DicomImageReader::ConvertImage3DToFloatImage3D(typename TInputImage::Pointer image)
108 //{
109 // typedef itk::CastImageFilter<TInputImage, ImageType3DFloat> CastFilterType;
110 // auto castFilter = CastFilterType::New();
111 // castFilter->SetInput(image);
112 // castFilter->Update();
113 // return castFilter->GetOutput();
114 //}
115 
116 #endif // DicomImageReader_H
TInputImage::Pointer ReadDicomImage(bool &readStatus)
Read dicom series.
Definition: DicomImageReader.h:66
Definition: DicomImageReader.h:26