MATLAB中提取多张图片的颜色及纹理特征
查了好多资料,纹理特征提取都是单图的,自己拼拼凑凑搞出来了可以同时提取多图的纹理特征(利用灰度共生矩阵),并将最后特征矩阵保存在excel表中,这样在后续特征矩阵拼凑的时候会比较方便一点。
后边加了HSV色彩空间利用颜色直方图法提取颜色特征矩阵,特征矩阵的维数可以根据自己的需要修改,我这里提取了256维。
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 j1&&j345
H(i,j) = 0;
end
if h(i,j)<=25&&h(i,j)>15
H(i,j) = 1;
end
if h(i,j)<=45&&h(i,j)>25
H(i,j) = 2;
end
if h(i,j)<=55&&h(i,j)>45
H(i,j) = 3;
end
if h(i,j)<=80&&h(i,j)>55
H(i,j) = 4;
end
if h(i,j)<=108&&h(i,j)>80
H(i,j) = 5;
end
if h(i,j)<=140&&h(i,j)>108
H(i,j) = 6;
end
if h(i,j)<=165&&h(i,j)>140
H(i,j) = 7;
end
if h(i,j)<=190&&h(i,j)>165
H(i,j) = 8;
end
if h(i,j)<=220&&h(i,j)>190
H(i,j) = 9;
end
if h(i,j)<=255&&h(i,j)>220
H(i,j) = 10;
end
if h(i,j)<=275&&h(i,j)>255
H(i,j) = 11;
end
if h(i,j)<=290&&h(i,j)>275
H(i,j) = 12;
end
if h(i,j)<=316&&h(i,j)>290
H(i,j) = 13;
end
if h(i,j)<=330&&h(i,j)>316
H(i,j) = 14;
end
if h(i,j)<=345&&h(i,j)>330
H(i,j) = 15;
end
end
end
for i = 1:M
for j = 1:N
if s(i,j)<=0.15&&s(i,j)>0
S(i,j) = 0;
end
if s(i,j)<=0.4&&s(i,j)>0.15
S(i,j) = 1;
end
if s(i,j)<=0.75&&s(i,j)>0.4
S(i,j) = 2;
end
if s(i,j)<=1&&s(i,j)>0.75
S(i,j) = 3;
end
end
end
for i = 1:M
for j = 1:N
if v(i,j)<=0.15&&v(i,j)>0
V(i,j) = 0;
end
if v(i,j)<=0.4&&v(i,j)>0.15
V(i,j) = 1;
end
if v(i,j)<=0.75&&v(i,j)>0.4
V(i,j) = 2;
end
if v(i,j)<=1&&v(i,j)>0.75
V(i,j) = 3;
end
end
end
for i = 1:M
for j = 1:N
L(i,j) = H(i,j)*16+S(i,j)*4+V(i,j); %归一化
end
end
for i = 0:255
HSVHist(i+1) = size(find(L==i),1);
end
m(ii,:)=HSVHist/sum(HSVHist);
filename = '颜色直方图颜色特征矩阵.xlsx';
writematrix(m,filename,'Sheet',1,'Range','A1');
end
上边这个代码属实是有点捞了现在,因为当时一直在改,想封装成函数,但是老不对,最后我改出来了,就放在下边了,有问题自己看着改改,然后你也可以加write函数或者是save直接保存成矩阵。读文件的程序我就不贴了,只要能读出来你文件夹里的图片这个应该都能用。
%**************************************************************************
% 图像检索——纹理特征
%基于共生矩阵纹理特征提取,d=1,θ=0°,45°,90°,135°共四个矩阵
%所用图像灰度级均为256
%function : T=Texture(Image)
%Image : 输入图像数据
%T : 返回15维纹理特征行向量
%**************************************************************************
function Tnum = Extract_GLCM_Features(Image)
[~,~,totalNum] = size(Image);
Tnum = [];
for i = 1 : totalNum
P = graycomatrix(Image(:,:,i),'Offset',[0,1;-1,1;-1,0;-1,-1],'NumLevels',16);
%%---------------------------------------------------------
% 对共生矩阵归一化
%%---------------------------------------------------------
for n = 1:4
P(:,:,n) = P(:,:,n)/sum(sum(P(:,:,n)));
end
%--------------------------------------------------------------------------
%4.对共生矩阵计算能量(角二阶矩)、熵、惯性矩(对比度)、相关及逆差距5个纹理参数
%--------------------------------------------------------------------------
for n = 1:4
H = zeros(1,4);
I = H;
Ux = H;
Uy = H;
deltaX= H;
deltaY = H;
C =H;
L=H;
T=[];
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维纹理特征
%--------------------------------------------------------------------------
% T=[T;E(1),E(2),E(3),E(4),H(1), H(2), H(3), H(4),I(1),I(2),I(3),I(4),L(1),L(2),L(3),L(4),C(1),C(2),C(3),C(4)];
%用20维的数据试了一下发现没有下边那个好,大家完了可以多试试看看结果怎么样。
% T=mapminmax(T,0,1);
% Tnum=[Tnum;T];
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=[T;a1,b1,c1,a2,b2,c2,a3,b3,c3,a4,b4,c4,a5,b5,c5];
T=mapminmax(T,0,1);
Tnum=[Tnum;T];
%将矩阵写入ecxel中
% filename = 'GLCM纹理特征矩阵.xlsx';
% writematrix(Tnum,filename,'Sheet',1,'Range','A1');
end
end
function [a,mnum1]=Extract_HSVhist_Features1(A,i,j)
global mnum1;
[M,N,~] = size(A);
a= rgb2hsv(A);
h= a(:,:,1);
s = a(:,:,2);
v = a(:,:,3);
H = h; S = s; V = v;
h = h*360; %转换为HSV格式后h的值变为0-1,所以要乘以360来进行量化
%H量化为16级 S量化为4级 V量化为4级
% mnum=[];
% ii=length(mnum);
for iii = 1:M
for ii = 1:N
if h(iii,ii)<=15||h(iii,ii)>345
H(iii,ii) = 0;
end
if h(iii,ii)<=25&&h(iii,ii)>15
H(iii,ii) = 1;
end
if h(iii,ii)<=45&&h(iii,ii)>25
H(iii,ii) = 2;
end
if h(iii,ii)<=55&&h(iii,ii)>45
H(iii,ii) = 3;
end
if h(iii,ii)<=80&&h(iii,ii)>55
H(iii,ii) = 4;
end
if h(iii,ii)<=108&&h(iii,ii)>80
H(iii,ii) = 5;
end
if h(iii,ii)<=140&&h(iii,ii)>108
H(iii,ii) = 6;
end
if h(iii,ii)<=165&&h(iii,ii)>140
H(iii,ii) = 7;
end
if h(iii,ii)<=190&&h(iii,ii)>165
H(iii,ii) = 8;
end
if h(iii,ii)<=220&&h(iii,ii)>190
H(iii,ii) = 9;
end
if h(iii,ii)<=255&&h(iii,ii)>220
H(iii,ii) = 10;
end
if h(iii,ii)<=275&&h(iii,ii)>255
H(iii,ii) = 11;
end
if h(iii,ii)<=290&&h(iii,ii)>275
H(iii,ii) = 12;
end
if h(iii,ii)<=316&&h(iii,ii)>290
H(iii,ii) = 13;
end
if h(iii,ii)<=330&&h(iii,ii)>316
H(iii,ii) = 14;
end
if h(iii,ii)<=345&&h(iii,ii)>330
H(iii,ii) = 15;
end
end
end
for iii = 1:M
for ii = 1:N
if s(iii,ii)<=0.15&&s(iii,ii)>0
S(iii,ii) = 0;
end
if s(iii,ii)<=0.4&&s(iii,ii)>0.15
S(iii,ii) = 1;
end
if s(iii,ii)<=0.75&&s(iii,ii)>0.4
S(iii,ii) = 2;
end
if s(iii,ii)<=1&&s(iii,ii)>0.75
S(iii,ii) = 3;
end
end
end
for iii = 1:M
for ii = 1:N
if v(iii,ii)<=0.15&&v(iii,ii)>0
V(iii,ii) = 0;
end
if v(iii,ii)<=0.4&&v(iii,ii)>0.15
V(iii,ii) = 1;
end
if v(iii,ii)<=0.75&&v(iii,ii)>0.4
V(iii,ii) = 2;
end
if v(iii,ii)<=1&&v(iii,ii)>0.75
V(iii,ii) = 3;
end
end
end
for iii = 1:M
for ii = 1:N
L(iii,ii) = H(iii,ii)*16+S(iii,ii)*4+V(iii,ii); %归一化
end
end
for iii = 0:255
HSVHist(iii+1) = size(find(L==iii),1);
end
m=HSVHist/sum(HSVHist);
jj=(192)*(i-1)+j;%(训练集)
% jj=48*(i-1)+j-192;%(测试集,训练集)这里是测试集和训练集我分开读取的,前边程序一模一样,只用修改这一行就行了,数字表示的含义在后边注释,大家照着修改一下就行了。
mnum1(jj,:)=m;
% filename = '颜色直方图颜色特征矩阵.xlsx';
% writematrix( mnum,filename,'Sheet',1,'Range','A1');
% end
end
说明一下,这个颜色特征提取函数当时确实改了很长时间,我参考的程序格式是这个博主的,大家可以看一下,参考博主。这个代码格式我很喜欢,后期在这个格式上添加修改也比较方便,大家可以找找灵感。