如何在MATLAB中从两种不同类型的目录中加载所有文件 [英] How to load all files from a directory of two different types in MATLAB
问题描述
我知道可以使用以下方式加载 .gif
类型的所有文件:
I know that it is possible to load all files of type .gif
by using:
files = dir('C:\myfolder\*.gif');
但是,我的问题是我想加载类型的所有文件。 gif
和 .jpg
。这样做的好方法是什么?
However, my problem is that I want to load all files of type .gif
and .jpg
. What would be a good way of doing this?
推荐答案
您只需搜索两者 .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.
这篇关于如何在MATLAB中从两种不同类型的目录中加载所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!