function fd=box_frac_dem(X);
% 差分盒维数
% Example:
% X=double(imread('rice.tif'));
% fd=box_frac_dem(X);
% Author email of the program:% zjliu2001@163.com
%
% Reference:
% Sarkar N,Chaudhuri B B. An efficient approach to estimate
% fractal dimensionof textural images [J].Pattern Recognition,
% 1992,25(9):1035-1041
if size(X,1)~=size(X,2);
error('The size of X must NxN.');
end
B=size(X,1);
if mod(log2(B),1)>0;
error('The size of X must 2^n');
end
t=log2(B);
s=2.^(1:t);
Nr=zeros(1,t);
for k=1:t;
d=s(k);
h=256/d; % Let B instead of 256 for the size [2^nX2^n]
for m=1:h;
for n=1:h;
A=X(d*(m-1)+[1:d],d*(n-1)+[1:d]);
mn=min(A(1:end));
mx=max(A(1:end));
nr=fix(mx/d)-fix(mn/d)+1; %k=fix(mx/d),l=fix(mn/d)
Nr(k)=Nr(k)+nr;
end
end
end
r=B./s;
p=polyfit(log10(r),log10(Nr),1);
fd=p(1);