【问题标题】:Converting DICOM image to numpy array of shape (s, 3, 256, 256)将 DICOM 图像转换为形状的 numpy 数组(s、3、256、256)
【发布时间】:2020-07-24 10:19:02
【问题描述】:

我的文件夹中包含 MRI 图像,我正在尝试使用我自己的数据复制 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) 或不管每个文件夹有多少片。

有没有人有这方面的经验并且可以提供帮助?

【问题讨论】:

    标签: numpy medical pydicom medical-imaging


    【解决方案1】:

    您可以修改这部分代码:

    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)
    

    到这里:

    unstacked_list = []
    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
        unstacked_list.append(np_pixel_array)
        cv2.imwrite(jpg_filepath, np_pixel_array)
    final_array = np.array(unstacked_list)
    

    下面是一个更简单的场景示例,假设数组 a、b 和 c 是 np_pixel_array 数组,而 final_array 是您想要的格式

    import numpy as np
    unstacked_list = []
    
    a = np.array([[1,2], [3,4]])
    b = np.array([[5,6], [7,8]])
    c = np.array([[9,10], [11,12]])
    
    for i in [a, b, c]:
        unstacked_list.append(i)
    final_array = np.array(unstacked_list)
    
    print(final_array.shape)
    print(f'shape of final_array is {shape}')
    print('')
    print(f'final array is{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]]]
    

    【讨论】:

      猜你喜欢
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      • 2014-03-17
      • 1970-01-01
      • 2019-09-06
      • 2023-03-29
      • 2011-12-20
      相关资源
      最近更新 更多