Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions monai/deploy/operators/dicom_series_to_volume_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def generate_voxel_data(self, series):
# with the NumPy array returned from the ITK GetArrayViewFromImage on the image
# loaded from the same DICOM series.
vol_data = np.stack([s.get_pixel_array() for s in slices], axis=0)
if slices[0][0x0028,0x0103].value == 1:
# The above get_pixel_array() already considers the PixelRepresentation attribute,
# 0 is unsigned int, 1 is signed int
if slices[0][0x0028, 0x0103].value == 0:
vol_data = vol_data.astype(np.uint16)

# For now we support monochrome image only, for which DICOM Photometric Interpretation
Expand Down Expand Up @@ -155,24 +157,35 @@ def generate_voxel_data(self, series):
except KeyError:
slope = 1


# check if vol_data, intercept, and slope can be cast to uint16 without data loss
if np.can_cast(vol_data, np.uint16, casting='safe') and np.can_cast(intercept, np.uint16, casting='safe') and np.can_cast(slope, np.uint16, casting='safe'):
logging.info(f"Casting to uint16")
if (
np.can_cast(vol_data, np.uint16, casting="safe")
and np.can_cast(intercept, np.uint16, casting="safe")
and np.can_cast(slope, np.uint16, casting="safe")
):
logging.info("Casting to uint16")
vol_data = np.array(vol_data, dtype=np.uint16)
intercept = np.uint16(intercept)
slope = np.uint16(slope)
elif np.can_cast(vol_data, np.float32, casting='safe') and np.can_cast(intercept, np.float32, casting='safe') and np.can_cast(slope, np.float32, casting='safe'):
logging.info(f"Casting to float32")
elif (
np.can_cast(vol_data, np.float32, casting="safe")
and np.can_cast(intercept, np.float32, casting="safe")
and np.can_cast(slope, np.float32, casting="safe")
):
logging.info("Casting to float32")
vol_data = np.array(vol_data, dtype=np.float32)
intercept = np.float32(intercept)
slope = np.float32(slope)
elif np.can_cast(vol_data, np.float64, casting='safe') and np.can_cast(intercept, np.float64, casting='safe') and np.can_cast(slope, np.float64, casting='safe'):
logging.info(f"Casting to float64")
elif (
np.can_cast(vol_data, np.float64, casting="safe")
and np.can_cast(intercept, np.float64, casting="safe")
and np.can_cast(slope, np.float64, casting="safe")
):
logging.info("Casting to float64")
vol_data = np.array(vol_data, dtype=np.float64)
intercept = np.float64(intercept)
slope = np.float64(slope)

if slope != 1:
vol_data = slope * vol_data

Expand Down