function F_crs=txsim_crs(f)
% TXSIM_CRS  Coarseness texture measure
%	F_crs=txsim_crs(f) computes the coarseness measure suggested by
%	H. Tamura, S. Mori, T. Yamawaki, ``Textural Features Corresponding
%	to Visual Perception'', IEEE Trans. Sys., Man, Cyb., June 1978
%	
%	Scaled by log10, to match other measure better.

[M,N]=size(f);
S_best=zeros(M,N);
E_max=zeros(M,N);

for NeighSize=2.^(0:5)
  A=avgsmth(f,NeighSize,NeighSize);
  E_h=abs(A-hshift(A,NeighSize));  
  E_v=abs(A-vshift(A,NeighSize));
  E_max_old=E_max;
  E_max=E_max .* ((E_max>=E_h) & (E_max>=E_v)) ...
    + E_v .* ((E_v>=E_h) & (E_v>E_max)) ...
    + E_h .* ((E_h > E_v) & (E_h>E_max));
  S_best=S_best .* (E_max_old == E_max) + NeighSize * (E_max_old ~= E_max);
end

F_crs=log10(mean(S_best(:)));