-
Notifications
You must be signed in to change notification settings - Fork 16
/
rectify_court.py
209 lines (157 loc) · 6.94 KB
/
rectify_court.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
import cv2
import numpy as np
from tools.plot_tools import plt_plot
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
def collage(frames, direction=1, plot=False):
sift = cv2.xfeatures2d.SIFT_create()
if direction == 1:
current_mosaic = frames[0]
else:
current_mosaic = frames[-1]
for i in range(len(frames) - 1):
# FINDING FEATURES
kp1 = sift.detect(current_mosaic)
kp1, des1 = sift.compute(current_mosaic, kp1)
kp2 = sift.detect(frames[i * direction + direction])
kp2, des2 = sift.compute(frames[i * direction + direction], kp2)
matches = flann.knnMatch(des1, des2, k=2)
good = []
for m, n in matches:
if m.distance < 0.7 * n.distance:
good.append(m)
# Finding an homography
src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
M, mask = cv2.findHomography(dst_pts, src_pts, cv2.RANSAC, 5.0)
result = cv2.warpPerspective(frames[i * direction + direction],
M,
(current_mosaic.shape[1] + frames[i * direction + direction].shape[1],
frames[i * direction + direction].shape[0] + 50))
result[:current_mosaic.shape[0], :current_mosaic.shape[1]] = current_mosaic
current_mosaic = result
# removing black part of the collage
for j in range(len(current_mosaic[0])):
if np.sum(current_mosaic[:, j]) == 0:
current_mosaic = current_mosaic[:, :j - 50]
break
if plot:
plt_plot(current_mosaic)
return current_mosaic
def add_frame(frame, pano, pano_enhanced, plot=False):
sift = cv2.xfeatures2d.SIFT_create() # sift instance
# FINDING FEATURES
kp1 = sift.detect(pano)
kp1, des1 = sift.compute(pano, kp1)
kp2 = sift.detect(frame)
kp2, des2 = sift.compute(frame, kp2)
matches = flann.knnMatch(des1, des2, k=2)
good = []
for m, n in matches:
if m.distance < 0.7 * n.distance:
good.append(m)
print(f"Number of good correspondences: {len(good)}")
if len(good) < 70: return pano
# Finding an homography
src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
M, mask = cv2.findHomography(dst_pts, src_pts, cv2.RANSAC, 5.0)
result = cv2.warpPerspective(frame,
M,
(pano.shape[1],
pano.shape[0]))
if plot: plt_plot(result, "Warped new image")
avg_pano = np.where(result < 100, pano_enhanced,
np.uint8(np.average(np.array([pano_enhanced, result]), axis=0, weights=[1, 0.7])))
if plot: plt_plot(avg_pano, "AVG new image")
return avg_pano
def binarize_erode_dilate(img, plot=False):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th, img_otsu = cv2.threshold(gray, thresh=100, maxval=255, type=cv2.THRESH_OTSU)
if plot: plt_plot(img_otsu, "Panorama after Otsu", cmap="gray")
kernel = np.array([[0, 0, 0],
[1, 1, 1],
[0, 0, 0]], np.uint8)
img_otsu = cv2.erode(img_otsu, kernel, iterations=20)
img_otsu = cv2.dilate(img_otsu, kernel, iterations=20)
if plot: plt_plot(img_otsu, "After Erosion-Dilation", cmap="gray")
return img_otsu
def rectangularize_court(pano, plot=False):
# BLOB FILTERING & BLOB DETECTION
# adding a little frame to enable detection
# of blobs that touch the borders
pano[-4: -1] = pano[0:3] = 0
pano[:, 0:3] = pano[:, -4:-1] = 0
mask = np.zeros(pano.shape, dtype=np.uint8)
cnts = cv2.findContours(pano, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours_court = []
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
threshold_area = 100000
for c in cnts:
area = cv2.contourArea(c)
if area > threshold_area:
cv2.drawContours(mask, [c], -1, (36, 255, 12), -1)
contours_court.append(c)
pano = mask
if plot: plt_plot(pano, "After Blob Detection", cmap="gray")
# pano = 255 - pano
contours_court = contours_court[0]
simple_court = np.zeros(pano.shape)
# convex hull
hull = cv2.convexHull(contours_court)
cv2.drawContours(pano, [hull], 0, 100, 2)
if plot: plt_plot(pano, "After ConvexHull", cmap="gray",
additional_points=hull.reshape((-1, 2)))
# fitting a poly to the hull
epsilon = 0.01 * cv2.arcLength(hull, True)
approx = cv2.approxPolyDP(hull, epsilon, True)
corners = approx.reshape(-1, 2)
cv2.drawContours(pano, [approx], 0, 100, 5)
cv2.drawContours(simple_court, [approx], 0, 255, 3)
if plot:
plt_plot(pano, "After Rectangular Fitting", cmap="gray")
plt_plot(simple_court, "Rectangularized Court", cmap="gray")
print("simplified contour has", len(approx), "points")
return simple_court, corners
def homography(rect, image, plot=False):
bl, tl, tr, br = rect
rect = np.array([tl, tr, br, bl], dtype="float32")
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB)) + 700
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype="float32")
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
if plot: plt_plot(warped)
return warped, M
def rectify(pano_enhanced, corners, plot=False):
# TODO: adapt this in a way that works in any setting
panoL = pano_enhanced[:, :1870]
panoR = pano_enhanced[:, 1870:]
cornersL = np.array([corners[0], corners[1], [1865, 55], [1869, 389]])
cornersR = np.array(
[[0, 389],
[0, 55],
[corners[2][0] - 1870, corners[2][1]],
[corners[3][0] - 1870, corners[3][1]]
])
M = homography(corners, pano_enhanced)[1]
np.save("Rectify1.npy", M)
h1, ML = homography(cornersL, panoL)
np.save("RectifyL.npy", ML)
h2, MR = homography(cornersR, panoR)
np.save("RectifyR.npy", MR)
# rectified = np.hstack((h1, cv2.resize(h2, (int((h2.shape[0] / h1.shape[0]) * h1.shape[1]), h1.shape[0]))))
rectified = np.hstack((h1, cv2.resize(h2, (h1.shape[1], h1.shape[0]))))
cv2.imwrite("rectified.png", rectified)
if plot: plt_plot(cv2.cvtColor(rectified, cv2.COLOR_BGR2RGB))
return rectified