MATLAB Answers

Reading 100 image in a loop and save them in a Matrix

6 views (last 30 days)
shawin
shawin on 15 Aug 2018
Edited: Stephen Cobeldick on 15 Aug 2018
I have the code below for reading 100 images in a loop and iam getting the error which is :
cd 'C:\Users\Shawin\Desktop\FR_Matlab\PCA_FRTest3\PCA_based Face Recognition System\TrainDatabase1'
M=100;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% number of images in the training set.
% M=100;
% Read in training set
X=[]; % img matrix
for i=1:M
str=strcat('(',int2str(i));
str=strcat(str,').jpg'); % concatenates
eval('img=imread(str);');
[irow icol]=size(img); % get the number of rows (N1) and columns (N2)
temp=reshape(img',irow*icol,1);
X=[X temp]; % S is a N1*N2xM matrix after finishing the loop
end
The error is "
*
Error using imread (line 349)
File "(1).jpg" does not exist.
Error in ImgRead_Test (line 16)
eval('img=imread(str);');*
Any help please

  2 Comments

Dennis
Dennis on 15 Aug 2018
Does (1).jpg exist in your current directory?
You can use fullfile to put together filenames including their path.
Is there any reason you are using eval to read your file?
Stephen Cobeldick
Stephen Cobeldick on 15 Aug 2018
Do NOT use cd like this: it is slow and makes debugging harder. Much better is to use fullfile and absolute/relative filenames.
Do NOT use eval pointlessly like that, unless you want to force yourself into writing slow, complex, buggy code. There is absolutely no reason why you could not just call imread directly:
img = imread(str);
Do NOT expand arrays like that inside loops. Read this to know why:

Sign in to comment.

Answers (3)

KALYAN ACHARJYA
KALYAN ACHARJYA on 15 Aug 2018
Edited: KALYAN ACHARJYA on 15 Aug 2018
% Save all images name in a sequence manner before doing the operation
% names for example im1,im2,im3...
%Save the folder of images in the current directory
path_directory='folder_name'; % 'Folder name'
original_files=dir([path_directory '/*.jpg']);
for k=1:length(original_files)
filename=[path_directory '/' original_files(k).name];
image_orginal{k}=imread(filename);
% Image_original{1} matrix of first image
end

  1 Comment

KALYAN ACHARJYA
KALYAN ACHARJYA on 15 Aug 2018
Learn from here:
image_original having multiples variables names, like image_original1, image_original2...) for different images, all variables as a matrix.
How to create dynamic variable names, please refer here

Sign in to comment.


Stephen Cobeldick
Stephen Cobeldick on 15 Aug 2018
Edited: Stephen Cobeldick on 15 Aug 2018
You should follow the examples in the MATLAB documentation, rather than using awful eval and cd:
You should do something like this:
D = 'C:\Users\Shawin\Desktop\FR_Matlab\PCA_FRTest3\PCA_based Face Recognition System\TrainDatabase1'
S = dir(fullfile(D,'*.jpg'));
N = numel(S);
C = cell(1,N);
for k = 1:N
C{k} = imread(fullfile(D,S(k).name));
end
Simpler, more efficient code. No slow and ugly eval, no slow cd, no slow expanding arrays inside any loops.

  3 Comments

shawin
shawin on 15 Aug 2018
I test the code and i got this error :
*
Transpose on ND array is not defined. Use PERMUTE instead.*
Stephen Cobeldick
Stephen Cobeldick on 15 Aug 2018
@shawin: your images are not grayscale matrices, as your question implies, they appear to actually be 3D RGB arrays, so using transpose will not work. If you want to swap the rows and columns of a 3D array then use permute as the error message states. However note that what you wrote in your question "S is a N1*N2xM matrix after finishing the loop" does not makes sense for 3D image arrays: 3D RGB image arrays do not have size N1xN2 as you seem to think, but actually have size N1xN2x3, so your matrix will have size N1*N2*3xM.
In any case, I removed that transpose from my answer, so the code will simply collect all of the images into the cell array C. Once you decide how you want to reshape the 3D image arrays, you can simply add that back into the loop. For example you might want to do this:
for ...
Im = imread(fullfile(D,S(k).name));
Im = permute(Im,[2,1,3]);
C{k} = Im(:);
end
M = [C{:}]
which would give you an N1*N2*3xM sized matrix, arranged like this:
M = [
Im1(1,1,1) Im2(1,1,1) ... ImM(1,1,1)
Im1(1,2,1) Im2(1,2,1) ... ImM(1,2,1)
...
Im1(1,N2,1) Im2(1,N2,1) ... ImM(1,N2,1)
Im1(2,1,1) Im2(2,1,1) ... ImM(2,1,1)
Im1(2,2,1) Im2(2,2,1) ... ImM(2,2,1)
...
Im1(N1,N2,1) Im2(N1,N2,1) ... ImM(N1,N2,1)
Im1(1,1,2) Im2(1,1,2) ... ImM(1,1,2)
...
Im1(N1,N2,3) Im2(N1,N2,3) ... ImM(N1,N2,3)
]
Image Analyst
Image Analyst on 15 Aug 2018
Or you could use a 4-D array with cat(4, ...) but all the images would need to be the same size. Using a cell array (or structure array if you'll want to store additional stuff along with the image) works regardless if the sizes match.

Sign in to comment.


Image Analyst
Image Analyst on 15 Aug 2018
If you want to stitch together 100 images to make a 3-D matrix, which is what it looks like you're trying to do with "X=[X temp];" and want "a N1*N2xM matrix" then do this
img=imread(str);
if i == 1
X = img;
else
X = cat(3, X, X); % Tack another matrix on in the 3rd dimension.
end
and of course, don't use eval().

  0 Comments

Sign in to comment.