From c1c790fc76f23dfeb6188b0c76f731f84d3b97c6 Mon Sep 17 00:00:00 2001
From: Unknown <elian.di@laposte.net>
Date: Tue, 26 Sep 2017 10:55:09 +0200
Subject: [PATCH] Cleaned up the code a bit

---
 LFtoSR.m        |  46 +++++++++------
 LFtoSRCentral.m | 150 ++++++++++++++++++++++++------------------------
 SRtoLF.m        |   4 +-
 draft.m         | 113 +++++++++++++++++++++++++-----------
 merge.m         |   2 +-
 script_plot.m   |  86 +++++++++++++++++++++++++++
 script_read.m   |  59 ++++++++++---------
 script_write.m  |  55 +++++++++---------
 8 files changed, 333 insertions(+), 182 deletions(-)

diff --git a/LFtoSR.m b/LFtoSR.m
index cfaaf66..44f6255 100644
--- a/LFtoSR.m
+++ b/LFtoSR.m
@@ -3,34 +3,45 @@ function [SRset,LFLabCent,SRRefBox,SRLabBox]=...
 %LFTOSR Summary of this function goes here
 %   Detailed explanation goes here
 
-LFSize = size(LFLab);
-LFLabCent = zeros(LFSize);
+%% Initialize constants
+disp('Initializing constants');
+
+LFSize = size(LFLab);      % Size of light field
+LFLabCent = zeros(LFSize); % Label buffer representing visible pixels and their labels
 
 numLab = max(LFLab(:));
 [SRRef,SRLab,SRRefBox,SRLabBox,SRDisp,SRPos] = deal(cell(numLab,1));
 
-ugv = reshape(1:LFSize(1),[],1);
+ugv = reshape(1:LFSize(1),[],1);     % Extent of light field
 vgv = reshape(1:LFSize(2),1,[]);
 xgv = reshape(1:LFSize(3),1,1,[]);
 ygv = reshape(1:LFSize(4),1,1,1,[]);
-[~,~,LFX,LFY]   = ndgrid(ugv,vgv,xgv,ygv);
 
-uref = floor(LFSize(1)/2)+1;
+[~,~,LFX,LFY]   = ndgrid(ugv,vgv,xgv,ygv); % Light field coordinates
+
+uref = floor(LFSize(1)/2)+1; % Central view coordinates
 vref = floor(LFSize(2)/2)+1;
 
-OriInt = griddedInterpolant(LFRef,varargin{:});
-LabInt = griddedInterpolant(LFLab,'nearest');
+LabRef  =  LFLab(uref,vref,:,:); % Central label     map
+DispRef = LFDisp(uref,vref,:,:); % Central disparity map
+
+OriInt = griddedInterpolant(LFRef,varargin{:}); % Interpolant on pixel values
+LabInt = griddedInterpolant(LFLab,'nearest');   % Interpolant on pixel labels
 
 disp(['Ref: Inter = ' OriInt.Method ', Extra = ' OriInt.ExtrapolationMethod]);
 disp(['Lab: Inter = ' LabInt.Method ', Extra = ' LabInt.ExtrapolationMethod]);
 
+%% Build SR
+disp('Building SR using labels');
+
 utils.printSameLine();
 for lab = 1:numLab
     utils.printSameLine(['lab: ' num2str(lab)]);
     
     Mask = LFLab==lab;
+    MaskRef = LabRef==lab;
     
-    SRDisp{lab} = median(LFDisp(Mask));
+    SRDisp{lab} = median(DispRef(MaskRef));
     
     dispX = (ugv-uref)*SRDisp{lab};
     dispY = (vgv-vref)*SRDisp{lab};
@@ -43,15 +54,17 @@ for lab = 1:numLab
     
     SRPos{lab} = [minprojX,minprojY]-1;
     
+    srugv = reshape(1:LFSize(1),[],1);
+    srvgv = reshape(1:LFSize(2),1,[]);
     srxgv = minprojX:maxprojX;
     srygv = minprojY:maxprojY;
     
-    [SRUq,SRVq,SRXq,SRYq] = ndgrid(ugv,vgv,srxgv,srygv);
+    [SRUq,SRVq,SRXq,SRYq] = ndgrid(srugv,srvgv,srxgv,srygv);
     
     SRXq = SRXq+dispX;
     SRYq = SRYq+dispY;
     
