将 DICOM 图像转换为 numpy 形状数组 (s, 3, 256, 256)
Converting DICOM image to numpy array of shape (s, 3, 256, 256)
我有包含 MRI 图像的文件夹,我正在尝试用我自己的数据复制 MRnet 研究。他们的模型适用于每个主题 1 个 .npy 文件,形状为 (s, 3, 256, 256),其中 s 是给定主题的切片数(因主题而异)。
我研究了几种不同的方法来解决这个问题,但 none 似乎对我有用。我最接近的是至少使用以下方法将 .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
中的信息,returns 形状为 256、216,但是我想要该数组中的文件夹中的每个 dcm 文件,所以它会变成(30, 256, 216) 或者每个文件夹有多少个切片。
有没有人有这方面的经验并且可以提供帮助?
您可以修改这部分代码:
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]]]
我有包含 MRI 图像的文件夹,我正在尝试用我自己的数据复制 MRnet 研究。他们的模型适用于每个主题 1 个 .npy 文件,形状为 (s, 3, 256, 256),其中 s 是给定主题的切片数(因主题而异)。
我研究了几种不同的方法来解决这个问题,但 none 似乎对我有用。我最接近的是至少使用以下方法将 .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
中的信息,returns 形状为 256、216,但是我想要该数组中的文件夹中的每个 dcm 文件,所以它会变成(30, 256, 216) 或者每个文件夹有多少个切片。
有没有人有这方面的经验并且可以提供帮助?
您可以修改这部分代码:
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]]]