-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacet.py
More file actions
280 lines (231 loc) · 11.7 KB
/
Copy pathfacet.py
File metadata and controls
280 lines (231 loc) · 11.7 KB
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
from enum import Enum
import cv2 as cv
import numpy as np
MASK_DOWNSCALE = 0.1
class Facet:
class Type(Enum):
FLAT = 1
TAB = 2
BLANK = 3
def __init__(self, strip_coordinates, piece, facet_id, next_facet=None, prev_facet=None):
"""
Constructor of Facet instance from given strip coordinates that are relative to the piece
:param strip_coordinates: ndarray:(N,1,2) - np array of coordinates, relative to cropped image
:param piece: Piece corresponding to Facet
:param facet_id: identifier of the facet in the context of piece facets
"""
def determine_type():
"""
Determines the facet type based on centroid position
:return: Facet.Type
"""
epsilon = cv.arcLength(strip_coordinates, False) * 0.1
approx = cv.approxPolyDP(strip_coordinates, epsilon, False)
xy = np.squeeze(approx)
if xy.shape[0] == 2:
return Facet.Type.FLAT
xy = align_along_x(xy)
x_r, y_r = xy[:, 0], xy[:, 1]
x_r_c, y_r_c = np.mean(x_r), np.mean(y_r)
if y_r_c > 0:
_facet_type = Facet.Type.TAB
elif y_r_c < 0:
_facet_type = Facet.Type.BLANK
else:
_facet_type = Facet.Type.FLAT
return _facet_type
def facet_mask():
"""
Retrieve the binary mask of the strip, relative to supplied cropped mask
:return: binary mask of the strip
"""
return facet_mask_outer(self)
def facet_image():
"""
Returns an image under the strip mask.
:return: RGB image with only contour strip visible
"""
strip_mask = self.facet_mask
image = np.zeros_like(self.piece.cropped_image, dtype=np.uint8)
image[np.where(strip_mask)] = self.piece.cropped_image[np.where(strip_mask)]
return image
def strip_image(coordinates, piece_cropped_image):
"""
Returns image under facet mask as 1DxRGB np.array
:return: Image under facet mask as 1DxRGB np.array
"""
coordinates_0 = coordinates[:, 0, 0]
coordinates_1 = coordinates[:, 0, 1]
image = piece_cropped_image
ret = image[coordinates_1, coordinates_0]
ret = np.expand_dims(ret, axis=1)
return ret
def corners():
"""
:return: Corners of the facet - first and last coordinates
"""
return self.strip_coordinates[0], self.strip_coordinates[-1]
def calculate_strip_coordinate_2nd_level(facet_mask, piece_cropped_mask, strip_coordinates):
"""
Calculates the 2nd layer contour under the facet
:return: 2nd layer contour
"""
shape = (piece_cropped_mask.shape[0], piece_cropped_mask.shape[1])
# polyline thickness == 2 for the second level layer
strip_mask = cv.polylines(np.zeros(shape), [strip_coordinates], False, 255, 2)
strip_mask_2nd_level = ((piece_cropped_mask - facet_mask) * strip_mask).astype(np.uint8)
contour = cv.findContours(strip_mask_2nd_level, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)[0][0]
head = strip_coordinates[0, :]
tail = strip_coordinates[-1, :]
head_idx = np.argmin(np.sum((np.squeeze(contour) - head) ** 2, axis=1))
tail_idx = np.argmin(np.sum((np.squeeze(contour) - tail) ** 2, axis=1))
if tail_idx > head_idx:
return contour[head_idx:tail_idx, :]
else:
return contour[head_idx:tail_idx:-1, :]
self.facet_id = facet_id
self.piece = piece
self.strip_coordinates = strip_coordinates
self.strip_coordinates_approx = cv.approxPolyDP(strip_coordinates, 0, False)
self.corners = corners() # identifiers
self.facet_mask = facet_mask() # binary mask
self.facet_image = facet_image() # pixel under mask
self.strip_image = strip_image(self.strip_coordinates, self.piece.cropped_image)
self.strip_coordinates_2nd_level = calculate_strip_coordinate_2nd_level(self.facet_mask,
self.piece.cropped_mask,
self.strip_coordinates)
self.strip_image_2nd_level = strip_image(self.strip_coordinates_2nd_level, self.piece.cropped_image)
self.type = determine_type()
self.next_facet: Facet = next_facet
self.prev_facet: Facet = prev_facet
def facet_mask_custom_contour_size(self, contour_size=1):
shape = (self.piece.cropped_mask.shape[0], self.piece.cropped_mask.shape[1])
mask = cv.polylines(np.zeros(shape), [self.strip_coordinates], False, 255, contour_size).astype(np.bool_)
return mask
def calculate_aligned_bitmaps(self, other):
blank, tab = self.assign_blank_and_tab(other)
epsilon = int(cv.arcLength(tab.strip_coordinates, False) * 0.1)
tab_coords_trim = tab.strip_coordinates[epsilon:-epsilon]
blank_coords_trim = blank.strip_coordinates[epsilon:-epsilon]
tab_xy = np.squeeze(cv.approxPolyDP(tab_coords_trim, 0, False))
blank_xy = np.squeeze(cv.approxPolyDP(blank_coords_trim, 0, False))
tab_xy_ht_diff = tab_xy[-1] - tab_xy[0]
tab_length = np.sqrt(tab_xy_ht_diff[0] ** 2 + tab_xy_ht_diff[1] ** 2)
blank_xy_ht_diff = blank_xy[-1] - blank_xy[0]
blank_length = np.sqrt(blank_xy_ht_diff[0] ** 2 + blank_xy_ht_diff[1] ** 2)
length_pad = 3
shape_pad = 3
max_length = np.ceil(max(tab_length, blank_length)).astype(np.int32) + length_pad
shape = (2 * max_length + shape_pad, 2 * max_length + shape_pad)
color = 1
aligned_tab_xy = align_along_x(tab_xy)
aligned_tab_xy[:, 1] = aligned_tab_xy[:, 1] + max_length
aligned_tab_xy = aligned_tab_xy.astype(np.int32)
aligned_tab_xy = shift_to_top(aligned_tab_xy)
aligned_tab_facet_bitmap = cv.fillPoly(np.zeros(shape), [aligned_tab_xy], color)
aligned_blank_xy = align_along_x(blank_xy)
aligned_blank_xy[:, 1] = aligned_blank_xy[:, 1] * -1
aligned_blank_xy[:, 0] = np.max(aligned_blank_xy[:, 0]) - aligned_blank_xy[:, 0]
aligned_blank_xy[:, 1] = aligned_blank_xy[:, 1] + max_length
aligned_blank_xy = aligned_blank_xy.astype(np.int32)
aligned_blank_xy = shift_to_top(aligned_blank_xy)
aligned_blank_facet_bitmap = cv.fillPoly(np.zeros(shape), [aligned_blank_xy], color)
return aligned_tab_facet_bitmap, aligned_blank_facet_bitmap
def verify_facets_snappable(self, other) -> bool:
if self.type is Facet.Type.FLAT or other.type is Facet.Type.FLAT:
return False
elif self.type == other.type:
return False
elif \
(self.next_facet.type is Facet.Type.FLAT and other.prev_facet.type is not Facet.Type.FLAT) or \
(self.prev_facet.type is Facet.Type.FLAT and other.next_facet.type is not Facet.Type.FLAT) or \
(other.next_facet.type is Facet.Type.FLAT and self.prev_facet.type is not Facet.Type.FLAT) or \
(other.prev_facet.type is Facet.Type.FLAT and self.next_facet.type is not Facet.Type.FLAT):
return False
else:
return True
def intersection_sum(self, other) -> int:
if not self.verify_facets_snappable(other):
return 0
aligned_tab_facet_bitmap, aligned_blank_facet_bitmap = self.calculate_aligned_bitmaps(other)
intersection = np.logical_and(aligned_tab_facet_bitmap, aligned_blank_facet_bitmap)
return int(np.sum(intersection))
def union_sum(self, other) -> int:
if not self.verify_facets_snappable(other):
return 0
aligned_facet_bitmap, other_aligned_facet_bitmap = self.calculate_aligned_bitmaps(other)
union = np.logical_or(aligned_facet_bitmap, other_aligned_facet_bitmap)
return int(np.sum(union))
def assign_blank_and_tab(self, other):
if self.type == Facet.Type.BLANK and other.type == Facet.Type.TAB:
return self, other
elif other.type == Facet.Type.BLANK and self.type == Facet.Type.TAB:
return other, self
def iou(self, other) -> float:
"""
IOU - Maximizes similarity
:param other: Facet to calculate iou with
:return: value between 0 and 1, 1 meaning identity of Facets
"""
if not self.verify_facets_snappable(other):
return 0
intersection_sum = self.intersection_sum(other)
union_sum = self.union_sum(other)
return intersection_sum / union_sum
def mgc(self, other, length_for_comparison=25) -> float:
"""
Mahalanobis Gradient Compatibility
:param other: Facet to calculate mahalanobis gradient compatibility with
:param length_for_comparison: normalizing parameter, as facets can be of different sizes
:return: mgc value
"""
if not self.verify_facets_snappable(other):
return 0
if self.type == Facet.Type.TAB and other.type == Facet.Type.BLANK:
tab, blank = self, other
elif self.type == Facet.Type.BLANK and other.type == Facet.Type.TAB:
blank, tab = self, other
else:
return 0
dim = (1, length_for_comparison)
tab_rear = cv.resize(tab.strip_image, dim).astype(np.int32)
tab_rear2nd = cv.resize(tab.strip_image_2nd_level, dim).astype(np.int32)
tab_gr = np.abs(tab_rear - tab_rear2nd)
tab_gr_mean = np.mean(tab_gr, axis=0, keepdims=True)
blank_rear = cv.resize(blank.strip_image, dim).astype(np.int32)
blank_rear2nd = cv.resize(blank.strip_image_2nd_level, dim).astype(np.int32)
blank_gr = np.flipud(np.abs(blank_rear - blank_rear2nd))
blank_gr_mean = np.mean(blank_gr, axis=0, keepdims=True)
tab_blank_gr = (np.abs(tab_gr_mean - blank_rear))
tab_cov = np.cov(np.squeeze(tab_gr.T))
blank_cov = np.cov(np.squeeze(blank_gr.T))
tab_cov_inv = np.linalg.inv(tab_cov)
blank_cov_inv = np.linalg.inv(blank_cov)
gr1_diff = np.squeeze(abs(tab_blank_gr - tab_gr_mean))
gr2_diff = np.squeeze(abs(tab_blank_gr - blank_gr_mean))
mahalanobis_dist_tab_blank = \
np.sqrt(np.sum(gr1_diff @ tab_cov_inv @ gr1_diff.T))
mahalanobis_dist_blank_tab = \
np.sqrt(np.sum(gr2_diff @ blank_cov_inv @ gr2_diff.T))
return mahalanobis_dist_tab_blank + mahalanobis_dist_blank_tab
def compatibility(self, other, P) -> float:
"""
:param other: Facet to calculate scaled compatibility with
:param P: normalizing parameter, as facets can be of different sizes
:return:
"""
return self.mgc(other, P) * (1 - self.iou(other))
def compatibility_func():
return lambda mgc_val, iou_val: mgc_val * (1 - iou_val)
def align_along_x(xy):
theta = np.arctan2(xy[-1, 1] - xy[0, 1], xy[-1, 0] - xy[0, 0])
c, s = np.cos(theta), np.sin(theta)
rotation_mat = np.array([[c, -s],
[s, c]])
return (xy - xy[0, :]) @ rotation_mat
def shift_to_top(xy):
return xy - (0, np.min(xy[:, 1]))
def facet_mask_outer(facet: Facet, as_bool=True):
shape = (facet.piece.cropped_mask.shape[0], facet.piece.cropped_mask.shape[1])
mask = cv.polylines(np.zeros(shape), [facet.strip_coordinates], False, 255, 1).astype(np.bool_)
return mask if as_bool else mask.astype(np.uint8)