1

I have six folders like this >> Images and each folder contains some images. I know how to read images in matlab BUT my question is how I can traverse through these folders and read images in abc.m file (this file is shown in this image)

ouflak
  • 2,458
  • 10
  • 44
  • 49
Muaz Usmani
  • 1,298
  • 6
  • 26
  • 48

3 Answers3

0

So basically you want to read images in different folders without putting all of the images into one folder and using imread()? Because you could just copy all of the images (and name them in a way that lets you know which folder they came from) into a your MATLAB working directory and then load them that way.

Use the cd command to change directories (like in *nix) and then load/read the images as you traverse through each folder. You might need absolute path names.

Adam27X
  • 889
  • 1
  • 7
  • 16
0

The easiest way is certainly a right clic on the forlder in matlab and "Add to Path" >> "Selected Folders and Subfolders"

Then you can just get images with imread without specifying the path.

Christopher Chiche
  • 15,075
  • 9
  • 59
  • 98
0

if you know the path to the image containing directory, you can use dir on it to list all the files (and directories) in it. Filter the files with the image extension you want and voila, you have an array with all the images in the directory you specified:

dirname = 'images';
ext = '.jpg';

sDir=  dir( fullfile(dirname ,['*' ext]) );;
sDir([sDir.isdir])=[]; % remove directories

% following is obsolete because wildcarded dir ^^
b=arrayfun(@(x) strcmpi(x.name(end-length(ext)+1:end),ext),sDir); % filter on extension
sFiles = sDir(b);

You probably want to prefix the name of each file with the directory before using:

sFileName(ii) = fullfile(dirname, sFiles(ii));

You can process this resulting files as you want. Loading all the files for example:

for ii=1:numel(sFiles)
    data{i}=imread(sFiles(ii).name)
end

If you also want to recurse the subdirectories, I suggest you take a look at:

How to get all files under a specific directory in MATLAB?

or other solutions on the FEX:

http://www.mathworks.com/matlabcentral/fileexchange/8682-dirr-find-files-recursively-filtering-name-date-or-bytes

http://www.mathworks.com/matlabcentral/fileexchange/15505-recursive-dir

EDIT: added Amro's suggestion of wildcarding the dir call

Community
  • 1
  • 1
Gunther Struyf
  • 11,158
  • 2
  • 34
  • 58
  • 1
    instead of manually filtering the files, you could specify something like: `dir('folder\*.ext')`. Here is an example: http://stackoverflow.com/a/7293443/97160 – Amro May 27 '12 at 21:58
  • @Amro: I was thinking that such wildcard didn't work.. I guess I'm confusing it with something else :p thx for the tip! – Gunther Struyf May 27 '12 at 22:41