-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolorAutoCorrelogram.m
90 lines (66 loc) · 1.71 KB
/
colorAutoCorrelogram.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
function result = colorAutoCorrelogram(I)
D = [1 3 5 7];
% Quantize the image
R = I(:,:,1);
G = I(:,:,2);
B = I(:,:,3);
R_BITS = 2;
G_BITS = 2;
B_BITS = 2;
colorCnt = 2^R_BITS * 2^G_BITS * 2^B_BITS;
R1 = bitshift(R,-(8-R_BITS));
G1 = bitshift(G,-(8-G_BITS));
B1 = bitshift(B,-(8-B_BITS));
I = R1 + G1*2^R_BITS + B1*2^R_BITS*2^B_BITS;
dCnt = length(D);
result = zeros(colorCnt,dCnt);
% Generate all possible indices in the given image
s = size(I);
[r,c] = meshgrid(1:s(1),1:s(2));
r = r(:);
c = c(:);
for k = 1:dCnt
d = D(k);
oI = computeOffsetIndices(d);
oCnt = size(oI,1);
temp = zeros(colorCnt,1);
% For each possible offset from a point given the distances
for i = 1:oCnt
% Compute the histogram by taking into account only a single offset and accumulate the results
offset = oI(i,:);
temp = temp + GLCMATRIX(I,r,c,offset,colorCnt);
end
hc = zeros(colorCnt,1);
for j = 0:colorCnt-1
hc(j+1) = numel(I(I == j));
end
temp = temp ./ (hc+eps);
result(:,k) = temp/(8*d);
end
result = result(:)';
end
function os = computeOffsetIndices(d)
[r,c] = meshgrid(-d:d,-d:d);
r = r(:);
c = c(:);
os = [r c];
good = max(abs(r),abs(c)) == d;
os = os(good,:);
end
function out = GLCMATRIX(I,r,c,offset,nl)
s = size(I);
r2 = r+offset(1);
c2 = c+offset(2);
good = c2>=1 & c2<=s(2) & r2>=1 & r2<=s(1);
Index = [r c r2 c2];
Index = Index(good,:);
v1 = I(sub2ind(s,Index(:,1),Index(:,2)));
v2 = I(sub2ind(s,Index(:,3),Index(:,4)));
good = v1 == v2;
v1 = v1(good,:);
if isempty(v1)
out = zeros(nl,1);
else
out = accumarray(v1(:,1)+1,1,[nl 1]);
end
end