-
Notifications
You must be signed in to change notification settings - Fork 1
/
AR_detection_tracking.py
250 lines (211 loc) · 7.4 KB
/
AR_detection_tracking.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
# @File - AR_detection_tracking.py
# @Author - Rishabh Choudhary
# @Description - AR tag detection and tracking and placement of lena image
import numpy as np
import cv2
from matplotlib import pyplot as plt
import copy
import imutils
import math
# to take the input from the user to use the video
print("Choose from the selected options for Tag videos")
print("press 1 for Tag0")
print("press 2 for Tag1")
print("press 3 for Tag2")
print("press 4 for Multiple_tags")
print("")
a = int(input("Make your selection: "))
if a == 1:
cap = cv2.VideoCapture('Tag0.mp4')
elif a == 2:
cap = cv2.VideoCapture('Tag1.mp4')
elif a == 3:
cap = cv2.VideoCapture('Tag2.mp4')
elif a == 4:
cap = cv2.VideoCapture('multipleTags.mp4')
else:
print("sorry selection could not be identified, exiting code")
exit(0)
lena_img = cv2.imread('Lena.png')
lena_resize = cv2.resize(lena_img, (200, 200))
dim = 200
p1 = np.array([
[0, 0],
[dim - 1, 0],
[dim - 1, dim - 1],
[0, dim - 1]], dtype="float32")
# Decode the tag ID and 4 digit binary and orientation
def id_decode(image):
ret, img_bw = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY)
corner_pixel = 255
cropped_img = img_bw[50:150, 50:150]
block_1 = cropped_img[37, 37]
block_3 = cropped_img[62, 37]
block_2 = cropped_img[37, 62]
block_4 = cropped_img[62, 62]
white = 255
if block_3 == white:
block_3 = 1
else:
block_3 = 0
if block_4 == white:
block_4 = 1
else:
block_4 = 0
if block_2 == white:
block_2 = 1
else:
block_2 = 0
if block_1 == white:
block_1 = 1
else:
block_1 = 0
if cropped_img[85, 85] == corner_pixel:
return list([block_3, block_4, block_2, block_1]), "BR"
elif cropped_img[15, 85] == corner_pixel:
return list([block_4, block_2, block_1, block_3]), "TR"
elif cropped_img[15, 15] == corner_pixel:
return list([block_2, block_1, block_3, block_4]), "TL"
elif cropped_img[85, 15] == corner_pixel:
return list([block_1, block_3, block_4, block_2]), "BL"
return None, None
# Function to return the order of points in camera frame
def order(pts):
rect = np.zeros((4, 2), dtype="float32")
s = pts.sum(axis=1)
# print(np.argmax(s))
rect[0] = pts[np.argmin(s)]
rect[2] = pts[np.argmax(s)]
diff = np.diff(pts, axis=1)
# print(np.argmax(diff))
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
# return the ordered coordinates
return rect
# Function to compute homography between world and camera frame
def homograph(p, p1):
A = []
p2 = order(p)
for i in range(0, len(p1)):
x, y = p1[i][0], p1[i][1]
u, v = p2[i][0], p2[i][1]
A.append([x, y, 1, 0, 0, 0, -u * x, -u * y, -u])
A.append([0, 0, 0, x, y, 1, -v * x, -v * y, -v])
A = np.array(A)
U, S, Vh = np.linalg.svd(A)
l = Vh[-1, :] / Vh[-1, -1]
h = np.reshape(l, (3, 3))
# print(l)
# print(h)
return h
# Generate contours to detect corners of the tag
def contour_generator(frame):
test_img1 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
test_blur = cv2.GaussianBlur(test_img1, (5, 5), 0)
edge = cv2.Canny(test_blur, 75, 200)
edge1 = copy.copy(edge)
contour_list = list()
r, cnts, h = cv2.findContours(edge1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
index = list()
for hier in h[0]:
if hier[3] != -1:
index.append(hier[3])
# loop over the contours
for c in index:
peri = cv2.arcLength(cnts[c], True)
approx = cv2.approxPolyDP(cnts[c], 0.02 * peri, True)
if len(approx) > 4:
peri1 = cv2.arcLength(cnts[c - 1], True)
corners = cv2.approxPolyDP(cnts[c - 1], 0.02 * peri1, True)
contour_list.append(corners)
new_contour_list = list()
for contour in contour_list:
if len(contour) == 4:
new_contour_list.append(contour)
final_contour_list = list()
for element in new_contour_list:
if cv2.contourArea(element) < 2500:
final_contour_list.append(element)
return final_contour_list
# Reorient the tag based on the original orientation
def reorient(location, maxDim):
if location == "BR":
p1 = np.array([
[0, 0],
[maxDim - 1, 0],
[maxDim - 1, maxDim - 1],
[0, maxDim - 1]], dtype="float32")
return p1
elif location == "TR":
p1 = np.array([
[maxDim - 1, 0],
[maxDim - 1, maxDim - 1],
[0, maxDim - 1],
[0, 0]], dtype="float32")
return p1
elif location == "TL":
p1 = np.array([
[maxDim - 1, maxDim - 1],
[0, maxDim - 1],
[0, 0],
[maxDim - 1, 0]], dtype="float32")
return p1
elif location == "BL":
p1 = np.array([
[0, maxDim - 1],
[0, 0],
[maxDim - 1, 0],
[maxDim - 1, maxDim - 1]], dtype="float32")
return p1
# main function to process the tag
def image_process(frame, p1):
final_contour_list = contour_generator(frame)
lena_list = list()
for i in range(len(final_contour_list)):
cv2.drawContours(frame, [final_contour_list[i]], -1, (0, 255, 0), 2)
cv2.imshow("Outline", frame)
# warped = homogenous_transform(small, final_contour_list[i][:, 0])
c_rez = final_contour_list[i][:, 0]
H_matrix = homograph(p1, order(c_rez))
# H_matrix = homo(p1,order(c))
tag = cv2.warpPerspective(frame, H_matrix, (200, 200))
cv2.imshow("Outline", frame)
cv2.imshow("Tag after Homo", tag)
tag1 = cv2.cvtColor(tag, cv2.COLOR_BGR2GRAY)
decoded, location = id_decode(tag1)
empty = np.full(frame.shape, 0, dtype='uint8')
if not location == None:
p2 = reorient(location, 200)
if not decoded == None:
print("ID detected: " + str(decoded))
H_Lena = homograph(order(c_rez), p2)
lena_overlap = cv2.warpPerspective(lena_resize, H_Lena, (frame.shape[1], frame.shape[0]))
if not np.array_equal(lena_overlap, empty):
lena_list.append(lena_overlap.copy())
# print(lena_overlap.shape)
mask = np.full(frame.shape, 0, dtype='uint8')
if lena_list != []:
for lena in lena_list:
temp = cv2.add(mask, lena.copy())
mask = temp
lena_gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
r, lena_bin = cv2.threshold(lena_gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(lena_bin)
mask_3d = frame.copy()
mask_3d[:, :, 0] = mask_inv
mask_3d[:, :, 1] = mask_inv
mask_3d[:, :, 2] = mask_inv
img_masked = cv2.bitwise_and(frame, mask_3d)
final_image = cv2.add(img_masked, mask)
cv2.imshow("Lena", final_image)
# cv2.waitKey(0)
if cv2.waitKey(1) & 0xff == 27:
cv2.destroyAllWindows()
# Read the input video frame by frame
while cap.isOpened():
success, frame = cap.read()
if success == False:
break
img = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
image_process(img, p1)
cap.release()