forked from weningerleon/TextileDefectDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yarn_common_functions.py
283 lines (235 loc) · 8.03 KB
/
yarn_common_functions.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
import matplotlib.pyplot as plt
import numpy as np
from statistics import median
import cv2
class floatPoint:
x=0
y=0
arr_x = -1
arr_y = -1
right_n = None
right_dist = -1
left_n = None
left_dist = -1
upper_n = None
upper_dist = -1
lower_n = None
lower_dist = -1
area = -1
convArea = -1
label = ""
def __init__(self,x,y,label, area, convArea):
self.x = x
self.y = y
self.label = label
self.area = area
self.convArea = convArea
@staticmethod
def dist_x(p1, p2):
return np.absolute(p1.x - p2.x)
@staticmethod
def dist_y(p1, p2):
return np.absolute(p1.y - p2.y)
@staticmethod
def dist(p1, p2):
return np.sqrt(np.square(p1.x - p2.x) + np.square(p1.y - p2.y))
def find_neighbors(self, all_points):
myX = self.x
myY = self.y
# ax.plot(myX, myY, '.r', markersize=15)
tube_radius = np.sqrt(self.area) / 4
no_x = False
try:
x_dists, x_points = zip(
*[(point.x - myX, point) for point in all_points if
(myY - tube_radius < point.y < myY + tube_radius and point.x != myX)])
x_dists = list(x_dists)
except Exception as inst:
no_x = True
try:
y_dists, y_points = zip(
*[(point.y - myY, point) for point in all_points if
(myX - tube_radius < point.x < myX + tube_radius and point.y != myY)])
y_dists = list(y_dists)
except Exception as inst:
no_y = True
if no_x == True:
return False
# right neighbor
try:
arg = min([x for x in x_dists if x > 0])
idx = x_dists.index(arg)
self.right_n = x_points[idx]
except Exception as inst:
self.right_n = None
''' '''
# left neighbor
try:
arg = max([x for x in x_dists if x < 0])
idx = x_dists.index(arg)
self.left_n = x_points[idx]
except Exception as inst:
self.left_n = None
# upper neighbor
try:
arg = max([y for y in y_dists if y < 0])
idx = y_dists.index(arg)
self.upper_n = y_points[idx]
except Exception as inst:
self.upper_n = None
# lower neighbor
try:
arg = min([y for y in y_dists if y > 0])
idx = y_dists.index(arg)
self.lower_n = y_points[idx]
except Exception as inst:
self.lower_n = None
return True
def find_warp_neighbors(self, all_points):
myX = self.x
myY = self.y
# ax.plot(myX, myY, '.r', markersize=15)
tube_radius = np.sqrt(self.area) / 4
try:
y_dists, y_points = zip(
*[(point.y - myY, point) for point in all_points if
(myX - tube_radius < point.x < myX + tube_radius and point.y != myY)])
y_dists = list(y_dists)
except Exception as inst:
return False
# upper neighbor
try:
arg = max([y for y in y_dists if y < 0])
idx = y_dists.index(arg)
self.upper_n = y_points[idx]
except Exception as inst:
self.upper_n = None
# lower neighbor
try:
arg = min([y for y in y_dists if y > 0])
idx = y_dists.index(arg)
self.lower_n = y_points[idx]
except Exception as inst:
self.lower_n = None
return True
def find_weft_neighbors(self, all_points):
myX = self.x
myY = self.y
# ax.plot(myX, myY, '.r', markersize=15)
tube_radius = np.sqrt(self.area) / 4
try:
x_dists, x_points = zip(
*[(point.x - myX, point) for point in all_points if
(myY - tube_radius < point.y < myY + tube_radius and point.x != myX)])
x_dists = list(x_dists)
except Exception as inst:
return False
# right neighbor
try:
arg = min([x for x in x_dists if x > 0])
idx = x_dists.index(arg)
self.right_n = x_points[idx]
except Exception as inst:
self.right_n = None
''' '''
# left neighbor
try:
arg = max([x for x in x_dists if x < 0])
idx = x_dists.index(arg)
self.left_n = x_points[idx]
except Exception as inst:
self.left_n = None
return True
class floatPointList(list):
def __init__(self, *args):
list.__init__(self, *args)
def remove(self, fp):
if fp.lower_n is not None:
fp.lower_n.upper_n = None
if fp.right_n is not None:
fp.right_n.left_n = None
if fp.upper_n is not None:
fp.upper_n.lower_n = None
if fp.left_n is not None:
fp.left_n.right_n = None
list.remove(self, fp)
def getMedianDists(self):
means_hor = []
means_ver = []
for fl in self:
if fl.right_dist > 0:
means_hor.append(fl.right_dist)
if fl.lower_dist > 0:
means_ver.append(fl.lower_dist)
mean_hor = median(means_hor)
mean_ver = median(means_ver)
return mean_hor, mean_ver
def getMedianWeftDist(self):
means_ver = []
for fl in self:
if fl.right_dist > 0:
means_ver.append(fl.right_dist)
mean_ver = median(means_ver)
return mean_ver
def getMedianWarpDist(self):
means_ver = []
for fl in self:
if fl.lower_dist > 0:
means_ver.append(fl.lower_dist)
mean_ver = median(means_ver)
return mean_ver
def showPoints(self, image=None):
fig, ax = plt.subplots(figsize=(10, 10))
axes = plt.gca()
axes.invert_yaxis()
if image is not None:
plt.axis('on')
gray_im = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
cl1 = clahe.apply(gray_im)
plt.imshow(cl1, cmap='gray')
axes.set_xlim(left=0, right=1900)
axes.set_ylim(top=0, bottom=1900)
for myFloatPoint in self:
if myFloatPoint.label == "warp_float":
ax.plot(myFloatPoint.x, myFloatPoint.y, '.b', markersize=10)
elif myFloatPoint.label == "weft_float":
ax.plot(myFloatPoint.x, myFloatPoint.y, '.g', markersize=10)
else:
ax.plot(myFloatPoint.x, myFloatPoint.y, '.r', markersize=10)
x1 = myFloatPoint.x
y1 = myFloatPoint.y
if myFloatPoint.lower_n:
x2 = myFloatPoint.lower_n.x
y2 = myFloatPoint.lower_n.y
plt.plot([x1, x2], [y1, y2], 'r')
if myFloatPoint.right_n:
x2 = myFloatPoint.right_n.x
y2 = myFloatPoint.right_n.y
plt.plot([x1, x2], [y1, y2], 'g')
def calcDistances(self):
for point in self:
if point.right_n:
dist = floatPoint.dist_x(point, point.right_n)
point.right_dist = dist
point.right_n.left_dist = dist
else:
point.right_dist = -1
if point.lower_n:
dist = floatPoint.dist_y(point, point.lower_n)
point.lower_dist = dist
point.lower_n.upper_dist = dist
else:
point.lower_dist = -1
def line_intersection(line1, line2):
xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1]) #Typo was here
def det(a, b):
return a[0] * b[1] - a[1] * b[0]
div = det(xdiff, ydiff)
if div == 0:
raise Exception('lines do not intersect')
d = (det(*line1), det(*line2))
x = det(d, xdiff) / div
y = det(d, ydiff) / div
return x, y