简体   繁体   中英

Viewing dicom image with Bokeh

I'm trying to set the graph background to a dicom image. I followed this example , but the image data given from dicom.pixel_array isn't RGBA. I'm not sure how to convert it, either. I'm also not sure what exactly bokeh is expecting. I've tried finding specifics in the documentation, but not such luck.

from bokeh.plotting import figure, show, output_file
import dicom
import numpy as np


path = "/pathToDicomImage.dcm"
data = dicom.read_file(path)
img = data.pixel_array

p = figure(x_range=(0,10), y_range=(0,10))

# must give a vector of images
p.image_rgba(image=[img], x=0, y=0, dw=10, dh=10)

output_file("image_rgba.html", title="image_rgba.py example")

show(p) 

This code doesnt give me any errors, but it doesn't display anything. Maybe the pixel array doesn't have alpha data, so alpha defaults to 0 ? I'm not sure. Also, I can't quite figure out how to test it.

SOLVED

As was pointed out, I just needed to map the pixel data to rgba space. for this instance, it means duplicating the data to each channel, and setting alpha all the way.

def dicom_image_to_RGBA(image_data):
    rows = len(image_data)
    cols = rows
    img = np.empty((rows,cols), dtype=np.uint32)
    view = img.view(dtype=np.uint8).reshape((rows, cols, 4))
    for i in range(0,rows):
        for j in range(0,cols):
            view[i][j][0] = image_data[i][j]
            view[i][j][1] = image_data[i][j]
            view[i][j][2] = image_data[i][j]
            view[i][j][3] = 255
    return img

Not being an expert in python, I have had a glance at pydicom's capabilities in handling pixel data. I figured out that pixel_array is the value of the pixel-data attribute of the DICOM dataset as is and pydicom does not offer any functionality to convert it into some standard format which can be handled uniformly. This means you will have to convert it to RGB in most cases which is a quite compilcated and error-prone task.

Things to consider in this:

  • The encoding (Big/Little Endian, various compression methods like JPEG, JPEG-LS, RLE, ZIP) - DICOM attribute (0002,0010) TransferSyntaxUID
  • The type of pixeldata (Grayscale, RGB, ...) - DICOM attribute (0028,0004) PhotometricInterpretation, (0028,0103) PixelRepresentation
  • In case of color images: are the values encoded colur by plane (RRRRR,.....GGGGG,.....BBBBB) or colour by pixel as you expect it to be (RGB RGB...)
  • The bit depth and which bits are used for actual pixel data values (0028,0100) BitsAllocated, (0028,0101) BitsStored, (0028,0102) Highbit.
  • are the pixel data values really the values to be displayed or are they indices to a colour/grayscale lookup table (0028,3000) ModalityLUTSequence, (0028,3002) LUTDescriptor, (0028,3003) LUTExplanation, (0028,3004) ModalityLUTType, (0028,3006) LUTData.

Scary, isn't it? For some modern image classes like Enhanced MR, there is even more than that.

However, if you constrain to a particular type of image (eg Computed Radiography). limitations to the above mentioned apply that make your life a bit easier.

If you would post a DICOM dump of the image header I could give you some hints how to display that particular image.

HTH

kritzel

What you need to do is map the pixel data returned from pixel_array to RGB space. Usually that is done using a look up table (LUT). Take a look at the functions GetImage and GetLUTValue in the dicomparser module in the dicompyler-core library.

In GetLUTValue it maps the data to an 8-bit greyscale image. If you want to use a different LUT, you would need to map the color space accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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