-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_edge.py
99 lines (69 loc) · 3.04 KB
/
gen_edge.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
import os
import numpy as np
from PIL import Image
from PIL import ImageFilter
def gen_edge(img_txt_path, mask_txt_path):
imgs = []
img_names = []
f_img = open(img_txt_path, 'r')
f_mask = open(mask_txt_path, 'r')
for img in f_img:
img = img.rstrip()
imgs.append([img])
img_names.append([img.split('/')[-1]])
i = 0
for mask in f_mask:
mask = mask.rstrip()
imgs[i].append(mask)
img_names[i].append(mask.split('/')[-1])
i += 1
f_img.close()
f_mask.close()
for i in range(len(imgs)):
x_path, y_path = imgs[i]
x_name, y_name = img_names[i]
img_x = Image.open(x_path).convert('RGB')
img_y = Image.open(y_path)
img_y = np.array(img_y)
img_y[img_y == 0] = 2
img_y[img_y == 128] = 1
img_y[img_y == 255] = 0
loc = np.where(img_y == 1)
center_dim0 = (np.max(loc[0]) + np.min(loc[0])) // 2
center_dim1 = (np.max(loc[1]) + np.min(loc[1])) // 2
img_y = Image.fromarray(img_y)
# crop size (512, 512)
if center_dim1 >= 256 and center_dim0 >= 256:
img_x = img_x.crop((center_dim1 - 256, center_dim0 - 256, center_dim1 + 256, center_dim0 + 256))
img_y = img_y.crop((center_dim1 - 256, center_dim0 - 256, center_dim1 + 256, center_dim0 + 256))
else:
img_x_crop = img_x.crop((0, center_dim0 - 256, center_dim1 + 256, center_dim0 + 256))
img_y_crop = img_y.crop((0, center_dim0 - 256, center_dim1 + 256, center_dim0 + 256))
img_black_rgb = Image.new('RGB', (512, 512), (0, 0, 0))
img_white_gray = Image.new('L', (512, 512), 0)
img_black_rgb.paste(img_x_crop, (256 - center_dim1, 0, 512, 512))
img_x = img_black_rgb
img_white_gray.paste(img_y_crop, (256 - center_dim1, 0, 512, 512))
img_y = img_white_gray
edge = np.array(img_y.filter(ImageFilter.Kernel((5, 5), (-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 24,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,-1, -1), 1, 0)))
img_y = np.zeros((512, 512))
img_y[edge > 0] = 255
img_y = Image.fromarray(img_y)
train_path = os.path.join("refuge", "Train_edge")
valid_path = os.path.join("refuge", "Valid_edge")
if not os.path.exists(train_path):
os.mkdir(train_path)
if not os.path.exists(valid_path):
os.mkdir(valid_path)
path = train_path
# path = valid_path
img_y.convert('L').save(os.path.join(path + y_name.split('.')[0] + '_edge.png'))
print(i)
if __name__ == '__main__':
train_txt_path = os.path.join("../", "refuge", "train.txt")
train_mask_txt_path = os.path.join("../", "refuge", "train_mask.txt")
valid_txt_path = os.path.join("../", "refuge", "valid.txt")
valid_mask_txt_path = os.path.join("../", "refuge", "valid_mask.txt")
gen_edge(train_txt_path, train_mask_txt_path)
# gen_edge(valid_txt_path, valid_mask_txt_path)