首页 > 解决方案 > 修改大量 DICOM (.dcm) 文件。

问题描述

也许是一个简单的问题,但我没有取得进展,希望能得到帮助。

我有一个大小为 422 的列表。在索引 0 中有 135 个文件路径指向 .dcm 图像。例如 '~/images/0001.dcm','~/images/0135.dcm' 在索引 1 内有 112 个图像路径,索引 2 有 110 个等。

所有图像的大小均为 512 x 512。我希望将它们重新调整为 64 x 64。

这是我第一次同时使用图像和 .dcm 数据,所以我非常不确定如何调整大小。如果您愿意,我也不确定如何访问和修改“内部”列表中的文件。

这样的事情是不是很离谱?

IMG_PX_SIZE = 64 
result = []
for i in test_list:
    result_inner_list = []        
    for image in i:
    # resize all images at index position i and store in list
        new_img = cv2.resize(np.array(image.pixel_array (IMG_PX_SIZE,IMG_PX_SIZE))
        result_inner_list.append(new_img)
    # Once all images at index point i are modified, append them these to a master list.
    result.append(result_inner_list)

标签: pythonnested-listsdicomcv2

解决方案


您似乎在两个问题上苦苦挣扎:

  • 访问文件路径
  • 调整大小

为了你赢,最好把这两个任务分开,下面的示例代码

IMG_PX_SIZE = 64 

def resize(image):
    # your resize code here similar to:        
    # return v2.resize(np.array(image.pixel_array(IMG_PX_SIZE,IMG_PX_SIZE))
    pass

def read(path):
    # your file read operation here
    pass


big_list = [['~/images/0001.dcm','~/images/0135.dcm'],
            ['~/images/0002.dcm','~/images/0136.dcm']]

resized_images = [[resize(read(path)) for path in paths] for paths in big_list]

推荐阅读