python - 如何读取文件夹中的所有图像?
问题描述
我正在尝试执行此代码以使用 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
解决方案
我看到您直接使用索引的唯一地方是行
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
的问题存在。
推荐阅读
- python - VS Code 中的 Python 终端与 bash 终端有什么区别?
- python - urllib.request.urlopen 不适用于特定网站
- react-navigation - this.props.navigation.state.params 未定义
- node.js - 正确使用 sequelize distinct 选项(在 postgres 上)
- javascript - 为什么我的数组项被添加到顶部,并且滚动条停留在顶部?
- azure - 使用 StackExchange.Redis 存储到 Azure 托管的 redis 缓存时出现问题
- sql - 将另一个表中的记录连接为 JSON 数组
- angular - 通过router-outlet调用子组件时,如何将数据从父组件传递给子组件?
- webview - 在 SwiftUI 中加载网站时加载活动指示器
- python-3.x - 针对 pandas 列的函数未生成预期输出