-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatcher.py
351 lines (254 loc) · 12.5 KB
/
matcher.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
from asyncore import read
from cv2 import VideoCapture
import numpy as np
import cv2
import copy
import sys
import time
from undistort import undistort
from square_decision import gen_aggregate_matrix, gen_fixed_coords, gen_scale_factor, gen_theta, gen_transform_matrix
from matplotlib import pyplot as plt
from PIL import Image
import datetime
import math
CONSTX = 500
CONSTY = 500
SMALLEST = "book3.jpg"
MIDDLEST = "book2.jpg"
BIGGEST = "book1.jpg"
def sanityCheck():
sift = cv2.SIFT_create()
print(cv2.__version__)
# find the keypoints and descriptors with SIFT
#kp1, des1 = sift.detectAndCompute(img1,None)
#kp2, des2 = sift.detectAndCompute(img2,None)
pointsAndDescriptors = []
for i in range(1, 3):
img_name = "{}.jpg".format(i)
img = cv2.imread(img_name,0)
kp, des = sift.detectAndCompute(img,None)
pointsAndDescriptors.append((kp, des))
#print(len(kp))
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)
print(len(pointsAndDescriptors))
matches = flann.knnMatch(pointsAndDescriptors[0][1], pointsAndDescriptors[1][1], k=2)
#print(matches)
good_matches = []
for i, (m,n) in enumerate(matches):
if m.distance < 0.3*n.distance:
print(m)
good_matches.append(m)
from_to = {}
for m in good_matches: #For loop which puts our matches as coordinate pairs into respective arrays
from_to[pointsAndDescriptors[0][0][m.queryIdx].pt] = pointsAndDescriptors[1][0][m.trainIdx].pt
#smaller.append(pointsAndDescriptors[0][0][m.queryIdx].pt)
#larger.append(pointsAndDescriptors[1][0][m.trainIdx].pt)
# if (pointsAndDescriptors[0][0][m.queryIdx].pt == None) ^ (pointsAndDescriptors[1][0][m.trainIdx].pt == None):
# print(":( points don't all have corresponding point")
#TODO Sort from_to based on distance so best match is first key pair, second best is second etc
###
#Larger image is always first, coordinates on larger image are key to coords on smaller image
(translate_x, translate_y) = list(from_to.keys())[0]
(translate_x_small, translate_y_small) = from_to[list(from_to.keys())[0]]
calculationCoords = gen_fixed_coords(from_to, translate_x, translate_y, translate_x_small, translate_y_small)
#print(calculationCoords)
scaleFactor = gen_scale_factor(calculationCoords)
theta = gen_theta(calculationCoords)
#print(smaller)
#print(larger)
smallToLarge = gen_transform_matrix(-translate_x_small, -translate_y_small, theta, scaleFactor, translate_x, translate_y)
transformed = []
avgDistance = 0
print(smallToLarge)
print(list(from_to.keys())[0], from_to[list(from_to.keys())[0]])
print(list(from_to.keys())[0], np.matmul(smallToLarge, np.array([[from_to[list(from_to.keys())[0]][0]], [from_to[list(from_to.keys())[0]][1]], [1]]) ))
for key in from_to.keys():
#print(np.matmul( smallToLarge, np.array([[from_to[key][0]],[ from_to[key][1]], [1]])))
transformed.append(np.matmul( smallToLarge, np.array([[from_to[key][0]],[ from_to[key][1]], [1]])))
for i in range(len(transformed)):
#transformed[i]
print("\n\n")
print(list(from_to.keys())[i], transformed[i])
print("\n")
print()
#print(np.sqrt((list(from_to.keys())[i][0] -transformed[i][0][0] )** 2 + (list(from_to.keys())[i][1] - transformed[i][1][0]) ** 2))
#avgDistance = avgDistance + np.sqrt((list(from_to.keys())[i][0] -transformed[i][0][0] )** 2 + (list(from_to.keys())[i][1] - transformed[i][1][0]) ** 2)
#print(avgDistance/ range(len(list(from_to.keys()))))
(coordx1, coordy1) = list(from_to.keys())[1]
(coordx2, coordy2) = from_to[list(from_to.keys())[1]]
#testCoords = np.array([[CONSTX], [CONSTY], [1]])
testCoordsOG = np.array([[CONSTX], [CONSTY], [1]])
(coordxTest, coordyTest) = from_to[list(from_to.keys())[0]]
print("test gen transform no trans", gen_transform_matrix())
testCoords = [CONSTX - coordxTest, CONSTY - coordyTest, 1] #Small image coords
testMat1 = gen_transform_matrix(translate_x=-coordxTest, translate_y=-coordyTest)
print("First transform testCoords:", testCoords, "\nGenerated Matrix: ", testMat1, "\nMatrix applied: ", np.matmul(testMat1, testCoordsOG))
seqMatCoords = np.matmul(testMat1, testCoordsOG)
#testMat1 = gen_aggregate_matrix
testCoords[0] = testCoords[0] * scaleFactor
testCoords[1] = testCoords[1] * scaleFactor
testMat2 = gen_transform_matrix(translate_x = -coordxTest, translate_y = -coordyTest, scale_factor=scaleFactor)
print("\nChecking for scale")
print("\nManual Test Coords: ", testCoords)
print("\nGenerated testMatrix: ", testMat2)
print("\nApplied test matrix: ", np.matmul(testMat2, testCoordsOG))
testCoords[0] = testCoords[0]*np.cos(theta) - testCoords[1]*np.sin(theta)
testCoords[1] = testCoords[0] * np.sin(theta) + testCoords[1] * np.cos(theta)
print("\nChecking Rotation, theta: ", theta)
print("\nManual test Coords", testCoords)
testRot = gen_transform_matrix(translate_x= -coordxTest, translate_y= - coordyTest, scale_factor = scaleFactor, theta=theta)
print("\nGenerated Matrix: ", testRot)
print("\nApplied Test Matrix: ", np.matmul(testRot, testCoordsOG))
testCoords[0] = testCoords[0] + list(from_to.keys())[0][0]
testCoords[1] = testCoords[1] + list(from_to.keys())[0][1]
print("Manual coords final translation: ", testCoords)
testFinal = gen_transform_matrix(translate_x= -coordxTest, translate_y= - coordyTest, scale_factor = scaleFactor, theta=theta,
translate_x_large=list(from_to.keys())[0][0], translate_y_large=list(from_to.keys())[0][1])
print("\nGenerated matrix: ", testFinal)
print("\nApplied matrix: ", np.matmul(testFinal, testCoordsOG))
def image_registration_matrix(img_name1 : str, img_name2 : str):
#Returns transform matrix between the two images
sift = cv2.SIFT_create()
img1 = cv2.imread(img_name1, 0)
img2 = cv2.imread(img_name2, 0)
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
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)
matches = flann.knnMatch(des1, des2, k=2)
good_matches = []
for i, (m,n) in enumerate(matches):
if m.distance < 0.6*n.distance:
good_matches.append(m)
from_to = {}
for m in good_matches: #For loop which puts our matches as coordinate pairs into respective arrays
from_to[kp1[m.queryIdx].pt] = kp2[m.trainIdx].pt
#TODO Sort matches based on quality or metric like distance
(translate_x, translate_y) = list(from_to.keys())[0]
#(coordxTest, coordyTest) = from_to[list(from_to.keys())[0]]
(translate_x_small, translate_y_small) = from_to[list(from_to.keys())[0]]
calculationCoords = gen_fixed_coords(from_to, translate_x, translate_y, translate_x_small, translate_y_small)
scaleFactor = gen_scale_factor(calculationCoords)
theta = gen_theta(calculationCoords)
return gen_transform_matrix(translate_x= -translate_x_small, translate_y= -translate_y_small, scale_factor = scaleFactor, theta=theta,
translate_x_large=list(from_to.keys())[0][0], translate_y_large=list(from_to.keys())[0][1])
def frame_registration_matrix(img11, img22):
#Returns transform matrix between the two images
sift = cv2.SIFT_create()
img1 = img11
img2 = img22
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
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)
matches = flann.knnMatch(des1, des2, k=2)
good_matches = []
for i, (m,n) in enumerate(matches):
if m.distance < 0.6*n.distance:
good_matches.append(m)
from_to = {}
for m in good_matches: #For loop which puts our matches as coordinate pairs into respective arrays
from_to[kp1[m.queryIdx].pt] = kp2[m.trainIdx].pt
#TODO Sort matches based on quality or metric like distance
(translate_x, translate_y) = list(from_to.keys())[0]
#(coordxTest, coordyTest) = from_to[list(from_to.keys())[0]]
(translate_x_small, translate_y_small) = from_to[list(from_to.keys())[0]]
calculationCoords = gen_fixed_coords(from_to, translate_x, translate_y, translate_x_small, translate_y_small)
scaleFactor = gen_scale_factor(calculationCoords)
theta = gen_theta(calculationCoords)
return gen_transform_matrix(translate_x= -translate_x_small, translate_y= -translate_y_small, scale_factor = scaleFactor, theta=theta,
translate_x_large=list(from_to.keys())[0][0], translate_y_large=list(from_to.keys())[0][1])
videoArrayFormat = []
import uptime
def curr_millis():
return uptime.uptime() * 1000
def recordVideo():
cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
writer = cv2.VideoWriter("flightVideo" + ".mp4", cv2.VideoWriter_fourcc(*'MPEG'), 60, (width,height))
# datetime.datetime.now().strftime("%I:%M:%S%p, %B %d, %Y")
frameCounter = 0
while True:
ret,frame = cap.read()
print(frameCounter)
if ret == True:
frameCounter += 1
videoArrayFormat.append((frameCounter, curr_millis()))
writer.write(frame)
#cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == 27 or frameCounter > 300: #escape key, if not, then its the q key
break
cap.release()
writer.release()
cv2.destroyAllWindows()
def readAndProcessVideo(file):
file_stream = cv2.VideoCapture(file)
if file_stream.isOpened() == False:
print("Failed to open video")
return
frameCounter = 0
#smaller, larger
#aggregateMatrix
while(file_stream.isOpened()):
ret, frame = file_stream.read()
if ret == True:
cv2.imshow('Frame',frame)
if(frameCounter == 0):
smaller = frame
frameCounter+=1
elif(frameCounter == 1):
larger = frame
frameCounter += 1
else:
smallToLarge = frame_registration_matrix(smaller, larger)
print(frameCounter) #, " ", smallToLarge)
frameCounter==1
smaller = larger
larger = frame
# if(frameCounter == 2):
# aggregateMatrix = smallToLarge
# frameCounter += 1
# else:
# aggregateMatrix = gen_aggregate_matrix(aggregateMatrix)
cv2.waitKey(1)
if cv2.waitKey(1) & 0xFF == 27: #escape key, if not, then its the q key
break
file_stream.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
#TODO Test surf by installing other opencv
#surf = cv2.xfeatures2d.SURF_create(400)
recordVideo()
readAndProcessVideo("flightVideo" + ".mp4")
# testCoordsOG = np.array([[CONSTX], [CONSTY], [1]])
# twoToOne = image_registration_matrix(BIGGEST, MIDDLEST)
# threeToTwo = image_registration_matrix(MIDDLEST, SMALLEST)
# threeToOne = gen_aggregate_matrix(twoToOne, threeToTwo)
# #print("\n\nGenerated matrix registration: ", np.matmul(twoToOne, testCoordsOG))
# img3 = cv2.imread(SMALLEST)
# img3 = cv2.circle(img3, (math.floor(CONSTX), math.floor(CONSTY)), 7, (255, 0, 0), 5)
# cv2.imshow("thing3", img3)
# fromThree = np.matmul(threeToTwo, testCoordsOG)
# print(fromThree)
# #print(fromThree[0, 0])
# #print(fromThree[1, 0])
# img2 = cv2.imread(MIDDLEST)
# img2 = cv2.circle(img2, (math.floor(fromThree[0, 0]), math.floor(fromThree[1, 0])), 7, (255, 0, 0), 5)
# cv2.imshow("thing2", img2)
# fromOG = np.matmul(threeToOne, testCoordsOG)
# fromTwo = np.matmul(twoToOne, fromThree)
# print(fromOG)
# img1 = cv2.imread(BIGGEST)
# img1 = cv2.circle(img1, (math.floor(fromOG[0, 0]), math.floor(fromOG[1, 0])), 7, (0, 0, 255), 5)
# img1 = cv2.circle(img1, (math.floor(fromTwo[0, 0]), math.floor(fromTwo[1, 0])), 30, (255, 0, 0), 5)
# cv2.imshow("thing", img1)
cv2.waitKey(0)
cv2.destroyAllWindows()