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

3 次查看(过去 30 天)
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 个评论
Rik
Rik 2018-1-17
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 2018-1-17
编辑:saeeda saher 2018-1-17
I am getting this error
Conversion to uint8 from cell is not possible.
Error in programm (line 47)
trainSet(:,i) = img(:);

请先登录,再进行评论。

回答(1 个)

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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Import, Export, and Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by