内联显示多张图像

时间:2018-08-27 10:48:57

标签: python python-3.x cv2

我在显示这4张图像时遇到问题,因为以下代码使结果非常混乱:

import cv2
import matplotlib.pyplot as plt

def main():

    path = r"data/dogscats1/pupper"
    path1 = r"data/dogscats1/test1"
    path2 = r"data/dogscats1/peeps"
    path3 = r"data/dogscats1/pupper"

    imgpath1 =  path + "cat.jpg"
    imgpath2 =  path1 + "01.jpg"
    imgpath3 =  path2 + "001.jpg"
    imgpath4 =  path3 + "dog.jpg"

    img1 = cv2.imread(imgpath1, 1)
    img2 = cv2.imread(imgpath2, 1)
    img3 = cv2.imread(imgpath3, 1)
    img4 = cv2.imread(imgpath4, 1)





    titles = ['Kitty', 'Original','Jumble', 'Pupper']
    images = [img1, img2, img3, img4]

    for i in range(4):
        plt.subplot(4,4,i+1)
        plt.imshow(img[i])
        plt.title(titles[i])
        plt.xticks([])
        plt.yticks([])

    plt.show()  

if __name__ == "__main__":
    main()

enter image description here

感谢您提供任何正确显示图像的帮助

1 个答案:

答案 0 :(得分:1)

plt.imshow(img[i])行正在搜索list尚未定义的img,图像当前存储在名为images的列表中,因此您只需需要将此行更改为;

plt.imshow(images[i])

它将返回您需要的内容!