MATLAB中利用灰度共生矩阵(GLCM)提取多张图片的纹理特征
查了好多资料,纹理特征提取都是单图的,自己拼拼凑凑搞出来了可以同时提取多图的纹理特征(利用灰度共生矩阵),并将最后特征矩阵保存在excel表中,这样在后续特征矩阵拼凑的时候会比较方便一点。
clear all;
clc;
%**************************************************************************
% 图像检索——纹理特征
%基于共生矩阵纹理特征提取,d=1,θ=0°,45°,90°,135°共四个矩阵
%所用图像灰度级均为256
%function : T=Texture(Image)
%Image : 输入图像数据
%T : 返回15维纹理特征行向量
%**************************************************************************
% function T = Texture(Image)
niiname=dir('C:\Users\Mus\Desktop\处理后的图像\*.png');%输入你图片的地址
str1='C:\Users\Mus\Desktop\处理后的图像\';
for ii=1:8
str=[str1 niiname(ii).name];
A = imread(str);
[xx,yy,zz]=size(A);
for i=1:zz
for j=1:yy
for k=1:xx
if(A(k,j,i) ~= 0)
a=i;
break;
end
end
end
end
G=A(:,:,a);
Gray=imresize(G,1/40);
[M,N] = size(Gray);
%M = 128;
%N = 128;
%--------------------------------------------------------------------------
%1.将各颜色分量转化为灰度
%--------------------------------------------------------------------------
% Gray = double(0.3*Image(:,:,1)+0.59*Image(:,:,2)+0.11*Image(:,:,3));
%--------------------------------------------------------------------------
%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
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
P(:,:,n) = P(:,:,n)/sum(sum(P(:,:,n)));
end
%--------------------------------------------------------------------------
%4.对共生矩阵计算能量(角二阶矩)、熵、惯性矩(对比度)、相关及逆差距5个纹理参数
%--------------------------------------------------------------------------
H = zeros(1,4);
I = H;
Ux = H;
Uy = H;
deltaX= H;
deltaY = H;
C =H;
L=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);
L(n)=P(i,j,n)^2/(1+(i-j)^2)+L(n);%逆差距
end
end
C(n) = (C(n)-Ux(n)*Uy(n))/deltaX(n)/deltaY(n); %相关性
end
%--------------------------------------------------------------------------
%求 求能量、熵、惯性矩、相关的均值、标准差和方差作为15维纹理特征
%--------------------------------------------------------------------------
a1 = mean(E) ;
b1 = sqrt(cov(E));
c1=var(E);
a2 = mean(H);
b2 = sqrt(cov(H));
c2=var(H);
a3 = mean(I) ;
b3 = sqrt(cov(I));
c3=var(I);
a4 = mean(C);
b4 = sqrt(cov(C));
c4=var(C);
a5=mean(L);
b5=sqrt(cov(L));
c5=var(L);
T(ii,:)={a1,b1,c1,a2,b2,c2,a3,b3,c3,a4,b4,c4,a5,b5,c5};
%将矩阵写入ecxel中
filename = 'GLCM纹理特征矩阵.xlsx';
writecell(T,filename,'Sheet',1,'Range','A1');
end
|