在保持文件夹结构的同时读取图像 [英] Reading images while maintaining folder structure

查看:127
本文介绍了在保持文件夹结构的同时读取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用python编写matlab脚本,因为我想实现的目标显然是在python中更有效地完成的.

I have to write a matlab script in python as apparently what I want to achieve is done much more efficiently in Python.

因此,第一个任务是在保持文件夹结构的同时,使用opencv将所有图像读入python.例如,如果父文件夹有50个子文件夹,而每个子文件夹有10张图像,则images变量在python中应该是这样,非常像matlab中的单元格.我读到python列表可以在不导入任何内容的情况下执行类似于单元格的行为,所以我猜这很好.

So the first task is to read all images into python using opencv while maintaining folder structure. For example if the parent folder has 50 sub folders and each sub folder has 10 images then this is how the images variable should look like in python, very much like a cell in matlab. I read that python lists can perform this cell like behaviour without importing anything, so thats good I guess.

例如,以下是我在Matlab中进行编码的方式:

For example, below is how I coded it in Matlab:

path = '/home/university/Matlab/att_faces';

subjects = dir(path);
subjects = subjects(~strncmpi('.', {subjects.name}, 1)); %remove the '.' and '..' subfolders
img = cell(numel(subjects),1); %initialize the cell equal to number of subjects

for i = 1: numel(subjects)
    path_now = fullfile(path, subjects(i).name);
    contents = dir([path_now, '/*.pgm']);
    for j = 1: numel(contents)
        img{i}{j} = imread(fullfile(path_now,contents(j).name));
        disp([i,j]);
    end
end

上面的img将有50个单元格,每个单元格将存储10张图像. img{1}将是属于主题1的所有图像,依此类推.

The above img will have 50 cells and each cell will have stored 10 images. img{1} will be all images belonging to subject 1 and so on.

我正在尝试在python中复制它,但是失败了,这就是我到目前为止所得到的:

Im trying to replicate this in python but am failing, this is what I have I got so far:

import cv2
import os
import glob


path = '/home/university/Matlab/att_faces'

sub_f = os.listdir(path)
images = []
for n in sub_f:
    path_now = os.path.join(path, sub_f[n], '*.pgm')
    images[n] = [cv2.imread(file) for file in glob.glob(path_now)]

这并非我所要寻找的,有些帮助将不胜感激.请忽略愚蠢的错误,因为这是我用python编写的第一天.

Its not exactly what I am looking for, some help would be appreciated. Please ignore silly mistakes as it is my first day writing in python.

谢谢

目录结构:

推荐答案

第一个问题是n不是数字或索引,它是包含路径名的字符串.要获取索引,可以使用enumerate,它给出indexvalue对.

The first problem is that n isn't a number or index, it is a string containing the path name. To get the index, you can use enumerate, which gives index, value pairs.

第二,与在MATLAB中不同,您不能将其分配给不存在的索引.您需要预先分配图像数组,或者更好的是,将其追加到该数组.

Second, unlike in MATLAB you can't assign to indexes that don't exist. You need to pre-allocate your image array or, better yet, append to it.

第三,最好不要使用变量file,因为在python 2中,它是内置数据类型,因此可能会使人感到困惑.

Third, it is better not to use the variable file since in python 2 it is a built-in data type so it can confuse people.

因此,通过预分配,这应该可以工作:

So with preallocating, this should work:

images = [None]*len(sub_f)
for n, cursub in enumerate(sub_f):
    path_now = os.path.join(path, cursub, '*.pgm')
    images[n] = [cv2.imread(fname) for fname in glob.glob(path_now)]

使用附加,这应该可以工作:

Using append, this should work:

for cursub in sub_f
    path_now = os.path.join(path, cursub, '*.pgm')
    images.append([cv2.imread(fname) for fname in glob.glob(path_now)])

话虽这么说,但有一种更简单的方法可以做到这一点.您可以使用 pathlib 模块来简化此操作.

That being said, there is an easier way to do this. You can use the pathlib module to simplify this.

所以这样的事情应该起作用:

So something like this should work:

from pathlib import Path

mypath = Path('/home/university/Matlab/att_faces')
images = []

for subdir in mypath.iterdir():
    images.append([cv2.imread(str(curfile)) for curfile in subdir.glob('*.pgm')])

这会遍历子目录,然后遍历每个子目录.

This loops over the subdirectories, then globs each one.

这甚至可以在嵌套列表理解中完成:

This can even be done in a nested list comprehension:

images = [[cv2.imread(str(curfile)) for curfile in subdir.glob('*.pgm')]
          for subdir in mypath.iterdir()]

这篇关于在保持文件夹结构的同时读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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