简体   繁体   English

无法获取dicom图像以便在python中显示

[英]Unable to get dicom image for display in python

I'm trying to display a DICOM image in opencv-python.I am using the pydicom library,And then adding API's to create a full fledged DICOM viewer with DOTNET, that runs python(C# calls python with process instance of course!!). 我正在尝试在opencv-python中显示DICOM图像。我正在使用pydicom库,然后添加API来创建一个完整的DICNET DICOM查看器,它运行python(当然,C#调用python与流程实例!!) 。 I am unable to convert or see the uncompressed DICOM image. 我无法转换或看到未压缩的DICOM图像。 whenever i try to load or modify the pixel_array. 每当我尝试加载或修改pixel_array时。 I get error messges. 我得到错误消息。

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

Please suggest me good way to convert the image making cv2.imshow() function o display the image 请建议我转换图像制作cv2.imshow()函数的好方法o显示图像

Thanks in Advance!! 提前致谢!!

Since pydicom do not support compressed dicom files, you will have to decompress it first. 由于pydicom不支持压缩的dicom文件,因此您必须先解压缩它。 You can use GDCM to do that. 您可以使用GDCM来执行此操作。

您可以使用python GDCM绑定首先解压缩文件,例如,请参见此处

Try pydicom 尝试pydicom

One reason for the error can be: the .dcm file used may contain the format that is not supported (eg JPEG 2000 is not supported by pillow in case of pydicom). 出错的一个原因可能是:使用的.dcm文件可能包含不支持的格式(例如,在pydicom的情况下枕头不支持JPEG 2000)。 This issue can be solved. 这个问题可以解决。 I was having the same issue (I am using pydicom instead of dicom) I guess you will get some direction from the solution that solved my problem: 我遇到了同样的问题(我正在使用pydicom而不是dicom)我想你会从解决我问题的解决方案中找到一些方向:

1st Platforma Information: 第一平台信息:

I am using: pydicom to read .dcm files, Python 3.6, Anaconda and Ubuntu, 15 GB RAM 我正在使用:pydicom读取.dcm文件,Python 3.6,Anaconda和Ubuntu,15 GB RAM

Solution: 解:

  1. Install pydicom using this command: pip install -U pydicom . 使用以下命令安装pydicompip install -U pydicom

Information can be found here: (link: https://pydicom.github.io/pydicom/dev/getting_started.html ) 信息可以在这里找到:(链接: https//pydicom.github.io/pydicom/dev/getting_started.html

  1. Anaconda is necessary. 蟒蛇是必要的。 Why? 为什么? Please check the official doc of pydicom ( https://pydicom.github.io/pydicom/dev/getting_started.html ) its mentioned "To install pydicom along with image handlers for compressed pixel data, we encourage you to use Miniconda or Anaconda" 请查看pydicom的官方文档( https://pydicom.github.io/pydicom/dev/getting_started.html ),其中提到“要安装pydicom以及压缩像素数据的图像处理程序,我们建议您使用Miniconda或Anaconda”

  2. If you are using Ubuntu directly open Terminal. 如果您正在使用Ubuntu直接打开终端。 If you are using Windows then on Anaconda Navigator go to Environment from here start terminal. 如果您使用的是Windows,那么在Anaconda Navigator上从这里开始终端转到Environment。 Execute the following commands on it: 执行以下命令:

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

    conda install pydicom --channel conda-forge

    conda install -c conda-forge gdcm

Cross Check: 交叉检查:

Now restart the notebook and then try to execute your code using pydicom. 现在重新启动笔记本,然后尝试使用pydicom执行代码。 It will display the output. 它将显示输出。

Also, you can use Matplotlib to display as follows: 此外,您可以使用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)

I hope it will help you. 我希望它会对你有所帮助。

you have to convert in RGB before, look at that for a monochrome dicom file: https://github.com/twanmal/dicom_monochrome_to_opencv 你必须先转换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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM