function T = Texture(Image,imgweishu)%%%%%%提取纹理,包括灰度共生矩阵和直方图灰度统计
[M,N,u] = size(Image);
k=0;
if (imgweishu==8)
for i = 1:M
f=false;
g=false;
starthang=0;
startlie=0;
endlie=0;
for j = 1:N
if (Image(i,j,1)~=0 & Image(i,j,2)~=0 & Image(i,j,3)~=0 & f==false)
f=true;
starthang=i;
startlie=j;
endlie=j;
end
if (j>1 & Image(i,j,1)==0 & Image(i,j,2)==0 & Image(i,j,3)==0 & g==false & f==true)
g=true;
endlie=j-1;
end
if (j==N & Image(i,j,1)~=0 & Image(i,j,2)~=0 & Image(i,j,3)~=0)
endlie=N;
end
end
if (starthang~=0)
k=k+1;
buguizehang(k)=starthang;
buguizelie(k,1)=startlie;
buguizelie(k,2)=endlie;
end
end
end
%%%%%%%针对16位影像
if (imgweishu==16)
for i = 1:M
f=false;
g=false;
starthang=0;
startlie=0;
endlie=0;
for j = 1:N
if (Image(i,j,1)~=256 & Image(i,j,2)~=256 & Image(i,j,3)~=256 & f==false)
f=true;
starthang=i;
startlie=j;
endlie=j;
end
if (j>1 & Image(i,j,1)==256 & Image(i,j,2)==256 & Image(i,j,3)==256 & g==false & f==true)
g=true;
endlie=j-1;
end
if (j==N & Image(i,j,1)~=256 & Image(i,j,2)~=256 & Image(i,j,3)~=256)
endlie=N;
end
end
if (starthang~=0)
k=k+1;
buguizehang(k)=starthang;
buguizelie(k,1)=startlie;
buguizelie(k,2)=endlie;
end
end
end
%--------------------------------------------------------------------------
%1.将各颜色分量转化为灰度
%--------------------------------------------------------------------------
Gray = double(0.3*Image(:,:,1)+0.59*Image(:,:,2)+0.11*Image(:,:,3));
%Gray = rgb2gray(Image);
%--------------------------------------------------------------------------
%2.为了减少计算量,对原始图像灰度级压缩,将Gray量化成16级
%--------------------------------------------------------------------------
for i = 1:M
for j = 1:N
for n = 1:256/16
if (n-1)*16<=Gray(i,j)&Gray(i,j)<=(n-1)*16+15
Gray(i,j) = n-1;
end
end
end
end
%--------------------------------------------------------------------------
%3.计算四个共生矩阵P,取距离为1,角度分别为0,45,90,135
%--------------------------------------------------------------------------
P = zeros(16,16,4);
for m = 1:16
for n = 1:16
% for i = 1:M
% for j = 1:N
for k=1:size(buguizehang,2)
i = buguizehang(k);
for j = buguizelie(k,1):buguizelie(k,2)
if j<N&Gray(i,j)==m-1&Gray(i,j+1)==n-1
P(m,n,1) = P(m,n,1)+1;
P(n,m,1) = P(m,n,1);
end
if i>1&j<N&Gray(i,j)==m-1&Gray(i-1,j+1)==n-1
P(m,n,2) = P(m,n,2)+1;
P(n,m,2) = P(m,n,2);
end
if i<M&Gray(i,j)==m-1&Gray(i+1,j)==n-1
P(m,n,3) = P(m,n,3)+1;
P(n,m,3) = P(m,n,3);
end
if i<M&j<N&Gray(i,j)==m-1&Gray(i+1,j+1)==n-1
P(m,n,4) = P(m,n,4)+1;
P(n,m,4) = P(m,n,4);
end
end
end
if m==n
P(m,n,:) = P(m,n,:)*2;
end
end
end
%%---------------------------------------------------------
% 对共生矩阵归一化
%%---------------------------------------------------------
for n = 1:4
if (sum(sum(P(:,:,n)))==0)
P(:,:,n)=100000000;
else
P(:,:,n) = P(:,:,n)/sum(sum(P(:,:,n)));
end
end
%--------------------------------------------------------------------------
%4.对共生矩阵计算能量、熵、惯性矩、相关4个纹理参数
%--------------------------------------------------------------------------
H = zeros(1,4);
I = H;
Ux = H; Uy = H;
deltaX= H; deltaY = H;
C =H;
for n = 1:4
E(n) = sum(sum(P(:,:,n).^2)); %%能量
for i = 1:16
for j = 1:16
if P(i,j,n)~=0
H(n) = -P(i,j,n)*log(P(i,j,n))+H(n); %%熵
end
I(n) = (i-j)^2*P(i,j,n)+I(n); %%惯性矩
Ux(n) = i*P(i,j,n)+Ux(n); %相关性中μx
Uy(n) = j*P(i,j,n)+Uy(n); %相关性中μy
end
end
end
for n = 1:4
for i = 1:16
for j = 1:16
deltaX(n) = (i-Ux(n))^2*P(i,j,n)+deltaX(n); %相关性中σx
deltaY(n) = (j-Uy(n))^2*P(i,j,n)+deltaY(n); %相关性中σy
C(n) = i*j*P(i,j,n)+C(n);
end
end
if (deltaX(n)==0||deltaY(n)==0)
C(n)=100000000;
else
C(n) = (C(n)-Ux(n)*Uy(n))/deltaX(n)/deltaY(n); %相关性
end
end
%--------------------------------------------------------------------------
%求熵、惯性矩作为最终2个维纹理特征
%--------------------------------------------------------------------------
T(1) = mean(E); T(2) = sqrt(cov(E));
T(3) = mean(H); T(4) = sqrt(cov(H));
T(5) = mean(I); T(6) = sqrt(cov(I));
T(7) = mean(C); T(8) = sqrt(cov(C));
numhuidu=0;
Gray = double(0.3*Image(:,:,1)+0.59*Image(:,:,2)+0.11*Image(:,:,3));
for k=1:size(buguizehang,2)
i = buguizehang(k);
for j = buguizelie(k,1):buguizelie(k,2)
numhuidu=numhuidu+1;
end
end
tongjihuidu=zeros(numhuidu,1);
num=0;
for k=1:size(buguizehang,2)
i = buguizehang(k);
for j = buguizelie(k,1):buguizelie(k,2)
num=num+1;
tongjihuidu(num)=Gray(i,j);
end
end
tongji=tabulate(tongjihuidu);
m=sum(tongji(:,1).*(tongji(:,3)./100));%%%统计均值
fangcha=sqrt(sum(((tongji(:,1)-m).^2).*(tongji(:,3)./100)));
nengliang=sum((tongji(:,3)./100).^2);
if (abs(fangcha)==0)
fengdu=10000000000;
else
fengdu=sum((((tongji(:,1)-m).^4).*(tongji(:,3)./100)))/(fangcha^4);
end
T(9) = m;
T(10) =fangcha;
T(11)=nengliang;
T(12)=fengdu;