function t = statxture(f,scale)
%   STATXTURE Computes statistical measures of texture in an image
%   T = STATXURE(F, SCALE) computes six measures of texture from an image
%   (region) F. Parameter SCALE is a 6-dim row vector whose
%   elements multiply the 6 corresponding elements of T for scaling 
%   purposes. If SCALE is not provided it defaults to all 1s. The
%   output T is 6-by-1 vector with the following elements:
%   T(1) = Average gray level
%   T(2) = Average contrast
%   T(3) = Measure of smoothness
%   T(4) = Third moment
%   T(5) = Measure of uniformity
%   T(6) = Entropy

if nargin ==1
    scale (1:6) = 1;
else %Make sure its a row vector.
    scale = scale(:);
end
%Obtain histogram and normalize it.
p = imhist(f);
p = p./numel(f);
L = length(p);

%Compute the three moments. We need the the unnormalized ones
%from function statmoments. These are in vector mu.
[v,mu]=statmoments(p,3);

%Compute the six texture measures:
%Average gray level.
t(1) = mu(1);
% Standart deviation
t(2) = mu(2).^0.5;
% Smoothness.
% First normalize the variance to [0 1] by
% dividing it by (L-1)^2.
varn = mu(2)/(L-1)^2;
t(3) = 1 -1/(1 + varn);
% Third moment (normalized by (L-1)^2 also).
t(4) = mu(3)/(L-1)^2;
% Uniformity.
t(5) = sum(p.^2);
% Entropy.
t(6) = -sum(p.*(log2(p+ eps)));

% scale the values

t = t.*scale;
