学习处理 DICOM 图像和 Python,如有任何愚蠢的问题,请多多包涵。
我正在努力将 CT DICOM 图像切片转换为体积,该体积可进一步用于以 3D 显示,也可用于处理更多方程。据我了解,像素值存储在 pixel_array 标头中,我无法弄清楚如何可视化这些单独切片的体积,我看到的一个建议是初始化空白矩阵,其中 vol = 大小矩阵(像素宽度、像素高度、切片) 使用 numpy zeros,虽然我知道 pixelwidth 和 pixelheight 是我可以从 DICOM 标头获得的列和行,但不确定切片是什么。另外,我发现下面的代码在网络上的多个区域给出了类似的结果,但是无法指出他们在下面使用体积的位置,我最终需要了解如何在 3D 体积矩阵中获取这些单独的图像数据,谢谢: https://www.raddq.com/dicom-processing-segmentation-visualization-in-python/
def load_scan(path):
slices = [dicom.read_file(path + '/' + s) for s in os.listdir(path)]
slices.sort(key = lambda x: int(x.InstanceNumber))
try:
slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
except:
slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)
for s in slices:
s.SliceThickness = slice_thickness
return slices
def get_pixels_hu(scans):
image = np.stack([s.pixel_array for s in scans])
# Convert to int16 (from sometimes int16),
# should be possible as values should always be low enough (<32k)
image = image.astype(np.int16)
# Set outside-of-scan pixels to 1
# The intercept is usually -1024, so air is approximately 0
image[image == -2000] = 0
# Convert to Hounsfield units (HU)
intercept = scans[0].RescaleIntercept
slope = scans[0].RescaleSlope
if slope != 1:
image = slope * image.astype(np.float64)
image = image.astype(np.int16)
image += np.int16(intercept)
return np.array(image, dtype=np.int16)
id = 0
patient = load_scan(data_path)
imgs = get_pixels_hu(patient)