-    inMask = SRXq>=0.5 & SRXq<=LFSize(3)+0.5 &...
+    LFMask = SRXq>=0.5 & SRXq<=LFSize(3)+0.5 &...
              SRYq>=0.5 & SRYq<=LFSize(4)+0.5;
 
     SRRefBox{lab} = OriInt(SRUq,SRVq,SRXq,SRYq);
@@ -62,15 +75,16 @@ for lab = 1:numLab
     
     SRMask = SRLab{lab}==lab;
     
-    tu = SRUq(SRMask&inMask);
-    tv = SRVq(SRMask&inMask);
-    tx = round(SRXq(SRMask&inMask));
-    ty = round(SRYq(SRMask&inMask));
+    inMask = SRMask&LFMask;
+    tu = SRUq(inMask);
+    tv = SRVq(inMask);
+    tx = round(SRXq(inMask));
+    ty = round(SRYq(inMask));
     t = sub2ind(LFSize,tu,tv,tx,ty);
     LFLabCent(t) = lab;
     
-    SRRef{lab}(~(SRMask&inMask)) = NaN;
-    SRLab{lab}(~(SRMask&inMask)) = NaN;
+    SRRef{lab}(~inMask) = NaN;
+    SRLab{lab}(~inMask) = NaN;
 end
 
 utils.printSameLine();
diff --git a/LFtoSRCentral.m b/LFtoSRCentral.m
index 6a18098..52a5f49 100644
--- a/LFtoSRCentral.m
+++ b/LFtoSRCentral.m
@@ -1,33 +1,35 @@
-function [SRset,LFLabCent,SRRefBox,SRLabBox]=...
+function [SRset,LabVis,SRRefBox,SRLabBox]=...
     LFtoSRCentral(LFRef,LFLab,LFDisp,varargin)
-%LFTOSR Summary of this function goes here
+%LFTOSRCentral Summary of this function goes here
 %   Detailed explanation goes here
 
-LFSize = size(LFLab);
-LFLabCent = zeros(LFSize);
+%% Initialize constants
+disp('Initializing constants');
+
+LFSize = size(LFLab);   % Size of light field
+LabVis = zeros(LFSize); % Label buffer representing visible pixels and their labels
 
 numLab = max(LFLab(:));
 [SRRef,SRLab,SRRefBox,SRLabBox,SRDisp,SRPos] = deal(cell(numLab,1));
 
-ugv = reshape(1:LFSize(1),[],1);
-vgv = reshape(1:LFSize(2),1,[]);
-xgv = reshape(1:LFSize(3),1,1,[]);
-ygv = reshape(1:LFSize(4),1,1,1,[]);
-[~,~,LFX,LFY]   = ndgrid(ugv,vgv,xgv,ygv);
-
-uref = floor(LFSize(1)/2)+1;
+srugv = reshape(1:LFSize(1),[],1); % Angular extent of super-ray
+srvgv = reshape(1:LFSize(2),1,[]);
+    
+uref = floor(LFSize(1)/2)+1; % Central view coordinates
 vref = floor(LFSize(2)/2)+1;
 
-LabRef  =  LFLab(uref,vref,:,:);
-DispRef = LFDisp(uref,vref,:,:);
+LabRef  =  LFLab(uref,vref,:,:); % Central label     map
+DispRef = LFDisp(uref,vref,:,:); % Central disparity map
 
-OriInt = griddedInterpolant(LFRef,varargin{:});
-LabInt = griddedInterpolant(LFLab,'nearest');
+RefInt = griddedInterpolant(LFRef,varargin{:}); % Interpolant on pixel values
+LabInt = griddedInterpolant(LFLab,'nearest');   % Interpolant on pixel labels
 
-disp(['Ref: Inter = ' OriInt.Method ', Extra = ' OriInt.ExtrapolationMethod]);
+disp(['Ref: Inter = ' RefInt.Method ', Extra = ' RefInt.ExtrapolationMethod]);
 disp(['Lab: Inter = ' LabInt.Method ', Extra = ' LabInt.ExtrapolationMethod]);
 
-% Find main disparity for each super-ray ans sort them by increasing order
+%% Find main disparity for each super-ray
+disp('Computing main disparity for each super-ray');
+
 utils.printSameLine();
 for lab = 1:numLab
     utils.printSameLine(['lab: ' num2str(lab)]);
