Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用pydicom从DICOM文件访问RGB像素阵列?_Python_Numpy_Dicom_Pydicom - Fatal编程技术网

Python 如何使用pydicom从DICOM文件访问RGB像素阵列?

Python 如何使用pydicom从DICOM文件访问RGB像素阵列?,python,numpy,dicom,pydicom,Python,Numpy,Dicom,Pydicom,我尝试访问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]

我尝试访问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\u数组
没有包含正确的像素数据。内容不会被错误缩放,而是在空间上受到干扰。问题在哪里

您现在可能已经解决了这个问题,但我认为pydicom的解释不正确

您需要首先执行以下操作:

img = data_set.pixel_array
img = img.reshape([img.shape[1], img.shape[2], 3])
从这里开始,您的图像将具有形状
[rows cols 3]
,通道分开

它不是RGB像素阵列,更好的方法是转换为灰度图像。 获取CT图像的方法是获取CT dicom文件中像素阵列的属性。 CT dicom文件像素_数组中的元素类型都是uint16。但是python中的很多工具,如OpenCV和一些AI工具,都不能与该类型兼容

从CT dicom文件中获取像素阵列(CT图像)后,始终需要将像素阵列转换为灰度图像,以便可以使用python中的大量图像处理工具处理此灰度图像

下面的代码是将像素数组转换为灰度图像的工作示例

导入matplotlib.pyplot作为plt
导入操作系统
导入pydicom
将numpy作为np导入
#Abvoe代码用于导入此代码的依赖库
#阅读pydicom库中的一些CT dicom文件
ct\u文件路径=r“”
ct\u dicom=pydicom.read\u文件(ct\u文件路径)
img=ct\U dicom.pixel\U阵列
#现在,img是像素阵列。它是我们演示代码的输入
#将像素阵列(img)转换为->灰度图像(img\U 2d\U缩放)
##第一步。转换为浮动以避免溢出或下溢损失。
img_2d=img.astype(浮点)
##第二步。在0-255之间重新缩放灰度
img_2d_scaled=(np.max(img_2d,0)/img_2d.max())*255.0
##第三步。转换为uint
img_2d_缩放=np.uint8(img_2d_缩放)
#在上述代码中显示输入和输出信息
##(1)显示原始CT图像信息
打印(img.dtype)
打印(图像形状)
打印(img)
##(2)显示其灰度图像信息
打印(img_2d_scaled.dtype)
打印(img\U 2d\U缩放形状)
打印(img\U 2d\U缩放)
##(3)用matplotlib显示缩放后的灰度图像
plt.imshow(img_2d_缩放,cmap='gray',vmin=0,vmax=255)
plt.show()
下面是我打印出来的结果


正如@Daniel所说,因为你有一个
==1
你必须在列中重新排列颜色,然后转换为灰度,例如使用:


可能是它的BGR而不是RGB?不,相应的DICOM标签上写着“RGB”,另一种是“OT”,它是一份转换成图像格式的患者报告。结果图像的分辨率和图像大小非常适合。但每个通道似乎只包含部分空间信息。什么是
pixel\u array.shape
?我问这个问题是因为我用来读取RGB图像的其他库(不是dicom,而是JPG、PNG等)通常返回一个具有形状(m,n,3),而不是(3,m,n)的数组。如果是这种情况,那么您将编写
chanR=pixel_array[:,:,0]
。如果您谈论的是
光度解释
,则相应的DICOM标签上会显示“RGB”,这并不意味着顺序。非常感谢!我很快就会试一试。实际上,我的解决方案是编写一个ITK方法。
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()
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)