-
Notifications
You must be signed in to change notification settings - Fork 1
/
CylinderTag_generator.m
286 lines (272 loc) · 10.1 KB
/
CylinderTag_generator.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
clc,clear;
global ID tag_length
tag_length = 1200;
ID=[];
CylinderTagGenerator(18, 4, 100, 15);
function max_number=select(feature_size)
global judge_usage
max_number=0;
for i = 1:size(judge_usage,1)
code = i - 1;
judge_usage(i) = 1;
illegal = 0;
for j=1:feature_size
a(j) = mod(code, 64);
if (((mod(a(j),8)<=3 && fix(a(j)/8)>=4) || (mod(a(j),8)>=4 && fix(a(j)/8)<=3)))
illegal = 1;
break;
end
code = fix(code/64);
end
if illegal
continue;
end
if ~illegal && i~=inverse(i,feature_size)
judge_usage(i) = 0;
max_number=max_number+1;
end
end
end
function CylinderTagGenerator(tag_col, feature_size, tag_number, height_width_ratio)
global flag judge_usage ID max_tag_number
judge_usage=zeros(64^feature_size,1);
max_tag_number=select(feature_size);
max_tag_number=fix(max_tag_number/(2*tag_col));
if tag_number > max_tag_number
tag_number = max_tag_number;
disp(['The size of CylinderTag is ', num2str(tag_number)]);
end
tic
while size(ID,1) < tag_number
if toc > 20
break;
end
flag=0;
dfs(tag_col,[],feature_size,tag_number);
end
if testConflict(ID, feature_size)
disp('The Marker Codes are unique!');
plot_tag(ID, height_width_ratio);
save ./CTag_Generated/ID.mat ID
else
disp('The Marker Codes are not unique!');
end
end
function dfs(col, ID_now, feature_size, tag_number)
global flag ID judge_usage
%% 搜索可行解成功,打标记
if size(ID_now, 2) == col
flag = 1;
ID=[ID; ID_now];
toc
disp(['Now: ' num2str(size(ID,1)) '/' num2str(tag_number)]);
return;
end
if size(ID_now, 2) < col - 1
if isempty(ID_now)
code_init = fix(rand*(64^feature_size))+1;
while judge_usage(code_init)
code_init=fix(rand*(64^feature_size))+1;
end
judge_usage(code_init)=1;
judge_usage(inverse(code_init,feature_size))=1;
code_initi = code_init;
code_init = code_init - 1;
for j=1:feature_size
a(j) = mod(code_init, 64);
code_init = fix(code_init/64);
end
dfs(col, a, feature_size, tag_number);
if flag
return;
end
judge_usage(code_initi)=0;
judge_usage(inverse(code_initi,feature_size))=0;
else
waitlist=[];
for i = 0:63
if (mod(i,8)<=3 && fix(i/8)>=4) || (mod(i,8)>=4 && fix(i/8)<=3)
continue;
end
now = 1;
for iter=1:feature_size-1
now = now + ID_now(end - feature_size + 1 + iter) * 64 ^ (iter - 1);
end
now = now + i * 64 ^ (feature_size - 1);
if ~judge_usage(now) && ~judge_usage(inverse(now,feature_size))
waitlist=[waitlist i];
end
end
if isempty(waitlist)
return;
end
choice_base = zeros(1, size(waitlist, 2));
for i = 1:size(waitlist, 2)
for j = 0:63
if (mod(j,8)<=3 && fix(j/8)>=4) || (mod(j,8)>=4 && fix(j/8)<=3)
continue;
end
now = 1;
if feature_size > 2
for iter=1:feature_size-2
now = now + ID_now(end - feature_size + 2 + iter) * 64 ^ (iter - 1);
end
else
now = now + waitlist(i);
end
now = now + j * 64 ^ (feature_size - 1);
if ~judge_usage(now) && ~judge_usage(inverse(now,feature_size))
choice_base(i) = choice_base(i) + 1;
end
end
end
[choice_base, p] = sort(choice_base, 'descend');
equal_elements = find(diff(choice_base)~=0);
equal_elements = [equal_elements size(choice_base, 2)];
start=1;
for ii = equal_elements
idx = start:ii;
p(idx) = p(idx(randperm(numel(idx))));
start=ii+1;
end
for i=1:size(p,2)
if choice_base(i)==0
break;
end
next_step = waitlist(p(i));
next_score = 1;
for iter = 1:feature_size - 1
next_score = next_score + ID_now(end - feature_size + 1 + iter) * 64 ^ (iter - 1);
end
next_score = next_score + next_step * 64 ^ (feature_size - 1);
judge_usage(next_score) = 1;
judge_usage(inverse(next_score,feature_size)) = 1;
dfs(col, [ID_now next_step], feature_size, tag_number);
if flag
return;
end
judge_usage(next_score) = 0;
judge_usage(inverse(next_score,feature_size)) = 0;
end
end
end
if size(ID_now, 2) == col - 1
for it=1:100
i=fix(rand*64);
while (mod(i,8)<=3 && fix(i/8)>=4) || (mod(i,8)>=4 && fix(i/8)<=3)
i=fix(rand*64);
end
for iter=1:feature_size
now_cyc(iter) = 1;
for j=1:feature_size
if (iter + j - feature_size) == 1
now_cyc(iter) = now_cyc(iter) + i * 64 ^ (j - 1);
elseif (iter + j - feature_size) < 1
now_cyc(iter) = now_cyc(iter) + ID_now(end - feature_size + iter + j) * 64 ^ (j - 1);
elseif (iter + j - feature_size) > 1
now_cyc(iter) = now_cyc(iter) + ID_now(-feature_size + iter + j - 1) * 64 ^ (j - 1);
end
end
end
if ~sum(judge_usage(now_cyc)) && ~sum(judge_usage(inverse(now_cyc,feature_size))) && isempty(intersect(now_cyc,inverse(now_cyc,feature_size)))
judge_usage(now_cyc) = 1;
judge_usage(inverse(now_cyc,feature_size)) = 1;
dfs(col, [ID_now i],feature_size, tag_number);
if flag
return;
end
judge_usage(now_cyc) = 0;
judge_usage(inverse(now_cyc,feature_size)) = 0;
end
end
end
end
function out = inverse(in,feature_size)
for iter=1:size(in,2)
code = in(iter)-1;
for j=1:feature_size
a(j) = mod(code, 64);
a1(j) = (7 - mod(a(j),8)) * 8 + (7 - fix(a(j)/8));
code = fix(code/64);
end
out(iter) = 1;
for i=1:feature_size
out(iter) = out(iter) + a1(i) * 64 ^ (feature_size - i);
end
end
end
function plot_tag(ID, ratio)
global tag_length
for i=1:size(ID,1)
background=ones(tag_length,1.5*tag_length/ratio*size(ID,2));
for j=1:size(ID,2)
background=draw(background,j-1,ID(i,j),ratio);
end
imshow(background)
imwrite(background,['./CTag_Generated/cy' num2str(i) '.bmp'])
end
end
function background=draw(background, cnt, ID_now, ratio)
global tag_length
decoder=[1.47,0;1.54,0;1.61,0;1.68,0;1.68,1;1.61,1;1.54,1;1.47,1];
white_ratio=0.2;
left=fix(ID_now/8)+1;
right=mod(ID_now,8)+1;
block_pos_left=roots([-1 tag_length (white_ratio/2+white_ratio*white_ratio/4-0.2*decoder(left,1))*tag_length*tag_length]);
block_pos_left=block_pos_left((block_pos_left>0));
block_pos_left=block_pos_left((block_pos_left<tag_length*(1-white_ratio)));
if decoder(left,2)
block_pos_left = max(block_pos_left);
else
block_pos_left = min(block_pos_left);
end
block_pos_right=roots([-1 tag_length (white_ratio/2+white_ratio*white_ratio/4-0.2*decoder(right,1))*tag_length*tag_length]);
block_pos_right=block_pos_right((block_pos_right>0));
block_pos_right=block_pos_right((block_pos_right<tag_length*(1-white_ratio)));
if decoder(right,2)
block_pos_right = max(block_pos_right);
else
block_pos_right = min(block_pos_right);
end
background = insertShape(background,'FilledPolygon',[tag_length/ratio * 1.5 * cnt 0; tag_length/ratio * 1.5 * cnt block_pos_left-tag_length*white_ratio/2; tag_length/ratio * 1.5 * cnt + tag_length/ratio block_pos_right-tag_length*white_ratio/2; tag_length/ratio * 1.5 * cnt + tag_length/ratio 0],'Color','black','Opacity',1,'SmoothEdges', false);
background = insertShape(background,'FilledPolygon',[tag_length/ratio * 1.5 * cnt tag_length; tag_length/ratio * 1.5 * cnt block_pos_left + tag_length*white_ratio/2; tag_length/ratio * 1.5 * cnt + tag_length/ratio block_pos_right + tag_length*white_ratio/2; tag_length/ratio * 1.5 * cnt + tag_length/ratio tag_length],'Color','black','Opacity',1,'SmoothEdges', false);
end
function res=testConflict(Code,feature_size)
judge_conf=zeros(64^feature_size,1);
for i=1:size(Code,1)
for j=1:size(Code,2)
score=1;
for k=1:feature_size
score=score+Code(i,mod(j+k-2,size(Code,2))+1)*64^(k-1);
end
if judge_conf(score)
res = 0;
return;
else
judge_conf(score)=1;
end
end
end
Code1 = Code;
Code=fliplr(Code);
for i=1:size(Code,1)
for j=1:size(Code,2)
Code(i,j)=(7 - mod(Code(i,j),8)) * 8 + (7 - fix(Code(i,j)/8));
end
end
for i=1:size(Code,1)
for j=1:size(Code,2)
score=1;
for k=1:feature_size
score=score+Code(i,mod(j+k-2,size(Code,2))+1)*64^(k-1);
end
if judge_conf(score)
res = 0;
return;
else
judge_conf(score)=1;
end
end
end
res = 1;
end