@@ -37,86 +39,82 @@ for lab = 1:numLab
 end
 utils.printSameLine();
 
+[~,sortedLab] = sort([SRDisp{:}],'ascend'); % Sort super-rays by ascending disparity order
 
-[~,sortedLab] = sort([SRDisp{:}],'ascend');
+%% Build label buffer corresponding to pixels visible in central view...
+% by projecting pixels visible in the central view in increasing disparity
+% order
+disp('Building label buffer');
 
+utils.printSameLine();
 for lab = sortedLab
     utils.printSameLine(['lab: ' num2str(lab)]);
     
-    Mask = LabRef==lab;
-    stats = regionprops(Mask,'BoundingBox','Image');
-    bb = stats.BoundingBox;
-    SRMask = stats.Image;
-    
-    dispX = (ugv-uref)*SRDisp{lab};
-    dispY = (vgv-vref)*SRDisp{lab};
-
-    ul = round(bb(1:4)+0.5); width = bb(5:8)-1;
-    minCentX = ul(3); maxCentX = minCentX+width(3);
-    minCentY = ul(4); maxCentY = minCentY+width(4);
-    SRPos{lab} = [minCentX,minCentY]-1;
-    
-    srxgv = minCentX:maxCentX;
-    srygv = minCentY:maxCentY;
-    
-    [SRUq,SRVq,SRXq,SRYq] = ndgrid(ugv,vgv,srxgv,srygv);
-    
-    SRXq = SRXq+dispX;
-    SRYq = SRYq+dispY;
-    
-    inMask = SRXq>=0.5 & SRXq<=LFSize(3)+0.5 &...
-             SRYq>=0.5 & SRYq<=LFSize(4)+0.5;
-         
-    tu = SRUq(SRMask&inMask);
-    tv = SRVq(SRMask&inMask);
-    tx = round(SRXq(SRMask&inMask));
-    ty = round(SRYq(SRMask&inMask));
-    t = sub2ind(LFSize,tu,tv,tx,ty);
-    LFLabCent(t) = lab;
+    [~,~,~,~,ind] = SRindices(lab);
+    LabVis(ind) = lab;
 end
+utils.printSameLine();
+
+LabInt.Values = LabVis;
 
-LabInt.Values = LFLabCent;
+%% Build SR
+disp('Building SR using labels');
 
+utils.printSameLine();
 for lab=1:numLab
     utils.printSameLine(['lab: ' num2str(lab)]);
-    
-    Mask = LabRef==lab;
-    stats = regionprops(Mask,'BoundingBox','Image');
-    bb = stats.BoundingBox;
-    SRMask = stats.Image;
-    
-    dispX = (ugv-uref)*SRDisp{lab};
-    dispY = (vgv-vref)*SRDisp{lab};
 
-    ul = round(bb(1:4)+0.5); width = bb(5:8)-1;
-    minCentX = ul(3); maxCentX = minCentX+width(3);
-    minCentY = ul(4); maxCentY = minCentY+width(4);
-    SRPos{lab} = [minCentX,minCentY]-1;
-    
-    srxgv = minCentX:maxCentX;
-    srygv = minCentY:maxCentY;
-    
-    [SRUq,SRVq,SRXq,SRYq] = ndgrid(ugv,vgv,srxgv,srygv);
-    
-    SRXq = SRXq+dispX;
-    SRYq = SRYq+dispY;
-    
-    inMask = SRXq>=0.5 & SRXq<=LFSize(3)+0.5 &...
-             SRYq>=0.5 & SRYq<=LFSize(4)+0.5;
+    [SRU,SRV,SRX,SRY,~,inMask] = SRindices(lab);
     
-    SRRefBox{lab} = OriInt(SRUq,SRVq,SRXq,SRYq);
-    SRLabBox{lab} = LabInt(SRUq,SRVq,SRXq,SRYq);
+    SRRefBox{lab} = RefInt(SRU,SRV,SRX,SRY);
+    SRLabBox{lab} = LabInt(SRU,SRV,SRX,SRY);
     
     SRRef{lab} = SRRefBox{lab};
     SRLab{lab} = SRLabBox{lab};
     Occlusion = SRLab{lab}~=lab;
 
