function th=TsallisGrayEntropy(imag); %参考文献《基于二维灰度熵及混沌粒子群的图像阈值选取》
%实现Tsallis灰度熵的最大化分割方法(此方法与Tsallis熵的区别在于,Tsallis熵只是考虑直方图的概率
%信息而未考虑图像中目标和背景类内灰度级的均匀性)
clc;
clear;
%imag=imread('cameraman.png');
imag=imread('Infrared footman.png');
subplot(132),imshow(imag)
imag=imag(:,:,1);
subplot(131),imhist(imag)
hold on
MAX=double(max(imag(:)));
MIN=double(min(imag(:)));
[m,n]=size(imag);
img=double(imag);
out_im=zeros(m,n);
[counts x]=imhist(imag);%x对应0~255的灰度级,counts对应每个灰度级的频数(即每个灰度级像素的个数)
h=counts(:);%频数
%hist=counts'./sum(counts(:));%归一化直方图数据
a=0.8; %产生一个0-1之间的随机数
for i=MIN:MAX
if h(i)~=0
st=i-1;
break;
end
end
st
for i=MAX:-1:MIN
if h(i)~=0
nd=i-1;
break;
end
end
nd
H=[];
for t= st+1:nd-1
u0(t)=0;v0(t)=0;
ub(t)=0;vb(t)=0;
for i=0:t
u0(t)=u0(t)+i*h(i+1);
v0(t)=v0(t)+i.^a*h(i+1);
end
for i=t+1:MAX-1
ub(t)=ub(t)+i*h(i+1);
vb(t)=vb(t)+i.^a*h(i+1);
end
if u0(t)*ub(t)~=0
E=v0(t)*vb(t)./(u0(t).^a*ub(t).^a);
else
break;
end
H(t-st+1)=1/(a-1)*(1-E);
end
position=find(H==max(H));
th=st+position-1
x(t)=th;
plot(th,x,'-r*')
hold on
gtext('th=101','color','r')
for i=1:m %用阈值th分割图像
for j=1:n
if(img(i,j)>th)
out_im(i,j)=255;
else
out_im(i,j)=0;
end
end
end
subplot(133),imshow(out_im),title('Tsallis Gray Entropy Threshold')
end