-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgs.py
155 lines (119 loc) · 4.65 KB
/
imgs.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
import codecs
import json
import os
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from skimage import io, morphology, feature
from skimage import measure, color
def complex_images():
bg_name = "data/B0FFF2CO42_1.png"
fname = "data/B0FFF2CO42_GD0003800210101072.png"
fname2 = "data/c_B0FFF2CO42_GD0003800210101072.png"
bg = Image.open(bg_name)
bg = bg.convert('L').convert('RGBA')
mask = Image.new("RGBA", bg.size)
final = Image.new("RGBA", bg.size)
source = Image.open(fname)
mask.paste(source, (546, 159))
final = Image.alpha_composite(final, bg)
final = Image.alpha_composite(final, mask)
final.save(fname2)
def split_space_content():
for root, dirs, files in os.walk('data'):
for fname in files:
bg_name = "data/"+fname
img = io.imread(bg_name, as_grey=True)
# 检测canny边缘,得到二值图片
edgs = feature.canny(img, sigma=3)
chull = morphology.convex_hull_object(edgs)
#
# fig, axes = plt.subplots(1, 2, figsize=(8, 8))
# ax0, ax1 = axes.ravel()
# ax0.imshow(edgs, plt.cm.gray)
# ax0.set_title('many objects')
# ax1.imshow(chull, plt.cm.gray)
# ax1.set_title('convex_hull image')
# # plt.show()
plt.imsave("data2/"+fname, chull)
def to_contours():
for root, dirs, files in os.walk('data2'):
for fname in files:
dst = io.imread('data2/'+fname, as_grey=True)
contours = measure.find_contours(dst, 0.5)
cords = np.concatenate(contours)
new_img = measure.subdivide_polygon(cords, degree=2, preserve_ends=True)
appr_img = measure.approximate_polygon(new_img, tolerance=1)
print(fname, len(appr_img.tolist()))
def labels():
bg_name = "data2/B0FFFF6TJG_1.png"
data = io.imread(bg_name)
data_rgb = color.rgba2rgb(data)
data_gray = color.rgb2gray(data_rgb) # drop transparent layer
# crop image
mask = ~(data_gray == 1)
mask_points = np.argwhere(mask)
top,left,bottom,right = np.min(mask_points[:,0]), np.min(mask_points[:,1]), \
np.max(mask_points[:,0]), np.max(mask_points[:,1])
top = top-10>0 and top-10 or 0
left = left-10>0 and left-10 or 0
bottom = bottom+10<data.shape[0] and bottom+10 or data.shape[0]
right = right+10<data.shape[1] and right+10 or data.shape[1]
print("top,left,bottom,right", top,left,bottom,right);
image = data_gray[top:bottom, left:right].copy()
mask = mask[top:bottom, left:right]
# print(image[0])
# (np.isclose(image,0.0)) |
"""
mask = ((np.isclose(image,0.928392)) | (np.isclose(image,0.844302)) |
(np.isclose(image,0.872443)) | (np.isclose(image,0.976991)) |
(np.isclose(image,0.868027)) | (np.isclose(image,0.868027)) |
(np.isclose(image,0.901961)) | (np.isclose(image,0.899898)) |
(np.isclose(image,0.884697)) |
(np.isclose(image,0.901674)) | (np.isclose(image,0.8058))
)
"""
#mask = ~(image == 1)
image[mask] = 0
image[~mask] = 1
#edgs = feature.canny(image, sigma=3)
#labels = measure.label(edgs, connectivity=1) # 8连通区域标记
#dst = color.label2rgb(labels) # 根据不同的标记显示不同的颜色
#print('regions number:', labels.max() + 1) # 显示连通区域块数(从0开始标记)
#dst = morphology.convex_hull_object(edgs)
contours = measure.find_contours(image, 0.5)
#cords = np.concatenate(contours)
cordarr = []
for cords in contours:
appr_img = measure.subdivide_polygon(cords, degree=2, preserve_ends=True)
appr_img = measure.approximate_polygon(appr_img, tolerance=1)
appr_img += np.array([top,left])
cordarr.append(appr_img.tolist())
print("appr_img:", len(appr_img.tolist()))
print("cordarr:", len(cordarr))
f, (ax0, ax1) = plt.subplots(2, figsize=(15, 10))
ax0.imshow(data)
ax0.set_title('Input image')
ax1.imshow(image)
ax1.set_title('After mask')
plt.show()
def trim_data():
suzu = codecs.open("extsource/poi_suzu_mall_names.txt", encoding="gbk")
names = codecs.open("extsource/poi_mall_names.txt", encoding="gbk")
am = set()
for line in suzu:
if line:
s = json.loads(line)
am.add(s["id"])
for line in names:
if line:
s = json.loads(line)
if s["id"] in am:
am.remove(s["id"])
print("\r\n".join(am))
if __name__ == '__main__':
# complex_images()
# split_space_content()
# to_contours()
labels()
# trim_data()