【问题标题】:How to access RGB pixel arrays from DICOM files using pydicom?如何使用 pydicom 从 DICOM 文件访问 RGB 像素阵列?
【发布时间】:2017-03-07 13:57:45
【问题描述】:

我尝试访问具有未知压缩(可能没有)的 DICOM 文件的 RGB 像素阵列。提取灰度像素阵列完全正常。

但是,使用

import dicom
import numpy as np

data_set = dicom.read_file(path)
pixel_array = data_set.pixel_array
size_of_array = pixel_array.shape

if len(size_of_array ) == 3:     
    chanR = pixel_array[0][0:size_of_array[1], 0:size_of_array[2]]
    chanG = pixel_array[1][0:size_of_array[1], 0:size_of_array[2]]
    chanB = pixel_array[2][0:size_of_array[1], 0:size_of_array[2]]
    output_array = (0.299 ** chanR) + (0.587 ** chanG) + (0.114 ** chanB)

目标是将其转换为通用灰度数组。不幸的是,结果数组output_array 不包含正确的像素数据。内容不是错误缩放的,它们在空间上受到干扰。问题出在哪里?

【问题讨论】:

  • 可能是 BGR 而不是 RGB?
  • 不,对应的 DICOM 标签显示为“RGB”,其他模式为“OT”,将患者报告转换为图像格式。结果图像的分辨率和图像大小确实很合适。但每个通道似乎只包含部分空间信息。
  • 什么是pixel_array.shape
  • 我问是因为我用来读取 RGB 图像的其他库(不是 dicom,而是 JPG、PNG 等)通常返回一个形状为 (m, n, 3) 的数组,而不是 (3 , 米, n)。如果是这样,那么你会写chanR = pixel_array[:,:,0]
  • 相应的 DICOM 标签写着“RGB”如果你说的是Photometric Interpretation,那并不意味着顺序。

标签: python numpy dicom pydicom


【解决方案1】:

它不是RGB像素阵列,更好的方法是转换为灰度图像。

CT Image的获取方式是获取CT dicom文件中pixel_array的属性。 CT dicom文件pixel_array中元素的类型都是uint16。但是python中的很多工具,比如OpenCV,一些AI的东西,都不能兼容这种类型。

从CT dicom文件中得到pixel_array(CT Image)后,总是需要将pixel_array转换成灰度图,这样你可以用python中的很多图像处理工具来处理这个灰度图 .

以下代码是将pixel_array转换为灰度图像的工作示例。

import matplotlib.pyplot as plt
import os
import pydicom
import numpy as np 
# Abvoe code is to import dependent libraries of this code

# Read some CT dicom file here by pydicom library
ct_filepath = r"<YOUR_CT_DICOM_FILEPATH>"
ct_dicom = pydicom.read_file(ct_filepath)
img = ct_dicom.pixel_array

# Now, img is pixel_array. it is input of our demo code

# Convert pixel_array (img) to -> gray image (img_2d_scaled)
## Step 1. Convert to float to avoid overflow or underflow losses.
img_2d = img.astype(float)

## Step 2. Rescaling grey scale between 0-255
img_2d_scaled = (np.maximum(img_2d,0) / img_2d.max()) * 255.0

## Step 3. Convert to uint
img_2d_scaled = np.uint8(img_2d_scaled)


# Show information of input and output in above code
## (1) Show information of original CT image 
print(img.dtype)
print(img.shape)
print(img)

## (2) Show information of gray image of it 
print(img_2d_scaled.dtype)
print(img_2d_scaled.shape)
print(img_2d_scaled)

## (3) Show the scaled gray image by matplotlib
plt.imshow(img_2d_scaled, cmap='gray', vmin=0, vmax=255)
plt.show()

以下是我打印出来的结果。

【讨论】:

    【解决方案2】:

    您现在可能已经解决了这个问题,但我认为 pydicom 没有正确解释 planar configuration

    你需要先这样做:

    img = data_set.pixel_array
    img = img.reshape([img.shape[1], img.shape[2], 3])
    

    从这里开始,您的图像将具有形状 [rows cols 3],通道分开

    【讨论】:

    • 非常感谢!我很快就会试一试。实际上我的解决方案是编写一个 ITK 方法。
    【解决方案3】:

    正如@Daniel 所说,因为您有PlanarConfiguration== 1,您必须通过np.reshape 重新排列列中的颜色,然后转换为灰度,例如使用OpenCV

    import pydicom as dicom
    import numpy as np
    import cv2 as cv
    
    data_set = dicom.read_file(path)
    pixel_array = data_set.pixel_array
    ## converting to shape (m,n,3)
    pixel_array_rgb = pixel_array.reshape((pixel_array.shape[1], pixel_array.shape[2], 3))
    ## converting to grayscale
    pixel_array_gs = cv.cvtColor(pixel_array_rgb, cv.COLOR_RGB2GRAY)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多