-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
answer_59.py
83 lines (66 loc) · 2.02 KB
/
answer_59.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
import cv2
import numpy as np
import matplotlib.pyplot as plt
# labeling 8 nearest neighbor
def labeling_8nn(img):
# get image shape
H, W, C = img.shape
# prepare labeling image
label = np.zeros((H, W), dtype=np.int)
label[img[..., 0]>0] = 1
# look up table
LUT = [0 for _ in range(H*W)]
n = 1
for y in range(H):
for x in range(W):
if label[y, x] == 0:
continue
# get right top pixel
c2 = label[max(y-1,0), min(x+1, W-1)]
# get top pixel
c3 = label[max(y-1,0), x]
# get left top pixel
c4 = label[max(y-1,0), max(x-1,0)]
# get left pixel
c5 = label[y, max(x-1,0)]
# if all pixel is non labeled
if c3 < 2 and c5 < 2 and c2 < 2 and c4 < 2:
n += 1
label[y, x] = n
else:
# get labeled index
_vs = [c3, c5, c2, c4]
vs = [a for a in _vs if a > 1]
v = min(vs)
label[y, x] = v
minv = v
for _v in vs:
if LUT[_v] != 0:
minv = min(minv, LUT[_v])
for _v in vs:
LUT[_v] = minv
count = 1
# integrate labeled index of look up table
for l in range(2, n+1):
flag = True
for i in range(n+1):
if LUT[i] == l:
if flag:
count += 1
flag = False
LUT[i] = count
# draw color
COLORS = [[0, 0, 255], [0, 255, 0], [255, 0, 0], [255, 255, 0]]
out = np.zeros((H, W, C), dtype=np.uint8)
for i, lut in enumerate(LUT[2:]):
out[label == (i+2)] = COLORS[lut-2]
return out
# Read image
img = cv2.imread("seg.png").astype(np.float32)
# labeling 8 nearest neighbor
out = labeling_8nn(img)
# Save result
cv2.imwrite("out.png", out)
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()