Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
saeeda saher
saeeda saher le 16 Jan 2018
Modifié(e) : saeeda saher le 17 Jan 2018
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 commentaires
Rik
Rik le 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
saeeda saher
saeeda saher le 17 Jan 2018
Modifié(e) : saeeda saher le 17 Jan 2018
I am getting this error
Conversion to uint8 from cell is not possible.
Error in programm (line 47)
trainSet(:,i) = img(:);

Connectez-vous pour commenter.

Réponses (1)

Rik
Rik le 16 Jan 2018
Image analyst has written a function that can do this, see this answer and its attachment.
  2 commentaires
saeeda saher
saeeda saher le 16 Jan 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 le 16 Jan 2018
Modifié(e) : Rik le 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Import, Export, and Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by