-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_2_B.py
177 lines (123 loc) · 5.12 KB
/
Problem_2_B.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
import numpy as np
import matplotlib.pyplot as plt
from scipy import fft
import cv2
import imutils
import math
cap = cv2.VideoCapture('Tag1.mp4')
#cap = cv2.VideoCapture('Tag0.mp4')
#cap = cv2.VideoCapture('Tag2.mp4')
out = cv2.VideoWriter('video.avi',cv2.VideoWriter_fourcc(*'XVID'), 15, (640,480))
# FFT to subract the background to get the tag
def Fourier(gray):
y1 = fft.fft2(gray)
y2 = fft.fftshift(y1)
(w, h) = gray.shape
half_w, half_h = int(w/2), int(h/2)
# high pass filter
n = 5
y2[half_w-n:half_w+n+1,half_h-n:half_h+n+1] = 0
y3 = fft.ifftshift(y2)
y4 = fft.ifft2(y3)
y = np.uint8(np.abs(y4))
return y
# Finding the corners of the tag from world co-ordinates
def contours(y):
edged = cv2.Canny(y, 90, 800)
contours = cv2.findContours(edged,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
contours = sorted(contours, key = cv2.contourArea, reverse = True)[:5]
#Finding the corners of the tag
for cnt in contours:
perimeter = cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,0.09*perimeter,True)
if cv2.contourArea(cnt) < 4000 and cv2.contourArea(cnt) > 100 and len(approx) == 4:
apx = approx
apx = apx.reshape(4,2)
corn = np.zeros((4,2))
sum_cnt = apx.sum(axis=1)
corn[0] = apx[np.argmin(sum_cnt)]
corn[2] = apx[np.argmax(sum_cnt)]
diff_cnt = np.diff(apx,axis=1)
corn[1] = apx[np.argmin(diff_cnt)]
corn[3] = apx[np.argmax(diff_cnt)]
break
return corn
#Calculating Homography Matrix
def homography(cornlist,wlist):
Alist = []
for i in range(len(cornlist)):
u, v = cornlist[i][0],cornlist[i][1]
X, Y = wlist[i][0],wlist[i][1]
Alist.append([X , Y , 1 , 0 , 0 , 0 , - X * u , - Y * u , - u])
Alist.append([0 , 0 , 0 , X , Y , 1 , - X * v , - Y * v , - v])
A = np.array(Alist)
U, sigma, VT = np.linalg.svd(A)
v= VT.T
rv = v[:,8]/v[8][8]
rv = rv.reshape((3,3))
return rv
# Projecting the CUBE onto the AR TAG by calculating the Projection Matrix
def projection(rv5,frame,points):
k = np.array([[1406.08415449821, 0, 0], [2.20679787308599, 1417.99930662800, 0], [1014.13643417416, 566.347754321696, 1]]).T
bnew = np.dot(np.linalg.inv(k), rv5)
b1 = bnew[:, 0].reshape(3, 1)
b2 = bnew[:, 1].reshape(3, 1)
r3 = np.cross(bnew[:, 0], bnew[:, 1])
b3 = bnew[:, 2].reshape(3, 1)
L = 2 / (np.linalg.norm((np.linalg.inv(k)).dot(b1)) + np.linalg.norm((np.linalg.inv(k)).dot(b2)))
r1 = L * b1
r2 = L * b2
r3 = (r3 * L * L).reshape(3, 1)
t = L * b3
r = np.concatenate((r1, r2, r3, t), axis=1)
P = np.dot(k, r)
cs = np.dot(P,points.T)
i1, j1, k1 = cs[:,0]
i2, j2, k2 = cs[:,1]
i3, j3, k3 = cs[:,2]
i4, j4, k4 = cs[:,3]
i5, j5, k5 = cs[:,4]
i6, j6, k6 = cs[:,5]
i7, j7, k7 = cs[:,6]
i8, j8, k8 = cs[:,7]
#Drawing lines through the co-ordinates
cv2.line(frame,( int(i1/k1), int(j1/k1)),( int(i2/k2), int(j2/k2)), (255,0,0), 2)
cv2.line(frame,( int(i2/k2), int(j2/k2)),( int(i3/k3), int(j3/k3)), (255,0,0), 2)
cv2.line(frame,( int(i3/k3), int(j3/k3)),( int(i4/k4), int(j4/k4)), (255,0,0), 2)
cv2.line(frame,( int(i4/k4), int(j4/k4)),( int(i1/k1), int(j1/k1)), (255,0,0), 2)
cv2.line(frame,( int(i1/k1), int(j1/k1)),( int(i5/k5), int(j5/k5)), (255,0,0), 2)
cv2.line(frame,( int(i2/k2), int(j2/k2)),( int(i6/k6), int(j6/k6)), (255,0,0), 2)
cv2.line(frame,( int(i3/k3), int(j3/k3)),( int(i7/k7), int(j7/k7)), (255,0,0), 2)
cv2.line(frame,( int(i4/k4), int(j4/k4)),( int(i8/k8), int(j8/k8)), (255,0,0), 2)
cv2.line(frame,( int(i5/k5), int(j5/k5)),( int(i6/k6), int(j6/k6)), (255,0,0), 2)
cv2.line(frame,( int(i6/k6), int(j6/k6)),( int(i7/k7), int(j7/k7)), (255,0,0), 2)
cv2.line(frame,( int(i7/k7), int(j7/k7)),( int(i8/k8), int(j8/k8)), (255,0,0), 2)
cv2.line(frame,( int(i8/k8), int(j8/k8)),( int(i5/k5), int(j5/k5)), (255,0,0), 2)
return frame
while True:
ret, frame = cap.read()
if ret == True:
b = cv2.resize(frame,(640,480),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)
b1 = b.copy()
b2 = b.copy()
b3 = b.copy()
gray = cv2.cvtColor(b1, cv2.COLOR_BGR2GRAY)
y = Fourier(gray)
#corn = contours(y)
try:
corn = contours(y)
except:
continue
cornlist = corn.tolist()
points = np.float32([[0,0,0,1],[0,1,0,1],[1,1,0,1],[1,0,0,1],[0,0,1,1],[0,1,1,1],[1,1,1,1],[1,0,1,1]])
rv2 = homography(cornlist,points)
image = projection(rv2,b1,points)
cv2.imshow('Cube',image)
#out.write(b1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cv2.waitKey(1)
cv2.destroyAllWindows()