简体   繁体   中英

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

      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. kindly suggest I am missing or doing something wrong..

You are loading the image variable multiple times, because you are in a for loop. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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