-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrayrlprops.m
211 lines (193 loc) · 7.45 KB
/
grayrlprops.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
function stats = grayrlprops(varargin)
%GRAYCOPROPS Properties of gray-level run-length matrix.
% -------------------------------------------
% STATS = GRAYRLPROPS(GLRLM,PROPERTIES) Each element in GLRLM, (r,c),
% is the probability occurrence of pixel having gray level values r, run-length c in the image.
% GRAYCOPROPS is to calculate PROPERTIES.
% -------------------------------------------
% Requirements:
% -------------------------------------------
% GLRLM mustbe an cell array of valid gray-level run-length
% matrices.Recall that a valid glrlm must be logical or numerical.
% -------------------------------------------
% Current supported statistics include:
% -------------------------------------------
% Short Run Emphasis (SRE)
% Long Run Emphasis (LRE)
% Gray-Level Nonuniformity (GLN)
% Run Length Nonuniformity (RLN)
% Run Percentage (RP)
% Low Gray-Level Run Emphasis (LGRE)
% High Gray-Level Run Emphasis (HGRE)
% Short Run Low Gray-Level Emphasis (SRLGE)
% Short Run High Gray-Level Emphasis (SRHGE)
% Long Run Low Gray-Level Emphasis (LRLGE)
% Long Run High Gray-Level Emphasis (LRHGE)
% --------------------------------------------
% Reference:
% --------------------------------------------
% Xiaoou Tang,Texture Information in Run-Length Matrices
% IEEE TRANSACTIONS ON IMAGE PROCESSING, VOL.7, NO.11,NOVEMBER 1998
% ---------------------------------------------
% See also GRAYRLMATRIX.
% ---------------------------------------------
% Author:
% ---------------------------------------------
% (C)Xunkai Wei <[email protected]>
% Beijing Aeronautical Technology Research Center
% Beijing %9203-12,10076
% ---------------------------------------------
% History:
% ---------------------------------------------
% Creation: beta Date: 01/10/2007
% Revision: 1.0 Date: 12/11/2007
% 1.Accept cell input now
% 2.Using MATLAB file style
% 3.Fully vectorized programming
% 4.Fully support the IEEE reference
% 5. ...
% Check GLRLM
[GLRLM numGLRLM] = ParseInputs(varargin{:});
% Initialize output stats structure.
% 11 statistics for each GLRLM
numStats = 11;
% % count number of GLRLM
% numGLRLM = length(GLRLM);
% Initialization default 4*11 matrix
stats = zeros(numGLRLM,numStats);
for p = 1 : numGLRLM
%N-D indexing not allowed for sparse.
if numGLRLM ~= 1
% transfer to double matrix
tGLRLM = GLRLM{p};
else
tGLRLM = GLRLM;
end
% if numGLRLM ~= 1
% % transfer to double matrix
% tGLRLM = normalizeGLRL(GLRLM{p});
% else
% tGLRLM = normalizeGLRL(GLRLM);
% end
% Get row and column subscripts of GLRLM. These subscripts correspond to the
% pixel values in the GLRLM.
s = size(tGLRLM);
% colum indicator
c_vector =1:s(1);
% row indicator
r_vector =1:s(2);
% matrix element indicator
% Matrix form col and row: using meshgrid, you should transpose before using
% i.e. if tGLRLM is m*n, then this function return c_matrix n*m,
% r_matrix n*m.
[c_matrix,r_matrix] = meshgrid(c_vector,r_vector);
% Total number of runs
N_runs = sum(sum(tGLRLM));
% total number of elements
N_tGLRLM = s(1)*s(2);
%--------------------Prepare four matrix for speedup--------------
% 1.Gray Level Run-Length Pixel Number Matrix
% p_p = calculate_p_p(tGLRLM,c_matrix');
% 2.Gray-Level Run-Number Vector
% This vector represents the sum distribution of the number of runs
% with gray level i.
p_g = sum(tGLRLM);
% 3.Run-Length Run-Number Vector
% This vector represents the sum distribution of the number of runs
% with run length j.
p_r = sum(tGLRLM,2)';
% 4.Gray-Level Run-Length-One Vector
%
% p_o = tGLRLM(:,1); % Not used yet
% ----------------------End four matrix---------------------------
%
%------------------------Statistics-------------------------------
% 1. Short Run Emphasis (SRE)
SRE = sum(p_r./(c_vector.^2))/N_runs;
% 2. Long Run Emphasis (LRE)
LRE = sum(p_r.*(c_vector.^2))/N_runs;
% 3. Gray-Level Nonuniformity (GLN)
GLN = sum(p_g.^2)/N_runs;
% 4. Run Length Nonuniformity (RLN)
RLN = sum(p_r.^2)/N_runs;
% 5. Run Percentage (RP)
RP = N_runs/N_tGLRLM;
% 6. Low Gray-Level Run Emphasis (LGRE)
LGRE = sum(p_g./(r_vector.^2))/N_runs;
% 7. High Gray-Level Run Emphasis (HGRE)
HGRE = sum(p_g.*r_vector.^2)/N_runs;
% 8. Short Run Low Gray-Level Emphasis (SRLGE)
SGLGE =calculate_SGLGE(tGLRLM,r_matrix',c_matrix',N_runs);
% 9. Short Run High Gray-Level Emphasis (SRHGE)
SRHGE =calculate_SRHGE(tGLRLM,r_matrix',c_matrix',N_runs);
% 10. Long Run Low Gray-Level Emphasis (LRLGE)
LRLGE =calculate_LRLGE(tGLRLM,r_matrix',c_matrix',N_runs);
% 11.Long Run High Gray-Level Emphasis (LRHGE
LRHGE =calculate_LRHGE(tGLRLM,r_matrix',c_matrix',N_runs);
%----------------insert statistics----------------------------
stats(p,:)=[SRE LRE GLN RLN RP LGRE HGRE SGLGE SRHGE LRLGE LRHGE ];
end % end all run length matrixs
% ----------------------Utility functions--------------------
%-----------------------------------------------------------------------------
% function glrl = normalizeGLRL(glrl)
%
% % Normalize glcm so that sum(glcm(:)) is one.
% if any(glrl(:))
% glrl = glrl ./ sum(glrl(:));
% end
% function p_p = calculate_p_p(GLRLM,c) % Note: currently not used
%
% % p_p(i; j) = GLRLM(i,j)*j
% % Each element of the matrix represents the number of pixels of run length
% % j and gray-level i. Compared to the original matrix, the new matrix gives
% % equal emphasis to all length of runs in an image.
%
% term1 = c; % j index in matrix size
% term2 = GLRLM;
% p_p = term1 .* term2;
%---------------------------------
function SGLGE =calculate_SGLGE(tGLRLM,r_matrix,c_matrix,N_runs)
% Short Run Low Gray-Level Emphasis (SRLGE):
term = tGLRLM./((r_matrix.*c_matrix).^2);
SGLGE= sum(sum(term))./N_runs;
%------------------------------------
function SRHGE =calculate_SRHGE(tGLRLM,r_matrix,c_matrix,N_runs)
% Short Run High Gray-Level Emphasis (SRHGE):
%
term = tGLRLM.*(r_matrix.^2)./(c_matrix.^2);
SRHGE = sum(sum(term))/N_runs;
%------------------------------------
function LRLGE =calculate_LRLGE(tGLRLM,r_matrix,c_matrix,N_runs)
% Long Run Low Gray-Level Emphasis (LRLGE):
%
term = tGLRLM.*(c_matrix.^2)./(r_matrix.^2);
LRLGE = sum(sum(term))/N_runs;
%---------------------------------------
function LRHGE =calculate_LRHGE(tGLRLM,r_matrix,c_matrix,N_runs)
% Long Run High Gray-Level Emphasis (LRHGE):
%
term = tGLRLM.*(c_matrix.^2).*(r_matrix.^2);
LRHGE = sum(sum(term))/N_runs;
%----------------------------------------
%-----------------------------------------------------------------------------
function [glrlm num_glrlm] = ParseInputs(varargin)
% check stability of inputs
%
% first receive all inputs
glrlm = varargin{:};
% get numbers total
num_glrlm=length(glrlm);
% then for each element, check its stability
for i=1:num_glrlm
% The 'nonnan' and 'finite' attributes are not added to iptcheckinput because the
% 'integer' attribute takes care of these requirements.
% iptcheckinput(glrlm,{'cell'},{'real','nonnegative','integer'}, ...
% mfilename,'GLRLM',1);
iptcheckinput(glrlm{i},{'logical','numeric'},{'real','nonnegative','integer'},...
mfilename,'GLRLM',1);
% Cast GLRLM to double to avoid truncation by data type. Note that GLRLM is not an
% image.
if ~isa(glrlm,'double')
glrlm{i}= double(glrlm{i});
end
end