-    SRRef{lab}(Occlusion|~(SRMask&inMask)) = NaN;
-    SRLab{lab}(Occlusion|~(SRMask&inMask)) = NaN;
+    SRRef{lab}(Occlusion|~inMask) = NaN;
+    SRLab{lab}(Occlusion|~inMask) = NaN;
 end
 utils.printSameLine();
 
 SRset = struct('Value',SRRef,'Label',SRLab,'Position',SRPos,'Disparity',SRDisp);
 
+%% Auxiliary functions
+
+    function [SRU,SRV,SRX,SRY,ind,inMask,SRMask,LFMask] = SRindices(lab)
+        Mask = LabRef==lab;
+        stats = regionprops(Mask,'BoundingBox','Image');
+        bb = stats.BoundingBox;
+        SRMask = stats.Image;
+        
+        % Bounding boxes of super-ray as multidimensional indices
+        ul = round(bb(1:4)+0.5); width = bb(5:8)-1;
+        minSRX = ul(3); maxSRX = minSRX+width(3);
+        minSRY = ul(4); maxSRY = minSRY+width(4);
+        SRPos{lab} = [minSRX,minSRY]-1;
+        
+        srxgv = minSRX:maxSRX;
+        srygv = minSRY:maxSRY;
+        
+        [SRU,SRV,SRX,SRY] = ndgrid(srugv,srvgv,srxgv,srygv);
+        
+        % X and Y disparity when moving along u and v axes
+        dispX = (srugv-uref)*SRDisp{lab};
+        dispY = (srvgv-vref)*SRDisp{lab};
+        
+        SRX = SRX+dispX;
+        SRY = SRY+dispY;
+        
+        LFMask = SRX>=0.5 & SRX<=LFSize(3)+0.5 &...
+            SRY>=0.5 & SRY<=LFSize(4)+0.5;
+        inMask = SRMask&LFMask;
+        
+        indu = SRU(inMask);
+        indv = SRV(inMask);
+        indx = round(SRX(inMask));
+        indy = round(SRY(inMask));
+        ind = sub2ind(LFSize,indu,indv,indx,indy);
+    end
 end
 
diff --git a/SRtoLF.m b/SRtoLF.m
index ebc7b10..782585a 100644
--- a/SRtoLF.m
+++ b/SRtoLF.m
@@ -13,8 +13,10 @@ SRDisp = {SRset.Disparity};
 
 numLab = numel(SRLab);
 
+[~,sortedLab] = sort([SRDisp{:}],'ascend');
+
 utils.printSameLine();
-for lab=1:numLab
+for lab = sortedLab
     utils.printSameLine(['lab = ' num2str(lab)]);
     SRSize = size(SRRef{lab});
     curRef = SRRef{lab};
diff --git a/draft.m b/draft.m
index c949174..1a3fd3c 100644
--- a/draft.m
+++ b/draft.m
@@ -1,45 +1,90 @@
+clear variables, close all, clc
+ProjDir = 'C:\Users\edib\Thesis\Code\Projects\+SR';
+Expe = 'test';
+ExpeDir = fullfile(ProjDir,Expe);
+
+%% Load Light Field, Disparity map and Segmentation Map
+% Segmentation obtained through SLIC with disparity (Xin)
+load('Gray.mat');
+load('Labels.mat');
+load('Disparity.mat');
+
+uref = 5;
+vref = 5;
+
+LFSize = size(Labels);
+numLab = max(Labels(:));
+
+LabRef = Labels(uref,vref,:,:);
+LabRep = repmat(LabRef,LFSize(1),LFSize(2),1,1);
+
+imgRes = [LFSize(1),LFSize(2)];
+imgSize = [LFSize(3),LFSize(4)];
+numView = prod(imgRes);
+numPix = prod(imgSize);
+numPixTot = numView*numPix;
+LFSize = size(Gray);
+
+interpMethod = 'nearest';
+compMethods = {'local'};%'global',
+minMethods = {'trace'};%'rank',
+
+compMethod = 'local';
+minMethod = 'trace';
+
 %%
-close all
-test = SR.BCtoSR(SRB,SRC,1);
-LF.Viewer(SRVis{1}).view
-LF.Viewer(test{1}).view
+[SRVisSet,LabVis] = SR.LFtoSRCentral(Gray,Labels,Disparity,interpMethod);
 
 %%
