将DICOM图像转换为形状(s、3、256、256)的numpy数组

将DICOM图像转换为形状(s、3、256、256)的numpy数组,numpy,medical,pydicom,medical-imaging,Numpy,Medical,Pydicom,Medical Imaging,我有一个文件夹,里面有核磁共振图像,我正试图用我自己的数据复制MRnet研究。他们的模型在每个主题的1.npy文件上工作,形状(s,3,256,256),s是给定主题的切片数(不同主题) 我已经研究了几种不同的方法来解决这个问题,但似乎没有一种适合我。我所得到的最接近的结果是至少使用以下方法将.dcm文件转换为JPEG: import pydicom import os import numpy as np import cv2 dicom_folder = 'C:/Users/GlaDOS/P

我有一个文件夹,里面有核磁共振图像,我正试图用我自己的数据复制MRnet研究。他们的模型在每个主题的1.npy文件上工作,形状(s,3,256,256),s是给定主题的切片数(不同主题)

我已经研究了几种不同的方法来解决这个问题,但似乎没有一种适合我。我所得到的最接近的结果是至少使用以下方法将.dcm文件转换为JPEG:

import pydicom
import os
import numpy as np
import cv2
dicom_folder = 'C:/Users/GlaDOS/PythonProjects/dicomnpy/DICOMFILES/sub1/' # Set the folder of your dicom files that inclued images 
jpg_folder = 'C:/Users/GlaDOS/PythonProjects/dicomnpy/DICOMFILES/jpg' # Set the folder of your output folder for jpg files 
# Step 1. prepare your input(.dcm) and output(.jpg) filepath 
dcm_jpg_map = {}
for dicom_f in os.listdir(dicom_folder):
    dicom_filepath = os.path.join(dicom_folder, dicom_f)
    jpg_f = dicom_f.replace('.dcm', '.jpg') 
    jpg_filepath = os.path.join(jpg_folder,jpg_f)
    dcm_jpg_map[dicom_filepath] = jpg_filepath

# Now, dcm_jpg_map is key,value pair of input dcm filepath and output jpg filepath

# Step 2. process your image by input/output information
for dicom_filepath, jpg_filepath in dcm_jpg_map.items():
    # convert dicom file into jpg file
    dicom = pydicom.read_file(dicom_filepath)
    np_pixel_array = dicom.pixel_array
    cv2.imwrite(jpg_filepath, np_pixel_array)
我知道我可以使用pydicom来实现这一点,但我在他们的文档中找不到关于如何实现这一结果的任何信息

我基本上想要上面代码的
np_pixel_array
中的信息,它返回一个256,216的形状,但是我想要该数组中文件夹中的每个dcm文件,使其成为(30,256,216)或每个文件夹有多少个切片


是否有人对此有经验并能提供帮助?

您可以修改代码的这一部分:

对于dicom_文件路径,dcm_jpg_映射中的jpg_文件路径。items():
#将dicom文件转换为jpg文件
dicom=pydicom.read_文件(dicom_文件路径)
np_像素_阵列=dicom.pixel_阵列
imwrite(jpg_文件路径,np_像素_数组)
为此:

unstacked_list=[]
对于dicom_文件路径,dcm_jpg_map.items()中的jpg_文件路径:
#将dicom文件转换为jpg文件
dicom=pydicom.read_文件(dicom_文件路径)
np_像素_阵列=dicom.pixel_阵列
未堆叠的\u列表。追加(np\u像素\u数组)
imwrite(jpg_文件路径,np_像素_数组)
最终_数组=np.数组(未堆叠的_列表)
下面是一个简单场景的示例,假设数组a、b和c是np_像素_数组,final_数组是您想要的格式

将numpy导入为np
未标记的_列表=[]
a=np.数组([[1,2],[3,4]]
b=np.数组([[5,6],[7,8]]
c=np.数组([[9,10],[11,12]]
因为我在[a,b,c]中:
未加标记的列表。附加(i)
最终_数组=np.数组(未堆叠的_列表)
打印(最终数组形状)
打印(最终_数组的f'形状为{shape}')
打印(“”)
打印(f'最终数组为{final_array}')
输出为

shape of final_array is (3, 2, 2)


final array is

[[[ 1  2]
  [ 3  4]]

 [[ 5  6]
  [ 7  8]]

 [[ 9 10]
  [11 12]]]

您可以修改代码的此部分:

对于dicom_文件路径,dcm_jpg_映射中的jpg_文件路径。items():
#将dicom文件转换为jpg文件
dicom=pydicom.read_文件(dicom_文件路径)
np_像素_阵列=dicom.pixel_阵列
imwrite(jpg_文件路径,np_像素_数组)
为此:

unstacked_list=[]
对于dicom_文件路径,dcm_jpg_map.items()中的jpg_文件路径:
#将dicom文件转换为jpg文件
dicom=pydicom.read_文件(dicom_文件路径)
np_像素_阵列=dicom.pixel_阵列
未堆叠的\u列表。追加(np\u像素\u数组)
imwrite(jpg_文件路径,np_像素_数组)
最终_数组=np.数组(未堆叠的_列表)
下面是一个简单场景的示例,假设数组a、b和c是np_像素_数组,final_数组是您想要的格式

将numpy导入为np
未标记的_列表=[]
a=np.数组([[1,2],[3,4]]
b=np.数组([[5,6],[7,8]]
c=np.数组([[9,10],[11,12]]
因为我在[a,b,c]中:
未加标记的列表。附加(i)
最终_数组=np.数组(未堆叠的_列表)
打印(最终数组形状)
打印(最终_数组的f'形状为{shape}')
打印(“”)
打印(f'最终数组为{final_array}')
输出为

shape of final_array is (3, 2, 2)


final array is

[[[ 1  2]
  [ 3  4]]

 [[ 5  6]
  [ 7  8]]

 [[ 9 10]
  [11 12]]]