python - 如何从图像测试文件夹中随机显示测试图像
问题描述
我正在使用 Haralick 纹理来解决医学图像分类问题。我想遍历带有未标记图像的测试文件夹,并将它们打印在带有预测标签的 jupyter 笔记本中。
cv2.imshow()
将输出一个随机图像来显示,但是,当我plt.imshow()
用来在 jupyter 笔记本中显示时,会返回相同的图像。
# loop over the test images
test_path = 'data/test/test'
for file in glob.glob(test_path + "/*.jpeg"):
# read the input image
image = cv2.imread(file)
# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# extract haralick texture from the image
features = extract_features(gray)
# evaluate the model and predict label
prediction = clf_svm.predict(features.reshape(1, -1))[0]
# show the label
cv2.putText(image, prediction, (20,30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,255), 3)
# display the output image
#cv2.imshow("Test_Image", image)
#cv2.waitKey(0)
# display the output image in notebook
plt.imshow(image)
plt.show()
使用 pyplot 返回相同的图像,我想从测试文件夹返回所有(或随机子集)图像。
如果有什么不清楚的,我会澄清的。谢谢你。
解决方案
核心问题是你的循环。您没有在循环期间制表 - 您正在使用一个变量。循环访问的最后一项将基于image
、gray
和。features
然后在这些单个项目的循环之外完成额外的处理,就像输出一样。
您可能希望将所有处理都带入 for 循环,或者您希望将项目存储在某种列表中,然后对循环项目进行进一步处理,可能会暂停以查看不同的输出。
推荐阅读
- android - 如何在单元测试中读取 Android 资源文件值
- dart - 扑动的列表视图中的无限高
- rust - 尝试使用 Serde 反序列化对象数组时出现“无效类型:映射,需要一个序列”
- python - DictWriter 抛出 UnicodeEncodeError
- localhost - localhost 在端口 8080 上请求凭据
- asp.net - 网站首次加载时模型返回 null
- python - 如何在 Python 3 中从文本文件中创建字符串?
- perl - 输出重定向无法通过 Windows 任务计划程序运行 Perl 程序
- r - 保持最大的重复观察
- javascript - 如何在 AJAX 响应中获取简单的文本