【问题标题】:Unable to get dicom image for display in python无法获取要在 python 中显示的 dicom 图像
【发布时间】:2014-06-03 17:47:57
【问题描述】:

我正在尝试在 opencv-python 中显示 DICOM 图像。我正在使用 pydicom 库,然后添加 API 以使用 DOTNET 创建一个完整的 DICOM 查看器,该查看器运行 python(C# 调用 python 与进程实例当然!!)。我无法转换或查看未压缩的 DICOM 图像。每当我尝试加载或修改pixel_array。我收到错误消息。

import dicom
import cv2
import numpy
df=dicom.read_file("IM-0001-0002.dcm")
df.pixel_array

Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
df.pixel_array
File "C:\Python27\lib\site-packages\dicom\dataset.py", line 394, in pixel_array
return self._get_pixel_array()
File "C:\Python27\lib\site-packages\dicom\dataset.py", line 376, in _get_pixel_array
raise NotImplementedError("Pixel Data is compressed in a format pydicom does not yet          handle. Cannot return array")
NotImplementedError: Pixel Data is compressed in a format pydicom does not yet handle.        Cannot return array

请建议我将图像转换为 cv2.imshow() 函数以显示图像的好方法

提前致谢!!

【问题讨论】:

    标签: python image-processing dicom pydicom


    【解决方案1】:

    试试 pydicom

    错误的一个原因可能是:使用的 .dcm 文件可能包含不受支持的格式(例如,对于 pydicom,pillow 不支持 JPEG 2000)。这个问题可以解决。我遇到了同样的问题(我使用的是 pydicom 而不是 dicom)我想你会从解决我的问题的解决方案中得到一些指导:

    第一平台信息:

    我正在使用:pydicom 读取 .dcm 文件,Python 3.6,Anaconda 和 Ubuntu,15 GB RAM

    解决办法:

    1. 安装 pydicom 使用以下命令:pip install -U pydicom

    信息可以在这里找到:(链接:https://pydicom.github.io/pydicom/dev/getting_started.html

    1. Anaconda 是必要的。为什么? 请查看 pydicom (https://pydicom.github.io/pydicom/dev/getting_started.html) 的官方文档,其中提到“要安装 pydicom 以及压缩像素数据的图像处理程序,我们鼓励您使用 Miniconda 或 Anaconda”

    2. 如果您使用的是 Ubuntu,请直接打开终端。如果您使用的是 Windows,那么在 Anaconda Navigator 上,从此处启动终端转到 Environment。对其执行以下命令:

      pip install -U git+https://github.com/pydicom/pydicom.git

      conda install pydicom --channel conda-forge

      conda install -c conda-forge gdcm

    交叉检查:

    现在重新启动笔记本,然后尝试使用 pydicom 执行您的代码。它将显示输出。

    另外,您可以使用 Matplotlib 显示如下:

    import matplotlib.pyplot as plt
    import pydicom
    filename = 'IM-0001-0002.dcm'
    ds = pydicom.dcmread(filename)
    plt.imshow(ds.pixel_array, cmap=plt.cm.bone)
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      由于 pydicom 不支持压缩的 dicom 文件,您必须先将其解压缩。 您可以使用GDCM 来执行此操作。

      【讨论】:

        【解决方案3】:

        可以先使用python GDCM绑定解压文件,例如here

        【讨论】:

          【解决方案4】:

          你必须先转换成 RGB,看看单色 dicom 文件: https://github.com/twanmal/dicom_monochrome_to_opencv

          # import the necessary packages
          from imutils import contours
          import scipy
          from skimage import measure
          import numpy as np # numeric library needed
          import pandas as pd #for datafrome
          import argparse # simple argparser
          import imutils
          import cv2  # for opencv image recognising tool
          import dicom
          filename = askopenfilename()
          dicom_file = dicom.read_file(filename) ## original dicom File
          #### a dicom monochrome-2 file has pixel value between approx -2000 and +2000, opencv doesn't work with it#####
          #### in a first step we transform those pixel values in (R,G,B)
          ### to have gray in RGB, simply give the same values for R,G, and B, 
          ####(0,0,0) will be black, (255,255,255) will be white,
          
          ## the threeshold to be automized with a proper quartile function of the pixel distribution
          black_threeshold=0###pixel value below 0 will be black,
          white_threeshold=1400###pixel value above 1400 will be white
          wt=white_threeshold
          bt=black_threeshold
          
          ###### function to transform a dicom to RGB for the use of opencv, 
          ##to be strongly improved, as it takes to much time to run,
          ## and the linear process should be replaced with an adapted weighted arctan or an adapted spline interpolation.
          def DicomtoRGB(dicomfile,bt,wt):
              """Create new image(numpy array) filled with certain color in RGB"""
              # Create black blank image
              image = np.zeros((dicomfile.Rows, dicomfile.Columns, 3), np.uint8)
              #loops on image height and width
              i=0
              j=0
              while i<dicomfile.Rows:
                  j=0
                  while j<dicomfile.Columns:
                      color = yaxpb(dicom_file.pixel_array[i][j],bt,wt) #linear transformation to be adapted
                      image[i][j] = (color,color,color)## same R,G, B value to obtain greyscale
                      j=j+1
                  i=i+1
              return image
          ##linear transformation : from [bt < pxvalue < wt] linear to [0<pyvalue<255]: loss of information... 
          def yaxpb(pxvalue,bt,wt):
              if pxvalue < bt:
                  y=0
              elif pxvalue > wt:
                  y=255
              else:
                  y=pxvalue*255/(wt-bt)-255*bt/(wt-bt)
              return y
          
          
          
          image=DicomtoRGB(dicom_file,bt=0,wt=1400)
          ## loading the RGB in a proper opencv format
          gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
          ## look at the gray file
          cv2.imshow("gray", gray)
          cv2.waitKey(0)
          cv2.destroyWindow("gray")
          

          【讨论】:

          • 这个答案没有任何意义。第一行 dicom_file = dicom.read_file(filename) ## original dicom File 将给出 OP 发布的相同错误。
          猜你喜欢
          • 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
          相关资源
          最近更新 更多