def to_volume(slices):
"""Creates ndarray volume in Hounsfield units (HU) from array of pydicom slices.
"""
volume = np.stack([s.pixel_array for s in slices])
volume = volume.astype(np.int16)
# Set outside-of-scan pixels to 0
# The intercept is usually -1024, so air is approximately 0
volume[volume == -2000] = 0
# Convert to Hounsfield units (HU)
for n in range(len(slices)):
intercept = slices[n].RescaleIntercept
slope = slices[n].RescaleSlope
if slope != 1:
volume[n] = slope * volume[n].astype(np.float64)
volume[n] = volume[n].astype(np.int16)
volume[n] += np.int16(intercept)
volume = np.array(volume, dtype=np.int16)
spacing = tuple(map(float, ([slices[0].SliceThickness] + slices[0].PixelSpacing)))
return volume, spacing
评论列表
文章目录