close all;clear all;clc;

id=1;
Im_name{id} = 'Max_03012013_Pos007_S001';id=id+1;
Im_name{id} = 'Max_02182013_Pos006_S001';id=id+1;
Im_name{id} = 'Max_03012013_Pos001_S001';id=id+1;
Im_name{id} = 'Max_02182013_Pos005_S001';id=id+1;
Im_name{id} = 'Max_03012013_Pos016_S001';id=id+1;
Im_name{id} = 'Max_03012013_Pos003_S001';id=id+1;
Im_name{id} = 'Max_03012013_Pos004_S001';id=id+1;
Im_name{id} = 'Max_03012013_Pos006_S001';id=id+1;
Im_name{id} = 'Max_02182013_Pos008_S001';id=id+1;
Im_name{id} = 'Max_02182013_Pos010_S001';id=id+1;
Im_name{id} = 'Max_03012013_Pos009_S001';id=id+1;
Im_name{id} = 'Max_02182013_Pos007_S001';id=id+1;
Im_name{id} = 'Max_02182013_Pos011_S001';id=id+1;
Im_name{id} = 'Max_02182013_Pos012_S001';id=id+1;
Im_name{id} = 'Max_03012013_Pos011_S001';id=id+1;
Im_name{id} = 'Max_03012013_Pos015_S001';id=id+1;
Im_name{id} = 'Max_03012013_Pos010_S001';id=id+1;
Im_name{id} = 'Max_02182013_Pos016_S001';id=id+1;
Im_name{id} = 'Max_03012013_Series007';id=id+1;
Im_name{id} = 'Max_03012013_Pos013_S001';
for i=1:length(Im_name),
    IM{i} = imread(['Proc\',Im_name{i},'.tif']);
end

%% Dorsal Side
close all
IM_vert_comb = [];
i_mid = 200;
for i=1:length(Im_name),
    IM_cut = [fliplr(IM{i}(:, 1:i_mid)), fliplr(IM{i}(:, (end-i_mid):end))];
    Inten(i) = mean(mean((IM_cut)));
end
Inten_factor = [1.3,.8,1.4,1,1.3,1,1,1,.6,.6,1,.5,.6,.5,1,1,1,.5,1];
for i=1:length(Im_name),
    IM_cut = [fliplr(IM{i}(:, 1:i_mid)), fliplr(IM{i}(:, (end-i_mid):end))];
    IM_vert_comb = [IM_vert_comb;IM_cut*Inten_factor(i)];
end

figure, image(IM_vert_comb);
%% Ventral Side
%close all;
IM_vert_comb = [];
width = 500*2;
for i=1:length(Im_name),
    if i<=12, 
        i_mid = round(size(IM{i}, 2)/2);
    else
        [~, i_mid] = min(sum(IM{i}));
    end
    IM_cut = IM{i}(:, (i_mid-width/2):(i_mid+width/2));
    IM_vert_comb = [IM_vert_comb;IM_cut*Inten_factor(i)];
end
figure, image(IM_vert_comb);
%%
figure, hold on;
for i=1:length(IM),
    plot(linspace(0,1,size(IM{i}, 2)), sum(IM{i})/max(sum(IM{i})));
end


%%
hold off;
i = 13;S = sum(IM{i}); D = mean(S([1:round(end/5),round(4*end/5):end]));
plot(linspace(0,1,size(IM{i}, 2)), S/D, 'm', 'linewidth', 3);hold on;
i = 17;S = sum(IM{i}); D = mean(S([1:round(end/5),round(4*end/5):end]));
plot(linspace(0,1,size(IM{i}, 2)), S/D, 'g', 'linewidth', 3);
i = 18;S = sum(IM{i}); D = mean(S([1:round(end/5),round(4*end/5):end]));
plot(linspace(0,1,size(IM{i}, 2)), S/D, 'b', 'linewidth', 3);
i = 20;S = sum(IM{i}); D = mean(S([1:round(end/5),round(4*end/5):end]));
plot(linspace(0,1,size(IM{i}, 2)), S/D, 'r', 'linewidth', 3);
legend('A', 'B', 'C', 'D');

%%

i = 13;S = sum(IM{i}); D = mean(S([1:round(end/5),round(4*end/5):end]));
subplot(4,1,1), plot(linspace(0,1,size(IM{i}, 2)), S/D, 'm', 'linewidth', 3);
legend('A');
i = 17;S = sum(IM{i}); D = mean(S([1:round(end/5),round(4*end/5):end]));
subplot(4,1,2), plot(linspace(0,1,size(IM{i}, 2)), S/D, 'g', 'linewidth', 3);
legend('B');
i = 18;S = sum(IM{i}); D = mean(S([1:round(end/5),round(4*end/5):end]));
subplot(4,1,3), plot(linspace(0,1,size(IM{i}, 2)), S/D, 'b', 'linewidth', 3);
legend('C');
i = 20;S = sum(IM{i}); D = mean(S([1:round(end/5),round(4*end/5):end]));
subplot(4,1,4), plot(linspace(0,1,size(IM{i}, 2)), S/D, 'r', 'linewidth', 3);
legend('D');

%%
win_size = 150;
for i=1:length(IM)
  mid_id = round(size(IM{i}, 2)/2);
  IM_meso_sum(i) = sum(sum(IM{i}(:, (mid_id-win_size):(mid_id+win_size))))/(size(IM{i}, 1))/(2*win_size);
  IM_ecto_left_sum(i) = sum(sum(IM{i}(:, 1:(mid_id-win_size))))/(size(IM{i}, 1))/(mid_id-win_size);
  IM_ecto_right_sum(i) = sum(sum(IM{i}(:, (mid_id+win_size):end)))/(size(IM{i}, 1))/(2*mid_id - mid_id+win_size);
end

plot(IM_meso_sum)
hold on
plot(IM_ecto_left_sum, 'r')
plot(IM_ecto_right_sum, 'm')



