I want to read the images from multiple sub-folders, kindly help

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Rik
Rik il 17 Gen 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
saeeda saher
saeeda saher il 17 Gen 2018
Modificato: saeeda saher il 17 Gen 2018
I am getting this error
Conversion to uint8 from cell is not possible.
Error in programm (line 47)
trainSet(:,i) = img(:);

Accedi per commentare.

Risposte (1)

Rik
Rik il 16 Gen 2018
Image analyst has written a function that can do this, see this answer and its attachment.
  2 Commenti
saeeda saher
saeeda saher il 16 Gen 2018
I tried it already but i do not understand how to put my code in it, will you kindly edit my code??
Rik
Rik il 16 Gen 2018
Modificato: Rik il 16 Gen 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

Accedi per commentare.

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by