Skip to content
Snippets Groups Projects

Load dicom

1 file
+ 30
2
Compare changes
  • Side-by-side
  • Inline
+ 30
2
@@ -445,12 +445,36 @@ class DataLoader:
Args:
path (str): Path to file
"""
dcm_data = pydicom.dcmread(path)
if self.virtual_stack:
defer_size = '2 MB'
else:
defer_size = None
dcm_data = pydicom.dcmread(path, defer_size=defer_size)
if self.return_metadata:
return dcm_data.pixel_array, dcm_data
else:
return dcm_data.pixel_array
def load_dicom_dir(self, path):
""" Load a directory of DICOM files into a numpy 3d array
Args:
path (str): Directory path
"""
# loop over all .dcm files in the directory
files = [f for f in os.listdir(path) if f.endswith('.dcm')]
files.sort()
# dicom_list contains the dicom objects with metadata
dicom_list = [pydicom.dcmread(os.path.join(path, f)) for f in files]
# vol contains the pixel data
vol = np.stack([dicom.pixel_array for dicom in dicom_list], axis=0)
if self.return_metadata:
return vol, dicom_list
else:
return vol
def load(self, path):
@@ -499,7 +523,11 @@ class DataLoader:
# Load a directory
elif os.path.isdir(path):
return self.load_tiff_stack(path)
# load dicom if directory contains dicom files else load tiff stack as default
if any([f.endswith('.dcm') for f in os.listdir(path)]):
return self.load_dicom_dir(path)
else:
return self.load_tiff_stack(path)
# Fails
else:
Loading