statxture纹理特征提取代码&…

原文地址:statxture纹理特征提取代码 matlab 作者:jiandanjinxin

这个函数MATLAB 不自带,可以添加到MATLAB函数库中,file-> set path -> add with subfolder

另外一种方法就是 把statxture.m 文件添加到MATLAB的工作路径里,这样做的好处就是,随时可以取消这个路径,不影响系统的稳定性。因为有些自定义的函数,会跟MATLAB自带的函数有冲突。

function [t]=statxture(f,scale) 
 
%STATXTURE Computes statistical measures of texture in an image.
%   T = STATXURE(F, SCALE) computes six measures of texture from an
%   image (region) F. Parameter SCALE is a 6-dim row vector whose 
%   elements multiply the 6 corresponding elements of T for scaling
%   purposes. If SCALE is not provided it defaults to all 1s.  The 
%   output T is 6-by-1 vector with the following elements: 
%     T(1) = Average gray level
%     T(2) = Average contrast
%     T(3) = Measure of smoothness
%     T(4) = Third moment
%     T(5) = Measure of uniformity
%     T(6) = Entropy
%   Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
%   Digital Image Processing Using MATLAB, Prentice-Hall, 2004
%   $Revision: 1.5 $  $Date: 2004/11/04 22:33:43 $

if nargin==1 
    scale(1:6)=1; 
else 
    scale=scale(:)';  % Make sure it's a row vector
end 

%Obtain histogram and normalize it.
p=imhist(f);                  %p是256*1的列向量 
p=p./numel(f); 
L=length(p); 

% Compute the three moments. We need the unnormalized ones
% from function statemoments. There are in vector mu.
[v,mu]=statmoments(p,3); 

%计算六个纹理特征  Compute the six texture measures
t(1)=mu(1);                   %平均值  Average gray level
t(2)=mu(2).^0.5;              %标准差   Standard deviation
varn=mu(2)/(L-1)^2;           % First normalize the variance to [0 1] by dividing it by (L-1)^2.
t(3)=1-1/(1+varn);            %平滑度首先为(0~1)区间通过除以(L-1)^2将变量标准化 
t(4)=mu(3)/(L-1)^2;           %三阶矩(通过除以(L-1)^2将变量标准化) Third moment (normalized by (L-1)^2 also) 
t(5)=sum(p.^2);               %一致性  Uniformity
t(6)=-sum(p.*(log2(p+eps)));  %熵  Entropy
T=[t(1) t(2) t(3) t(4) t(5) t(6)] 
%缩放值,默认为1  Scale the value
t=t.*scale; 
end 
 
function [v,unv]=statmoments(p,n) 
Lp=length(p); 
if (Lp~=256)&(Lp~=65536) 
    error('p must be a 256- or 65536-element vector.'); 
end 
G=Lp-1; 
p=p/sum(p);p=p(:); 
z=0:G; 
z=z./G; 
m=z*p; 
z=z-m; 
v=zeros(1,n); 
v(1)=m; 
for j=2:n 
    v(j)=(z.^j)*p; 
end 
if nargout>1 
    unv=zeros(1,n); 
    unv(1)=m.*G; 
    for j=2:n 
        unv(j)=((z*G).^j)*p 
    end 
end 
end

你可能感兴趣的:(statxture纹理特征提取代码&…)