graycoprops 计算 灰度共生矩阵(GLCM)的特征1

matlab 函数名称: graycoprops()

功                   能: 计算灰度共生矩阵(GLCM)的各个特征值+

句法:

    stats = graycoprops(glcm, properties)

描述:

    stats = graycoprops(glcm, properties) 从灰度共生矩阵(glcm)中,计算properties指定的特征的统计值。glcm是一个m x n x p 的矩阵,它由有效的灰度共生矩阵(m x n)组成。如果glcm是一个GLCMs的数组,stats是由每个glcm的统计值组成的矩阵。

    graycoprops() 归一化灰度共生矩阵,以便它的元素的和等于1。归一化的GLCM中的每一个元素(r,c)是图像中像素值分别为r和c的像素对出现的联合概率。graycoprops使用归一化的GLCM来计算特征值。

    properties可以是由逗号隔开的字符串,字符元胞数组,'all’字符串,或者空格隔开的字符串。

property的取值:

   'Contrast'  即对比度,返回整幅图像中某个像素与它的邻居之间的对比度。常量组成的图像的Contrast是0。计算公式为:

    'Correlation'  即互相关,返回整幅图像中某个像素与它的邻居之间的互相关度。取值范围是 [-1 , 1]。常量组成的图像的互相关度Correlation是NaN。相关度1和-1分别对应完全正相关和完全负相关。计算公式为:

     'Energy'  即能量,返回GLCM中所有元素的平方和。取值范围是 [0 , 1]。常量组成的图像的能量Energy是1。计算公式为:

    'Homogeneity'  即齐次性(同质性),返回的值反映了GLCM中元素相对于GLCM对角线的分布的紧密度。取值范围是 [0 , 1]。对角的GLCM的Homogeneity齐次性是1。公式为:

       graycoprops()的返回变量states是一个结构体,它的域fields由properties指定。每一个field包含一个 1 x p 的数组,p是GLCM中灰度共生矩阵的数目。例如,GLCM是一个8 x 8 x 3 的矩阵,并且属性为‘Energy’,那么,states是一个包含‘Energy’域的结构体,其中,Energy包含一个 1 x 3 的数组。

注释:

     Energy(能量)也叫做uniformity, uniformity of energy, 和 angular second moment。

     Contrast(对比度)也叫做variance 和 inertia。

支持的类型:

     glcm可以是逻辑型或数值型,并且它必须由非负的、有限的(和实数的)整数组成。     states是一个结构体。

举例说明:

GLCM = [0 1 2 3;1 1 2 3;1 0 2 0;0 0 0 3];
stats = graycoprops(GLCM)

