Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

image - How to load all files from a directory of two different types in MATLAB

I know that it is possible to load all files of type .gif by using:

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

Something like this:

%// 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 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...