-
Notifications
You must be signed in to change notification settings - Fork 29
/
LFReadESLF.m
63 lines (53 loc) · 2.05 KB
/
LFReadESLF.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
55
56
57
58
59
60
61
62
63
% LFReadESLF.m - Read ESLF-encoded light fields
%
% Usage:
% LF = LFReadESLF( FName )
% LF = LFReadESLF( FName, [LensletSize_pix], [LoadAlpha], [<imread options>] )
%
% This function reads the ESLF files commonly used to store light fields, e.g. those at
% lightfields.stanford.edu.
%
% Inputs:
% FName: path to input file
%
% Optional Inputs:
% LensletSize_pix: number of pixels per lenslet, default 14
%
% LoadAlpha: default true; if true, aattempts to load an alpha channel from the ESLF and use as
% a weight channel for the LF; useful for indicating empty subaperture images; use in
% conjunction with png or other formats with alpha channels
%
% <imread options>: additional options are passed to imread; useful examples: 'BackgroundColor'
%
% Outputs:
% LF: a 4D light field with index order [t,s,v,u,c], where s,t are horizontal and vertical
% subaperture index; u,v are horizontal and vertical pixel index; and c is colour channel.
%
% User guide: <a href="matlab:which LFToolbox.pdf; open('LFToolbox.pdf')">LFToolbox.pdf</a>
% See also: LFWriteESLF, LFReadGantryArray, LFUtilDecodeLytroFolder
% Copyright (c) 2013-2020 Donald G. Dansereau
function LF = LFReadESLF( FName, LensletSize_pix, LoadAlpha, varargin )
LensletSize_pix = LFDefaultVal('LensletSize_pix', [14,14]);
LoadAlpha = LFDefaultVal('LoadAlpha', true);
NChans = 3;
if( LoadAlpha )
[Img,ColorMap, Alpha] = imread( FName, varargin{:} ); % note: requesting Alpha sets bg to black
if( ~isempty(Alpha) )
Img(:,:,NChans+1) = Alpha;
NChans = NChans + 1;
end
else
[Img,ColorMap] = imread( FName, varargin{:} );
end
if( ~isempty(ColorMap) ) % indexed colour image
Img = ind2rgb(Img, ColorMap); % todo[optimization] may wish to convert to uint8
end
ImgSize = size(Img(:,:,1));
LFSize(3:4) = ceil(ImgSize./LensletSize_pix);
PadSize = LFSize(3:4) .* LensletSize_pix;
PadAmt = PadSize-ImgSize;
LFSize(1:2) = PadSize ./ LFSize(3:4);
LFSize(5) = NChans;
ImgPad = padarray(Img, PadAmt, 0,'post');
LF = reshape(ImgPad, LFSize([1,3,2,4,5]));
LF = permute(LF, [1,3,2,4,5]);