如何从图像测试文件夹中随机显示测试图像

How to display test images at random from image test folder

我正在使用 Haralick 纹理解决医学图像分类问题。我想遍历带有未标记图像的测试文件夹,并将它们打印在带有预测标签的 jupyter 笔记本中。

cv2.imshow() 将输出随机图像进行显示,但是,当我使用 plt.imshow() 在 jupyter notebook 中显示时,相同的图像是 returned.

# 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 对同一图像进行 return 编辑,我想 return 测试文件夹中的所有(或随机子集)图像。

有什么不明白的地方,我会澄清的。谢谢。

example image

核心问题是你的循环。您在循环期间没有制表 - 您使用的是一个变量。循环访问的最后一项将是 imagegrayfeatures 全部基于的项。

然后在循环外对这些单个项目进行附加处理,输出也是如此。

您可能希望将所有处理都放在 for 循环中,或者您希望将项目存储在某种列表中,然后对循环项目进行进一步处理,可能会暂停查看不同的输出。