不同物体的散射特性不同,因此不同的物体表现在图像上将会有不同的亮度和不同的纹理。当图像的局部有较小的方差时,则灰度值占有支配地位,当图像的局部有较大的方差时,则纹理占有支配地位。纹理是和局部灰度及其空间组织相联系的,纹理在识别感兴趣的目标和地区中有着非常重要的作用。
灰度共生矩阵表示了灰度的空间依赖性,它表示了在一种纹理模式下的像素灰度的空间关系。它的弱点是没有完全抓住局部灰度的图形特点,因此对于较大的局部,此方法的效果不太理想。灰度共生矩阵为方阵,维数等于图像的灰度级。灰度共生矩阵中的元素(i,j)的值表示了在图像中其中一个像素的灰度值为i,另一个像素的灰度值为j,并且相邻距离为d,方向为A的这样两个像素出现的次数。在实际应用中A一般选择为0°、45°、90°、135°。一般来说灰度图像的灰度级为256,在计算由灰度共生矩阵推导出的纹理特征时,要求图像的灰度级远小于256,主要是因为矩阵维数较大而窗口的尺寸较小则灰度共生矩阵不能很好表示纹理,如要能够很好表示纹理则要求窗口尺寸较大,这样使计算量大大增加,而且当窗口尺寸较大时对于每类的边界区域误识率较大。所以在计算灰度共生矩阵之前需要对图像进行直方图规定化,以减小图像的灰度级,一般规定化后的图像的灰度级为8或16。由灰度共生矩阵能够导出许多纹理特征。
效果图
主函数
clc;close all;clear;
%%
name_photo='复叶槭(灰叶枫木)';
Image = imread(strcat(name_photo,'.png'));
T = Texture(Image);
X=[1,2,3,4];
data_0=[T(1),T(2),T(3),T(4),T(5),T(6)];
data_45=[T(7),T(8),T(9),T(10),T(11),T(12)];
data_90=[T(13),T(14),T(15),T(16),T(17),T(18)];
data_135=[T(19),T(20),T(21),T(22),T(23),T(24)];
data=[data_0;data_45;data_90;data_135];
%%
%作归一化图
figure('numbertitle','off','name',strcat(name_photo,'归一化特征参数'))
data_1=[data_0(1),data_45(1),data_90(1),data_135(1)];
data_2=[data_0(2),data_45(2),data_90(2),data_135(2)];
data_3=[data_0(3),data_45(3),data_90(3),data_135(3)];
data_4=[data_0(4),data_45(4),data_90(4),data_135(4)];
data_5=[data_0(5),data_45(5),data_90(5),data_135(5)];
data_6=[data_0(6),data_45(1),data_90(6),data_135(6)];
data_1=(data_1-min(data_1))./(max(data_1)-min(data_1));
data_2=(data_2-min(data_2))./(max(data_2)-min(data_2));
data_3=(data_3-min(data_3))./(max(data_3)-min(data_3));
data_4=(data_4-min(data_4))./(max(data_4)-min(data_4));
data_5=(data_5-min(data_5))./(max(data_5)-min(data_5));
data_6=(data_6-min(data_6))./(max(data_6)-min(data_6));
subplot(2,3,1);plot(X,data_1,'r');title('粗糙度');set(gca,'xticklabel',{'0°','45°','90°','135°'});
subplot(2,3,2);plot(X,data_2,'g');title('惯性矩');set(gca,'xticklabel',{'0°','45°','90°','135°'});
subplot(2,3,3);plot(X,data_3,'b');title('纹理熵');set(gca,'xticklabel',{'0°','45°','90°','135°'});
subplot(2,3,4);plot(X,data_4,'c');title('相关度');set(gca,'xticklabel',{'0°','45°','90°','135°'});
subplot(2,3,5);plot(X,data_5,'m');title('方差 ');set(gca,'xticklabel',{'0°','45°','90°','135°'});
subplot(2,3,6);plot(X,data_6,'k');title('逆差据');set(gca,'xticklabel',{'0°','45°','90°','135°'});
%% 生成表格行列名称,m行n列
% m=4;n=6;
column_name=['粗糙度';'惯性矩';'纹理熵';'相关度';'方差 ';'逆差据'];
row_name=[' 0°';' 45°';' 90°';'135°'];
%% 表格作图
set(figure('numbertitle','off','name',name_photo),'position',[25 25 450 450]);
uitable(gcf,'Data',data,'Position',[10 25 600 400],'Columnname',column_name,'Rowname',row_name);
子函数(共生矩阵纹理提取)
%**************************************************************************
%图像检索——纹理特征
%基于共生矩阵纹理特征提取,d=1,θ=0°,45°,90°,135°共四个矩阵
%所用图像灰度级均为256
%参考《基于颜色空间和纹理特征的图像检索》
%function : T=Texture(Image)
%Image : 输入图像数据
%T : 返回八维纹理特征行向量
%**************************************************************************
function T = Texture(Image)
% Image = imread('1.png');
[M,N,~] = size(Image);
% M = 256;
% N = 256;
%--------------------------------------------------------------------------
%1.将各颜色分量转化为灰度
%--------------------------------------------------------------------------
%Gray = double(0.3*Image(:,:,1)+0.59*Image(:,:,2)+0.11*Image(:,:,3));
Gray=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
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.对共生矩阵计算粗糙度A1, 惯性矩A2,纹理熵A3,相关度A4,方差A5,逆差矩阵A6
%--------------------------------------------------------------------------
A3 = zeros(1,4);
Ux = A3; Uy = A3;
deltaX= A3; deltaY = A3;
A4 =A3;A1=A3;u=A3;
A5 =A3;A6 =A3;A2 =A3;
for n = 1:4
u(n)=sum(sum(P(:,:,n)))/16*16;
for i = 1:16
for j = 1:16
if P(i,j,n)~=0
A3(n) = -P(i,j,n)*log(P(i,j,n))+A3(n); %%熵
end
A1(n) = P(i,j,n)^2+A1(n); %粗糙度
A2(n) = (i-j)^2*P(i,j,n)+A2(n); %%惯性矩
A5(n) = P(i,j,n)*(i-u(n))^2+A5(n); %方差
A6(n) = P(i,j,n)/(1+(i-j)^2)+A6(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
A4(n) = i*j*P(i,j,n)+A4(n);
end
end
A4(n) = (A4(n)-Ux(n)*Uy(n))/deltaX(n)/deltaY(n); %相关度
end
%--------------------------------------------------------------------------
%最终特征向量T=[0°*6 ,45°*6,90°*6,135°*6]
%--------------------------------------------------------------------------
T(1) = A1(1);T(2) = A2(1);T(3) = A3(1);T(4) = A4(1);T(5) = A5(1);T(6) = A6(1);
T(7) = A1(2);T(8) = A2(2);T(9) = A3(2);T(10) = A4(2);T(11) = A5(2);T(12) = A6(2);
T(13) = A1(3);T(14) = A2(3);T(15) = A3(3);T(16) = A4(3);T(17) = A5(3);T(18) = A6(3);
T(19) = A1(4);T(20) = A2(4);T(21) = A3(4);T(22) = A4(4);T(23) = A5(4);T(24) = A6(4);
注意:灰度共生矩阵代码仅供学习参考
相关木材图片资源:木材纹理图片,可以用于纹理特征提取-图像处理文档类资源-CSDN下载