clc; clear all; close all;
%------------------------------------
% This Matlab program demomstrates the SR algorithms in paper:
%   Huihui Song,Lei Zhang, Peikang Wang,Kaihua Zhang,Xin Li "AN ADAPTIVE L1-L2 HYBRID ERROR MODEL TO SUPER-RESOLUTION" ICIP, 2010    
% Author: Huihui Song
% E-mail: freebird@mail.ustc.edu.cn, cslzhang@comp.polyu.edu.hk  
% http://www4.comp.polyu.edu.hk/~cslzhang/
% Date: 19/6/2010
%------------------------------------

o=load('foreman.mat'); %LR images including extracted 5 LR frames with estimation information of PSF and motion model H
o=o.o; 

K = numel(o);
[smv smh]=size(o(1,1).im);
biv=2*smv;
bih=2*smh;

%setting relevant parameters of three comparative methods
alpL2BTV = .2; nuL2BTV = .02;
alpL1BTV = .7; nuL1BTV = .003;
alpHEM = .5; nuHEM = .04;
P=2;

[W, Y] = makeW(biv,bih,o);%generating system matrix
opts = zeros(1,18); % This is the "options" vector for the Netlab "scg" routine, which could be downloaded at "http://www1.aston.ac.uk/ncrg/"
opts(1) = 1; % verbose
opts(2:3) = 0.005; % convergeance criteria
opts(14) = 50;

% Find average image as the initial estimation of the HR image
avim = getAvim(biv,bih,o);

KM = K*smv*smh;
W1 = W*sparse(1:KM,1:KM,ones(KM,1));
x1 = avim(:)';
e = Y'-x1*W1;

ml=median(e);
sigmal=mean(abs(e-ml));
sigmag=std(e,1);
gamma=sigmal/sigmag;
threshold=gamma-.05;

%LR algorithm of L2BTV
tic
[l2im,opts,flog1,plog1,sclog1,rlog1]=L2BTV_superres(W,Y,avim,alpL2BTV,nuL2BTV,P,threshold,opts);
figure(1);imshow(l2im+.5)
result(1,1)=toc;

%LR algorithm of L1BTV
tic;
[l1im,opts,flog2,plog2,sclog2,rlog2,diff2] =L1BTV_superres(W,Y,avim,alpL1BTV,nuL1BTV,P,threshold,opts);
figure(2);imshow(l1im+.5)
result(2,1)=toc;

%LR algorithm of HEM
tic;
[hemim,opts,flog4,plog4,sclog4,rlog4] =HEM_superres(W,Y,avim,alpHEM,nuHEM,P,threshold,opts);
figure(3);imshow(hemim+.5)
result(3,1)=toc;