@@ -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