% For each window, calculate statistical measures

function [M, D, ECH, ECV, EM] = windowstats(window)

% r = no of rows
% c = no of columns

r = size(window, 1);
c = size(window, 2);

% find mean pixel intensity M(W)

sumTotal = 0;
for i = 1:r,
  for j = 1:c,
    sumTotal = sumTotal + double(window(i, j));
  end
end

M = sumTotal/(r*c);

sumDev = 0; 
for i = 1:r   
  for j = 1:c
    sumDev = sumDev + abs(double(window(i, j)) - M);
  end
end

D = sumDev/(r*c);


% find threshold tau(W) = lambda(D(W)) choose lambda between 0.8
% and 1.2

lambda = 0.75;
tau = lambda * D;

% find horizontal edge count ECH(W)

ECH = 0;
for i = 1:r,
  for j = 1:c-1,  
    absDifference = abs(double(window(i, j)) - double(window(i, j+1)));
    ECH = ECH + thld(absDifference, tau);  
  end
end

ECH;

% find vertical edge count ECV(W)

ECV = 0;
for i = 1:r-1,
  for j = 1:c,  
    absDifference = abs(double(window(i, j)) - double(window(i+1, j)));
    ECV = ECV + thld(absDifference, tau);
  end
end

ECV;

%find normalized mean edge magnitude EMH(W)

EMH = 0;
for i = 1:r,   
  for j = 1:c-1,
    absDifference = abs(double(window(i, j)) - double(window(i, j+1)));
    EMH = EMH + clip(absDifference - tau);
  end
end


%find EMV(W)

EMV = 0;
for i = 1:r-1,   
  for j = 1:c,
    absDifference = abs(double(window(i, j)) - double(window(i+1, j)));
    EMV = EMV + clip(absDifference - tau);
  end
end

% find normalized mean edge magnitude EM(W)

EM = ((EMH + EMV)/(ECH + ECV)) / D;





















