1

I have a J2k Lossless dicom image and upon reading it with pydicom I see that it's in the YBR_RCT Color Space.

I want to convert the Color Space to RGB. I tried using the convert_color_space method from pydicom however, apparently this conversion is not implemented.

In their documentation they mention this link as reference for their implementation of other conversions. That link also mentions some equations for converting from YBR_RCT to RGB.

I implemented the equations like so:

rgb_arr = np.zeros(orig_arr.shape).astype(np.float)
g = orig_arr[:,:,0] - np.floor((orig_arr[:,:,1]+orig_arr[:,:,2])/4)
r = g + orig_arr[:,:,2]
b = g + orig_arr[:,:,1]
rgb_arr[:,:,0] = r
rgb_arr[:,:,1] = g
rgb_arr[:,:,2] = b
rgb_arr = np.clip(rgb_arr,0,255).astype(orig_arr.dtype)

However, after this when I visualize the image through plt.imshow(), I see that it's very purplish. I doubt what I have done is right and I haven't really found another package that does this conversion.

I have tried using SimpleITK, however its not clear to me if it does the color space conversion or not.

Please let me know if you know how to do this conversion.

4

3 回答 3

2

没有数据集很难判断,但鉴于 RCT 旨在无损可逆,以下内容应该足够了(不需要剪辑):

from pydicom import dcmread
import numpy as np

ds = dcmread("path/to/file")
arr = ds.pixel_array

rgb = np.empty(arr.shape, dtype=arr.dtype)
rgb[..., 1] = arr[..., 0] - np.floor((arr[..., 1] + arr[..., 2]) / 4)
rgb[..., 0] = arr[..., 2] + rgb[..., 1]
rgb[..., 2] = arr[..., 1] + rgb[..., 1]

但是当使用pydicom时,这一步不是必需的,因为各种像素数据处理程序都在解码时隐式执行 RCT 到 RGB 转换。

于 2021-03-08T00:39:18.663 回答
1

我也有同样的问题。使用链接中的公式会产生奇怪的彩色图片,我不知道为什么,但如果我将 YBR_RCT 光度解释文件视为 RGB,它将产生可接受的颜色(虽然不太亮......)

例如:将 dicom 文件加载为 'RGB'

ds = pydicom.dcmread(file_path, force=True)
pixel_array = ds.pixel_array.astype(float) 

靠枕头展示,简单

from PIL import Image
display(Image.fromarray(np.uint8(pixel_array)))

要通过opencv保存图像,我们必须将pixel_array转换为BGR

import cv2
cv2.imwrite('E:\\test.jpg', cv2.cvtColor(np.float32(pixel_array), cv2.COLOR_RGB2BGR))
于 2021-06-04T09:38:45.890 回答
0

您应该考虑使用 SimpleITK。它建立在 GDCM 之上。GDCM 将确保将 JPEG 2000 码流/YBR_RCT 解压缩到 RGB 空间中适当的解压缩缓冲区。

请注意,这是 DICOM 标准规定的:

如果像素数据 (7FE0,0010) 以原生格式发送,则光度解释 (0028,0004) 应不同于:

YBR_RCT

于 2021-03-08T07:58:18.303 回答