-
Notifications
You must be signed in to change notification settings - Fork 5
/
load_images.m
54 lines (44 loc) · 1.21 KB
/
load_images.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
% This procedure loads a sequence of images
%
% Arguments:
% 'path', refers to a directory which contains a sequence of JPEG or PPM
% images
% 'reduce' is an optional parameter that controls downsampling, e.g., reduce = .5
% downsamples all images by a factor of 2.
function I = load_images(path, reduce)
if ~exist('reduce')
reduce = 1;
end
if (reduce > 1 || reduce <= 0)
error('reduce must fulfill: 0 < reduce <= 1');
end
% find all JPEG or PPM files in directory
files = dir(path);
files(1:2)=[];
N = length(files);
if (N == 0)
files = dir([path '/*.ppm']);
N = length(files);
if (N == 0)
error('no files found');
end
end
% allocate memory
sz = size(imread([path '/' files(1).name]));
r = floor(sz(1)*reduce);
c = floor(sz(2)*reduce);
I = zeros(r,c,3,N);
% read all files
for i = 1:N
% load image
filename = [path '/' files(i).name];
im = double(imread(filename)) / 255;
if (size(im,1) ~= sz(1) || size(im,2) ~= sz(2))
error('images must all have the same size');
end
% optional downsampling step
if (reduce < 1)
im = imresize(im,[r c],'bicubic');
end
I(:,:,:,i) = im;
end