How to read multiple text.jpg images in a loop?
2 views (last 30 days)
Show older comments
i saved the images as text.jpg,text(1).jpg,text(2).jpg as indexes in a folder...while i running this following code in which the loop starts with i=1 to trainnumber which shows the error ..
function T = CreateDatabase(~)
% Align a set of face images (the training set T1, T2, ... , TM )
%
% Description: This function reshapes all 2D images of the training database
% into 1D column vectors. Then, it puts these 1D column vectors in a row to
% construct 2D matrix 'T'.
%
%
% Argument: TrainDatabasePath - Path of the training database
%
% Returns: T - A 2D matrix, containing all 1D image vectors.
% Suppose all P images in the training database
% have the same size of MxN. So the length of 1D
% column vectors is MN and 'T' will be a MNxP 2D matrix.
%
% See also: STRCMP, STRCAT, RESHAPE
% Original version by Amir Hossein Omidvarnia, October 2007
% Email: aomidvar@ece.ut.ac.ir
%%%%%%%%%%%%%%%%%%%%%%%% File management
TrainFiles = dir('C:\Users\HP\Documents\MATLAB\Attendance\TrainDatabase\');
Train_Number = 0;
for i = 1:size(TrainFiles,1)
if not(strcmp(TrainFiles(i).name,'.')|strcmp(TrainFiles(i).name,'..')|strcmp(TrainFiles(i).name,'Thumbs.db'))
Train_Number = Train_Number + 1; % Number of all images in the training database
end
end
%%%%%%%%%%%%%%%%%%%%%%%% Construction of 2D matrix from 1D image vectors
T = [];
for i = 1 : Train_Number
% I have chosen the name of each image in databases as a corresponding
% number. However, it is not mandatory!
str = num2str(i);
str = strcat(str,'.jpg');
str = strcat('C:\Users\HP\Documents\MATLAB\Attendance\TrainDatabase\',str);
img = imread(str);
img = rgb2gray(img);
[irow, icol] = size(img);
temp = reshape(img',irow*icol,1); % Reshaping 2D images into 1D image vectors
T = [T, temp]; % 'T' grows after each turn
end
1 Comment
Stephen23
on 5 Jan 2020
Edited: Stephen23
on 5 Jan 2020
You wrote that your filenames are "text.jpg,text(1).jpg,text(2).jpg", but the error message clearly states that it tried to open a file named "1.jpg". So apparently you are generating the wrong filename in your code.
"How to read multiple text.jpg images in a loop?"
Use dir or sprintf, just like the examples in the documentation:
Note that fullfile is recommended for creating file paths, rather than string concatenation.
Answers (1)
Meg Noah
on 6 Jan 2020
It might be easier if you use wildcard in the dir function, then the .db or .. won't be in the list of files in TrainFiles.
TrainFiles = dir('C:\Users\HP\Documents\MATLAB\Attendance\TrainDatabase\*.jpg');
0 Comments
See Also
Categories
Find more on Manage Products in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!