How do I read multiple images into an array in MATLAB? -
How do I read multiple images into an array in MATLAB? -
i working on pca face recognition project , wondering how read multiple images matrix , resize them 50x50. im aware need utilize imread , pass in images, followed using imresize. following?
myfolder = 'c:\users\x'; filepattern = fullfile(myfolder, '*.jpg'); jpegfiles = dir(filepattern); k = 1:length(jpegfiles) basefilename = jpegfiles(k).name; fullfilename = fullfile(myfolder, basefilename); fprintf(1, 'now reading %s\n', fullfilename); imagearray50x50 = imread(fullfilename); imagearray50x50new = imresize(imagearray50x50, [50 50]); imshow(imagearray30x40new)
is approach? how resize images correctly?
thanks in advance, mark
from have dealt with, way read in multiple images file serially , through for
loop. have indeed approach, need determine how want store of these images in matlab. 2 easiest options create 3d matrix each piece 50 x 50 image read file or cell array each cell 50 x 50 image.
if want first option, this:
%// code myfolder = 'c:\users\x'; filepattern = fullfile(myfolder, '*.jpg'); jpegfiles = dir(filepattern); %// new - 3d matrix store images imagematrix = uint8(zeros(50,50,numel(jpegfiles))); %// code k = 1:length(jpegfiles) basefilename = jpegfiles(k).name; fullfilename = fullfile(myfolder, basefilename); fprintf(1, 'now reading %s\n', fullfilename); imagearray50x50 = imread(fullfilename); imagearray50x50new = imresize(imagearray50x50, [50 50]); %// new imagematrix(:,:,k) = imagearray50x50new; end
to access kth image, do:
img = imagematrix(:,:,k);
the above code assuming of images of type uint8
. if isn't case images of different types, cell array approach preferred.... that'd sec approach. if case, instead:
%// code myfolder = 'c:\users\x'; filepattern = fullfile(myfolder, '*.jpg'); jpegfiles = dir(filepattern); %// new - 3d matrix store images imagematrix = cell(1,numel(jpegfiles)); %// code k = 1:length(jpegfiles) basefilename = jpegfiles(k).name; fullfilename = fullfile(myfolder, basefilename); fprintf(1, 'now reading %s\n', fullfilename); imagearray50x50 = imread(fullfilename); imagearray50x50new = imresize(imagearray50x50, [50 50]); %// new imagematrix{k} = imagearray50x50new; end
to access kth image, do:
img = imagematrix{k};
however, if dealing pca, suggest instead create 2d matrix each row unrolled version of image , have many rows have images. therefore, each row 1 x 250 vector of intensities. reason why you'd want because if utilize pca
function in matlab, each row info point while each column variable. therefore, instead:
%// code myfolder = 'c:\users\x'; filepattern = fullfile(myfolder, '*.jpg'); jpegfiles = dir(filepattern); %// new - 3d matrix store images imagematrix = zeros(numel(jpegfiles), 250); %// code k = 1:length(jpegfiles) basefilename = jpegfiles(k).name; fullfilename = fullfile(myfolder, basefilename); fprintf(1, 'now reading %s\n', fullfilename); imagearray50x50 = imread(fullfilename); imagearray50x50new = imresize(imagearray50x50, [50 50]); %// new imagematrix(k,:) = double(imagearray50x50new(:).'); end
therefore, each row image represented single vector. statement: imagearray50x50new(:).'
first converts 50 x 50 image column vector, transposed becomes row vector. also, take notice made image matrix double precision. did because pca
best suited floating-point data, , when transformed each image row vector, i've casted info double
facilitate this.
image matlab image-processing pca face-recognition
Comments
Post a Comment