% visual analysis for each segment 
% windowstats return in the order = [M, D, ECH, ECV, EM]


function [Contrast, Focus, Texture] = segmentAnalysis(seg)

% code to divide segments into 6x6 windows
% recall : image size : 480 x 640
%          segment size : 120 * 156
%          window size : 20 * 26

window1 = seg(1:20, 1:26);
window2 = seg(1:20, 27:52);
window3 = seg(1:20, 53:78);
window4 = seg(1:20, 79:104);
window5 = seg(1:20, 105:130);
window6 = seg(1:20, 131:156);
window7 = seg(21:40, 1:26);
window8 = seg(21:40, 27:52);
window9 = seg(21:40, 53:78);
window10 = seg(21:40, 79:104);
window11 = seg(21:40, 105:130);
window12 = seg(21:40, 131:156);
window13 = seg(41:60, 1:26);
window14 = seg(41:60, 27:52);
window15 = seg(41:60, 53:78);
window16 = seg(41:60, 79:104);
window17 = seg(41:60, 105:130);
window18 = seg(41:60, 131:156);
window19 = seg(61:80, 1:26);
window20 = seg(61:80, 27:52);
window21 = seg(61:80, 53:78);
window22 = seg(61:80, 79:104);
window23 = seg(61:80, 105:130);
window24 = seg(61:80, 131:156);
window25 = seg(81:100, 1:26);
window26 = seg(81:100, 27:52);
window27 = seg(81:100, 53:78);
window28 = seg(81:100, 79:104);
window29 = seg(81:100, 105:130);
window30 = seg(81:100, 131:156);
window31 = seg(101:120, 1:26);
window32 = seg(101:120, 27:52);
window33 = seg(101:120, 53:78);
window34 = seg(101:120, 79:104);
window35 = seg(101:120, 105:130);
window36 = seg(101:120, 131:156);


[m1, d1, ech1, ecv1, em1] = windowstats(window1);
[m2, d2, ech2, ecv2, em2] = windowstats(window2);
[m3, d3, ech3, ecv3, em3] = windowstats(window3);
[m4, d4, ech4, ecv4, em4] = windowstats(window4);
[m5, d5, ech5, ecv5, em5] = windowstats(window5);
[m6, d6, ech6, ecv6, em6] = windowstats(window6);
[m7, d7, ech7, ecv7, em7] = windowstats(window7);
[m8, d8, ech8, ecv8, em8] = windowstats(window8);
[m9, d9, ech9, ecv9, em9] = windowstats(window9);
[m10, d10, ech10, ecv10, em10] = windowstats(window10);
[m11, d11, ech11, ecv11, em11] = windowstats(window11);
[m12, d12, ech12, ecv12, em12] = windowstats(window12);
[m13, d13, ech13, ecv13, em13] = windowstats(window13);
[m14, d14, ech14, ecv14, em14] = windowstats(window14);
[m15, d15, ech15, ecv15, em15] = windowstats(window15);
[m16, d16, ech16, ecv16, em16] = windowstats(window16);
[m17, d17, ech17, ecv17, em17] = windowstats(window17);
[m18, d18, ech18, ecv18, em18] = windowstats(window18);
[m19, d19, ech19, ecv19, em19] = windowstats(window19);
[m20, d20, ech20, ecv20, em20] = windowstats(window20);
[m21, d21, ech21, ecv21, em21] = windowstats(window21);
[m22, d22, ech22, ecv22, em22] = windowstats(window22);
[m23, d23, ech23, ecv23, em23] = windowstats(window23);
[m24, d24, ech24, ecv24, em24] = windowstats(window24);
[m25, d25, ech25, ecv25, em25] = windowstats(window25);
[m26, d26, ech26, ecv26, em26] = windowstats(window26);
[m27, d27, ech27, ecv27, em27] = windowstats(window27);
[m28, d28, ech28, ecv28, em28] = windowstats(window28);
[m29, d29, ech29, ecv29, em29] = windowstats(window29);
[m30, d30, ech30, ecv30, em30] = windowstats(window30);
[m31, d31, ech31, ecv31, em31] = windowstats(window31);
[m32, d32, ech32, ecv32, em32] = windowstats(window32);
[m33, d33, ech33, ecv33, em33] = windowstats(window33);
[m34, d34, ech34, ecv34, em34] = windowstats(window34);
[m35, d35, ech35, ecv35, em35] = windowstats(window35);
[m36, d36, ech36, ecv36, em36] = windowstats(window36);


ma = [m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12];
mb = [m13 m14 m15 m16 m17 m18 m19 m20 m21 m22 m23 m24];
mc = [m25 m26 m27 m28 m29 m30 m31 m32 m33 m34 m35 m36];

m = [ma mb mc];

da = [d1 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 d12];
db = [d13 d14 d15 d16 d17 d18 d19 d20 d21 d22 d23 d24];
dc = [d25 d26 d26 d28 d29 d30 d31 d32 d33 d34 d35 d36];

d = [da db dc];

echa = [ech1 ech2 ech3 ech4 ech5 ech6 ech7 ech8 ech9 ech10 ech11 ech12];
echb = [ech13 ech14 ech15 ech16 ech17 ech18 ech19 ech20 ech21 ech22 ech23 ech24];
echc = [ech25 ech26 ech27 ech28 ech29 ech30 ech31 ech32 ech33 ech34 ech35 ech36];

ech = [echc echb echc];

ecva = [ecv1 ecv2 ecv3 ecv4 ecv5 ecv6 ecv7 ecv8 ecv9 ecv10 ecv11 ecv12];
ecvb = [ecv13 ecv14 ecv15 ecv16 ecv17 ecv18 ecv19 ecv20 ecv21 ecv22 ecv23 ecv24];
ecvc = [ecv25 ecv26 ecv27 ecv28 ecv29 ecv30 ecv31 ecv32 ecv33 ecv34 ecv35 ecv36];

ecv = [ecva ecvb ecvc];

ema = [em1 em2 em3 em4 em5 em6 em7 em8 em9 em10 em11 em12];
emb = [em13 em14 em15 em16 em17 em18 em19 em20 em21 em22 em23 em24]; 
emc = [em25 em26 em27 em28 em29 em30 em31 em32 em33 em34 em35 em36];

em = [ema emb emc];


meanM = sum(m)/size(m, 2);
meanD = sum(d)/size(d, 2);
meanECH = sum(ech)/size(ech, 2);
meanECV = sum(ecv)/size(ecv, 2);
meanEM = sum(em)/size(em, 2);

devM = abs(double(m) - meanM);
devD = abs(double(d) - meanD);
devECH = abs(double(ech) - meanECH);
devECV = abs(double(ecv) - meanECV);
devEM = abs(double(em) - meanEM);

meanDevM = sum(devM)/size(devM, 2);
meanDevD = sum(devD)/size(devD, 2)
meanDevECH = sum(devECH)/size(devECH, 2)
meanDevECV = sum(devECV)/size(devECV, 2)
meanDevEM = sum(devEM)/size(devEM, 2);

% Constrast = M(S)_D(W) + D(S)_M(W)

Contrast = meanD + meanDevM;

% Focus = M(S)_EM(W)

Focus = meanEM;

% Texture = 1/D(S)_D(W) + 1/min(D(S)_ECH(W), D(S)_ECV(W))

Texture = (1/(meanDevD)) + (1/min(meanDevECH, meanDevECV));













