问题描述
我正在尝试将图形背景设置为dicom图像.我遵循了此示例,但是图像数据来自dicom.pixel_array
不是RGBA.我也不知道如何转换它.我也不确定散景到底是什么.我试过在文档中查找详细信息,但运气还不是这样.
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)
此代码不会给我任何错误,但不会显示任何内容.也许像素阵列没有alpha数据,所以alpha
默认为0
吗?我不知道.另外,我不太清楚如何测试它.
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.
已解决
如前所述,我只需要将像素数据映射到rgba空间.在这种情况下,这意味着将数据复制到每个通道,并始终设置alpha.
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
推荐答案
您需要做的是将从pixel_array返回的像素数据映射到RGB空间.通常,这是使用查找表(LUT)完成的.看看 dicomparser 中的函数GetImage和GetLUTValue > dicompyler-core 库中的模块.
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.
在GetLUTValue中,它将数据映射到8位灰度图像.如果要使用其他LUT,则需要相应地映射色彩空间.
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.
这篇关于使用散景查看dicom图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!