首页 > 解决方案 > 使用 cv2 和 Matplotlib 从 8 个子文件夹中打印 5 张图像

问题描述

我正在尝试显示每个子文件夹中的 5 张图像,这是图像类。我的代码仅显示 5 个头等舱 (AK) 的图像。我还将图像的标题显示到其对应的类。请参考下面的代码。

label = ['AK', 'BCC','BKL','DF','MEL','NV','SCC','VASC']
label_images = []
classes = [ 'Actinic Keratoses', 'Basal cell carcinoma', 'Benign Keratosis-like lesions', 
           'Dermatofibroma','Melanoma', 'Melanocytic nevi', 'Squamous cell carcinoma' , 'Vascular lesions']

fig = plt.figure(figsize=(20, 20))
for l in label:
  dir_path = os.path.join(path,l)
  for i,file in enumerate(os.listdir(dir_path)[0:5]):
    fullpath = dir_path+ "/" + file
    img=cv2.imread(fullpath)
    plt.subplot(8, 5, i+1)
    plt.imshow(img)
    plt.axis('off')
    if i%5 == 0:
        title = int(i/5)
        plt.title(classes[title], loc='left', size=20)
plt.tight_layout()
plt.show()

标签: pythonmatplotlibcv2

解决方案


推荐阅读