【问题标题】:How to load all files from a directory of two different types in MATLAB如何从MATLAB中两种不同类型的目录中加载所有文件
【发布时间】:2015-04-23 15:44:59
【问题描述】:

我知道可以通过以下方式加载.gif 类型的所有文件:

files = dir('C:\myfolder\*.gif');

但是,我的问题是我想加载所有.gif.jpg 类型的文件。这样做的好方法是什么?

【问题讨论】:

    标签: image matlab image-processing


    【解决方案1】:

    您可以简单地搜索两者 .gif.jpg 文件,然后一张一张地加载和处理图像。

    只需调用两次dir - 每种类型的图像调用一次,并将结果存储在两个单独的结构中。接下来,将所有文件名连接到一个结构中,然后继续处理所有图像。

    类似这样的:

    %// Specify the folder where your images are stored
    folder = fullfile('path', 'to', 'your', 'folder');
    
    %// Specify search pattern for JPEG and GIF files
    jpgFileFolder = fullfile(folder, '*.jpg');
    gifFileFolder = fullfile(folder, '*.gif');
    
    %// Invoke dir for both types of images
    d1 = dir(jpgFileFolder);
    d2 = dir(gifFileFolder);
    
    %// Concatenate both dir structures together into a single structure
    d = [d1; d2];
    
    %// For each image we have...
    for idx = 1 : numel(d)
        %// Get full path to file
        f = fullfile(folder, d(idx).name);
    
        %// Read in the image
        im = imread(f);
    
        %// Do something with this image
        %//...
        %//...
    end
    

    fullfile 允许您创建一个独立于操作系统的目录字符串。只需将作为字符串一部分的每个子目录作为单独的字符串参数放入fullfile,它应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 2010-10-17
      • 2012-05-13
      • 2013-06-11
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 2021-08-12
      相关资源
      最近更新 更多