-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmergeAll.m
152 lines (103 loc) · 3.67 KB
/
mergeAll.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
function [sets , scores] = mergeAll(I,labels,numlabels,iterationCount)
graph = getLabelGraph(labels, numlabels+1);
graphDistances= getGraphDistance(graph,numlabels);
labelIndices = cell(1,numlabels);
for i = 1:numlabels
[row,col] = find(labels == i);
labelIndices{1,i} = [row,col];
end
%GET GRADIENT OF IMAGE FOR COLOR-TEXTURE DISTANCE
sigma = 0.5;
Wx = floor((5/2)*sigma);
if Wx < 1
Wx = 1;
end
x = -Wx:Wx;
% Evaluate 1D Gaussian filter (and its derivative).
g = exp(-(x.^2)/(2*sigma^2));
gp = -(x/sigma).*exp(-(x.^2)/(2*sigma^2));
gradient = cell(2,3);
gradient{1,1} = convolve2(convolve2(I(:,:,1),-gp,'same'),g','same');
gradient{2,1} = convolve2(convolve2(I(:,:,1),g,'same'),-gp','same');
gradient{1,2}= convolve2(convolve2(I(:,:,2),-gp,'same'),g','same');
gradient{2,2} = convolve2(convolve2(I(:,:,2),g,'same'),-gp','same');
gradient{1,3} = convolve2(convolve2(I(:,:,3),-gp,'same'),g','same');
gradient{2,3} = convolve2(convolve2(I(:,:,3),g,'same'),-gp','same');
irfx = gradient{1,1};
irfy = gradient{2,1};
igfx = gradient{1,2};
igfy = gradient{2,2};
ibfx = gradient{1,3};
ibfy = gradient{2,3};
%%%%%%%%
orientations = cell(3,8);
i = 1;
for angle = 0:45:315
orientations{1,i} = cos(angle*(pi/180))*irfx+sin(angle*(pi/180))*irfy;
orientations{2,i} = cos(angle*(pi/180))*igfx+sin(angle*(pi/180))*igfy;
orientations{3,i} = cos(angle*(pi/180))*ibfx+sin(angle*(pi/180))*ibfy;
i = i + 1;
end
edges = -25:5:25;
oHists = cell(numlabels,3,8);
for l = 1:numlabels
for o = 1:8
for color = 1:3
oHists{l,color, o} = histcounts(orientations{1,o}(labels == l),edges);
oHists{l,color,o} = oHists{l,color, o} / sum(oHists{l,color, o});
end
end
end
ohists = cell(1,numlabels);
for l = 1:numlabels
ohists{1,numlabels} = zeros(24,10);
for i = 1:8
for j = 1:3
for k = 1:10
ohists{1,l}((i-1) * 3 + j ,k) = oHists{l,j,i}(1,k);
end
end
end
end
%GET COLOR HISTOGRAMS
colorHists = cell(1,numlabels);
for l = 1:numlabels
[row1,col1] = find(labels == l);
edges = 0:1/20:1;
rc1 = cat(2, row1,col1);
ir1 = zeros(size(rc1,1) ,1);
ig1 = zeros(size(rc1,1) ,1);
ib1 = zeros(size(rc1,1), 1);
for i = 1:size(rc1,1)
ir1(i,1) = I(rc1(i,1),rc1(i,2) ,1);
ig1(i,1) = I(rc1(i,1),rc1(i,2) ,2);
ib1(i,1) = I(rc1(i,1),rc1(i,2) ,3);
end
r1 = histcounts(ir1,edges);
r1= r1 / sum(r1);
g1 = histcounts(ig1,edges);
g1= g1 / sum(g1);
b1 = histcounts(ib1,edges);
b1= b1 / sum(b1);
colorHists{1,l} = cat(1, r1 , g1 , b1);
sets = cell(1,numlabels);
for i = 1:numlabels
sets{1,i} = i;
end
end
edgeImg = edge(rgb2gray(I),'Prewitt');
%[edgeImg, ~] = imgradient(rgb2gray(I),'prewitt');
scores = cell(1,2);
scoreCount = 1;
for i = 1:iterationCount
[sets, lastMerged,~] = mergePixels(I,edgeImg, labels ,numlabels , graphDistances ,colorHists, ohists , sets , labelIndices);
%score = scoreSet(edgeImg, labels, numlabels,lastMerged , labelIndices);
score = getSophisticatedEdgeScore(edgeImg, labels, labelIndices, lastMerged);
fprintf("Score: %f \n" , score);
if(score > 0)
scores{1,1}(1,scoreCount) = score;
scores{1,2}{1,scoreCount} = lastMerged;
scoreCount = scoreCount + 1;
end
end
end