-
Notifications
You must be signed in to change notification settings - Fork 0
/
superpixel_clustering.m
97 lines (67 loc) · 1.97 KB
/
superpixel_clustering.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
% Super pixel clustering.
imgCopy = imrotate(higResRgb, 90);
no_Of_Superpixels = 200;
[L,N] = superpixels(imgCopy, no_Of_Superpixels);
figure
BW = boundarymask(L);
imshow(imoverlay(imgCopy,BW,'cyan'),'InitialMagnification',67)
areaMean = zeros(N, 3, 'uint8');
[h, w] = size(L);
temp = zeros(645*645, 3);
sparseimage = zeros(h, w, 3, 'uint8');
for k = 1:N
cnt = 1;
for i = 1:h
for j = 1:w
if L(i, j) == k
temp(cnt, :) = imgCopy(i, j, :);
cnt = cnt + 1;
end
end
end
areaMean(k, :) = uint8(mean(temp(1:cnt, :)));
end
for k = 1:N
for i = 1:h
for j = 1:w
if L(i, j) == k
sparseimage(i, j ,:) = areaMean(k, :);
end
end
end
end
figure
imshow(sparseimage);
%% Second SLIC
[L2,N2] = superpixels(sparseimage, classesCount);
figure
BW2 = boundarymask(L2);
imshow(imoverlay(imgCopy,BW2,'cyan'),'InitialMagnification',67)
pointsPerClass = zeros(classesCount, 1);
tempClassPoints = zeros(645 * 645, 14, 2);
for k = 1:classesCount
cnt = 1;
for i = 1:h
for j = 1:w
if L2(i, j) == k
pointsPerClass(k, 1) = pointsPerClass(k, 1) + 1;
tempClassPoints(cnt, k, :) = [i, j];
cnt = cnt + 1;
end
end
end
end
%% Get 10 random samples from each class and
augClassPoints = zeros(645 * 645, 14, 2);
pixelPairs = zeros(10 * classesCount, 2);
pixeCnt = 1;
for class = 1: classesCount
rand_idx = randperm(pointsPerClass(class), pointsPerClass(class));
for n = 1: length(rand_idx)
augClassPoints(n, class, :) = tempClassPoints(rand_idx(n), class, :);
end
for pixel = 1:10
pixelPairs(((class - 1) * 10) + pixel, :) = augClassPoints(pixel, class, :);
pixeCnt = pixeCnt + 1;
end
end