%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% This script is used to reproduce the results in the following papers:
%%%% 1) Junzhou Huang, Shaoting Zhang and Dimitris Metaxas. " Fast Optimization 
%%%%    for Mixture Prior Models ", In Proc. of the 11th European Conference 
%%%%    on Computer Vision, ECCV'10, Crete, Greece, September 2010.
%%%%
%%%% 2) Junzhou Huang, Shaoting Zhang, Hongsheng Li, Dimitris Metaxas,
%%%%    "Composite Splitting Algorithms for Convex Optimization", Computer 
%%%%     Vision and Image Understanding, Volume 115, Number 12, pp. 1610-1622, 
%%%%     December 2011.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%   Written by Junzhou Huang, Dept. CSE, University of Texas Arlington
%%%%   Please contact jzhuang@uta.edu if you have any questions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear;
close all;

for ii=1:1,
n=256;
inData0 = double(imread('Fish256.jpg','jpg')); 
ratio=0.5;
temp=randperm(n*n);k=round((1-ratio)*n*n);
mark = zeros(n, n); mark(temp(1:k))=1;
mark=repmat(mark, [1 1 3]); mark=logical(mark);
inData=inData0; inData(mark)=255;

figure; subplot(2,2,1); imshow(uint8(inData));
subplot(2,2,2); imshow(255*mark);
subplot(2,2,3); imshow(uint8(inData0));
% of the missing value: 1 means mising, 0 means observed
                                        
maxIter =300;                
initial = [];                          
                                          

%% parameters setting %%
alpha = [1, 1, 1];                           
beta = [1, 1, 1];                   
gamma = [100, 100, 0];             
 

%% tensor completion
% rImg:     recover Result 
% errList:  the function value
% R:        the real rank value of each mode

tt=cputime;
[rImg1, errList1,snrList1, timeList1] = LRTC(inData, alpha, beta, gamma, mark, maxIter, initial,inData0);
time1=cputime-tt;

tt=cputime;
for i=1:3
[rImg2(:,:,i), errList(:,i), snrList(:,i), timeList(:,i)] = APG_MC(inData(:,:,i), gamma(1)/2, mark(:,:,i), maxIter, inData0(:,:,i));
end
time2=cputime-tt;
errList2=sqrt(sum(errList.*errList,2));
snrList2=mean(snrList, 2);
timeList2=sum(timeList, 2);

tt=cputime;
[rImg3, errList3, snrList3, timeList3] = FCSA_TC(inData, gamma/2, mark, maxIter, inData0);
time3=cputime-tt;

RSE(:,ii,1)=errList1; RSE(:,ii,2)=errList2; RSE(:,ii,3)=errList3; 
Time(:,ii,1)=timeList1; Time(:,ii,2)=timeList2; Time(:,ii,3)=timeList3;

end

no=3;

RSE_M(:,:)=mean(RSE, 2); RSE_S(:,:)=std(RSE, 0, 2);
Time_M(:,:)=mean(Time, 2); Time_S(:,:)=std(Time, 0, 2);

no1=3; no2=0;
num=size(RSE_M,1); 
start=1;

figure; hold on;
plot(RSE_M(start:num,1), 'b--', 'linewidth', no1);
plot(RSE_M(start:num,2), 'g-.', 'linewidth', no1);
plot(RSE_M(start:num,3), 'r-', 'linewidth', no1);
legend('CGD-LRTC', 'APG-LRMC', 'FCSA-LRTC');
xlabel('Iterations'); ylabel('RSE'); box on;
textobj = findobj('type', 'text');
set(textobj, 'fontsize', 16);
h_xlabel = get(gca,'XLabel');
set(h_xlabel,'FontSize',16); 
h_xlabel = get(gca,'YLabel');
set(h_xlabel,'FontSize',16); 

figure; hold on;
plot(Time_M(start:num,1), 'b--', 'linewidth', no1);
plot(Time_M(start:num,2), 'g-.', 'linewidth', no1);
plot(Time_M(start:num,3), 'r-', 'linewidth', no1);
legend('CGD-LRTC', 'APG-LRMC', 'FCSA-LRTC');
xlabel('Iterations'); ylabel('CPU Time (s)'); box on;
textobj = findobj('type', 'text');
set(textobj, 'fontsize', 16);
h_xlabel = get(gca,'XLabel');
set(h_xlabel,'FontSize',16); 
h_xlabel = get(gca,'YLabel');
set(h_xlabel,'FontSize',16); 
