使用给定文件夹中的imread加载所有图像 [英] Loading all images using imread from a given folder

查看:113
本文介绍了使用给定文件夹中的imread加载所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 or .jpg extensions, store the names and use imread with every file? Or is there a better way?

推荐答案

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

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

这篇关于使用给定文件夹中的imread加载所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