-
Notifications
You must be signed in to change notification settings - Fork 2
/
findtc.py
309 lines (234 loc) · 8.51 KB
/
findtc.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
#!/usr/bin/env python3
import os
import sys
import time
import numpy as np
import cv2 as cv
import threading
from utils import *
import showtc # for nrows, ncols, nbits
def simplify_contour(c):
length = cv.arcLength(c, True)
approx = cv.approxPolyDP(c, length * 0.01, True)
# length changed noticeably?
if length > 10 and abs(cv.arcLength(approx, True) / length - 1) > 0.1:
return None
return approx
def refine_corners(contour, image):
#return contour
return cv.cornerSubPix(image,
corners=contour.astype(np.float32),
winSize=(7,7),
zeroZone=(2,2),
criteria=(cv.TERM_CRITERIA_COUNT*0 | cv.TERM_CRITERIA_EPS, 10, 0.1))
def filter_and_refine_quads(monochrome, mask, minarea=70**2):
(contours, hierarchy) = cv.findContours(mask, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
# simplify runs of straight lines
contours = [simplify_contour(c) for c in contours]
# find quads
contours = [c for c in contours if c is not None and len(c) == 4]
# that are white (contour goes counterclockwise in screen coords)
contours = [c for c in contours if contour_sense(c) < -np.pi]
contours = [refine_corners(c, monochrome) for c in contours]
# drop small ones right here
contours = [c for c in contours if cv.contourArea(c) >= minarea]
# sort by size
contours.sort(key=lambda c: -cv.contourArea(c))
return contours
#ft2 = cv.freetype.createFreeType2()
#ft2.loadFontData("C:\\Windows\\Fonts\\times.ttf", 0)
#ft2.loadFontData("C:\\Windows\\Fonts\\consola.ttf", 0)
def centeredText(im, text, origin, fontScale, color, thickness, background=None, *args, **kwargs):
fontFace = cv.FONT_HERSHEY_SIMPLEX
((w,h), baseLine) = cv.getTextSize(text, fontFace, fontScale, thickness)
ox,oy = origin
if background is not None:
cv.rectangle(im,
fixn((ox - w/2 - 10, oy - h/2 - 10, w+20, h+20), 4),
color=background,
thickness=cv.FILLED, lineType=cv.LINE_AA, shift=4)
cv.putText(im,
text,
fixn((ox - w/2, oy + h/2), 0),
fontFace, fontScale, color, thickness)
#ft2.putText(im,
# text,
# (ox - w//2, oy + h//2),
# fontHeight=fontHeight,
# color=color,
# thickness=-1,
# line_type=cv.LINE_AA,
# bottomLeftOrigin=False)
pass
drawscale = 32
# counterclockwise, order of findcontours for white blobs
# screen space coordinates
# content + 1px white border around
tch,tcw = (showtc.nrows+2, showtc.ncols+2)
tc_model = (np.array([
[0,0], # top left
[0,1], # bottom left
[1,1],
[1,0],
]) * (tcw,tch)).astype(np.float32)
cap = cv.VideoCapture(int(sys.argv[1]) if len(sys.argv) >= 2 else 0)
cap.set(cv.CAP_PROP_FPS, 30)
capw = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
caph = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
cap = FreshestFrame(cap)
cv.namedWindow("camera", cv.WINDOW_NORMAL) # resizable
cv.resizeWindow("camera", capw, caph)
maxquads = 3
alpha = 0.05
meanval = [0.0] * maxquads
meanerr = [0.0] * maxquads
while True:
if not cap.running: break
rv,im = cap.read()
frameh,framew = im.shape[:2]
frame_capture_time = time.time()
if not rv: break
# image analysis...
monochrome = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
th_effective, mask = cv.threshold(monochrome, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow("thresholded", mask)
quads = filter_and_refine_quads(monochrome, mask)[:maxquads]
print(f"{len(quads)} quads")
for index,quad in enumerate(quads):
#quad = rotate_topleft(quad)
#quad = np.float32(quad)
# some measures of size
quadarea = cv.contourArea(quad)
quadsize = np.sqrt(quadarea)
# sense: from screen space (where quad is) to quad space (where bits in the marker are)
H = cv.getPerspectiveTransform(quad, tc_model)
# sense: from quad space to screen space
Hinv = np.linalg.inv(H)
# if you wanna see more of what these homographies do, look below
# sample cells inside the quad/marker
# a sampling grid, sample center of each cell
rx = np.arange(tcw) + 0.5
ry = np.arange(tch) + 0.5
sgrid_qs = np.dstack(np.meshgrid(rx, ry)) # sample grid in quad space
sgrid_ss = cv.perspectiveTransform(sgrid_qs.reshape((-1, 1, 2)), Hinv).astype(np.float32) # sample grid in screen space
sgrid_ss.shape = (tch, tcw, 2)
# use cv.remap to sample at these coordinates
# split into x coordinates and y coordinates, each 2d array
xmap,ymap = np.moveaxis(sgrid_ss, 2, 0)
sampled = cv.remap(mask, xmap, ymap, cv.INTER_AREA)
# or simply take mask[xmap, ymap] with integer xmap/ymap
# binarize
sampled = (sampled >= 128)
# try all four orientations
for _ in range(4):
if (sampled[-2,1:-1] == 0).all(): # top 8 bits should be zero (row -1 is white border, -2 has content)
break
else:
sampled = np.rot90(sampled)
quad = np.roll(quad, 1, axis=0)
else:
print("quad has no rotation where upper 8 bits are zero")
continue
try:
# this can throw an assertion too, for the border
codevalue = decode_marker(sampled)
assert codevalue >> (showtc.nbits-8) == 0, "invalid marker contents; upper 8 bits expected to be zero"
except AssertionError as e:
#print(e)
continue
timecode = codevalue * 1e-3 # ms -> s
# some time statistics
delta = timecode - frame_capture_time
err = abs(delta - meanval[index])
meanval[index] += (delta - meanval[index]) * alpha
meanerr[index] += (err - meanerr[index]) * alpha
if err > 1.0:
meanval[index] = meanerr[index] = 0
print(f"#{index}: {timecode:.3f} s, delta {delta:+.3f} s")
print(f"mean {meanval[index]:.3f}, mean err {meanerr[index]:.3f}")
# mark the quad
cv.polylines(im,
pts=fixn(np.array([quad]), 4),
isClosed=True,
color=(255,0,255),
thickness=1,
lineType=cv.LINE_AA,
shift=4)
# top left corner
cv.circle(im,
center=fixn(quad[0,0], 4),
radius=fixn(10, 4),
color=(255, 255, 0),
thickness=2,
lineType=cv.LINE_AA,
shift=4)
# sample grid
for coord in sgrid_ss.reshape((-1,2)):
cv.circle(im,
center=fixn(coord, 4),
radius=fixn(2, 4),
color=(0,0,255),
thickness=1,
lineType=cv.LINE_AA,
shift=4)
# text: time delay
fontScale = 1.5
fontScale = quadsize / 150
thickness = int(np.ceil(fontScale * 2))
centeredText(im,
f"{meanval[index]:.2f}s",
quad.mean(axis=(0,1)),
fontScale=fontScale, color=(255,255,255), thickness=thickness, background=(0, 201, 106))
# demonstration of homographies
if 1:
# (x,y) is a point
# (x,y,1)*w for any w are homogeneous coords representing (x,y)
# to get (x,y) from (xw,yw,w), divide by w
# it can be understood as a projective space,
# where any (x,y,z) is projected onto a z=1 screen to become (x/z,y/z,1)
# p' = M p is how you apply a transformation matrix to a point
# H transforms from screen space coords to quad space coords ("cells")
# we'll add some translation and scaling to it
# matrixes are 3x3
# top left 2x2 is scaling
# right column topmost 2 are translation
# bottom row is the w-row, involved in perspective (think "divide by z")
# scaling for display
S = np.matrix(np.diag([drawscale, drawscale, 1]))
# shift down and to the right by one
T = np.matrix(np.eye(3))
T[0,2] = +1
T[1,2] = +1
Hdisp = S * T * np.matrix(H)
# matrix multiplication, read it right to left for the succession of transformations
# enlarged output space coordinate, each cell is `drawscale` wide
# ^ (S)
# "quad" space, but now origin is a cell to the left and up of the corner
# ^ (T)
# quad space coordinate/pixel, origin is the top left white corner
# ^ (H)
# screen space coordinate
# creates a canvas of (tcw+2,tch+2)*drawscale pixels
# that's enough space for the quad including black quiet zone
# fills each output pixel with what corresponds to the data in screen space (input)
# implicitly inverts the matrix because it has to calculate backwards from output space coordinates
outsize = ((tcw+2)*drawscale, (tch+2)*drawscale)
output = cv.warpPerspective(mask, Hdisp, outsize, flags=cv.INTER_AREA)
output = cv.cvtColor(output, cv.COLOR_GRAY2BGR)
# draw sample grid
for coord in ((sgrid_qs + (1,1)) * drawscale).reshape((-1,2)):
cv.circle(output,
center=fixn(coord, 4),
radius=fixn(2, 4),
color=(0,0,255),
thickness=1,
lineType=cv.LINE_AA,
shift=4)
cv.imshow(f"straight quad {index}", output)
print()
cv.imshow("camera", im)
k = cv.waitKey(1)
if k == 27:
break
cap.release()
cv.destroyAllWindows()