-close all
-test = SR.BCtoLF(LFB,LFC,3);
-LF.Viewer(LFVis).view
-LF.Viewer(test).view
+SRExtSet = SR.LFtoSR(Gray,Labels,Disparity,interpMethod);
+%%
+SRExt = {SRExtSet.Value};
+SRExtComp = SR.completeSR(SRExt,minMethod);
+SRExtCompB = SR.factorizeSR(SRExtComp);
 
 %%
-close all
-test = zeros(size(LFVis));
-ref = LFVis;
-ref(isnan(ref)) = 0;
-peaksnr = zeros(1,numView);
-
-for k = 1:numView
-    curLF = SR.BCtoLF(LFBRec,LFC,k);
-    test = test + curLF;
-    
-    peaksnr(k) = psnr(test,ref);
-    format = ['PSNR(' num2str(k,'%.3d') ') = ' num2str(peaksnr(k))];
-    utils.printSameLine(format);
+testSet = struct(SRExtSet);
+for lab=1:numLab
+    testSet(lab).Value = SRExtCompB{lab};
+end
+
+%%
+[VisBSet,HidBSet] = SR.crop(testSet);
+
+%%
+HidB1Set = struct(HidBSet);
+for lab=1:numLab
+    val = HidBSet(lab).Value;
+    HidB1Set(lab).Value = repmat(val(1,1,:,:),LFSize(1),LFSize(2),1,1);
+    lab_ = lab*double(~isnan(HidBSet(lab).Value));
+    HidB1Set(lab).Label = lab_;
 end
 
-figure, plot(peaksnr)
-grid on
-grid minor
+%%
+LFVisB = SR.SRtoLF(VisBSet,LFSize);
+LFHidB = SR.SRtoLF(HidB1Set,LFSize);
+LFHidB(isnan(LFHidB))=0;
 
 %%
+close all
+LF.Viewer(LFVisB).view
+LF.Viewer(LFHidB).view
+
 
-LFRef = LFVis;
-LFRef(isnan(LFRef)) = 0;
+%%
 close all
-hold on
-[LFRec1,peaksnr1] = SR.progPSNR(LFB,   LFC,LFRef);
-[LFRec2,peaksnr2] = SR.progPSNR(LFBRec,LFC,LFRef);
-hold off
-
-LF.Viewer(LFRec1).view
-LF.Viewer(LFRec2).view
-LF.Viewer(LFRec1-LFRec2).view
+bla = sum(sum(double(LFHidB~=0),1),2);
+figure, imshow(squeeze(bla),[])
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/merge.m b/merge.m
index 73f377d..21d235a 100644
--- a/merge.m
+++ b/merge.m
@@ -6,7 +6,7 @@ LFVis = zeros(size(LabRep));
 numLab = max(LabRep(:));
 
 for lab = 1:numLab
-    curSR = SRVis(lab);
+    curSR = SRVis{lab};
     SRMask = ~isnan(curSR);
     LFMask = LabRep==lab;
     LFVis(LFMask) = curSR(SRMask);
