read image from different folders?
Show older comments
hi all, i have 20 folders which named with dates (i.e. 1.1.2016, 2.1.2016.....20.1.2016) and each folder has two images, please let me know how can i read each image. it is important for me to read images respectively. for example: read image 1 in day one then image 2 day one, image 1 day 2.....
Accepted Answer
More Answers (1)
Image Analyst
on 4 Feb 2016
I know you already accepted a solution, but here's the code I usually post for questions like this, for whatever it's worth. Change the filepattern to get the file types you want, if they're not jpg:
% Start with a folder and get a list of all subfolders.
% Finds and prints names of all files in
% that folder and all of its subfolders.
% Similar to imageSet() function in the Computer Vision System Toolbox: http://www.mathworks.com/help/vision/ref/imageset-class.html
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define a starting folder.
start_path = fullfile(matlabroot, '\toolbox');
if ~exist(start_path, 'dir')
start_path = matlabroot;
end
% Ask user to confirm or change.
uiwait(msgbox('Pick a starting folder on the next window that will come up.'));
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames)
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
% Get ALL files.
filePattern = sprintf('%s/*.jpg', thisFolder);
baseFileNames = dir(filePattern);
% % Get m files.
% filePattern = sprintf('%s/*.m', thisFolder);
% baseFileNames = dir(filePattern);
% % Add on FIG files.
% filePattern = sprintf('%s/*.fig', thisFolder);
% baseFileNames = [baseFileNames; dir(filePattern)];
% Now we have a list of all files in this folder.
numberOfImageFiles = length(baseFileNames);
if numberOfImageFiles >= 1
% Go through all those files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
fprintf(' Processing file %s\n', fullFileName);
figure; % Comment out this line if you want them all in the same axes.
imshow(fullFileName);
drawnow;
end
else
fprintf(' Folder %s has no files in it.\n', thisFolder);
end
end
By the way, if you're doing image analysis, you should not use jpg if at all possible because of JPG artifact corruption.
6 Comments
Marry M
on 4 Feb 2016
F Sp
on 7 Jun 2020
Thank you, this was extremely helpful for me too!!
Image Analyst
on 7 Jun 2020
Since this question was asked they've added the capability for dir() to go into subfolders. Just use **/*.png instead of */png:
filePattern = fullfile(folder, '**/*.png');
fileList = dir(filePattern); % Get all PNG files in folder and subfolders of that folder.
for k = 1 : length(fileList)
thisFileName = fullfile(fileList(k).folder, fileList(k).name)
% Now process it...
end
Walter Roberson
on 7 Jun 2020
Or for batch construction of the names,
filenames = fullfile( {fileList.folder}, {fileList.name} );
lydie Nsangou
on 25 Mar 2021
thanks a lot it was very helpful
@Image Analyst please is it possible to store all the images in a list or array? later on i would like to pick just a few a them .
Image Analyst
on 25 Mar 2021
Yes, but you may run out of memory:
rgbImage{k, f} = imread(fullFileName); % Store in cell array
imshow(rgbImage{k, f});
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
