Skip to main content
 首页 » 编程设计

python之如何将 dicom 图像分割成图 block

2025年01月19日8think

我正在尝试使用 image_slicer 函数将 DICOM 图像分割成图 block ,但它无法识别 DICOM。

我已经阅读了 DICOM 并将它们转换为 np 数组:

dcm_文件[0]

array([[-1024, -1024, -1024, ..., -1024, -1024, -1024], 
       [-1024, -1024, -1024, ..., -1024, -1024, -1024], 
       [-1024, -1024, -1024, ..., -1024, -1024, -1024], 
       ..., 
       [-1024, -1024, -1024, ..., -1024, -1024, -1024], 
       [-1024, -1024, -1024, ..., -1024, -1024, -1024], 
       [-1024, -1024, -1024, ..., -1024, -1024, -1024]], dtype=int16) 

并且能够通过以下方式查看图像:

from PIL import Image 
import numpy as np 
 
img = Image.fromarray(dcm_files[0]) 
img.show() 

然后尝试切片:

import image_slicer 
image_slicer.slice(img, 64) 

错误:“图像”对象没有属性“读取”

谢谢!

请您参考如下方法:

image_slicer 模块使用文件名,而不是文件实例。因此,您必须将数组保存到某种本地化。您可以使用 tempfile module为了这个目的。

from PIL import Image 
import numpy as np 
import image_slicer 
import tempfile 
 
array = np.random.randint(0,200, size=(128,128), dtype='int32') 
 
img = Image.fromarray(array) 
 
temp = tempfile.NamedTemporaryFile() 
img.save(temp.name, format="png") 
 
image_slicer.slice(temp.name, 4) 
#if you want to play with the slices: 
tiles = image_slicer.slice(temp.name, 4, save=False)