diff --git a/script_plot.m b/script_plot.m
index e69de29..94eb649 100644
--- a/script_plot.m
+++ b/script_plot.m
@@ -0,0 +1,86 @@
+clear variables, close all, clc
+ProjDir = 'C:\Users\edib\Thesis\Code\Projects\+SR';
+Expe = 'article';
+ExpeDir = fullfile(ProjDir,Expe);
+
+%% Load Light Field, Disparity map and Segmentation Map
+% Segmentation obtained through SLIC with disparity (Xin)
+
+compMethods = {'local'};%'global',
+minMethods = {'trace'};%'rank',
+
+%% HEVC Parameter Initialization
+QPmin=0; QPstep=4; QPmax=20;
+QPList = QPmin:QPstep:QPmax;
+QPstring = ['QP = ' num2str(QPmin) ':' num2str(QPstep) ':' num2str(QPmax)];
+
+%%
+for compMethod = compMethods
+    compMethod = compMethod{:};
+    for minMethod = minMethods
+        minMethod = minMethod{:};
+        
+        %%
+        methstr = [compMethod  ' ' minMethod ' minimization']
+        RecDir = fullfile(ExpeDir,compMethod,minMethod,'Rec');
+        mkdir(RecDir);
+        
+        peaksnrsmatname = fullfile(RecDir,'..','peaksnrs.mat');
+        matpeaksnrs = matfile(peaksnrsmatname,'Writable',false);
+        
+        bitratesmatname = fullfile(RecDir,'..','bitrates.mat');
+        matbitrates = matfile(bitratesmatname,'Writable',false);
+        
+        peaksnr = matpeaksnrs.peaksnr;
+        bitrate = matbitrates.bitrate;
+        
+        assert(all(size(peaksnr)==size(bitrate)));
+        numView = size(peaksnr,1);
+        
+        psnrbitratesvgname = fullfile(RecDir,'..','psnr_bitrate.svg');
+        psnrbitratefigname = fullfile(RecDir,'..','psnr_bitrate.fig');
+        
+        %% Visualization
+        %% PSNR vs BitRate
+        %close all
+        subkList = [1,5:5:30];
+        figure, hold on
+        cmap = hsv(2*numView);
+        
+        QPstr = arrayfun(@(qp) ['QP = ' num2str(qp)],QPList,'UniformOutput',false);
+        kstr  = arrayfun(@(k)  ['k = '  num2str(k) ],subkList,'UniformOutput',false);
+        
+        for k=1:numView %Plot iso-rank curves with respective names
+            curName = ['k = ',num2str(k)];
+            curCol = cmap(k,:);
+            h(k) = plot(bitrate(k,:),peaksnr(k,:),'DisplayName',curName,'Color',curCol);
+        end
+        
+        for k=subkList %Plot iso-rank curves with respective names
+           h(k).Color = 0.25*[1 1 1];
+           h(k).LineStyle='-.';
+        end
+        
+        plot(bitrate,peaksnr,'--','Color',0.25*[1 1 1],'DisplayName','')
+        hold off
+        xlabel('BitRate')
+        ylabel('PSNR')
+        title({'PSNR vs BitRate, various QP and target ranks(k)',...
+            'Visible Light Field after back-warping',...
+            methstr})
+        grid on
+        grid minor
+        axis square
+        %legend('show','Location','NorthEast')
+        text(bitrate(end,:),peaksnr(end,:),QPstr);
+        text(bitrate(subkList,1),peaksnr(subkList,1),kstr);
+        set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
+        savefig(psnrbitratefigname);
+        saveas(gcf,psnrbitratesvgname);
+    end
+end
+
+linkaxes(findall(0,'type','axes'));
+%%
+
+
diff --git a/script_read.m b/script_read.m
index 074888b..d1655f6 100644
--- a/script_read.m
+++ b/script_read.m
@@ -1,5 +1,7 @@
 clear variables, close all, clc
-ExpeDir = 'C:\Users\edib\Thesis\Code\Projects\+SR\test';
+ProjDir = 'C:\Users\edib\Thesis\Code\Projects\+SR';
+Expe = 'test';
+ExpeDir = fullfile(ProjDir,Expe);
 
 %% Load Light Field, Disparity map and Segmentation Map
 % Segmentation obtained through SLIC with disparity (Xin)
@@ -24,9 +26,8 @@ numPixTot = numView*numPix;
 LFSize = size(Gray);
 
 interpMethod = 'nearest';
-SRMethods = {'global','local'};%
-compMethods = {'rank','trace'};%
-coding = false;
+compMethods = {'local'};%'global',
+minMethods = {'trace'};%'rank',
 
 %% HEVC Parameter Initialization
 QPmin=0; QPstep=4; QPmax=20;
@@ -37,17 +38,17 @@ HEVCParams.InputBitDepth = '16';
 HEVCParams.InternalBitDepth = '16';
 
 %%
-for SRMethod = SRMethods
-    SRMethod = SRMethod{:};
-    for compMethod = compMethods
-        compMethod = compMethod{:};
+for compMethod = compMethods
+    compMethod = compMethod{:};
+    for minMethod = minMethods
+        minMethod = minMethod{:};
         
         %%
-        SRMethod,compMethod
+        methstr = [compMethod  ' ' minMethod ' minimization']
         
         LFName = 'B';
-        RecDir = fullfile(ExpeDir,'Rec',SRMethod,compMethod);
-        mkdir(RecDir);
+        RefDir = fullfile(ExpeDir,compMethod,minMethod,'Ref');
+        RecDir = fullfile(ExpeDir,compMethod,minMethod,'Rec');
         
         subSamp = '400';
         resCell = num2cell(imgRes);
@@ -58,16 +59,16 @@ for SRMethod = SRMethods
         logFile = strjoin([LFName logFile subSamp],'_');
         logFile = [logFile,'.rtf'];
         
