forked from sunsmarterjie/SDL-Skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataRN.py
151 lines (144 loc) · 5.98 KB
/
dataRN.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
import os
import numpy as np
import pandas as pd
import skimage.io as io
import matplotlib.pyplot as plt
import cv2
def thinImage(src, maxIterations=-1):
assert len(src.shape) == 2, 'please binarify pictures'
img_height, img_width = src.shape
dst = src.copy()
count = 0
while True:
count += 1
if maxIterations != -1 and count > maxIterations:
break
mFlag = []
for i in range(img_height):
for j in range(img_width):
p1 = dst[i, j]
if p1 != 1:
continue
p4 = 0 if j == img_width - 1 else dst[i, j + 1]
p8 = 0 if j == 0 else dst[i, j - 1]
p2 = 0 if i == 0 else dst[i - 1, j]
p3 = 0 if i == 0 or j == img_width - 1 else dst[i - 1, j + 1]
p9 = 0 if i == 0 or j == 0 else dst[i - 1, j - 1]
p6 = 0 if i == img_height - 1 else dst[i + 1, j]
p5 = 0 if i == img_height - 1 or j == img_width - 1 else dst[i + 1, j + 1]
p7 = 0 if i == img_height - 1 or j == 0 else dst[i + 1, j - 1]
if p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 >= 2 and p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 <= 6:
ap = 0
if p2 == 0 and p3 == 1:
ap += 1
if p3 == 0 and p4 == 1:
ap += 1
if p4 == 0 and p5 == 1:
ap += 1
if p5 == 0 and p6 == 1:
ap += 1
if p6 == 0 and p7 == 1:
ap += 1
if p7 == 0 and p8 == 1:
ap += 1
if p8 == 0 and p9 == 1:
ap += 1
if p9 == 0 and p2 == 1:
ap += 1
if ap == 1 and p2 * p4 * p6 == 0 and p4 * p6 * p8 == 0:
mFlag.append([i, j])
for flag in mFlag:
dst[flag[0], flag[1]] = 0
if len(mFlag) == 0:
break
else:
mFlag.clear()
for i in range(img_height):
for j in range(img_width):
p1 = dst[i, j]
if p1 != 1:
continue
p4 = 0 if j == img_width - 1 else dst[i, j + 1]
p8 = 0 if j == 0 else dst[i, j - 1]
p2 = 0 if i == 0 else dst[i - 1, j]
p3 = 0 if i == 0 or j == img_width - 1 else dst[i - 1, j + 1]
p9 = 0 if i == 0 or j == 0 else dst[i - 1, j - 1]
p6 = 0 if i == img_height - 1 else dst[i + 1, j]
p5 = 0 if i == img_height - 1 or j == img_width - 1 else dst[i + 1, j + 1]
p7 = 0 if i == img_height - 1 or j == 0 else dst[i + 1, j - 1]
if p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 >= 2 and p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 <= 6:
ap = 0
if p2 == 0 and p3 == 1:
ap += 1
if p3 == 0 and p4 == 1:
ap += 1
if p4 == 0 and p5 == 1:
ap += 1
if p5 == 0 and p6 == 1:
ap += 1
if p6 == 0 and p7 == 1:
ap += 1
if p7 == 0 and p8 == 1:
ap += 1
if p8 == 0 and p9 == 1:
ap += 1
if p9 == 0 and p2 == 1:
ap += 1
if ap == 1 and p2 * p4 * p8 == 0 and p2 * p6 * p8 == 0:
mFlag.append([i, j])
for flag in mFlag:
dst[flag[0], flag[1]] = 0
if len(mFlag) == 0:
break
else:
mFlag.clear()
return dst
rootDir = './SKLARGE_RN'
fileNames = './SKLARGE_RN/aug_data/train_pair.lst'
frame = pd.read_csv(fileNames, dtype=str, delimiter=' ', header=None)
f = open(os.path.join(rootDir, 'train_pairRN60_255_s_all.lst'), 'w')
for idx in range(len(frame)):
inputName = os.path.join(rootDir, frame.iloc[idx, 0])
targetName = os.path.join(rootDir, frame.iloc[idx, 1])
inputImage = io.imread(inputName)
targetImage = io.imread(targetName)
targetImage[targetImage > 0] = 255
K = 60000.0
# K = 180000.0
H, W = targetImage.shape[0], targetImage.shape[1]
sy = np.sqrt(K * H / W) / float(H)
sx = np.sqrt(K * W / H) / float(W)
inputImage_RN = cv2.resize(inputImage, None, None, fx=sx, fy=sy, interpolation=cv2.INTER_LINEAR)
targetImage_RN = cv2.resize(targetImage, None, None, fx=sx, fy=sy, interpolation=cv2.INTER_LINEAR)
targetImage_RN[targetImage_RN > 0] = 255
aa, bb = np.nonzero(targetImage_RN)
cc = targetImage_RN[aa, bb]
if (cc.min() != 255 or cc.max() != 255):
print('min max error!')
print('RN error')
break
targetImage_BN = targetImage_RN.copy()
targetImage_BN[targetImage_BN == 255] = 1
targetImage_TN = thinImage(targetImage_BN)
targetImage_TN[targetImage_TN == 1] = 255
aa, bb = np.nonzero(targetImage_TN)
cc = targetImage_TN[aa, bb]
if (cc.min() != 255 or cc.max() != 255):
print('TN error')
break
inputDir_TN = frame.iloc[idx, 0].replace('scale', 'scaleRN60_')
targetDir_TN = frame.iloc[idx, 1].replace('scale', 'scaleRN60_')
inputName_TN = os.path.join(rootDir, inputDir_TN)
targetName_TN = os.path.join(rootDir, targetDir_TN)
inputsDir_TN = inputName_TN[:inputName_TN.find(inputDir_TN.split('/')[-1])]
if not os.path.exists(inputsDir_TN):
os.makedirs(inputsDir_TN)
targetsDir_TN = targetName_TN[:targetName_TN.find(targetDir_TN.split('/')[-1])]
if not os.path.exists(targetsDir_TN):
os.makedirs(targetsDir_TN)
io.imsave(inputName_TN, inputImage_RN)
io.imsave(targetName_TN, targetImage_TN)
f.write(inputDir_TN + ' ' + targetDir_TN + '\n')
if idx % 12 == 0:
print('idx:{} {} {}'.format(idx, inputDir_TN, targetDir_TN))
f.close()