close all
clear all
clc
%% 灰度共生矩阵法
A=imread('wenli1.jpg');?
%%图像平滑
[m,n]=size(A);
hw=2;%步长
B=zeros(m-2*hw,n-2*hw);
for i=(1+hw):(m-hw)
? ? for j=(1+hw):(n-hw)
? ? ? ? wi=A((i-hw):(i+hw),(j-hw):(j+hw));
? ? ? ? B(i-hw,j-hw,1)=mean2(wi);
? ? end
end
B(:,:,2)=A(1+hw:m-hw,1+hw:n-hw);
B=uint8(B);
[m,n,l]=size(B); ? ?
%%求联合分布直方图
P=zeros(256,256);
for i=1:1:m
? ? for j=1:1:n
? ? ? ? x=B(i,j,1)+1;
? ? ? ? y=B(i,j,2)+1;
? ? ? ? P(x,y)=P(x,y)+1;
? ? end
end
P=P./m./n;%归一化 ?
h0=zeros(256,256); ?
i=1:256;
ux=mean2(i*sum(P,2)); %2是各列相加赋予第一列,
uy=mean2(sum(P,1)*i');%1是各行相加赋予第一行,
dx=sqrt(mean2(((i-ux).^2)*sum(P,2)));
dy=sqrt(mean2(sum(P,1)*(((i-uy).^2)')));
%% 求反差(惯性矩)
for i=1:256
? ? for j=1:256
? ? ? ? h0(i,j)=(abs(i-j))^2*P(i,j);
? ? end
end
f(1)=sum(sum(h0));%第z幅图像的反差
%%求熵
for i=1:256
? ? for j=1:256
? ? ? ? if P(i,j)>0
? ? ? ? ? ? h0(i,j)=-P(i,j)*log2(P(i,j));
? ? ? ? end
? ? end
end
f(2)=sum(sum(h0));
%求逆差距
for i=1:256
? ? for j=1:256
? ? ? ? h0(i,j)=P(i,j)/((abs(i-j))^2+1);
? ? end
end
f(3)=sum(sum(h0));
%求灰度相关
for i=1:256
? ? for j=1:256
? ? ? ? h0(i,j)=(i-ux)*(j-uy)*P(i,j);
? ? end
end
f(4)=(sum(sum(h0)))/dx/dy;
%求能量(角二阶矩)
for i=1:256
? ? for j=1:256
? ? ? ? h0(i,j)=P(i,j)^2;
? ? end
end
f(5)=sum(sum(h0));
%求集群荫
for i=1:256
? ? for j=1:256
? ? ? ? h0(i,j)=((i-ux)+(j-uy))^3*P(i,j);
? ? end
end
f(6)=mean2(h0); ??
%求集群突出(的均方差)
for i=1:256
? ? for j=1:256
? ? ? ? h0(i,j)=(((i-ux)+(j-uy))^4)*P(i,j);
? ? end
end
f(7)=mean2(h0);
%显示计算结果
disp('图像的纹理特征分别为:')
disp(['f1=',num2str(f(1)),', f2=',num2str(f(2)),', f3=',num2str(f(3)),...
? ? ', f4=',num2str(f(4)),', f5=',num2str(f(5)),', f6=',num2str(f(6)),...
? ? ', f7=',num2str(f(7)),])
?
close all
clear all
clc
%% 灰度共生矩阵法
A=imread('wenli1.jpg');
A_gray=rgb2gray(A);
P=graycomatrix(A_gray,'Offset',[0,-2]);
%[1 0;0 1;-1 0;0 -1])代表上下左右四个方向,步长为1
[colum,line]=size(P);
P=mapminmax(P,0,1);%归一化
h0=zeros(colum,line);
i=1:line;
ux=mean2(i.*sum(P,2)); %2是各列相加赋予第一列,
uy=mean2(sum(P,1).*i');%1是各行相加赋予第一行,
dx=sqrt(mean2(((i-ux).^2).*sum(P,2)));
dy=sqrt(mean2(sum(P,1).*(((i-uy).^2)')));
%% 求反差(惯性矩)
for i=1:line
for j=1:line
h0(i,j)=(abs(i-j))^2*P(i,j);
end
end
f(1)=sum(sum(h0));%第z幅图像的反差
%%求熵
for i=1:line
for j=1:line
if P(i,j)>0
h0(i,j)=-P(i,j)*log2(P(i,j));
end
end
end
f(2)=sum(sum(h0));
%求逆差距
for i=1:line
for j=1:line
h0(i,j)=P(i,j)/((abs(i-j))^2+1);
end
end
f(3)=sum(sum(h0));
%求灰度相关
for i=1:line
for j=1:line
h0(i,j)=(i-ux)*(j-uy)*P(i,j);
end
end
f(4)=(sum(sum(h0)))/dx/dy;
%求能量(角二阶矩)
for i=1:line
for j=1:line
h0(i,j)=P(i,j)^2;
end
end
f(5)=sum(sum(h0));
%求集群荫
for i=1:line
for j=1:line
h0(i,j)=((i-ux)+(j-uy))^3*P(i,j);
end
end
f(6)=mean2(h0);
%求集群突出(的均方差)
for i=1:line
for j=1:line
h0(i,j)=(((i-ux)+(j-uy))^4)*P(i,j);
end
end
f(7)=mean2(h0);
%显示计算结果
disp('图像的纹理特征分别为:')
disp(['f1=',num2str(f(1)),', f2=',num2str(f(2)),', f3=',num2str(f(3)),...
', f4=',num2str(f(4)),', f5=',num2str(f(5)),', f6=',num2str(f(6)),...
', f7=',num2str(f(7)),])