Mentions légales du service

Skip to content
Snippets Groups Projects
Commit a4252828 authored by DIB Elian's avatar DIB Elian
Browse files

Added function to compute super-ray coordinates and use them as params

parent 3669fb22
Branches
No related tags found
No related merge requests found
getLF.m 0 → 100644
function [LFRef,LFDisp] = getLF(SRRef,SRDisp,SRX,SRY,lfxgv,lfygv,Method)
%GETLF Builds a super-ray set from a light-field
% SRTOLF(LFSet)
%%
SRCol = {SRRef.Color};
SRLab = {SRRef.Label};
SROffset = {SRRef.Offset};
SRSize = cellfun(@size,{SRRef.Color},'UniformOutput',false);
[SRImgSize,SRImgRes] = cellfun(@(x) deal(x(1:3),x(4:end)),SRSize,...
'UniformOutput',false);
seh = strel('arbitrary',[1,0,1]);
sev = strel('arbitrary',[1,0,1]');
epsilon = 0;
%% Initialize constants and interpolants
numChan = size(SRCol{1},3);
ImgSize = [numel(lfxgv),numel(lfygv),numChan];
ImgRes = SRImgRes{1};
Offset = [min(lfxgv),min(lfygv),1,1,1]-1;
numLab = numel(SRCol);
numView = prod(ImgRes);
Color = nan([ImgSize(1:2),numChan,ImgRes]);
Label = zeros([ImgSize(1:2),1,ImgRes]);
LFDisp = -inf([ImgSize(1:2),1,ImgRes]);
progress('',0);
wgriddata = warning('query','MATLAB:griddata:DuplicateDataPoints');
wscattered = warning('query','MATLAB:scatteredInterpolant:DupPtsAvValuesWarnId');
warning('off','MATLAB:griddata:DuplicateDataPoints');
warning('off','MATLAB:scatteredInterpolant:DupPtsAvValuesWarnId');
%% Compute light field using interpolation
% Compute light field super-ray by super-ray
for lab = 1:numLab
% Replace missing values by zero (to avoid interpolation errors)
% SRCol{lab} = utils.fillmissing(SRCol{lab},seh,1);
% SRLab{lab} = utils.fillmissing(SRLab{lab},seh,1);
% SRDisp{lab} = utils.fillmissing(SRDisp{lab},seh,1);
%
% SRCol{lab} = utils.fillmissing(SRCol{lab},sev,1);
% SRLab{lab} = utils.fillmissing(SRLab{lab},sev,1);
% SRDisp{lab} = utils.fillmissing(SRDisp{lab},sev,1);
%
% SRCol{lab}(isnan(SRCol{lab})) = 0;
% SRLab{lab}(isnan(SRLab{lab})) = 0;
% SRDisp{lab}(isnan(SRDisp{lab})) = 0;
% Compute super-ray view by view
for v = 1:numView
msg = ['Constructing light field from super-ray ',num2str(lab),'/',num2str(numLab),...
'\nView ',num2str(v),'/',num2str(numView),'\n'];
progress(msg);
SRXsub = SRX {lab}(:,:,1,v);
SRYsub = SRY {lab}(:,:,1,v);
SRColsub = SRCol {lab}(:,:,:,v);
SRLabsub = SRLab {lab}(:,:,1,v);
SRDispsub = SRDisp{lab}(:,:,1,v);
SRColsub = utils.fillmissing(SRColsub,seh,1);
SRLabsub = utils.fillmissing(SRLabsub,seh,1);
SRDispsub = utils.fillmissing(SRDispsub,seh,1);
SRColsub = utils.fillmissing(SRColsub,sev,1);
SRLabsub = utils.fillmissing(SRLabsub,sev,1);
SRDispsub = utils.fillmissing(SRDispsub,sev,1);
% Compute super-ray boundary
srxqmin = ceil (min(SRXsub(:)));
srxqmax = floor(max(SRXsub(:)));
sryqmin = ceil (min(SRYsub(:)));
sryqmax = floor(max(SRYsub(:)));
% Reduce number of query points for speed
xgv = lfxgv>=srxqmin & lfxgv<=srxqmax;
ygv = lfygv>=sryqmin & lfygv<=sryqmax;
[LFXsub,LFYsub] = ndgrid(lfxgv(xgv),lfygv(ygv));
[SRXsub,SRYsub,LFXsub,LFYsub] = utils.gridCoords(SRXsub,SRYsub,LFXsub,LFYsub);
queryNaN = isnan(LFXsub)|isnan(LFYsub);
LFXsub(queryNaN) = 1;
LFYsub(queryNaN) = 1;
% Interpolate disparity
tempDisp = interp2(SRYsub,SRXsub,SRDispsub,LFYsub,LFXsub,Method);
% Interpolate label
tempLab = interp2(SRYsub,SRXsub,SRLabsub,LFYsub,LFXsub,'nearest');
% Find out of boundary values in label using cubic interpolation
LabNaN = isnan(interp2(SRYsub,SRXsub,SRLabsub,LFYsub,LFXsub,'cubic'));
% Combined depth + stencil buffer
depthStencilMask = tempDisp>(LFDisp(xgv,ygv,1,v)-epsilon) & tempLab==lab;
combinedMask = ~(queryNaN|LabNaN)&depthStencilMask;
buffDisp = LFDisp(xgv,ygv,1,v);
buffLab = Label (xgv,ygv,1,v);
buffDisp(combinedMask) = tempDisp(combinedMask);
buffLab (combinedMask) = tempLab (combinedMask);
LFDisp(xgv,ygv,1,v) = buffDisp;
Label (xgv,ygv,1,v) = buffLab;
% Interpolate color
for c = 1:numChan
buffCol = Color(xgv,ygv,c,v);
tempCol = interp2(SRYsub,SRXsub,SRColsub(:,:,c),LFYsub,LFXsub,Method);
buffCol(combinedMask) = tempCol(combinedMask);
Color(xgv,ygv,c,v) = buffCol;
end
end
end
fprintf('\n\n');
warning(wgriddata.state,'MATLAB:griddata:DuplicateDataPoints');
warning(wscattered.state,'MATLAB:scatteredInterpolant:DupPtsAvValuesWarnId');
LFRef = SR.FieldsToSet(Offset,Color,Label);
function progress(msg,varargin)
persistent sz
if isempty(sz); sz = 0; end
if nargin>1; sz = varargin{1}; end
fprintf(repmat('\b',1,sz));
fprintf(msg);
sz = numel(msg)-count(msg,'\n');
end
end
\ No newline at end of file
function [SRXq,SRYq,SRDisp] = getQueryCoords(LFRef,LFDisp,Method)
%LFTOSR Builds a super-ray set from a light-field
% LFTOSR(LFSet)
%% Initialize constants and interpolants
% Lightfield properties
LFSize = size(LFRef.Color);
ImgSize = LFSize(1:3);
numChan = LFSize(3);
ImgRes = LFSize(4:end);
Offset = LFRef.Offset;
Label = LFRef.Label;
centerView = floor(ImgRes/2)+1;
numView = prod(ImgRes);
numLab = max(Label(:));
% Initialize super-ray fields
[SROff,SRCol,SRLab,SRDisp,SRXq,SRYq] = deal(cell(numLab,1));
% Pad values
Label = padarray(Label,[1,1,0,0,0],nan,'both');
LFDisp = padarray(LFDisp,[1,1,0,0,0],nan,'both');
ImgSize(1:2) = ImgSize(1:2)+2;
Offset(1:2) = Offset(1:2)-1;
LFSize = [ImgSize,ImgRes];
seh = strel('arbitrary',[1,0,1]);
sev = strel('arbitrary',[1,0,1]');
Label = utils.fillmissing(Label,seh,1);
LFDisp = utils.fillmissing(LFDisp,seh,1);
Label = utils.fillmissing(Label,sev,1);
LFDisp = utils.fillmissing(LFDisp,sev,1);
% Replace missing values by zero (to avoid interpolation errors)
Label(isnan(Label))=0;
LFDisp(isnan(LFDisp))=0;
% Compute projected coordinates of reference samples
gv = arrayfun(@(x) 1:x,LFSize,'UniformOutput',false);
ugv = reshape(gv{4},1,1,1,[],1);
vgv = reshape(gv{5},1,1,1,1,[]);
ugv = ugv-centerView(1);
vgv = vgv-centerView(2);
[LFX,LFY] = ndgrid(gv{:});
LFX = LFX-ugv.*reshape(LFDisp,LFSize) + Offset(1);
LFY = LFY-vgv.*reshape(LFDisp,LFSize) + Offset(2);
progress('');
wgriddata = warning('query','MATLAB:griddata:DuplicateDataPoints');
wscattered = warning('query','MATLAB:scatteredInterpolant:DupPtsAvValuesWarnId');
warning('off','MATLAB:griddata:DuplicateDataPoints');
warning('off','MATLAB:scatteredInterpolant:DupPtsAvValuesWarnId');
%% Initialize super-rays
fprintf('Super-ray computation...\n');
for lab = 1:numLab
msg = ['Initializing super-ray ',num2str(lab),'/',num2str(numLab),'\n'];
progress(msg);
% Compute super-ray grid boundary
SRX = LFX(Label==lab);
SRY = LFY(Label==lab);
srxqmin = floor(min(SRX(:)));
srxqmax = ceil (max(SRX(:)));
sryqmin = floor(min(SRY(:)));
sryqmax = ceil (max(SRY(:)));
srxqgv = srxqmin:srxqmax;
sryqgv = sryqmin:sryqmax;
SROff{lab} = [srxqmin,sryqmin,1,1,1]-1;
% Initialize super-ray color, label and disparities
SRCol {lab} = nan([numel(srxqgv),numel(sryqgv),numChan,ImgRes]);
SRLab {lab} = nan([numel(srxqgv),numel(sryqgv),1 ,ImgRes]);
SRDisp{lab} = nan([numel(srxqgv),numel(sryqgv),1 ,ImgRes]);
% Compute query grid for current super-ray
[SRXq{lab},SRYq{lab}] = ndgrid(srxqgv,sryqgv);
% Compute super-ray view by view
for v = 1:numView
msg = ['Interpolating super-ray ',num2str(lab),'/',num2str(numLab),...
'\nView ',num2str(v),'/',num2str(numView),'\n'];
progress(msg);
% Reduce number of reference points for speed
Mask = ...true(size(LFX(:,:,v)));
LFX(:,:,v)>=min(srxqgv)-1 & LFX(:,:,v)<=max(srxqgv)+1 & ...
LFY(:,:,v)>=min(sryqgv)-1 & LFY(:,:,v)<=max(sryqgv)+1;
[~,mgv] = utils.tighten(Mask);
LFXsub = LFX(mgv{:},v);
LFYsub = LFY(mgv{:},v);
[LFXsub,LFYsub,Xq,Yq] = utils.gridCoords(LFXsub,LFYsub,SRXq{lab},SRYq{lab});
M = isnan(Xq)|isnan(Yq);
Xq(M) = 1;
Yq(M) = 1;
% Interpolate disparity
LFDispsub = LFDisp(mgv{:},v);
temp = interp2(LFYsub,LFXsub,LFDispsub,Yq,Xq,Method);
temp(M) = nan;
SRDisp{lab}(:,:,1,v) = temp;
end
SRXq{lab} = SRXq{lab}+ugv.*SRDisp{lab};
SRYq{lab} = SRYq{lab}+vgv.*SRDisp{lab};
end
fprintf('\n\n');
warning(wgriddata.state,'MATLAB:griddata:DuplicateDataPoints');
warning(wscattered.state,'MATLAB:scatteredInterpolant:DupPtsAvValuesWarnId');
% Create super-ray structure from fields
function progress(msg,varargin)
persistent sz
if isempty(sz); sz = 0; end
if nargin>1; sz = varargin{1}; end
fprintf(repmat('\b',1,sz));
fprintf(msg);
sz = numel(msg)-count(msg,'\n');
end
end
\ No newline at end of file
function [SRXq,SRYq,SRDisp] = getQueryCoordsAffine(LFRef,LFDisp)
%GETQUERYCOORDSAFFINE Summary of this function goes here
% Detailed explanation goes here
%%
%% Initialize constants and interpolants
% Lightfield properties
LFSize = size(LFRef.Color);
ImgRes = LFSize(4:end);
Label = LFRef.Label;
centerView = floor(ImgRes/2)+1;
numLab = max(Label(:));
% Replace missing values by zero (to avoid interpolation errors)
Label(isnan(Label))=0;
LFDisp(isnan(LFDisp))=0;
% Compute projected coordinates of reference samples
gv = arrayfun(@(x) 1:x,LFSize,'UniformOutput',false);
ugv = reshape(gv{4},1,1,1,[],1);
vgv = reshape(gv{5},1,1,1,1,[]);
ugv = ugv-centerView(1);
vgv = vgv-centerView(2);
%%
[X,Y,~,U,V] = ndgrid(gv{:});
U = U-centerView(1);
V = V-centerView(2);
Xp = X-U.*LFDisp;
Yp = Y-V.*LFDisp;
%%
s = cell(1,numLab);
[SRXq,SRYq,SRDisp] = deal(cell(1,numLab));
fprintf('Super-ray computation...\n');
for lab = 1:numLab
msg = ['Initializing super-ray ',num2str(lab),'/',num2str(numLab),'\n'];
progress(msg);
M = Label==lab;
X_ = Xp(M(:));
Y_ = Yp(M(:));
D_ = LFDisp(M(:));
s{lab} = fit([X_,Y_],D_,'poly11','Robust','Bisquare');
a = s{lab}.p10;
b = s{lab}.p01;
c = s{lab}.p00;
X_ = X(M(:));
Y_ = Y(M(:));
U_ = U(M(:));
V_ = V(M(:));
tX = X_-U_.*(a.*X_+b.*Y_+c)./( 1+a.*U_+b.*V_);
tY = Y_-V_.*(a.*X_+b.*Y_+c)./( 1+a.*U_+b.*V_);
srxgv = floor(min(tX(:))):ceil(max(tX(:)));
srygv = floor(min(tY(:))):ceil(max(tY(:)));
[X_,Y_] = ndgrid(srxgv,srygv);
D_ = s{lab}([X_(:),Y_(:)]);
D_ = reshape(D_,size(X_));
[SRXq{lab},SRYq{lab}] = ndgrid(srxgv,srygv,gv{3:end});
SRXq{lab} = SRXq{lab}+ugv.*D_;
SRYq{lab} = SRYq{lab}+vgv.*D_;
SRDisp{lab} = ones(size(SRXq{lab})).*D_;
end
function progress(msg,varargin)
persistent sz
if isempty(sz); sz = 0; end
if nargin>1; sz = varargin{1}; end
fprintf(repmat('\b',1,sz));
fprintf(msg);
sz = numel(msg)-count(msg,'\n');
end
end
\ No newline at end of file
getSR.m 0 → 100644
function [SRRef,SRDisp] = getSR(LFRef,LFDisp,SRXq,SRYq,Method)
%GETSR Summary of this function goes here
% Detailed explanation goes here
%% Initialize constants and interpolants
% Lightfield properties
LFSize = size(LFRef.Color);
ImgSize = LFSize(1:3);
numChan = LFSize(3);
ImgRes = LFSize(4:end);
Offset = LFRef.Offset;
Color = LFRef.Color;
Label = LFRef.Label;
centerView = floor(ImgRes/2)+1;
numView = prod(ImgRes);
numLab = numel(SRXq);
% Initialize super-ray fields
[SROff,SRCol,SRLab,SRDisp] = deal(cell(numLab,1));
% Pad values
Color = padarray(Color,[1,1,0,0,0],nan,'both');
Label = padarray(Label,[1,1,0,0,0],nan,'both');
LFDisp = padarray(LFDisp,[1,1,0,0,0],nan,'both');
ImgSize(1:2) = ImgSize(1:2)+2;
Offset(1:2) = Offset(1:2)-1;
LFSize = [ImgSize,ImgRes];
seh = strel('arbitrary',[1,0,1]);
sev = strel('arbitrary',[1,0,1]');
Color = utils.fillmissing(Color,seh,1);
Label = utils.fillmissing(Label,seh,1);
LFDisp = utils.fillmissing(LFDisp,seh,1);
Color = utils.fillmissing(Color,sev,1);
Label = utils.fillmissing(Label,sev,1);
LFDisp = utils.fillmissing(LFDisp,sev,1);
% Replace missing values by zero (to avoid interpolation errors)
Color(isnan(Color))=0;
Label(isnan(Label))=0;
LFDisp(isnan(LFDisp))=0;
% Compute projected coordinates of reference samples
gv = arrayfun(@(x) 1:x,LFSize,'UniformOutput',false);
ugv = reshape(gv{4},1,1,1,[],1);
vgv = reshape(gv{5},1,1,1,1,[]);
ugv = ugv-centerView(1);
vgv = vgv-centerView(2);
[LFX,LFY] = ndgrid(gv{:});
[LFX,LFY] = ndgrid(gv{1:2});
LFX = LFX + Offset(1);
LFY = LFY + Offset(2);
progress('');
wgriddata = warning('query','MATLAB:griddata:DuplicateDataPoints');
wscattered = warning('query','MATLAB:scatteredInterpolant:DupPtsAvValuesWarnId');
warning('off','MATLAB:griddata:DuplicateDataPoints');
warning('off','MATLAB:scatteredInterpolant:DupPtsAvValuesWarnId');
%% Initialize super-rays
fprintf('Super-ray computation...\n');
for lab = 1:numLab
msg = ['Initializing super-ray ',num2str(lab),'/',num2str(numLab),'\n'];
progress(msg);
% Compute super-ray grid boundary
SRXc = SRXq{lab}(:,:,1,centerView(1),centerView(2));
SRYc = SRYq{lab}(:,:,1,centerView(1),centerView(2));
Mask = ~(isnan(SRXc)|isnan(SRYc));
[~,mgv] = utils.tighten(Mask);
srxqmin = SRXc(mgv{1}(1),mgv{2}(1)) - mgv{1}(1);
sryqmin = SRYc(mgv{1}(1),mgv{2}(1)) - mgv{2}(1);
SROff{lab} = [srxqmin,sryqmin,0,0,0];
% Initialize super-ray color, label and disparities
SRCol {lab} = nan(size(SRXq{lab}));
SRCol {lab} = repmat(SRCol{lab},[1,1,numChan]);
SRLab {lab} = nan(size(SRXq{lab}));
SRDisp{lab} = nan(size(SRXq{lab}));
% Compute super-ray view by view
for v = 1:numView
msg = ['Interpolating super-ray ',num2str(lab),'/',num2str(numLab),...
'\nView ',num2str(v),'/',num2str(numView),'\n'];
progress(msg);
Xq = SRXq{lab}(:,:,1,v);
Yq = SRYq{lab}(:,:,1,v);
% Reduce number of reference points for speed
Mask = ...true(size(LFX(:,:,v)));
LFX>=min(Xq(:))-1 & LFX<=max(Xq(:))+1 & ...
LFY>=min(Yq(:))-1 & LFY<=max(Yq(:))+1;
[~,mgv] = utils.tighten(Mask);
LFXsub = LFX(mgv{:});
LFYsub = LFY(mgv{:});
% [LFXsub,LFYsub,Xq,Yq] = utils.gridCoords(LFXsub,LFYsub,SRXq,SRYq);
M = isnan(Xq)|isnan(Yq);
Xq(M) = 1;
Yq(M) = 1;
% Interpolate color
for c = 1:numChan
Colsub = Color(mgv{:},c,v);
temp = interp2(LFYsub,LFXsub,Colsub,Yq,Xq,Method);
temp(M) = nan;
SRCol{lab}(:,:,c,v) = temp;
end
% Interpolate disparity
LFDispsub = LFDisp(mgv{:},v);
temp = interp2(LFYsub,LFXsub,LFDispsub,Yq,Xq,Method);
temp(M) = nan;
SRDisp{lab}(:,:,1,v) = temp;
% Interpolate label
Labelsub = Label(mgv{:},v);
temp = interp2(LFYsub,LFXsub,Labelsub,Yq,Xq,'nearest');
temp(M) = nan;
Lab = temp;
% Replace out of boundary values in label using cubic interpolation
NaNLab = isnan(interp2(LFYsub,LFXsub,Labelsub,Yq,Xq,'cubic'));
Lab(NaNLab) = nan;
SRLab{lab}(:,:,1,v) = Lab;
end
end
fprintf('\n\n');
warning(wgriddata.state,'MATLAB:griddata:DuplicateDataPoints');
warning(wscattered.state,'MATLAB:scatteredInterpolant:DupPtsAvValuesWarnId');
% Create super-ray structure from fields
SRRef = SR.FieldsToSet(SROff,SRCol,SRLab);
function progress(msg,varargin)
persistent sz
if isempty(sz); sz = 0; end
if nargin>1; sz = varargin{1}; end
fprintf(repmat('\b',1,sz));
fprintf(msg);
sz = numel(msg)-count(msg,'\n');
end
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment