如何将dicom图像分割成瓷砖?

时间:2018-04-29 18:38:49

标签: python image python-imaging-library slice dicom

我尝试使用image_slicer函数将DICOM图像拆分为图块,但它没有识别DICOM。

我已经读过DICOM并将它们转换成np数组:

dcm_files [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)

错误:'图片'对象没有属性'读'

谢谢!

1 个答案:

答案 0 :(得分:0)

模块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)