-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgen_supperpixel_info.m
executable file
·116 lines (57 loc) · 1.89 KB
/
gen_supperpixel_info.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
function sp_info=gen_supperpixel_info(img_data, sp_size)
sp_info=do_gen_sp_info(img_data, sp_size);
sp_info.relation_infos=do_gen_relation_info(sp_info);
end
function sp_info=do_gen_sp_info(img_data, sp_size)
img_data=im2single(img_data);
seg_map = vl_slic(img_data, sp_size, 0.1, 'MinRegionSize', 10) ;
[sp_inds, ~, tmp_seg_map]=unique(seg_map);
seg_map=uint16(seg_map);
seg_map(:)=tmp_seg_map;
sp_num=length(sp_inds);
assert(sp_num<2^16);
pixel_ind_sps=cell(sp_num, 1);
for sp_idx=1:sp_num
one_pixel_inds=find(seg_map==sp_idx);
pixel_ind_sps{sp_idx}=uint32(one_pixel_inds);
end
sp_info=[];
sp_info.sp_ind_map=seg_map;
sp_info.img_size=size(seg_map);
sp_info.sp_num=sp_num;
sp_info.pixel_ind_sps=pixel_ind_sps;
end
function relation_infos=do_gen_relation_info(sp_info)
map=sp_info.sp_ind_map;
pixel_ind_sps=sp_info.pixel_ind_sps;
sp_num=length(pixel_ind_sps);
relation_infos=cell(sp_num, 1);
map1 = circshift(map, [1 0]);
map1(1,:) = map(1,:);
map2 = circshift(map, [-1 0]);
map2(end,:) = map(end,:);
map3 = circshift(map, [0 1]);
map3(:,1) = map(:,1);
map4 = circshift(map, [0 -1]);
map4(:,end) = map(:,end);
adjacent_mat=false(sp_num, sp_num);
for sp_idx=1:sp_num
one_sp_info=[];
ind = pixel_ind_sps{sp_idx};
adj = [map1(ind) map2(ind) map3(ind) map4(ind)];
adj = unique(adj(:));
adj = setdiff(adj, sp_idx);
one_sp_info.adjacent_sp_inds = adj;
adjacent_mat(sp_idx, adj)=true;
adjacent_mat(adj, sp_idx)=true;
adjacent_mat(sp_idx, sp_idx)=false;
relation_infos{sp_idx}=one_sp_info;
end
adjacent_mat=adjacent_mat|adjacent_mat';
for sp_idx=1:sp_num
one_sp_info=relation_infos{sp_idx};
adj_sp_inds=find(adjacent_mat(:, sp_idx));
one_sp_info.adjacent_sp_inds=uint16(adj_sp_inds);
relation_infos{sp_idx}=one_sp_info;
end
end