

clear all
close all
clc

% file_name = 'tsukuba_extracted4.mat';
% file_name = 'animal_farm.mat';
% file_name = 'cones.mat';
file_name = 'teddy.mat';

load(file_name, 'D')
load(file_name, 'label')

% Obtain the stack of labels
layer_stack     = generate_layer_stack(label, D);
[r, c, k]       = size(layer_stack);

% Try to obtain background's layer approximation
layer  = layer_stack(:,:,end);
verts1 = estimate_contours_vertices(layer, 3, 31, true);
layer1 = contour2shape(verts1, c, r);
d1     = get_distortion(layer_stack(:,:,end), layer1);

% Modify it to add a line of zeros around the borders
layer  = padarray(layer_stack(:,:,end), [1 1]);
verts2 = estimate_contours_vertices(layer, 3, 31, true);
layer2 = contour2shape(verts2, c+2, r+2);
layer2 = layer2(2:end-1, 2:end-1);
d2     = get_distortion(layer_stack(:,:,end), layer2);

disp(['Original distortion : ' num2str(d1) ' - verts_len : ' num2str(size(verts1,2))]);
disp(['Modified distortion : ' num2str(d2) ' - verts_len : ' num2str(size(verts2,2))]);

figure
imagesc(layer1)
figure
imagesc(layer2)
