【问题标题】:Reading CT Scan dicom file读取 CT Scan dicom 文件
【发布时间】:2021-11-04 06:04:39
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • 看来你需要安装GDCMpylibjpeg
  • 其实你只需要其中一个。查看安装pylibjpegGDCM的文档。

标签: python pydicom


【解决方案1】:

尝试运行以下命令:

!pip install pylibjpeg
!pip install gdcm

【讨论】:

  • 这并不完全正确 - 请参阅我上面评论中的文档链接。
  • 嗨,谢谢你提出这个问题,我也尝试了卢克的方式,但错误仍然存​​在,我摆脱了 conda 中的错误。我做了我在谷歌 colab 和 pycharm 做的同样的事情。最后 conda 解决了我的问题,但我不知道为什么它在 conda 上工作而在 colab 和 pycharm 上失败。
  • 不,运行时错误是我在转换时遇到的唯一错误。我在 conda 中做了同样的事情,它已经启动并运行了。
【解决方案2】:

在您的similar problem 中,我们可以看到问题在像素数据 级别仍然存在。您需要install one or more optional libraries,以便您可以处理各种压缩。

首先,你应该这样做:

$ 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)

【讨论】:

  • 嗨,感谢您提出这个问题,我尝试在 colab 中测试此代码,但我收到了 ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject 错误。我尝试更新/重新安装 NumPy(在 GitHub 和 StackOverflow 上建议),但这个错误仍然存​​在。
  • 尝试关注这个问题:stackoverflow.com/q/66060487/2371987
  • 我试过这个链接,但错误仍然存​​在。当我尝试导入 pydicom 时出现此错误。
  • Google collab,它在 conda 中运行良好,我在 pycharm 和 colab 中遇到了这个错误。我只是好奇为什么它在 colab 和 pycharm 上不起作用。
  • 仍然,错误仍然存​​在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多