读取CT扫描dicom文件



我正在尝试使用pydicom python库读取CT扫描Dicom文件,但即使我安装gdcm和pylibjpeg,我也无法摆脱以下错误

RuntimeError: The following handlers are available to decode the pixel data however they are missing required dependencies: GDCM (req. ), pylibjpeg (req. )

我的代码

!pip install pylibjpeg pylibjpeg-libjpeg pylibjpeg-openjpeg
!pip install python-gdcm
import gdcm
import pylibjpeg
import numpy as np
import pydicom
from pydicom.pixel_data_handlers.util import apply_voi_lut
import matplotlib.pyplot as plt
%matplotlib inline

def read_xray(path, voi_lut = True, fix_monochrome = True):
dicom = pydicom.read_file(path)

# VOI LUT (if available by DICOM device) is used to transform raw DICOM data to "human-friendly" view
if voi_lut:
data = apply_voi_lut(dicom.pixel_array, dicom)
else:
data = dicom.pixel_array

# depending on this value, X-ray may look inverted - fix that:
if fix_monochrome and dicom.PhotometricInterpretation == "MONOCHROME1":
data = np.amax(data) - data

data = data - np.min(data)
data = data / np.max(data)
data = (data * 255).astype(np.uint8)

return data
img = read_xray('/content/ActiveTB/2018/09/17/1.2.392.200036.9116.2.6.1.44063.1797735841.1537157438.869027/1.2.392.200036.9116.2.6.1.44063.1797735841.1537157440.863887/1.2.392.200036.9116.2.6.1.44063.1797735841.1537154539.142332.dcm')
plt.figure(figsize = (12,12))
plt.imshow(img)

这是我试图运行此代码的图像链接

https://drive.google.com/file/d/1-xuryA5VlglaumU2HHV7-p6Yhgd6AaCC/view?usp=sharing

尝试运行以下命令:

!pip install pylibjpeg
!pip install gdcm

与您的问题类似,我们可以看到问题仍然存在于Pixel Data级别。您需要安装一个或多个可选库,以便您可以处理各种压缩。

首先,你应该做:

$ pip uninstall pycocotools
$ pip install pycocotools --no-binary :all: --no-build-isolation

从这里开始,你应该做如下的事情:

$ pip install pylibjpeg pylibjpeg-libjpeg pydicom
那么,你的代码应该是这样的:
from pydicom import dcmread
import pylibjpeg
import gdcm
import numpy as np
import pydicom
from pydicom.pixel_data_handlers.util import apply_voi_lut
import matplotlib.pyplot as plt
%matplotlib inline

def read_xray(path, voi_lut = True, fix_monochrome = True):
dicom = dcmread(path)

# VOI LUT (if available by DICOM device) is used to transform raw DICOM data to "human-friendly" view
if voi_lut:
data = apply_voi_lut(dicom.pixel_array, dicom)
else:
data = dicom.pixel_array

# depending on this value, X-ray may look inverted - fix that:
if fix_monochrome and dicom.PhotometricInterpretation == "MONOCHROME1":
data = np.amax(data) - data

data = data - np.min(data)
data = data / np.max(data)
data = (data * 255).astype(np.uint8)

return data
img = read_xray('/content/ActiveTB/2018/09/17/1.2.392.200036.9116.2.6.1.44063.1797735841.1537157438.869027/1.2.392.200036.9116.2.6.1.44063.1797735841.1537157440.863887/1.2.392.200036.9116.2.6.1.44063.1797735841.1537154539.142332.dcm')
plt.figure(figsize = (12,12))
plt.imshow(img)

最新更新