• 大小: 6KB
    文件类型: .m
    金币: 1
    下载: 0 次
    发布日期: 2023-07-18
  • 语言: Matlab
  • 标签: 图像  纹理特征  

资源简介

Matlab提取图像的形状、纹理、颜色特征,其中纹理特征是用灰度梯度共生矩阵来实现的

资源截图

代码片段和文件信息

clc;
clear all
fileName = ‘C:\Users\Public\Pictures\Sample Pictures\‘;
for z=1:9
    ImgName=[fileNamenum2str(z)‘.jpg‘];
    rgb=imread(ImgName);
R=rgb(::1);
G=rgb(::2);
B=rgb(::3);
HSV=rgb2hsv(rgb);
H=HSV(::1);
S=HSV(::2);
V=HSV(::3);
R=R(R~=0);
G=G(G~=0);
B=B(B~=0);
H=H(H~=0);
S=S(S~=0);
V=V(V~=0);
Rmean=mean(R);
Gmean=mean(G);
Bmean=mean(B);
Hmean=mean(H);
Smean=mean(S);
Vmean=mean(V);
R=double(R);
G=double(G);
B=double(B);
H=double(H);
S=double(S);
V=double(V);
Rvar=var(R(:));
Gvar=var(G(:));
Bvar=var(B(:));
Hvar=var(H(:));
Svar=var(S(:));
Vvar=var(V(:));
Rs=skewness(R);
Gs=skewness(G);
Bs=skewness(B);
Hs=skewness(H);
Ss=skewness(S);
Vs=skewness(V);
Rk=kurtosis(R);
Gk=kurtosis(G);
Bk=kurtosis(B);
Hk=kurtosis(H);
Sk=kurtosis(S);
Vk=kurtosis(V);
colors=[RmeanRvarRsRkGmeanGvarGsGkBmeanBvarBsBkHmeanHvarHsHkSmeanSvarSsSkVmeanVvarVsVk];

%纹理特征
Gray=rgb2gray(rgb);
figureimshow(Gray);
% imwrite(Gray ‘E:/镉胁迫叶子图片/4/灰度/14.jpg‘);
IN=Gray;
gray=256;
[RC]=size(IN);
%采用平方求和计算梯度矩阵
GM=zeros(R-1C-1);
for i=1:R-1
    for j=1:C-1
        n_GM=(IN(ij+1)-IN(ij))^2+(IN(i+1j)-IN(ij))^2;
        GM(ij)=sqrt(double(n_GM));
    end
end
% figureimshow(GM);
%找出最大值最小值        
n_min=min(GM(:));
n_max=max(GM(:));
%把梯度图象灰度级离散化
%设置新的灰度级为new_gray
new_gray=32;
%新的梯度矩阵为new_GM
new_GM=zeros(R-1C-1);
new_GM=uint8((GM-n_min)/(n_max-n_min)*(new_gray-1));

%计算灰度梯度共生矩阵
%梯度矩阵比灰度矩阵维数少1,忽略灰度矩阵最外围
H=zeros(graynew_gray);
for i=1 :R-1
    for j=1:C-1
         H(IN(ij)+1new_GM(ij)+1)= H(IN(ij)+1new_GM(ij)+1)+1;
     end
end
%归一化灰度梯度矩阵 H_basic
total=i*j;
H_basic=H/total;

%小梯度优势 T1
TT=sum(H);
T1=0;
for j=1:new_gray
    T1=T1+TT(1j)/j^2;
end
T1=T1/total;

%计算大梯度优势 T2
T2=0;
for j=1:new_gray
    T2=T2+TT(1j)*(j-1);
end
T2=T2/total;

%计算灰度分布的不均匀性 T3
T3=0;
TT1=sum(H‘);
for j=1:gray
    T3=T3+TT1(1j)^2;
end
T3=T3/total;

%计算梯度分布的不均匀性 T4
T4=0;
for j=1:new_gray
    T4=T4+TT(1j)^2;
end
T4=T4/total;

%计算能量 T5
T5=0;
for i=1:gray
    for j=1:new_gray
        T5=T5+H_basic(ij)^2;
    end
end

%计算灰度平均 T6
TT2=sum((H_basic)‘);
T6=0;
for j=1:gray
    T6=T6+(j-1)*TT2(1j);
end

%计算梯度平均 T7
T7=0;
TT3=sum(H_basic);
for j=1:new_gray
    T7=T7+(j-1)*TT3(1j);
end

%计算灰度均方差 T8
T8=0;
for j=1:gray
    T8=T8+(j-1-T6)^2*TT2(1j);
end
T8=sqrt(T8);

%计算梯度均方差 T9
T9=0;
for j=1:new_gray
    T9=T9+(j-1-T7)^2*TT3(1j);
end
T9=sqrt(T9);

% 计算相关 T10
T10=0;
for i=1:gray
    for j=1:new_gray
        T10=T10+(i-1-T6)*(j-1-T7)*H_basic(ij);
    end
end

%计算灰度熵 T11
T11=0;
for j=1:gray
    T11=T11+TT2(1j)*log10(TT2(1j)+eps);
end
T11=-T11;

%计

评论

共有 条评论