且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在MATLAB中从两种不同类型的目录中加载所有文件

更新时间:2023-11-13 21:58:40

您只需搜索两者 .gif .jpg 文件然后逐个加载和处理图像。

You can simply search for both .gif and .jpg files then load and process the images one by one.

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

Just invoke dir twice - one for each type of image and store the results in two separate structures. Next, concatenate all of the file names to one structure, then go ahead and do your processing for all of the images.

这样的事情:

%// 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 中,它应该可以正常工作。

fullfile allows you to create a directory string that is OS independent. Simply take each subdirectory that is part of your string and place them as separate string arguments into fullfile and it should work fine.