【问题标题】:Iterate through Files, loading images - Matlab遍历文件,加载图像 - Matlab
【发布时间】:2014-04-08 01:42:27
【问题描述】:

我需要加载大约 1000 张图像作为面部识别程序的训练数据。

有 100 个人,每个人有 10 张独特的照片。

保存在如下文件夹中:

    myTraining  //main folder
         - John // sub folder
               - John_Smith_001, John_Smith_002, ... , 00n, //images
         - Mary // sub folder
               - Mary_Someone_001... you get the idea :)

我熟悉很多 matlab,但不熟悉遍历外部文件的方法。

一个简单的实现每个文件夹并加载图像,理想情况下使用检索文件名并将它们用作变量/图像名称。

提前致谢。

【问题讨论】:

标签: matlab file-io iteration


【解决方案1】:

使用以下命令将递归列出特定目录及其子目录中的所有文件。我已经为 Windows 和 Mac/Linux 列出了它。不幸的是,我无法测试 Mac/Linux 版本,因为我不在这些机器附近,但它与下面写的非常相似。

Windows

[~,result] = system('dir C:\Users\username\Desktop /a-d /s /b');
files = regexp(result,'\n','Split')

Mac/Linux

[~,result] = system('find /some/Directory -type file);
files = regexp(result,'\n','Split')

然后您可以遍历创建的元胞数组files,并使用imread 或类似的方式执行您可能需要的任何加载

【讨论】:

    【解决方案2】:

    你可以这样做:

    basePath = pwd;  %your base path which is in your case myTraining  
    allPaths = dir(basePath);  %get all directory content
    subFolders = [allPaths(:).isdir]; %get only indices of folders
    foldersNames = {allPaths(subFolders).name}'; % filter folders names
    foldersNames(ismember(foldersNames,{'.','..'})) = []; %delete default paths for parents return '.','..'
    for i=1:length(foldersNames), %loop through all folders
        tmp = foldersNames{i};  %get folder by index
        p = strcat([basePath '\']); 
        currentPath =strcat([p tmp]); % add base to current folder
        cd(currentPath);   % change directory to new path
        files = dir('*.jpg'); % list all images in your path which in your case could be John or Mary 
        for j=1:length(files), % loop through your images 
            img = imread(files(j).name); % read each image and do what you want 
        end
    end
    

    【讨论】:

    • 啊,我想这就是我一直在寻找的东西。感谢您抽出宝贵时间发帖。
    • 做需要做的事情似乎有点冗长,你不觉得吗?如果操作系统已经内置了这个功能,为什么还要重新发明轮子?
    【解决方案3】:

    对于 jpg 图像,它会是

    files = dir('*.jpg');
    for file = files'
        img = imread(file.name);
        % Do some stuff
    end
    

    如果你有多个扩展使用

    files = [dir('*.jpg'); dir('*.gif')]
    

    希望对你有帮助

    【讨论】:

    • 感谢发帖。很高兴知道。
    猜你喜欢
    • 2017-08-17
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    相关资源
    最近更新 更多