【问题标题】:Loading all images using imread from a given folder使用给定文件夹中的 imread 加载所有图像
【发布时间】:2015-07-25 16:07:07
【问题描述】:

在 OpenCV 中加载和保存图像非常有限,所以......从给定文件夹加载所有图像的首选方法是什么?我是否应该在该文件夹中搜索具有 .png 或 .jpg 扩展名的文件,存储名称并将imread 用于每个文件?还是有更好的办法?

【问题讨论】:

  • 这些方法都没有按字母或数字顺序读取文件,例如我有一个图像文件夹,其中包含来自1.png...150.png 的图像,但它们似乎是随机读取的。跨度>

标签: python opencv


【解决方案1】:

为什么不尝试加载文件夹中的所有文件?如果OpenCV打不开,哦。继续下一个。如果图像无法打开,cv2.imread() 返回None。有点奇怪,它没有引发异常。

import cv2
import os

def load_images_from_folder(folder):
    images = []
    for filename in os.listdir(folder):
        img = cv2.imread(os.path.join(folder,filename))
        if img is not None:
            images.append(img)
    return images

【讨论】:

    【解决方案2】:

    我使用了 skimage。您可以使用标准方式创建集合并访问元素,即 col[index]。这将为您提供 RGB 值。

    from skimage.io import imread_collection
    
    #your path 
    col_dir = 'cats/*.jpg'
    
    #creating a collection with the available images
    col = imread_collection(col_dir)
    

    【讨论】:

      【解决方案3】:
      import glob
      cv_img = []
      for img in glob.glob("Path/to/dir/*.jpg"):
          n= cv2.imread(img)
          cv_img.append(n)`
      

      【讨论】:

      • 这可行,但文件没有任何特定的顺序。
      • 只需按 list = sort(glob.glob("Path/to/dir/*.jpg"))) 排序即可。
      【解决方案4】:

      如果所有图片格式相同:

      import cv2
      import glob
      
      images = [cv2.imread(file) for file in glob.glob('path/to/files/*.jpg')]
      

      用于读取不同格式的图像:

      import cv2
      import glob
      
      imdir = 'path/to/files/'
      ext = ['png', 'jpg', 'gif']    # Add image formats here
      
      files = []
      [files.extend(glob.glob(imdir + '*.' + e)) for e in ext]
      
      images = [cv2.imread(file) for file in files]
      

      【讨论】:

      • 一旦我实例化了图像(images = [cv2.imread(file) for file in glob.glob('path/to/files/*.jpg')])它们如何显示在窗户?
      • for im in images: plt.figure() plt.imshow(im)
      【解决方案5】:

      您可以使用 glob 函数来执行此操作。看例子

      import cv2
      import glob
      for img in glob.glob("path/to/folder/*.png"):
          cv_img = cv2.imread(img)
      

      【讨论】:

        【解决方案6】:

        您也可以为此使用matplotlib,试试这个:

        import matplotlib.image as mpimg
        
        def load_images(folder):
            images = []
            for filename in os.listdir(folder):
                img = mpimg.imread(os.path.join(folder, filename))
                if img is not None:
                    images.append(img)
            return images
        

        【讨论】:

        • @Nirmal 是的,其他文件格式也受支持,但您需要安装 Pillow
        【解决方案7】:
        import os
        import cv2
        rootdir = "directory path"
        for subdir, dirs, files in os.walk(rootdir):
            for file in files:
                frame = cv2.imread(os.path.join(subdir, file)) 
        
        

        【讨论】:

        • 虽然此代码可能会为 OP 的问题提供解决方案,但最好添加有关其工作原理/方式的上下文。这可以帮助未来的用户学习并将这些知识应用到他们自己的代码中。解释代码时,您也可能会以赞成票的形式从用户那里获得积极的反馈。
        【解决方案8】:

        添加到 Rishabh 的答案并使其能够处理不是文件夹中找到的图像的文件。

        import matplotlib.image as mpimg
        
        images = []
        folder = './your/folder/'
        for filename in os.listdir(folder):
            try:
                img = mpimg.imread(os.path.join(folder, filename))
                if img is not None:
                    images.append(img)
            except:
                print('Cant import ' + filename)
        images = np.asarray(images)
        

        【讨论】:

          【解决方案9】:
          import cv2 as cv
          import glob
          
          images = glob.glob("*.jpg")
          for image in images:
              img = cv.imread(image, 1)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-05-28
            • 2011-12-09
            • 2012-06-24
            • 2014-08-26
            • 1970-01-01
            • 1970-01-01
            • 2020-07-23
            • 1970-01-01
            相关资源
            最近更新 更多