首页 > 解决方案 > 如何读取文件夹中的所有图像?

问题描述

我正在尝试执行此代码以使用 python 语言读取文件夹中存在的一组图像,但如果您能帮助我,我会遇到问题

patient_uids = train_annotations.seriesuid.unique()

patients_processed_files = glob.glob(OUTPUT_FOLDER + '[0-9\.]*_X.npy')
patients_processed = set()
for filename in patients_processed_files:
    m = re.match(r'([0-9\.]*)_X.npy', os.path.basename(filename))
    patients_processed.add(m.group(1))

print(len(patient_uids))

graph = tf.Graph()
with graph.as_default():
    img_chunk = tf.placeholder(tf.float32, shape=[CHUNK_SIZE, CHUNK_SIZE, CHUNK_SIZE], name='img_chunk')
    img_flipped_up_down = tf.image.flip_up_down(img_chunk)
    img_flipped_left_right = tf.image.flip_left_right(img_chunk)
    img_rot_90 = tf.image.rot90(img_chunk)
    img_trans = tf.image.transpose_image(img_chunk)

weird_chunks = {}

for patient_uid in patient_uids:
    if patient_uid in patients_processed:
        print('Skipping already processed patient {}'.format(patient_uid))
        continue
    print('Processing patient {}'.format(patient_uid))

    patient_annotations = train_annotations[train_annotations.seriesuid == patient_uid]
    patient_scans_path = glob.glob(DATA_PATH + 'subset?/{}.mhd'.format(patient_uid))[0]
    img, origin, spacing = load_itk(patient_scans_path)

但是我的代码的执行返回了我:

IndexError: list index out of range

标签: pythonpython-3.x

解决方案


我看到您直接使用索引的唯一地方是行

patient_scans_path = glob.glob(DATA_PATH + 'subset?/{}.mhd'.format(patient_uid))[0]

所以这很可能导致您的错误。看起来没有任何路径与您的模式匹配。请直接在该行之前添加

print(len(glob.glob(DATA_PATH + 'subset?/{}.mhd'.format(patient_uid))))

检查有多少元素匹配,如果它是相等0的问题存在。


推荐阅读