how can i read/train all images from subfolders

hello can you help me to modified my code? so i have 3 subfolder in database folder and every subfolder there is 5 pictures on it and i want to read all images in all subfolders and train it.
this is my code that i use in training all images from database folder:
clc;
dn='.\database\';
db=dir(strcat(dn,'*.jpg'));
k=1;
%length(db)
p=1;
for(i=1:1:length(db))
fname=db(i).name;
fname=strcat(dn,fname);
im=imread(fname);
axes(handles.axes1);
imshow(im);
im=rgb2gray(im);
im=imresize(im,[256 256]);
X=double(im);
k=k+1;
f=lbp_sir(X);
plot(f);
Features(:,p)=f;
TrnFile(p).name=fname;
p=p+1;
end;
save features Features TrnFile

 Réponse acceptée

Image Analyst
Image Analyst le 12 Jan 2015

0 votes

See my attached demo that will recurse into all subfolders and get image file names. Then process them in whatever way you want.

13 commentaires

mark
mark le 12 Jan 2015
hello, i tried to apply it in my code but i got some error. can you just help me to modified my code? thankyou so much in advance sorry for spoonfeeding. :)
OK - when I get time. Post your new code.
mark
mark le 12 Jan 2015
ok thankyou so much :) i will wait. i need it as soon as possible :) thanks again :)
mark
mark le 12 Jan 2015
hello sir, are you still there? im sorry i really need it now :(
sir i tried this one. the problem is it will train only the last image in last subfolder.
topLevelFolder = '.\DATABASE\';
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 PNG files.
filePattern = sprintf('%s/*.png', thisFolder);
baseFileNames = dir(filePattern);
% Add on TIF files.
filePattern = sprintf('%s/*.tif', thisFolder);
baseFileNames = [baseFileNames; dir(filePattern)];
% Add on JPG files.
filePattern = sprintf('%s/*.jpg', thisFolder);
baseFileNames = [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);
fprintf(' Processing image file %s\n', fullFileName);
end
else
fprintf(' Folder %s has no image files in it.\n', thisFolder);
end
end
k=1;
%length(db)
p=1;
im=imread(fullFileName);
axes(handles.axes1);
imshow(im);
im=rgb2gray(im);
im=imresize(im,[256 256]);
X=double(im);
k=k+1;
f=lbp_sir(X);
plot(f);
Features(:,p)=f;
TrnFile(p).name=fullFileName;
p=p+1;
save features Features TrnFile
Well, your code is outside the loop! You need to put your algorithm right after the line:
fprintf(' Processing image file %s\n', fullFileName);
sorry for my mistake i tried to put the algorithm in the loop. but i got same result.
clc;
% Ask user to confirm or change.
topLevelFolder ='.\database\';
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 PNG files.
filePattern = sprintf('%s/*.png', thisFolder);
baseFileNames = dir(filePattern);
% Add on TIF files.
filePattern = sprintf('%s/*.bmp', thisFolder);
baseFileNames = [baseFileNames; dir(filePattern)];
% Add on JPG files.
filePattern = sprintf('%s/*.jpg', thisFolder);
baseFileNames = [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);
fprintf(' Processing image file %s\n', fullFileName);
k=1;
p=1;
im=imread(fullFileName);
axes(handles.axes1);
imshow(im);
im=rgb2gray(im);
im=imresize(im,[256 256]);
X=double(im);
k=k+1;
f=lbp_sir(X);
plot(f);
Features(:,p)=f;
TrnFile(p).name=fullFileName;
p=p+1;
save features Features TrnFile
end;
else fprintf(' Folder %s has no image files in it.\n', thisFolder); end end
mark
mark le 12 Jan 2015
please help me. i need to finish it now. :(
Sorry I don't have time to fix it for you now - I need to leave. But p should be k, and don't set it equal to 1 or increment it - the loop counter k is handling that.
And because Features is a 2D array, you'll need to preallocate it before the loop
Features = zeros(10, 1);
The first element should be however many elements lbp_sir() returns instead of 10.
by using this code now. i can now get train the all the images in last subfolder pls check it for the last time. clc;
% Ask user to confirm or change.
topLevelFolder ='.\database\';
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 PNG files.
filePattern = sprintf('%s/*.png', thisFolder);
baseFileNames = dir(filePattern);
% Add on TIF files.
filePattern = sprintf('%s/*.bmp', thisFolder);
baseFileNames = [baseFileNames; dir(filePattern)];
% Add on JPG files.
filePattern = sprintf('%s/*.jpg', thisFolder);
baseFileNames = [baseFileNames; dir(filePattern)];
numberOfImageFiles = length(baseFileNames);
% Now we have a list of all files in this folder.
k=1;
p=1;
if numberOfImageFiles >= 1
% Go through all those image files.
for f = 1:numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
fprintf(' Processing image file %s\n', fullFileName);
im=imread(fullFileName);
axes(handles.axes1);
imshow(im);
im=rgb2gray(im);
im=imresize(im,[256 256]);
X=double(im);
k=k+1;
f=lbp_sir(X);
plot(f);
Features(:,p)=f;
TrnFile(p).name=fullFileName;
p=p+1;
end;
save features Features TrnFile
else fprintf(' Folder %s has no image files in it.\n', thisFolder); end end
Aliff Zin
Aliff Zin le 19 Jan 2017
Hi can i know which part i need to modify in the recursive function? I am planning to implement the HOG feature extraction. Thank you in advanced.
while running your code,the files were not loading in order,if there are 100 images of each 8 samples..then from 1_1,1_2..,1_8....2_1,2_2...2_8...100_1,..100_8 must be loaded....instead of that by your code images were loading starting from 100_1,100_2...next 10_1,10_2...last 9_1..9_8 were loading..there is solution by using natsortfiles function..but i am not knowing where i have to call natsortfile function..please tell me.
I imagine you'd put the call to replace this line:
baseFileNames = dir(filePattern);
which is the line that gets all the filenames in the folder you're looking in at the moment. But actually, it looks like Stephen's function goes into subdirectories, so maybe all you need it his function, not mine.

Connectez-vous pour commenter.

Plus de réponses (3)

Dima Lisin
Dima Lisin le 12 Jan 2015

0 votes

If you have a recent version of MATLAB with the Computer Vision System Toolbox, then you can use imageSet.

2 commentaires

mark
mark le 12 Jan 2015
thankyou so much for your response. can you help me to modified my code ?:(
Dima Lisin
Dima Lisin le 13 Jan 2015
Modifié(e) : Dima Lisin le 13 Jan 2015
imgSets = imageSet('.\database\', 'recursive');
will return an array of imageSet objects.
imgSets(1).Description
will contain the name of the first subfolder.
imshow(read(imgSets(2), 2));
will display the second image in the second folder. Please see the documentation of imageSet for more details.

Connectez-vous pour commenter.

You can use the following code. Cut and paste the whole code from below, insert the path of your main database folder in 'Database Path' section. sourceFiles consists of the paths of individual images which you can read using imread function.[imread(sourceFiles(i).name]. labels which is needed during classification will give you the corresponding labels of individuals images in sourceFiles.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
imgSets = imageSet('Database Path', 'recursive'); m=1;sourceFiles=[];labels=[]; for i=1:length(imgSets) srcFiles=dir(strcat('Database Path\',imgSets(i).Description,'\*.jpg'));
for j=1:length(srcFiles)
srcFiles(j).name = strcat('Database Path\',imgSets(i).Description,'\',srcFiles(j).name);
end
l=length(srcFiles);
label=m*ones(l,1);
sourceFiles=[sourceFiles;srcFiles];
labels=[labels;label];
m=m+1;
end
aras masood
aras masood le 28 Jan 2017

0 votes

hello every on please anyone tell me i have a database for hand recognition i want replace this database to a folder which is consists of a set of images i want use them to train and recognize faces by this folder could anyone tell me how to do such things please ?

1 commentaire

We need further information about what is stored in the database and how the program accesses the data. For example the database might contain the weights of a trained neural network that has already been trained on extracted features and so it might be necessary to do a bunch of computing on your custom images. We do not know, as you do not give enough information.
But instead of replying here you should open a new Question for this and give the details there and then delete this post.

Connectez-vous pour commenter.

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by