Skip to content Skip to sidebar Skip to footer

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# cal

Solution 1:

Try pydicom

One reason for the error can be: the .dcm file used may contain the format that is not supported (e.g. JPEG 2000 is not supported by pillow in case of pydicom). 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:

1st Platforma Information:

I am using: pydicom to read .dcm files, Python 3.6, Anaconda and Ubuntu, 15 GB RAM

Answer :

  1. Install pydicom using this command: pip install -U pydicom.

Information can be found here: (link: 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"

  2. If you are using Ubuntu directly open Terminal. If you are using Windows then on Anaconda Navigator go to Environment from here start terminal. 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. It will display the output.

Also, you can use Matplotlib to display as follows:

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

I hope it will help you.

Solution 2:

Since pydicom do not support compressed dicom files, you will have to decompress it first. You can use GDCM to do that.

Solution 3:

You can use the python GDCM binding to decompress the file first, see for example here

Solution 4:

you have to convert in RGB before, look at that for a monochrome dicom file: https://github.com/twanmal/dicom_monochrome_to_opencv

# import the necessary packagesfrom imutils import contours
import scipy
from skimage import measure
import numpy as np # numeric library neededimport pandas as pd #for datafromeimport argparse # simple argparserimport imutils
import cv2  # for opencv image recognising toolimport 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.defDicomtoRGB(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=0while i<dicomfile.Rows:
        j=0while 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+1return image
##linear transformation : from [bt < pxvalue < wt] linear to [0<pyvalue<255]: loss of information... defyaxpb(pxvalue,bt,wt):
    if pxvalue < bt:
        y=0elif pxvalue > wt:
        y=255else:
        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")

Post a Comment for "Unable To Get Dicom Image For Display In Python"