-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyMarchingSquares.py
394 lines (340 loc) · 13 KB
/
MyMarchingSquares.py
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# from pylab import *
import numpy as np
# 将大于阈值的数统一一下便于分类处理+2-2,小于阈值的就是+1-1
def labels_matrix(ori_matrix, value):
x, y = ori_matrix.shape
new_matrix = np.zeros((x, y))
for i in range(x):
for j in range(y):
if abs(ori_matrix[i][j]) <= value:
if ori_matrix[i][j] > 0: # 0继续保持
new_matrix[i][j] = 1
elif ori_matrix[i][j] < 0:
new_matrix[i][j] = -1
elif abs(ori_matrix[i][j]) > value:
if ori_matrix[i][j] > 0:
new_matrix[i][j] = 2
elif ori_matrix[i][j] < 0:
new_matrix[i][j] = -2
return new_matrix
# 将大于阈值的数统一一下便于分类处理+2-2,小于阈值的就是+1-1
def labels_matrix_new(ori_matrix, value, low):
x, y = ori_matrix.shape
new_matrix = np.zeros((x, y))
for i in range(x):
for j in range(y):
if low <= abs(ori_matrix[i][j]) <= value:
if ori_matrix[i][j] > 0: # 0继续保持
new_matrix[i][j] = 1
elif ori_matrix[i][j] < 0:
new_matrix[i][j] = -1
elif abs(ori_matrix[i][j]) > value:
if ori_matrix[i][j] > 0:
new_matrix[i][j] = 2
elif ori_matrix[i][j] < 0:
new_matrix[i][j] = -2
return new_matrix
# 四个像素的local 的坐标
xx = [0, 0, 1, 1]
yy = [0, 1, 1, 0]
# 作用:验证local是不是大于阈值且是非交叉矛盾的
def isVerifi(mul_list):
neg = 0
pos = 0
for i in range(4):
if mul_list[i] == -4:
neg += 1
elif mul_list[i] == 4:
pos += 1
# if neg == 2 and pos == 2:
if neg == 2:
return True
# if neg != 4 and (pos+neg) == 4:
# return True
# 作用:验证local即使包含有0但只要是可以画边的就进行
def isContinue(mul_list):
neg = 0
pos = 0
for i in range(4):
if mul_list[i] < 0:
neg += 1
elif mul_list[i] == 4:
pos += 1
# if neg == 2 and pos == 2:
if neg == 2:
return True
# 作用:验证-1+1交叉分布,特殊进行延伸
def equal4(mul_list):
tag = 0
for i in range(4):
# if (mul_list[i] == -1):
if (mul_list[i] < 0):
tag += 1
if tag == 4:
return False
else:
return True
# 特殊的起始点,周围有两个大于阈值的并且相邻的点,那么那些+1-1的点可以当做正常点进行延伸
def isSpecial(mul_list):
tag1 = 0
tag2 = 0
tagTack = 0
tagPre = 0
# for i in range(4):
# if mul_list[i] == -4:
# tag1 += 1
# for i in range(4):
# if mul_list[i] < 0:
# tag2 += 1
# if mul_list[i] != -4:
# tagTack = i
# else:
# tagPre = i
for i in range(4):
if mul_list[i] == -4:
tag1 += 1
tagPre = i
for i in range(4):
if mul_list[i] == -2 or mul_list[i] == -1:
tag2 += 1
tagTack = i
# if tag1 == 1 and tag2 == 2:
if tag1 == 1 and tag2 == 1:
print(1)
return tagTack, tagPre
else:
return 9, 9
# 根据当前的local的画边点的序号找到当前画边点的中点
# 参数: 当前local的边序号,当前local的坐标
# 返回: 当前local的画边的坐标中点
def point(k, i, j):
if k == 0:
return i + 0.5, j
if k == 1:
return i, j + 0.5
if k == 2:
return i + 0.5, j + 1
if k == 3:
return i + 1, j + 0.5
# 参数:当前local的边序号,当前local的坐标
# 返回值:下一个local的坐标值
# 作用:根据当前延伸的边,延伸的下一个local区域的左上角的坐标点
def next_local(list_n, i, j):
if list_n == 0:
return i, j - 1
if list_n == 1:
return i - 1, j
if list_n == 2:
return i, j + 1
if list_n == 3:
return i + 1, j
# 参数:前一个local的终止边序号,当前local的左上坐标
# 返回值: 当前local中新增加的两个点的坐标
def next_two_point(list_n, i, j):
if list_n == 0:
return i, j, i + 1, j
if list_n == 1:
return i, j, i, j + 1
if list_n == 2:
return i, j + 1, i + 1, j + 1
if list_n == 3:
return i + 1, j, i + 1, j + 1
# 参数:当前local的四个边的乘积,当前local中起始延伸边的序号
def next_mid(mul_list, n):
for i in range(4):
if mul_list[i] < 0 and i != n:
return i
# 参数:上一个local的边序号
# 返回值:当前local的边序号
def local_side(n):
if n == 0:
return 2
if n == 1:
return 3
if n == 2:
return 0
if n == 3:
return 1
# 参数:矩阵,当前local的坐标
# 返回值: void
# 作用:修复当前local的所有标记,修复为正常值
def repair(matrix, i, j):
for k in range(len(xx)):
# if matrix[i + xx[k], j + yy[k]] > 0:
if matrix[i + xx[k], j + yy[k]] == 1:
matrix[i + xx[k], j + yy[k]] = 3
# matrix[i + xx[k], j + yy[k]] = 2
# elif matrix[i + xx[k], j + yy[k]] < 0:
elif matrix[i + xx[k], j + yy[k]] == -1:
matrix[i + xx[k], j + yy[k]] = -3
# matrix[i + xx[k], j + yy[k]] = -2
# 追踪延伸
# 参数: 矩阵,上一个local的终止延伸点,当前新延伸的local的坐标点
def track(matrix, list_n, i, j, point_x, point_y):
if i >= matrix.shape[0] - 1 or j >= matrix.shape[1] - 1 or i < 0 or j < 0:
return
# 根据上一个延伸终点边序号,找到当前local新增加的两个点的坐标
x1, y1, x2, y2 = next_two_point(list_n, i, j)
mul_list = []
for k in range(len(xx)):
mul_list.append(matrix[i + xx[k], j + yy[k]] * matrix[i + xx[k - 1], j + yy[k - 1]])
if x1 >= matrix.shape[0] - 1 or y1 >= matrix.shape[1] - 1 or x1 < 0 or y1 < 0:
return
if x2 >= matrix.shape[0] - 1 or y2 >= matrix.shape[1] - 1 or x2 < 0 or y2 < 0:
return
# 新增加的两个点 1.不能满足终止的-2,2的情况,不能包含0,不能正负交叉
# if matrix[x1, y1] * matrix[x2, y2] != -4 and matrix[x1, y1] * matrix[x2, y2] != 4 and matrix[x1, y1] * matrix[
# x2, y2] != 0 and equal4(mul_list) == False:
if matrix[x1, y1] * matrix[x2, y2] != -4 and matrix[x1, y1] * matrix[x2, y2] != 4 and matrix[x1, y1] * matrix[
x2, y2] != -9 and matrix[x1, y1] * matrix[x2, y2] != 9 and matrix[x1, y1] * matrix[
x2, y2] != -6 and matrix[x1, y1] * matrix[x2, y2] != 6 and isContinue(mul_list) and equal4(mul_list):
# if matrix[x1, y1] * matrix[x2, y2] == 1 or matrix[x1, y1] * matrix[x2, y2] == -1 or matrix[x1, y1] * matrix[
# x2, y2] == -2 or matrix[x1, y1] * matrix[x2, y2] == 2 or matrix[x1, y1] * matrix[x2, y2] == 3 or matrix[
# x1, y1] * matrix[x2, y2] == -3 and equal4(mul_list) == False:
# if matrix[x1, y1] * matrix[x2, y2] != -4 and matrix[x1, y1] * matrix[x2, y2] != 0 and equal4(mul_list) == False:
# 找到当前local的延伸终止边的序号
n = next_mid(mul_list, local_side(list_n))
# 根据当前local的延伸终点的边序号,找到下一个local的左上角坐标
x, y = next_local(n, i, j)
point_x.append(point(local_side(list_n), i, j)[0])
point_y.append(point(local_side(list_n), i, j)[1])
point_x.append(point(n, i, j)[0])
point_y.append(point(n, i, j)[1])
repair(matrix, i, j)
track(matrix, n, x, y, point_x, point_y)
# 第二种+1-1矛盾交叉的情况
elif matrix[x1, y1] * matrix[x2, y2] != -4 and matrix[x1, y1] * matrix[x2, y2] != 4 and matrix[x1, y1] * matrix[
x2, y2] != -9 and matrix[x1, y1] * matrix[x2, y2] != 9 and matrix[x1, y1] * matrix[
x2, y2] != -6 and matrix[x1, y1] * matrix[x2, y2] != 6 and isContinue(mul_list) and not equal4(mul_list):
# if matrix[x1, y1] * matrix[x2, y2] == 1 or matrix[x1, y1] * matrix[x2, y2] == -1 or matrix[x1, y1] * matrix[
# x2, y2] == -2 or matrix[x1, y1] * matrix[x2, y2] == 2 or matrix[x1, y1] * matrix[x2, y2] == 3 or matrix[
# x1, y1] * matrix[x2, y2] == -3 and equal4(mul_list) == True:
# if matrix[x1, y1] * matrix[x2, y2] != -4 and matrix[x1, y1] * matrix[x2, y2] != 0 and equal4(mul_list) == True:
n = (local_side(list_n) + 2) % 4
x, y = next_local(n, i, j)
repair(matrix, i, j)
track(matrix, n, x, y, point_x, point_y)
# 画边
def traverse(matrix):
w, h = matrix.shape
points_x = []
points_y = []
for i in range(w - 1):
for j in range(h - 1):
mid_x = []
mid_y = []
mul_list = []
for k in range(len(xx)):
mul_list.append(matrix[i + xx[k], j + yy[k]] * matrix[i + xx[k - 1], j + yy[k - 1]])
n, n1 = isSpecial(mul_list)
if isVerifi(mul_list):
for k in range(len(xx)):
if mul_list[k] == -4:
mid_x.append(point(k, i, j)[0])
mid_y.append(point(k, i, j)[1])
repair(matrix, i, j) # 修复起始延伸点
elif n != 9:
x, y = next_local(n, i, j)
track(matrix, n, x, y, points_x, points_y)
repair(matrix, i, j) # 修复起始延伸点
points_x.append(point(n, i, j)[0])
points_y.append(point(n, i, j)[1])
points_x.append(point(n1, i, j)[0])
points_y.append(point(n1, i, j)[1])
if len(mid_x) == 2:
points_x.append(mid_x[0])
points_x.append(mid_x[1])
points_y.append(mid_y[0])
points_y.append(mid_y[1])
return points_x, points_y
# 不加入延伸
def traverse1(matrix):
w, h = matrix.shape
points_x = []
points_y = []
for i in range(w - 1):
for j in range(h - 1):
mid_x = []
mid_y = []
mul_list = []
for k in range(len(xx)):
mul_list.append(matrix[i + xx[k], j + yy[k]] * matrix[i + xx[k - 1], j + yy[k - 1]])
if isVerifi(mul_list):
for k in range(len(xx)):
if mul_list[k] == -4:
mid_x.append(point(k, i, j)[0])
mid_y.append(point(k, i, j)[1])
if len(mid_x) == 2:
points_x.append(mid_x[0])
points_x.append(mid_x[1])
points_y.append(mid_y[0])
points_y.append(mid_y[1])
return points_x, points_y
def tag_cross(matrix, i, j):
for k in range(len(xx)):
matrix[i + xx[k], j + yy[k]] = 255
def find_cross(matrix):
re = np.zeros((matrix.shape[0], matrix.shape[1]))
for i in range(matrix.shape[0] - 1):
for j in range(matrix.shape[1] - 1):
mul_list = []
for k in range(len(xx)):
mul_list.append(matrix[i + xx[k], j + yy[k]] * matrix[i + xx[k - 1], j + yy[k - 1]])
if not equal4(mul_list):
tag_cross(re, i, j)
return re
# 画边
def traverse_new(matrix):
w, h = matrix.shape
points_x = []
points_y = []
extend_n = []
extend_n1 = []
extend_i = []
extend_j = []
for i in range(w - 1):
for j in range(h - 1):
mid_x = []
mid_y = []
mul_list = []
for k in range(len(xx)):
mul_list.append(matrix[i + xx[k], j + yy[k]] * matrix[i + xx[k - 1], j + yy[k - 1]])
n, n1 = isSpecial(mul_list)
if isVerifi(mul_list):
for k in range(len(xx)):
if mul_list[k] == -4:
mid_x.append(point(k, i, j)[0])
mid_y.append(point(k, i, j)[1])
repair(matrix, i, j) # 修复起始延伸点
elif n != 9:
extend_n1.append(n1)
extend_n.append(n)
print(2)
extend_i.append(i)
extend_j.append(j)
if len(mid_x) == 2:
points_x.append(mid_x[0])
points_x.append(mid_x[1])
points_y.append(mid_y[0])
points_y.append(mid_y[1])
# return points_x, points_y, extend_n, extend_n1, extend_i, extend_j
return extend_new(matrix, points_x, points_y, extend_n, extend_n1, extend_i, extend_j)
def extend_new(matrix, points_x, points_y, extend_n, extend_n1, extend_i, extend_j):
print(extend_i)
print(extend_j)
print(points_x)
print(points_y)
for m in range(len(extend_i)):
i = extend_i[m]
j = extend_j[m]
n = extend_n[m]
n1 = extend_n1[m]
x, y = next_local(n, i, j)
track(matrix, n, x, y, points_x, points_y)
repair(matrix, i, j) # 修复起始延伸点
points_x.append(point(n, i, j)[0])
points_y.append(point(n, i, j)[1])
points_x.append(point(n1, i, j)[0])
points_y.append(point(n1, i, j)[1])
return points_x, points_y
# def