-        rangematname = fullfile(RecDir,'range.mat');
+        rangematname = fullfile(RecDir,'..','range.mat');
         matrange = matfile(rangematname,'Writable',false);
         
-        coeffsmatname = fullfile(RecDir,'coeffs.mat');
+        coeffsmatname = fullfile(RecDir,'..','coeffs.mat');
         matcoeffs = matfile(coeffsmatname,'Writable',false);
         
-        peaksnrsmatname = fullfile(RecDir,'peaksnrs.mat');
-        peaksnrscsvname = fullfile(RecDir,'peaksnrs.csv');
-        bitratesmatname = fullfile(RecDir,'bitrates.mat');
-        bitratescsvname = fullfile(RecDir,'bitrates.csv');
+        peaksnrsmatname = fullfile(RecDir,'..','peaksnrs.mat');
+        peaksnrscsvname = fullfile(RecDir,'..','peaksnrs.csv');
+        bitratesmatname = fullfile(RecDir,'..','bitrates.mat');
+        bitratescsvname = fullfile(RecDir,'..','bitrates.csv');
         
         matpeaksnrs = matfile(peaksnrsmatname,'Writable',true);
         matbitrates = matfile(bitratesmatname,'Writable',true);
@@ -75,27 +76,29 @@ for SRMethod = SRMethods
         peaksnr = zeros(numView,numel(QPList));
 
         %% Build super-ray set from light field
