a b/Image features calculation code/Working/haralick/calcHaralick.m
1
function [ F ] = calcHaralick( glcm )
2
% HARALICK Fast Calculation of Haralick Features
3
%   IN:   glcm = Co-Occurrence Matrix     
4
%   OUT:  F = Feature Vector   
5
%
6
%   Stefan Winzeck 2012   
7
%   winzeck@hm.edu
8
% 
9
%   Feature Calculation according to:
10
%   [1] R. Haralick: 'Textural Feature for Image Classification' (1979)
11
%   [2] E. Miyamoto: 'Fast Calculation of Haralick Texture Features' 
12
% 
13
% MISSING:   f14  [1]
14
15
%% ALLOCATION
16
S=size(glcm,1);
17
18
f_2=zeros(S);
19
f_3=zeros(S);
20
f_4=zeros(S);
21
f_5=zeros(S);
22
f_6=zeros(1,2*S);
23
f_7=zeros(1,2*S);
24
f_8=zeros(1,2*S);
25
f_9=zeros(S);
26
f_11=zeros(1,S);
27
28
pxy=zeros(1,2*S);
29
px_y=zeros(1,S);
30
31
HX_=zeros(1,S);
32
HY_=zeros(1,S);
33
HXY_1=zeros(S);
34
HXY_2=zeros(S);
35
36
%% CALCULATION
37
% Normalization
38
M = glcm/sum(glcm(:));
39
40
% Energy
41
f_1 = M.^2;
42
f1 = sum(f_1(:));
43
44
%-------------------------------------------------------------------------%
45
46
u = mean2(M);
47
py = sum(M,1);
48
px = sum(M,2);
49
50
51
%OPTIMIZATION NEEDED: change the indicies to j = i and multiple the 
52
% end summations by 2.
53
for i=1:S
54
    for j=1:S
55
        Mij = M(i,j);
56
       
57
        f_3(i,j) = i*j*Mij;
58
        f_4(i,j) = (i-u)^2*Mij;
59
        f_5(i,j) = Mij/(1+(i-j)^2);
60
        %OPTIMIZATION NEEDED: Use of log tables
61
        f_9(i,j) = Mij*log(Mij+eps);
62
    
63
        %Equation (5) from [2]
64
        pxy(i+j) = pxy(i+j)+Mij;
65
        %Equation (6) from [2]
66
        px_y(abs(i-j)+1) = px_y(abs(i-j)+1)+Mij;
67
         
68
        %Different than the paper
69
        
70
        %OPTIMIZATION NEEDED: Use of log tables
71
        %Related to Equation (20) from [2]
72
        HX_(i)= px(i)*log(px(i)+eps);
73
        
74
        %Related to Equation (20) from [2]
75
        HY_(j)= py(j)*log(py(j)+eps);
76
        
77
        %Equation (7) from [2]
78
        HXY_1(i,j) = Mij*log(px(i)*py(j)+eps);
79
        
80
        %Equation (8) from [2]
81
        HXY_2(i,j) = px(i)*py(j)*log(px(i)*py(j)+eps);
82
        
83
    end
84
end
85
86
% Correlation
87
ux = mean(px); sx=std(px);
88
uy = mean(py); sy=std(py);
89
f3 =(sum(f_3(:))-(ux*uy))/(sx*sy);
90
91
% Sum of Variances
92
f4 = sum(f_4(:));
93
94
% Inverse Difference Moment
95
f5 = sum(f_5(:));
96
97
% Entropy
98
f9 = -sum(f_9(:));
99
100
% Information Measures of Correlation 1&2
101
HX = -sum(HX_);
102
HY = -sum(HY_);
103
HXY = f9;
104
HXY1 = -sum(HXY_1(:));
105
HXY2 = -sum(HXY_2(:));
106
107
f12 = (HXY-HXY1)/max([HX, HY]);
108
f13 = (1 - exp((-2)*(HXY2 - HXY)))^0.5;
109
110
%-------------------------------------------------------------------------%
111
112
for i=2:2*S
113
    f_6(i) = i*pxy(i);
114
    %OPTIMIZATION NEEDED: Use of log tables
115
    f_8(i) = pxy(i)*log(pxy(i)+eps);
116
end
117
118
% Sum Average
119
%f_6(1) = [];       % not necessary f_6(1) is zero anyway
120
f6 = sum(f_6);
121
122
123
% Sum Entropy
124
%f_8(1)=[];         % not necessary f_8(1) is zero anyway
125
f8 = -sum(f_8);
126
127
%-------------------------------------------------------------------------%
128
129
%Different than the paper, f7 should be calculated along side f6 and f8
130
for i=2:2*S
131
    f_7(i)=(i-f8)^2*pxy(i);
132
end
133
134
% Sum Variance
135
%f_7(1) = [];       % not necessary f_7(1) is zero anyway
136
f7 = sum(f_7);
137
138
139
% Difference Variance
140
f10 = var(px_y);
141
142
%-------------------------------------------------------------------------%
143
144
for k=1:S
145
    f_2(k) = (k-1)^2*px_y(k);
146
    %OPTIMIZATION NEEDED: Use of log tables
147
    f_11(k) = px_y(k)*log(px_y(k)+eps);
148
end
149
150
% Contrast
151
f2 = sum(f_2(:));
152
153
% Difference Entropy
154
f11 = -sum(f_11);
155
%-------------------------------------------------------------------------%
156
157
158
F = [f1;f2;f3;f4;f5;f6;f7;f8;f9;f10;f11;f12;f13];
159
160
end
161