Loading all images using imread from a given folder

后端 未结 8 1996
长发绾君心
长发绾君心 2021-01-30 21:48

Loading and saving images in OpenCV is quite limited, so... what is the preferred ways to load all images from a given folder? Should I search for files in that folder with .png

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 22:19

    Why not just try loading all the files in the folder? If OpenCV can't open it, oh well. Move on to the next. cv2.imread() returns None if the image can't be opened. Kind of weird that it doesn't raise an exception.

    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
    

提交回复
热议问题