-        SRset = SR.LFtoSRCentral(Gray,Labels,Disparity,...
+        SRVisSet = SR.LFtoSRCentral(Gray,Labels,Disparity,...
             interpMethod);
         
         %%
-        SRRef  = {SRset.Value};
-        SRLab  = {SRset.Label};
-        SRPos  = {SRset.Position};
-        SRDisp = {SRset.Disparity};
+        SRRef  = {SRVisSet.Value};
+        SRLab  = {SRVisSet.Label};
+        SRPos  = {SRVisSet.Position};
+        SRDisp = {SRVisSet.Disparity};
         
         %% Rebuild light field from super-ray set
-        [LFRef,LFLabCent] = SR.SRtoLFCentral(SRset,LFSize,interpMethod);
+        [LFRef,LFLabCent] = SR.SRtoLFCentral(SRVisSet,LFSize,interpMethod);
         
         %% Normalize and cast as uint16
         range_ = matrange.range;
         
         %% HEVC Decoding
         for QP = QPList
+            %%
             HEVCParams.QP = num2str(QP);
-            HEVCParams.saveDir = fullfile(RecDir,num2str(QP));
+            LFParams.refDir = fullfile(RefDir);
+            LFParams.recDir = fullfile(RecDir,num2str(QP));
             
-            BRec_uint16 = LF.decode(LFName,imgRes,imgSize,HEVCParams,...
+            BRec_uint16 = LF.decode(LFName,imgRes,imgSize,LFParams,HEVCParams,...
                 'readPrecision','uint16','writePrecision','uint16');
             
             %% recast to original range
@@ -103,13 +106,13 @@ for SRMethod = SRMethods
             LFBRec = -utils.growMat(LF_,range_);
             
             %% PSNR as a function of target rank
-            switch SRMethod
+            switch compMethod
                 case 'global'
                     LFC = matcoeffs.C;
-                    peaksnr_ = SR.progPSNR(LFBRec,LFC,LabRep,LFRef,SRset,interpMethod);
+                    peaksnr_ = SR.progPSNR(LFBRec,LFC,LabRep,LFRef,SRVisSet,interpMethod);
                 case 'local'
                     SRC = matcoeffs.C;
-                    peaksnr_ = SR.progPSNR(LFBRec,SRC,LabRep,LFRef,SRset,interpMethod);
+                    peaksnr_ = SR.progPSNR(LFBRec,SRC,LabRep,LFRef,SRVisSet,interpMethod);
                 otherwise
                     error('Error');
             end
diff --git a/script_write.m b/script_write.m
index e9883e9..4dc59b0 100644
--- a/script_write.m
+++ b/script_write.m
@@ -1,5 +1,7 @@
 clear variables, close all, clc
-ExpeDir = 'C:\Users\edib\Thesis\Code\Projects\+SR\test';
+ProjDir = 'C:\Users\edib\Thesis\Code\Projects\+SR';
+Expe = 'test';
+ExpeDir = fullfile(ProjDir,Expe);
 
 %% Load Light Field, Disparity map and Segmentation Map
 % Segmentation obtained through SLIC with disparity (Xin)
@@ -24,9 +26,8 @@ numPixTot = numView*numPix;
 LFSize = size(Gray);
 
 interpMethod = 'nearest';
-SRMethods = {'global','local'};
-compMethods = {'rank','trace'};
-coding = false;
+compMethods = {'local'};%'global',
+minMethods = {'trace'};%'rank',
 
 %% HEVC Parameter Initialization
 QPmin=0; QPstep=4; QPmax=20;
@@ -37,48 +38,49 @@ HEVCParams.InputBitDepth = '16';
 HEVCParams.InternalBitDepth = '16';
 
 %%
-for SRMethod = SRMethods
-    SRMethod = SRMethod{:};
-    for compMethod = compMethods
-        compMethod = compMethod{:};
+for compMethod = compMethods
+    compMethod = compMethod{:};
+    for minMethod = minMethods
+        minMethod = minMethod{:};
         
         %%
-        SRMethod,compMethod
+        methstr = [compMethod  ' ' minMethod ' minimization']
         
         LFName = 'B';
-        RecDir = fullfile(ExpeDir,'Rec',SRMethod,compMethod);
+        RefDir = fullfile(ExpeDir,compMethod,minMethod,'Ref');
+        mkdir(RefDir);
+        RecDir = fullfile(ExpeDir,compMethod,minMethod,'Rec');
         mkdir(RecDir);
         
-        rangematname = fullfile(RecDir,'range.mat');
+        rangematname = fullfile(RecDir,'..','range.mat');
         matrange = matfile(rangematname,'Writable',true);
         
-        coeffsmatname = fullfile(RecDir,'coeffs.mat');
+        coeffsmatname = fullfile(RecDir,'..','coeffs.mat');
         matcoeffs = matfile(coeffsmatname,'Writable',true);
         
         %% Build super-ray set from light field
-        SRset = SR.LFtoSRCentral(Gray,Labels,Disparity,...
-            interpMethod);
+        SRVisSet = SR.LFtoSRCentral(Gray,Labels,Disparity,interpMethod);
+        SRExtSet = SR.LFtoSR       (Gray,Labels,Disparity,interpMethod);
         
         %%
-        SRRef  = {SRset.Value};
-        SRLab  = {SRset.Label};
-        SRPos  = {SRset.Position};
-        SRDisp = {SRset.Disparity};
+        SRRef  = {SRVisSet.Value};
+        SRLab  = {SRVisSet.Label};
+        SRPos  = {SRVisSet.Position};
+        SRDisp = {SRVisSet.Disparity};
         
         %% Rebuild light field from super-ray set
-        [LFRef,LFLabCent] = SR.SRtoLFCentral(SRset,LFSize,interpMethod);
+        [LFRef,LFLabCent] = SR.SRtoLFCentral(SRVisSet,LFSize,interpMethod);
         
         %% Complete superrays
-        disp(['Method : ' SRMethod]);
-        switch SRMethod
+        switch compMethod
             case 'local'
-                SRComp = SR.completeSR(SRRef,compMethod);
+                SRComp = SR.completeSR(SRRef,minMethod);
                 [SRB,SRC] = SR.factorizeSR(SRComp);
                 LFBRef = SR.ReftoVisCentral(SRB,Labels,SRLab);
-                C = SRC;
+                C = SRC';
             case 'global'
                 LFVis = SR.ReftoVisCentral(SRRef,Labels,SRLab);
-                LFComp = SR.completeLF(LFVis,compMethod);
+                LFComp = SR.completeLF(LFVis,minMethod);
                 [LFBRef,LFC] = SR.factorizeLF(LFComp);
                 C = LFC;
             otherwise
@@ -95,9 +97,10 @@ for SRMethod = SRMethods
         %% HEVC Coding
         for QP = QPList
             HEVCParams.QP = num2str(QP);
-            HEVCParams.saveDir = fullfile(RecDir,num2str(QP));
+            LFParams.refDir = fullfile(RefDir);
+            LFParams.recDir = fullfile(RecDir,num2str(QP));
             
-            %LF.encode(BRef_uint16,LFName,HEVCParams);
+            LF.encode(BRef_uint16,LFName,LFParams,HEVCParams);
         end
     end
 end
-- 
GitLab