I = imread(‘circuit.tif’);
GLCM2 = graycomatrix(I,‘Offset’,[2 0;0 2]);
stats = graycoprops(GLCM2,{‘contrast’,‘homogeneity’})

 

 

 ************************************************************************************************************************************************************************

 

 *************************************************************  graycoprops源程序代码  *****************************************************************************

 ************************************************************************************************************************************************************************

 


 
 
  1. function stats = graycoprops(varargin)
  2. %GRAYCOPROPS Properties of gray-level co-occurrence matrix.
  3. % STATS = GRAYCOPROPS(GLCM,PROPERTIES) normalizes the gray-level
  4. % co-occurrence matrix (GLCM) so that the sum of its elements is one. Each
  5. % element in the normalized GLCM, (r,c), is the joint probability occurrence
  6. % of pixel pairs with a defined spatial relationship having gray level
  7. % values r and c in the image. GRAYCOPROPS uses the normalized GLCM to
  8. % calculate PROPERTIES.
  9. %
  10. % GLCM can be an m x n x p array of valid gray-level co-occurrence
  11. % matrices. Each gray-level co-occurrence matrix is normalized so that its
  12. % sum is one.
  13. %
  14. % PROPERTIES can be a comma-separated list of strings, a cell array
  15. % containing strings, the string 'all', or a space separated string. They
  16. % can be abbreviated, and case does not matter.
  17. %
  18. % Properties include:
  19. %
  20. % 'Contrast' the intensity contrast between a pixel and its neighbor
  21. % over the whole image. Range = [ 0 (size(GLCM, 1)- 1)^ 2].
  22. % Contrast is 0 for a constant image.
  23. %
  24. % 'Correlation' statistical measure of how correlated a pixel is to its
  25. % neighbor over the whole image. Range = [- 1 1].
  26. % Correlation is 1 or - 1 for a perfectly positively or
  27. % negatively correlated image. Correlation is NaN for a
  28. % constant image.
  29. %
  30. % 'Energy' summation of squared elements in the GLCM. Range = [ 0 1].
  31. % Energy is 1 for a constant image.
  32. %
  33. % 'Homogeneity' closeness of the distribution of elements in the GLCM to
  34. % the GLCM diagonal. Range = [ 0 1]. Homogeneity is 1 for
  35. % a diagonal GLCM.
  36. %
  37. % If PROPERTIES is the string 'all', then all of the above properties are
  38. % calculated. This is the default behavior. Please refer to the
  39. % Documentation for more information on these properties.
  40. %
  41. % STATS is a structure with fields that are specified by PROPERTIES. Each
  42. % field contains a 1 x p array, where p is the number of gray-level
  43. % co-occurrence matrices in GLCM. For example, if GLCM is an 8 x 8 x 3 array
  44. % and PROPERTIES is 'Energy', then STATS is a structure containing the
  45. % field 'Energy'. This field contains a 1 x 3 array.
  46. %
  47. % Notes
  48. % -----
  49. % Energy is also known as uniformity, uniformity of energy, and angular second
  50. % moment.
  51. %
  52. % Contrast is also known as variance and inertia.
  53. %
  54. % Class Support
  55. % -------------
  56. % GLCM can be logical or numeric, and it must contain real, non-negative, finite,
  57. % integers. STATS is a structure.
  58. %
  59. % Examples
  60. % --------
  61. % GLCM = [ 0 1 2 3; 1 1 2 3; 1 0 2 0; 0 0 0 3];
  62. % stats = graycoprops(GLCM)
  63. %
  64. % I = imread( 'circuit.tif');
  65. % GLCM2 = graycomatrix(I, 'Offset',[ 2 0; 0 2]);
  66. % stats = graycoprops(GLCM2,{ 'contrast', 'homogeneity'})
  67. %
  68. % See also GRAYCOMATRIX.
  69. % Copyright 2003- 2009 The MathWorks, Inc.
  70. allStats = { 'Contrast', 'Correlation', 'Energy', 'Homogeneity'};
  71. [glcm, requestedStats] = ParseInputs(allStats, varargin{ :});
  72. % Initialize output stats structure.
  73. numStats = length(requestedStats);
  74. numGLCM = size(glcm, 3);
  75. empties = repmat({zeros( 1,numGLCM)},[numStats 1]);
  76. stats = cell2struct(empties,requestedStats, 1);
  77. for p = 1 : numGLCM
  78. if numGLCM ~= 1 %N-D indexing not allowed for sparse.
  79. tGLCM = normalizeGLCM(glcm( :, :,p));
  80. else
  81. tGLCM = normalizeGLCM(glcm);
  82. end
  83. % Get row and column subscripts of GLCM. These subscripts correspond to the
  84. % pixel values in the GLCM.
  85. s = size(tGLCM);
  86. [c,r] = meshgrid( 1 :s( 1), 1 :s( 2));
  87. r = r( :);
  88. c = c( :);
  89. % Calculate fields of output stats structure.
  90. for k = 1 :numStats
  91. name = requestedStats{k};
  92. switch name
  93. case 'Contrast'
  94. stats.(name)(p) = calculateContrast(tGLCM,r,c);
  95. case 'Correlation'
  96. stats.(name)(p) = calculateCorrelation(tGLCM,r,c);
  97. case 'Energy'
  98. stats.(name)(p) = calculateEnergy(tGLCM);
  99. case 'Homogeneity'
  100. stats.(name)(p) = calculateHomogeneity(tGLCM,r,c);
  101. end
  102. end
  103. end
  104. %-----------------------------------------------------------------------------
  105. function glcm = normalizeGLCM(glcm)
  106. % Normalize glcm so that sum(glcm( :)) is one.
  107. if any(glcm( :))
  108. glcm = glcm ./ sum(glcm( :));
  109. end
  110. %-----------------------------------------------------------------------------
  111. function C = calculateContrast(glcm,r,c)
  112. % Reference: Haralick RM, Shapiro LG. Computer and Robot Vision: Vol. 1,
  113. % Addison-Wesley, 1992, p. 460.
  114. k = 2;
  115. l = 1;
  116. term1 = abs(r - c).^k;
  117. term2 = glcm.^l;
  118. term = term1 .* term2( :);
  119. C = sum(term);
  120. %-----------------------------------------------------------------------------
  121. function Corr = calculateCorrelation(glcm,r,c)
  122. % References:
  123. % Haralick RM, Shapiro LG. Computer and Robot Vision: Vol. 1, Addison-Wesley,
  124. % 1992, p. 460.
  125. % Bevk M, Kononenko I. A Statistical Approach to Texture Description of Medical
  126. % Images: A Preliminary Study., The Nineteenth International Conference of
  127. % Machine Learning, Sydney, 2002.
  128. % http:/ /www.cse.unsw.edu.au/~icml2002/workshops/MLCV02/MLCV02-Bevk.pdf, p. 3.
  129. % Correlation is defined as the covariance(r,c) / S(r)*S(c) where S is the
  130. % standard deviation.
  131. % Calculate the mean and standard deviation of a pixel value in the row
  132. % direction direction. e.g., for glcm = [ 0 0; 1 0] mr is 2 and Sr is 0.
  133. mr = meanIndex(r,glcm);
  134. Sr = stdIndex(r,glcm,mr);
  135. % mean and standard deviation of pixel value in the column direction, e.g.,
  136. % for glcm = [ 0 0; 1 0] mc is 1 and Sc is 0.
  137. mc = meanIndex(c,glcm);
  138. Sc = stdIndex(c,glcm,mc);
  139. term1 = (r - mr) .* (c - mc) .* glcm( :);
  140. term2 = sum(term1);
  141. Corr = term2 / (Sr * Sc);
  142. %-----------------------------------------------------------------------------
  143. function S = stdIndex(index,glcm,m)
  144. term1 = (index - m).^ 2 .* glcm( :);
  145. S = sqrt(sum(term1));
  146. %-----------------------------------------------------------------------------
  147. function M = meanIndex(index,glcm)
  148. M = index .* glcm( :);
  149. M = sum(M);
  150. %-----------------------------------------------------------------------------
  151. function E = calculateEnergy(glcm)
  152. % Reference: Haralick RM, Shapiro LG. Computer and Robot Vision: Vol. 1,
  153. % Addison-Wesley, 1992, p. 460.
  154. foo = glcm.^ 2;
  155. E = sum(foo( :));
  156. %-----------------------------------------------------------------------------
  157. function H = calculateHomogeneity(glcm,r,c)
  158. % Reference: Haralick RM, Shapiro LG. Computer and Robot Vision: Vol. 1,
  159. % Addison-Wesley, 1992, p. 460.
  160. term1 = ( 1 + abs(r - c));
  161. term = glcm( :) ./ term1;
  162. H = sum(term);
  163. %-----------------------------------------------------------------------------
  164. function [glcm,reqStats] = ParseInputs(allstats,varargin)
  165. numstats = length(allstats);
  166. iptchecknargin( 1,numstats+ 1,nargin,mfilename);
  167. reqStats = '';
  168. glcm = varargin{ 1};
  169. % The 'nonnan' and 'finite' attributes are not added to iptcheckinput because the
  170. % 'integer' attribute takes care of these requirements.
  171. iptcheckinput(glcm,{ 'logical', 'numeric'},{ 'real', 'nonnegative', 'integer'}, ...
  172. mfilename, 'GLCM', 1);
  173. if ndims(glcm) > 3
  174. eid = sprintf( 'Images:%s:invalidSizeForGLCM',mfilename);
  175. msg = 'GLCM must either be an m x n or an m x n x p array.';
  176. error(eid, '%s',msg);
  177. end
  178. % Cast GLCM to double to avoid truncation by data type. Note that GLCM is not an
  179. % image.
  180. if ~isa(glcm, 'double')
  181. glcm = double(glcm);
  182. end
  183. list = varargin( 2 :end);
  184. if isempty(list)
  185. % GRAYCOPROPS(GLCM) or GRAYCOPROPS(GLCM,PROPERTIES) where PROPERTIES is empty.
  186. reqStats = allstats;
  187. else
  188. if iscell(list{ 1}) || numel(list) == 1
  189. % GRAYCOPROPS(GLCM,{...})
  190. list = list{ 1};
  191. end
  192. if ischar(list)
  193. %GRAYCOPROPS(GLCM,SPACE-SEPARATED STRING)
  194. list = strread(list, '%s');
  195. end
  196. anyprop = allstats;
  197. anyprop{ end+ 1} = 'all';
  198. for k = 1 : length(list)
  199. match = iptcheckstrs(list{k}, anyprop, mfilename, 'PROPERTIES', k+ 1);
  200. if strcmp(match, 'all')
  201. reqStats = allstats;
  202. break;
  203. end
  204. reqStats{k} = match;
  205. end
  206. end
  207. % Make sure that reqStats are in alphabetical order.
  208. reqStats = sort(reqStats);
  209. if isempty(reqStats)
  210. eid = sprintf( 'Images:%s:internalError',mfilename);
  211. msg = 'Internal error: requestedStats has not been assigned.';
  212. error(eid, '%s',msg);
  213. end


 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值