-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2.py
333 lines (287 loc) · 12.6 KB
/
p2.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
import matplotlib.pyplot as plt
import modify
import numpy as np
import cv2
import sys
sys.setrecursionlimit(100000)
# gray = np.array([[10, 10, 10, 10], [10, 10, 11, 10], [10, 12, 5, 10], [10, 10, 10, 10]])
# gray = np.array([[87, 87, 87], [87, 87, 87], [87, 87, 85]])
gray = np.array([[100, 100, 100], [80, 80, 80], [80, 110, 80]])
gray1 = np.array([[80, 80, 80], [80, 80, 80], [80, 110, 80]])
def method_name(gray):
a, b, c = modify.point_classification_new(gray, 1, 1, 1)
for x in range(len(a)):
print(gray[a[x][0], a[x][1]], end=" ")
print(a[x], end=" ")
print()
for x in range(len(b)):
print(gray[b[x][0], b[x][1]], end=" ")
print(b[x], end=" ")
print()
for x in range(len(c)):
print(gray[c[x][0], c[x][1]], end=" ")
print(c[x], end=" ")
print()
# 将list转换为map的函数
def listtomap(li):
mp = {}
for i in range(len(li)):
mp[((li[i][0][0][0], li[i][0][0][1]), (li[i][0][1][0], li[i][0][1][1]))] = li[i][1]
return mp
xx = [-1, -1, -1, 0, +1, +1, +1, 0]
yy = [+1, 0, -1, -1, -1, 0, +1, +1]
xxx = [-1, -1, -1, 0, +1, +1, +1, 0, -1, -1, -1, 0, +1, +1, +1, 0]
yyy = [+1, 0, -1, -1, -1, 0, +1, +1, +1, 0, -1, -1, -1, 0, +1, +1]
# 进行噪声的修复
def fix_noise(gray, i, j, x, y):
for k in range(len(xx)):
if x == i + xxx[k] and y == j + yyy[k]:
if abs(gray[i + xxx[k + 1], j + yyy[k + 1]] - gray[x, y]) > abs(
gray[i + xxx[k - 1], j + yyy[k - 1]] - gray[x, y]):
gray[x, y] = gray[i + xxx[k - 1], j + yyy[k - 1]]
else:
gray[x, y] = gray[i + xxx[k + 1], j + yyy[k + 1]]
# 区域分割的函数
def cut(gray, th, th2):
re = [] # 存储符合条件的八邻域分割点
re_weak = []
noise_num = 0
noise_pos = np.zeros((gray.shape[0], gray.shape[1]))
for i in range(1, gray.shape[0] - 1):
for j in range(1, gray.shape[1] - 1):
mul_list = {} # 存储每个边的能量值与坐标信息
poit = {} # 存储每个点的能量值与坐标信息
for k in range(len(xx)):
poit[(i + xx[k], j + yy[k])] = gray[i + xx[k], j + yy[k]]
# if i == 1 and j == 1:
# # print(i + xx[k], j + yy[k])
# # print(i + xx[k - 1], j + yy[k - 1])
# print(type(gray[i + xx[k], j + yy[k]]))
# print(type(gray[i + xx[k - 1], j + yy[k - 1]]))
# # print((gray[i + xx[k], j + yy[k]] - gray[i + xx[k - 1], j + yy[k - 1]]))
# print(int(gray[i + xx[k], j + yy[k]]) - int(gray[i + xx[k - 1], j + yy[k - 1]]))
mul_list[((i + xx[k], j + yy[k]), (i + xx[k - 1], j + yy[k - 1]))] = pow(
int(gray[i + xx[k], j + yy[k]]) - int(gray[i + xx[k - 1], j + yy[k - 1]]), 2)
noise, a, b = modify.point_classification_new(gray, i, j, 1) # 找到八邻域中的一个噪声点,后续判断噪声点是否影响分割点
noise_pos[noise[0][0], noise[0][1]] = 1
sum_a = 0
num_a = 0
sum_b = 0
num_b = 0
raws = gray
point_a = []
point_b = []
for k in range(len(a)):
sum_a += raws[a[k][0], a[k][1]]
num_a += 1
point_a.append(raws[a[k][0], a[k][1]])
for k in range(len(b)):
sum_b += raws[b[k][0], b[k][1]]
num_b += 1
point_b.append(raws[b[k][0], b[k][1]])
maxa = max(point_a) # 两个区域的的四个极值
maxb = max(point_b)
mina = min(point_a)
minb = min(point_b)
# if (maxa >= maxb and mina <= maxb):
# re[i, j] = 0
# elif (maxb >= maxa and minb <= maxa):
# re[i, j] = 0
# print(mul_list)
# 将每条边的能量进行排序,得到能量最大的两条边为索引1和0, 但是得到的数据类型是列表
mul_list = sorted(mul_list.items(), key=lambda item: item[1], reverse=True)
# print(mul_list)
# print(type(mul_list))
# mina = min(mul_list[0][0][0][0], mul_list[1][0][0][0])
# minb = min(mul_list[0][0][0][1], mul_list[1][0][0][1])
# x1 = (mul_list[0][0][0][0], mul_list[0][0][0][1])
# x2 = (mul_list[1][0][0][0], mul_list[1][0][0][1])
n1 = mul_list[0][0] # 两个含有噪声点的边的坐标信息(不一定含有噪声,只是能量边最大)
n2 = mul_list[1][0]
# if i == 1 and j == 1:
# print(mul_list)
# print(a)
# print(b)
# print(n1)
# print(n2)
# print(gray[a, b])
# del (poit[(a, b)]) # 删除是噪声点那个点
# print(poit)
noise_po, po1, po2 = find_(n1, n2)
# print(noise_po)
# print(po1)
# print(po2)
# if(a1[0][0]==a and a1[0][1]== b):
# if noise[0] == (mina, minb): # 判断噪声点影响了八邻域分隔
if po1 != 0 and noise_po == noise[0]:
noise_num += 1
# while po1 != 0 and noise_po == noise[0]:
# fix_noise(gray, i, j, noise[0][0], noise[0][1])
# noise, a, b = modify.point_classification_new(gray, i, j, 1) # 找到八邻域中的一个噪声点,后续判断噪声点是否影响分割点
# mul_list = {}
# for k in range(len(xx)):
# mul_list[((i + xx[k], j + yy[k]), (i + xx[k - 1], j + yy[k - 1]))] = (
# pow((gray[i + xx[k], j + yy[k]] - gray[i + xx[k - 1], j + yy[k - 1]]), 2))
# mul_list = sorted(mul_list.items(), key=lambda item: item[1], reverse=True)
# n1 = mul_list[0][0] # 两个含有噪声点的边的坐标信息(不一定含有噪声,只是能量边最大)
# n2 = mul_list[1][0]
# noise_po, po1, po2 = find_(n1, n2)
# print("while")
# print("fix")
if mina > maxb or minb > maxa:
if abs((sum_a / num_a) - (sum_b / num_b)) > th:
# continue
re.append(n1) # 如果噪声对八邻域分隔没有影响,那么把认为分割正确的两个点加入进去,两个tuple,一共四个坐标
re.append(n2)
elif th >= abs((sum_a / num_a) - (sum_b / num_b)) >= th2:
re_weak.append(n1) # 如果噪声对八邻域分隔没有影响,那么把认为分割正确的两个点加入进去,两个tuple,一共四个坐标
re_weak.append(n2)
# r1 = mul_list[0][0]
# del mul_list[0] # 删除最大能量的两条边
# r2 = mul_list[0][0]
# del mul_list[0]
#
#
# m1, m2, m3 = find_(r1, r2)
#
# mul_list = listtomap(mul_list) # 把相应的列表重新转为map类型
#
# mul_list[((m2), (m3))] = pow((gray[m2[0]][m2[1]] - gray[m3[0]][m3[1]]), 2) # 去掉一个噪声点之后的边的能量加入进去
# re.append(n1) # 如果噪声对八邻域分隔没有影响,那么把认为分割正确的两个点加入进去,两个tuple,一共四个坐标
# re.append(n2)
else:
if mina > maxb or minb > maxa:
if abs((sum_a / num_a) - (sum_b / num_b)) > th:
# continue
re.append(n1) # 如果噪声对八邻域分隔没有影响,那么把认为分割正确的两个点加入进去,两个tuple,一共四个坐标
re.append(n2)
elif th >= abs((sum_a / num_a) - (sum_b / num_b)) >= th2:
re_weak.append(n1) # 如果噪声对八邻域分隔没有影响,那么把认为分割正确的两个点加入进去,两个tuple,一共四个坐标
re_weak.append(n2)
# print("re")
# print(re)
print(noise_num)
return re, re_weak, noise_pos
# 含有噪声点的两条边,共有三个点组成
# 如果返回 0 0 0 则表示两条边不相邻,也就是噪声不影响原始的区域分割点
def find_(x, y): # 输入含有噪声的两条边的信息,返回第一个噪声点,和另外的两个点的信息
x1 = x[0]
x2 = x[1]
y1 = y[0]
y2 = y[1]
if x1 == y1:
return x1, x2, y2
if x1 == y2:
return x1, x2, y1
if x2 == y1:
return x2, x1, y2
if x2 == y2:
return x2, x1, y1
else:
return 0, 0, 0
# method_name(gray1)
# cut(gray1)
# src = "41004"
# inpath = "D:\\experiment\\pic\\q\\"
# outpath = "D:\\out\\"
#
# raw = cv2.imread(inpath + src + ".jpg")
# raw_Filter = cv2.bilateralFilter(raw, 7, 50, 50)
#
# raw2 = cv2.cvtColor(raw_Filter, cv2.COLOR_BGR2GRAY)
# raw2_Filter = cv2.bilateralFilter(raw2, 7, 50, 50)
#
# # re = []
# re = cut(raw2_Filter)
# print(re)
# plt_cs.point_show(raw2_Filter, re)
# print(gray.shape[0])
# print(gray.shape[1])
# print(xx[-2])
# a = [1, 2, 3]
# print(a)
# del a[0]
# print(a)
# b = (1,2,3)
# del b[1]
# print(b)
# 遍历二维数组,查找延伸的起始点,进行延伸处理
def find_weak(min_point, show):
x = []
y = []
for i in range(min_point.shape[0]):
for j in range(min_point.shape[1]):
if min_point[i, j] == 2: # 四邻域范围内存在明显的区域分割点
if i % 2 == 0 and j % 2 != 0: # 区域的分割点在行上
print(end="")
find_next(i / 2, j / 2, 1, show, x, y)
find_next(i / 2, j / 2, 3, show, x, y)
elif i % 2 != 0 and j % 2 == 0: # 区域的分割点在列上
find_next(i / 2, j / 2, 0, show, x, y)
find_next(i / 2, j / 2, 2, show, x, y)
print(end="")
return x, y
# 开始延伸处理
# 横纵坐标i,j n 当前四邻域的边的序号(i,j所在边的序号)
def find_next(i, j, n, show, x, y):
next = [] # 四邻域中四个中点的数值
point = [] # 四邻域的四个中点的坐标
next_point = [] # 下一个邻域的边的索引编号
if i <= 2 or i >= show.shape[0] / 2 - 2:
return
if j <= 2 or j >= show.shape[1] / 2 - 2:
return
if n == 0:
next.append(-1)
next.append(show[int((i - 0.5) * 2), int((j - 0.5) * 2)])
next.append(show[int(i * 2), int((j - 1) * 2)])
next.append(show[int((i + 0.5) * 2), int((j - 0.5) * 2)])
point.append((-1, -1))
point.append(((i - 0.5), (j - 0.5)))
point.append((i, j - 1))
point.append(((i + 0.5), (j - 0.5)))
next_point = [-1, 3, 0, 1]
if n == 1:
next.append(show[int((i + 0.5) * 2), int((j + 0.5) * 2)])
next.append(-1)
next.append(show[int((i + 0.5) * 2), int((j - 0.5) * 2)])
next.append(show[int((i + 1) * 2), int(j * 2)])
point.append(((i + 0.5), (j + 0.5)))
point.append((-1, -1))
point.append(((i + 0.5), (j - 0.5)))
point.append(((i + 1), j))
next_point = [2, -1, 0, 1]
if n == 2:
next.append(show[int(i * 2), int((j + 1) * 2)])
next.append(show[int((i - 0.5) * 2), int((j + 0.5) * 2)])
next.append(-1)
next.append(show[int((i + 0.5) * 2), int((j + 0.5) * 2)])
point.append((i, (j + 1)))
point.append(((i - 0.5), (j + 0.5)))
point.append((-1, -1))
point.append(((i + 0.5), (j + 0.5)))
next_point = [2, 3, -1, 1]
if n == 3:
next.append(show[int((i - 0.5) * 2), int((j + 0.5) * 2)])
next.append(show[int((i - 1) * 2), int(j * 2)])
next.append(show[int((i - 0.5) * 2), int((j - 0.5) * 2)])
next.append(-1)
point.append(((i - 0.5), (j + 0.5)))
point.append(((i - 1), j))
point.append(((i - 0.5), (j - 0.5)))
point.append((-1, -1))
next_point = [2, 3, 0, -1]
flag = 0
for k in range(4):
if next[k] == 2 or next[k] == 3:
flag = 1
print("aaa")
return
for k in range(4):
if next[k] == 1 and flag == 0:
x.append(point[k][0])
y.append(point[k][1])
show[int(point[k][0] * 2), int(point[k][1] * 2)] = 3 # 延伸之后将标记进行修改
print("nnnnn")
find_next(point[k][0], point[k][1], next_point[k], show, x, y)
# cut(gray,1,1)