Skip to content

Commit a10b634

Browse files
author
will tepe
committed
fix casting, add check
Signed-off-by: will tepe <[email protected]>
1 parent e7420e0 commit a10b634

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

monai/deploy/operators/dicom_series_to_volume_operator.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def generate_voxel_data(self, series):
112112
# with the NumPy array returned from the ITK GetArrayViewFromImage on the image
113113
# loaded from the same DICOM series.
114114
vol_data = np.stack([s.get_pixel_array() for s in slices], axis=0)
115-
vol_data = vol_data.astype(np.int16)
115+
vol_data = vol_data.astype(np.uint16)
116116

117117
# For now we support monochrome image only, for which DICOM Photometric Interpretation
118118
# (0028,0004) has defined terms, MONOCHROME1 and MONOCHROME2, with the former being:
@@ -156,9 +156,14 @@ def generate_voxel_data(self, series):
156156

157157
if slope != 1:
158158
vol_data = slope * vol_data.astype(np.float64)
159-
vol_data = vol_data.astype(np.int16)
160-
vol_data += np.int16(intercept)
161-
return np.array(vol_data, dtype=np.int16)
159+
vol_data += intercept
160+
161+
# Check if vol_data can be cast to uint16 without data loss
162+
if np.can_cast(vol_data, np.uint16, casting='safe'):
163+
vol_data = np.array(vol_data, dtype=np.uint16)
164+
else:
165+
vol_data = np.array(vol_data, dtype=np.int32)
166+
return vol_data
162167

163168
def create_volumetric_image(self, vox_data, metadata):
164169
"""Creates an instance of 3D image.

0 commit comments

Comments
 (0)