I want to read the images from multiple sub-folders, kindly help
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Here is the code which reads images from one folder. but I want to read images from multiple sub-folders. I am new to MATLAB, kindly help me by editing my code
myDir = 'Training\0\';
dd = dir([myDir '*.jpg']);
%%Initilizing the variable for fast loading
trainSet = uint8(zeros(48*48,length(dd)));
for i=1:length(dd)
%%reading the images
img = imread([myDir dd(1).name]);
%%now storing it into one variable
trainSet(:,i) = img(:);
end
10 Kommentare
Rik
am 17 Jan. 2018
You removed some code. If you try to merge code, you need to make sure to understand what the code is doing. Your pre-allocation is now inside a loop, which is never good idea.
% Define a starting folder.
start_path = 'C:\Users\saeeda\Desktop\Training';
% Ask user to confirm or change.
topLevelFolder = 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, ';');%#ok (suppres m-lint warning)
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];%#ok (suppres m-lint warning)
end
numberOfFolders = length(listOfFolderNames);
totalFileList={};%this will contain all file names of jpg files
% 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 JPG files.
filePattern = sprintf('%s/*.jpg', thisFolder);
baseFileNames = dir(filePattern);
numberOfImageFiles = length(baseFileNames);
% Now we have a list of all files in this folder.
if numberOfImageFiles >= 1
% Go through all those image files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
totalFileList{end+1}=fullFileName;%#ok (suppres m-lint warning)
%fprintf(' Processing image file %s\n', fullFileName);
end
else
%fprintf(' Folder %s has no image files in it.\n', thisFolder);
end
end
% Initilizing the variable for fast loading
trainSet = uint8(zeros(48*48,length(totalFileList)));
for i = 1 :length(totalFileList)
img = fullfile(totalFileList, totalFileList{i});
trainSet(:,i) = img(:);
end
Antworten (1)
Rik
am 16 Jan. 2018
2 Kommentare
Rik
am 16 Jan. 2018
Bearbeitet: Rik
am 16 Jan. 2018
These are really minimal edits. You really should be able to do this yourself, especially with code that has this many comment explaining what it is doing.
% Define a starting folder.
start_path = 'Training\';
% Ask user to confirm or change.
topLevelFolder = 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, ';');%#ok (suppres m-lint warning)
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];%#ok (suppres m-lint warning)
end
numberOfFolders = length(listOfFolderNames);
totalFileList={};%this will contain all file names of jpg files
% 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 JPG files.
filePattern = sprintf('%s/*.jpg', thisFolder);
baseFileNames = dir(filePattern);
numberOfImageFiles = length(baseFileNames);
% Now we have a list of all files in this folder.
if numberOfImageFiles >= 1
% Go through all those image files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
totalFileList{end+1}=fullFileName;%#ok (suppres m-lint warning)
%fprintf(' Processing image file %s\n', fullFileName);
end
else
%fprintf(' Folder %s has no image files in it.\n', thisFolder);
end
end
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!