简体   繁体   English

无法通过openCV打开多个图像

[英]Unable to open multiple images through openCV

I am learning image processing for my project, I want to open multiple images from a folder, but the problem is the image files are loaded but when I tried to display it through matplot lib only one image is shown.Code is 我正在为我的项目学习图像处理,我想从一个文件夹中打开多个图像,但是问题是图像文件已加载,但是当我尝试通过matplot lib显示它时仅显示一个图像。

      img_dir=r"D:\UCP\Machine Learning A-Z\facialExp\images"
      valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"] 
      #specify your vald extensions here
      valid_image_extensions = [item.lower() for item in 
      valid_image_extensions]
      image_path_list=[]

      for file in os.listdir(img_dir):
        extension = os.path.splitext(file)[1]
        if extension.lower() not in valid_image_extensions:
         continue
        image_path_list.append(os.path.join(img_dir, file))



      for image_file in image_path_list:
         image=cv2.imread(image_file)

        if image is not None:
          plt.imshow(image, cmap='gray')
        elif image is None:
          print ("Error loading: " + image_file)
          #end this loop iteration and move on to next image
          continue

In first loop the dirrectories of all images are saved in image_path_list, but when I want to plot in second loop only one is plotted. 在第一个循环中,所有图像的目录都保存在image_path_list中,但是当我要在第二个循环中绘制时,仅绘制一个。 kindly suggest I am missing or doing something wrong.. 请暗示我想念或做错了..

You are loading the image variable multiple times, because you are in a for loop. 您正在多次加载image变量,因为您处于for循环中。 So you are basically loading an image on top of the other, and only the last image will show. 因此,您基本上是将一个图像加载到另一个图像之上,并且仅显示最后一个图像。

For your code to work you will need to plot everytime you load the image. 为了使代码正常工作,您每次加载图像时都需要绘图。

Your code probably is just wrongly indented, if you indent one tab the if statement for the plotting, it should work 您的代码可能只是错误地缩进,如果您将一个制表符的if语句缩进一个选项卡,它应该可以工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM