function [contours, a, b] = estimate_subsampled_contours(shape, subs_fact)
% -------------------------------------------------------------------------
% MSc Project - MSc in Communications and Signal Processing
% Department of Electrical and Electronic Engineering
% Imperial College London, 2011
%
% Supervisor  : Dr Pier Luigi Dragotti
% Contributor : Andriy Gelman
% Author      : Jon Onativia Bravo
%
% File        : estimate_subsampled_contours.m
% -------------------------------------------------------------------------
% 
%
% function [contours, a, b] = estimate_subsampled_contours(shape, subs_fact)
%
% INPUT:
%  - 
%
% OUTPUT:
%  - 
%

% Original shape's size
[height, width] = size(shape);

% Output variables
contours         = [];
a                = [];
b                = [];

a_vec = 1:subs_fact;
b_vec = 1:subs_fact;

min_distortion = Inf;

idx = 1;
for cur_a = a_vec
    for cur_b = b_vec
        % Subsample image
        sub_shape = shape(cur_a:subs_fact:height,cur_b:subs_fact:width);

        % Obtain contours of the subsampled image
        cur_verts = estimate_contours_vertices(sub_shape, 0, 1);

        % Reconstruct shape
        approx_shape = reconstruct_subsampled_contours(cur_verts, subs_fact, cur_a, cur_b, width, height);

        % Measure the distortion
        d = get_distortion(shape, approx_shape);

        % Store verts if the current distortion is the smallest
        if d < min_distortion
            min_distortion = d;

            % Store the current information in the output variables
            contours = cur_verts;
            a        = cur_a;
            b        = cur_b;
        end

        idx = idx + 1;
    end
end
