-
Notifications
You must be signed in to change notification settings - Fork 5
/
DicomReader.py
75 lines (66 loc) · 2.28 KB
/
DicomReader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 25 13:45:50 2018
@author: Raluca Sandu
"""
import os
import numpy as np
import SimpleITK as sitk
import pydicom
#%%
def read_dcm_series(folder_path, reader_flag=True):
"""
Read DICOM Series/Single Image from a folder path into a SimpleITK Image Object.
:param folder_path: directory address containing DICOM Images
:param reader_flag:
:return: SimpleITK Image Object
"""
try:
if next(os.walk(folder_path), None) is None:
# single DICOM File
image = sitk.ReadImage(os.path.normpath(folder_path), sitk.sitkInt16)
return image, None
except Exception:
print('Non-readable DICOM Data: ', folder_path)
return None
# DICOM Series
reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(os.path.normpath(folder_path))
reader.SetFileNames(dicom_names)
# Configure the reader to load all of the DICOM tags (public+private):
# By default tags are not loaded (saves time).
# By default if tags are loaded, the private tags are not loaded.
# We explicitly configure the reader to load tags, including the
# private ones.
reader.MetaDataDictionaryArrayUpdateOn()
reader.LoadPrivateTagsOn()
try:
image = reader.Execute()
if reader_flag:
return image, reader
else:
return image
except Exception:
print('Non-readable DICOM Data: ', folder_path)
if reader_flag:
return None, None
else:
return None
def read_single_dcm(path):
try:
ds = pydicom.read_file(path)
except Exception:
return None
return ds
def read_dcm_series_pydicom(path):
# Load the scans in given folder path
slices = [pydicom.read_file(path + '/' + s) for s in os.listdir(path)]
# slices.sort(key = lambda x: float(x.ImagePositionPatient[2]), reverse=True)
slices.sort(key=lambda x: int(x.InstanceNumber))
try:
slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
except Exception:
slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)
for s in slices:
s.SliceThickness = slice_thickness
return slices