-
Notifications
You must be signed in to change notification settings - Fork 0
/
stages.py
359 lines (290 loc) · 11 KB
/
stages.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import math
import statistics
from math import cos, pi, sin
import cv2
import numpy as np
import dataclasses
import pickle as pkl
import utils
thresh_cache = None
hsv_cache = None
def find_filter_contours(img):
global thresh_cache, hsv_cache
utils.timeit("hsv", True)
hsv_cache = cv2.cvtColor(img, cv2.COLOR_RGB2HSV, hsv_cache)
utils.timeit("hsv")
utils.timeit("inRange", True)
thresh_cache = cv2.inRange(hsv_cache, (30, 100, 80), (60, 255, 255), thresh_cache)
utils.timeit("inRange")
utils.timeit("contours", True)
contours, _ = cv2.findContours(thresh_cache, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
utils.timeit("contours")
utils.timeit("filter", True)
output = []
for c in contours:
area = cv2.contourArea(c)
# if area < 75:
# continue
output.append(c)
utils.timeit("filter")
if utils.DISPLAY:
im2 = img.copy()
cv2.drawContours(im2, output, -1, (255, 0, 0))
cv2.imshow("contours", im2)
return output
@dataclasses.dataclass
class Point:
x: float
y: float
theta = -pi / 4
rotation_matrix = np.array([
[cos(theta), -sin(theta)],
[sin(theta), cos(theta)],
])
inv_rotation_matrix = np.array([
[cos(-theta), -sin(-theta)],
[sin(-theta), cos(-theta)],
])
def wrapping_slice(arr, start, end, t):
if end < start:
start -= t
end += t
else:
start += t
end -= t
end %= len(arr)
start %= len(arr)
if end < start or end < 0 or start < 0:
return np.array(np.ndarray.tolist(arr[:end]) + np.ndarray.tolist(arr[start:]))
else:
return arr[start:end]
# no questions allowed
def find_corners_2(corners, contour):
def find_corner(c):
corn = -1
value = 10000000
for index, item in enumerate(contour):
dst = abs(item[0] - c.x) + abs(item[1] - c.y)
if dst < value:
corn = index
value = dst
return corn
corner1 = find_corner(corners[0])
corner2 = find_corner(corners[1])
corner3 = find_corner(corners[2])
corner4 = find_corner(corners[3])
segment1 = wrapping_slice(contour, corner1, corner2, 2)
segment2 = wrapping_slice(contour, corner2, corner3, 2)
segment3 = wrapping_slice(contour, corner3, corner4, 2)
segment4 = wrapping_slice(contour, corner4, corner1, 2)
if len(segment1) < 2 or len(segment2) < 2 or len(segment3) < 2 or len(segment4) < 2:
return corners
def fit(a, b):
if len(set(a)) < 2:
return np.array([1000, a[0] * -1000])
return np.polyfit(a, b, deg=1)
regression1 = fit(segment1[:, 0], segment1[:, 1])
regression2 = fit(segment2[:, 0], segment2[:, 1])
regression3 = fit(segment3[:, 0], segment3[:, 1])
regression4 = fit(segment4[:, 0], segment4[:, 1])
def solve(a, b):
m1 = a[0]
m2 = b[0]
b1 = a[1]
b2 = b[1]
x = (b2 - b1) / (m1 - m2)
return Point(x, m1 * x + b1)
c1 = solve(regression1, regression4)
c2 = solve(regression1, regression2)
c3 = solve(regression2, regression3)
c4 = solve(regression3, regression4)
def dst(a, b):
return abs(a.x - b.x) + abs(a.y - b.y)
t = 10
if dst(c1, corners[0]) > t or dst(c2, corners[1]) > t or dst(c3, corners[2]) > t or dst(c4, corners[3]) > t:
return None
return [c1, c2, c3, c4]
def find_corners(contours, img):
corners = []
for contour in contours:
contour = np.squeeze(contour)
if len(contour) < 4:
continue
# for i in range(0, len(contour)):
# contour[i][0] = 1920 - contour[i][0]
# contour[i][1] = 1080 - contour[i][1]
rotated = np.matmul(contour, rotation_matrix)
leftbottom_rot = rotated[0]
righttop_rot = rotated[0]
lefttop_rot = rotated[0]
rightbottom_rot = rotated[0]
for item in rotated:
if isinstance(item, float):
continue
if item[0] < leftbottom_rot[0]:
leftbottom_rot = item
if item[0] > righttop_rot[0]:
righttop_rot = item
if item[1] < lefttop_rot[1]:
lefttop_rot = item
if item[1] > rightbottom_rot[1]:
rightbottom_rot = item
lb = np.matmul(leftbottom_rot, inv_rotation_matrix)
rt = np.matmul(righttop_rot, inv_rotation_matrix)
lt = np.matmul(lefttop_rot, inv_rotation_matrix)
rb = np.matmul(rightbottom_rot, inv_rotation_matrix)
c = [
Point(round(lb[0]), round(lb[1])),
Point(round(rb[0]), round(rb[1])),
Point(round(rt[0]), round(rt[1])),
Point(round(lt[0]), round(lt[1])),
]
# corners.append(c)
res = find_corners_2(c, contour)
if res:
corners.append(res)
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#
# criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
converted_corners = np.zeros((len(corners * 4), 2), dtype=np.float32)
for i in range(0, len(corners)):
converted_corners[i * 4][0] = corners[i][0].x
converted_corners[i * 4][1] = corners[i][0].y
converted_corners[i * 4 + 1][0] = corners[i][1].x
converted_corners[i * 4 + 1][1] = corners[i][1].y
converted_corners[i * 4 + 2][0] = corners[i][2].x
converted_corners[i * 4 + 2][1] = corners[i][2].y
converted_corners[i * 4 + 3][0] = corners[i][3].x
converted_corners[i * 4 + 3][1] = corners[i][3].y
# for item in refined:
# item[0] = 1920 - item[0]
# item[1] = 1080 - item[1]
# refined = np.matmul(refined, invert_matrix)
# for item in refined:
# item[0] += 1920 / 2
# item[1] += 1080 / 2
output = corners
# leaving this commented out, it's slow and the window it makes is huge
img2 = cv2.resize(img, (1920 * 4, 1080 * 4), interpolation=cv2.INTER_NEAREST)
# #img2 = img.copy()
# for quad in output:
# print(quad)
# cv2.drawMarker(img2, (int(quad[0].x / (1920 / 500)), int(quad[0].y / (1080 / 500))), (0, 0, 255))
# cv2.drawMarker(img2, (int(quad[1].x / (1920 / 500)), int(quad[1].y / (1080 / 500))), (0, 255, 0))
# cv2.drawMarker(img2, (int(quad[2].x / (1920 / 500)), int(quad[2].y / (1080 / 500))), (0, 255, 255))
# cv2.drawMarker(img2, (int(quad[3].x / (1920 / 500)), int(quad[3].y / (1080 / 500))), (255, 0, 0))
for quad in corners:
cv2.drawMarker(img2, (int(quad[0].x * 4), int(quad[0].y * 4)), (0, 0, 255), markerType=cv2.MARKER_TILTED_CROSS)
cv2.drawMarker(img2, (int(quad[1].x * 4), int(quad[1].y * 4)), (0, 255, 0), markerType=cv2.MARKER_TILTED_CROSS)
cv2.drawMarker(img2, (int(quad[2].x * 4), int(quad[2].y * 4)), (0, 255, 255), markerType=cv2.MARKER_TILTED_CROSS)
cv2.drawMarker(img2, (int(quad[3].x * 4), int(quad[3].y * 4)), (255, 0, 0), markerType=cv2.MARKER_TILTED_CROSS)
cv2.imshow("corners", img2)
return output
w = 0.063405 * 2
h = 2 * 2.54 / 100
real_coords = np.array([
[0, 0, 0], # left bottom
[w, 0, 0], # right bottom
[w, h, 0], # right top
[0, h, 0] # left top
], dtype="double")
for triple in real_coords:
triple[0] -= w / 2
triple[2] += 0.674882
# /home/pi/2898-2022-vision-py/
# mtx, dist, rvecs, tvecs = pkl.load(open("calib/picam-2/calib.pkl", "rb"))
# mtx, dist, rvecs, tvecs = pkl.load(open("/home/pi/2898-2022-vision-py/calib/picam-2/calib.pkl", "rb"))
# mtx, dist, rvecs, tvecs = pkl.load(open("calib/virtual-camera-2/calib.pkl", "rb"))
mtx, dist = pkl.load(open("/home/max/synced/vision-testing/calib/calib.pkl", "rb"))
# print(dist)
tilt_angle = math.radians(-20)
# x
tilt_matrix = np.array([
[1, 0, 0],
[0, cos(-tilt_angle), -sin(-tilt_angle)],
[0, sin(-tilt_angle), cos(-tilt_angle)]
])
# # y
# tilt_matrix = np.array([
# [cos(-tilt_angle), 0, sin(-tilt_angle)],
# [0, 1, 0],
# [-sin(-tilt_angle), 0, cos(-tilt_angle)]
# ])
# # z
# tilt_matrix = np.array([
# [cos(-tilt_angle), -sin(-tilt_angle), 0],
# [sin(-tilt_angle), cos(tilt_angle), 0],
# [0, 0, 1]
# ])
"""
z pos 6.780974423515335 neg 6.709745617917127
y pos 6.747855029228697 neg 6.747855029228697
x pos 6.104506452572762 neg 6.617734721310734
aaa
z pos 7.079253621741147 neg 7.1586818131666545
y pos 7.130354067016877 neg 7.130354067016876
x pos 6.93618229082667 neg 6.497784797552637
"""
def compute_output_values(rvec, tvec):
"""
Computes the distance and angle to the target.
:param rvec: The solvepnp rotation vector, unused as of now.
:param tvec: The solvepnp translation vector.
:return: A tuple containing distance (with the vertical angle compensated for)
and the angle from the direction the camera is facing to the center of the target.
"""
# tvec = np.array([1.5, 0.37234, 6.3851])
# tvec format:
# 0 - x (side to side from the camera's perspective)
# 1 - y (up and down from the camera)
# 2 - z (in and out from the camera)
tx = tvec[0][0]
ty = tvec[1][0]
tz = tvec[2][0]
p1 = (0.0, sin(tilt_angle) * tz, cos(tilt_angle) * tz)
a1 = pi / 2 + tilt_angle
p2 = (tx, p1[1] + ty * sin(a1), p1[2] + ty * cos(a1))
rotated = p2
distance = math.sqrt(rotated[0] ** 2 + rotated[2] ** 2)
angle1 = math.atan2(rotated[0], rotated[2])
# print(rotated)
# print(tvec)
return distance, angle1
out = []
def solvepnp(corners, img):
distances = []
angles = []
tvecs = []
for target in corners: # [2:-2]:
imagepoints = np.zeros((4, 2), dtype=np.float64)
for index, p in enumerate(target):
imagepoints[index][0] = p.x
imagepoints[index][1] = p.y
# imagepoints = cv2.fisheye.undistortPoints(np.expand_dims(np.asarray(imagepoints), -2), mtx, dist)
# imagepoints = cv2.undistortPoints(imagepoints, mtx, dist)
# success, rotation_vector, translation_vector \
# = cv2.solvePnP(real_coords, imagepoints, np.identity(3), np.zeros(5), flags=0)
success, rotation_vector, translation_vector \
= cv2.solvePnP(real_coords, imagepoints, mtx, dist, flags=0)
# print(translation_vector)
if utils.DISPLAY:
cv2.aruco.drawAxis(img, mtx, dist, rotation_vector, translation_vector, w / 2)
# translation_vector[1] *= -1
# print(translation_vector)
distance, angle1 = compute_output_values(rotation_vector, translation_vector)
distances.append(distance)
angles.append(angle1)
tvecs.append(translation_vector)
if utils.DISPLAY:
cv2.imshow("axis", img)
if len(distances) == 0:
return 0.0, 0.0
# print(f"{statistics.median(map(lambda x: x[0][0], tvecs))}, {statistics.median(map(lambda x: x[1][0], tvecs))}, {statistics.median(map(lambda x: x[2][0], tvecs))}")
x_vals = []
y_vals = []
z_vals = []
for item in tvecs:
x_vals.append(item[0])
y_vals.append(item[1])
z_vals.append(item[2])
return statistics.median(distances), statistics.median(angles)