clear;
% close all
clc
addpath(genpath('./util/'))
use_gpu = 1;
if exist('../+caffe', 'dir')
    addpath('..');
else
    error('Please run this demo from caffe/matlab/demo');
end

% Set caffe mode
if exist('use_gpu', 'var') && use_gpu
    caffe.set_mode_gpu();
    gpu_id = 0;  % we will use the first gpu in this demo
    caffe.set_device(gpu_id);
else
    caffe.set_mode_cpu();
end

model_dir = '../../examples/qu_learning/shadow_removal/';
net_model_rgb = [model_dir 'model_def/deploy/matte_VGG_conv3_conv5_3out3_deploy_ver1.prototxt'];

net_weights_rgb = [model_dir 'model/matte_3_5_same.caffemodel'];


phase = 'test'; % run with phase test (so that dropout isn't applied)
if ~exist(net_weights_rgb, 'file')
    error('Please download CaffeNet from Model Zoo before you run this demo');
end

net_rgb = caffe.Net(net_model_rgb, net_weights_rgb, phase);


size_input = 224;
size_label = 224;


tic
init_name = '_MG_4091.jpg';

im_Shadow = imread(init_name);

[m,n,t]= size(im_Shadow);


im_Shadow1  = double(im_Shadow(:, :, [3, 2, 1]));  % permute channels from RGB to BGR

im_Shadow1(:,:,1) = im_Shadow1(:,:,1) - 104;
im_Shadow1(:,:,2) = im_Shadow1(:,:,2) - 117;
im_Shadow1(:,:,3) = im_Shadow1(:,:,3) - 123;

sub_ii = 1; sub_jj = 1;
ind_tmp = 1;ind_i=1;
im_input = imresize(im_Shadow1,[size_input,size_input],'bicubic');


%% calculating the shadow matte 
data_1(:,:,1:3) = (im_input);
im_data_ALL = {data_1};
output_rgb=net_rgb.forward(im_data_ALL);
output_rgb = output_rgb{1};
output_rgb_o = imresize(output_rgb,[m,n]);

[im_non_sr,im_non_sg,im_non_sb] = shadow_recover(im_Shadow,output_rgb_o(:,:,1),output_rgb_o(:,:,2),output_rgb_o(:,:,3));
im_non_s_o(:,:,1) = im_non_sr;
im_non_s_o(:,:,2) = im_non_sg;
im_non_s_o(:,:,3) = im_non_sb;

%%

figure;imshow([im_Shadow,im_non_s_o,255.*normalization(output_rgb_o)])


