Tuesday, June 3, 2008

Load a sequence of images into matlab

Following script allows a user to read images in a folder; in a sequence.
=================================================


%list all the files in current folder
filelist = dir();
images = [];

% the first two in filelist are . and ..
for i=3:size(filelist,1)

%filelist is not a folder
if filelist(i).isdir ~= true
fname = filelist(i).name;

% if file extension is jpg
if strcmp( fname(size(fname,2)-3:size(fname,2)) ,'.jpg' ) == 1
tmp = imread(fname);

% Concatenate images one after other ( in sequence)

images = [images tmp];
disp([fname 'loaded']);

end
end
end

2 comments:

Anonymous said...

if you're interested in processing all the jpeg files, you can pass a filter into the dir command:

filelist = dir('*.jpg');

Anonymous said...

The way proposed by the previous anonymous is preferred, but you should also pre-allocate the images array before the loop starts.
Reallocating on each iteration (i.e. the command "images = [images tmp];") could take a lot of time.