编写Dicom图像数组

2024-10-01 00:16:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个dicom图像的文件夹,我把这些图像存储在一个数组中,我想把它们打印到另一个文件夹中。在

我找不到像cv2.imwrite这样写出每个图像的方法

import pydicom
import skimage, os
import numpy as np
FolderPathName = r'FolderPathName'
slices = [pydicom.read_file(FolderPathName + imagename) for imagename in os.listdir(FolderPathName)]
    # Sort the dicom slices in their respective order
slices.sort(key=lambda x: int(x.InstanceNumber))

for x in range(len(slices)): 
    #write the images in a new folder

Tags: thein图像import文件夹foros数组
1条回答
网友
1楼 · 发布于 2024-10-01 00:16:52

方法1: 就你而言, 答案是。。。在

import pydicom
import skimage, os
import numpy as np
FolderPathName = r'FolderPathName'
slices = [pydicom.read_file(FolderPathName + imagename) for imagename in os.listdir(FolderPathName)]
# Sort the dicom slices in their respective order
slices.sort(key=lambda x: int(x.InstanceNumber))
jpg_folder = '' # Set your jpg folder
for idx in range(len(slices)): 
    #write the images in a new folder
    jpg_filepath = os.path.join( jpg_folder, "pic-{}.jpg".format(x) )
    np_pixel_array = slices[idx].pixel_array
    cv2.imwrite(jpg_filepath, np_pixel_array)

方法2: 但是,有更好的方法来处理dicom文件。。。在

^{pr2}$

在上述代码中, 步骤1的重点是文件路径处理。将代码轻松地移植到不同的环境中对您是有好处的。在

步骤2是主要的代码,主要用于任何类型的图像处理。在

相关问题 更多 >