-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathapriltag.py
135 lines (126 loc) · 5.01 KB
/
apriltag.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
import cv2
import matplotlib.pyplot as plt
import tagFamilies as tf
import numpy as np
import time
from scipy import misc
from scipy import ndimage
class Apriltag(object):
def __init__(self):
self.tagfamily = None
self.tagdetector = None
def create_detector(self,family = 'tag36h11',sigma=0.8,nthread =1,debug = False,minarea = 400,thresholding = 'adaptive',downsampling = False):
'''
init what kind of tag you will detect
'''
self._downsampling = downsampling
self._quad_sigma = sigma
self._nthread = nthread
self._minarea = minarea
self._debug = debug
self._thresholding = thresholding
if(family == 'tag36h11'):
self.tagfamily = tf.Tag36h11class(debug=self._debug)
elif(family == 'tag25h9'):
self.tagfamily = tf.Tag25h9class(debug = self._debug)
elif(family == 'tag16h5'):
self.tagfamily = tf.Tag16h5class(debug = self._debug)
else:
print("Do not support this tag")
def detect(self,frame):
gray = np.array(cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY))
if self._downsampling:
gray = misc.imresize(gray, [int(gray.shape[0] / 2), int(gray.shape[1] / 2)])
gray = ndimage.zoom(gray,2,order=0)
"""
1 blur
"""
img = cv2.GaussianBlur(gray, (3, 3), self._quad_sigma)
if (self._debug):
plt.figure().set_size_inches(19.2,10.8)
plt.imshow(img)
plt.gray()
plt.show()
"""
2 adaptive thresholding or canny
"""
time_start = time.time()
if(self._thresholding=='canny'):
img = cv2.Canny(img,50,350,apertureSize=3)
if(self._debug):
print("Canny")
elif(self._thresholding=='adaptive'):
img = np.array(cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 9, 5),dtype='uint8')
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(2,2))
img = cv2.morphologyEx(img, cv2.MORPH_OPEN,kernel)
# img = cv2.GaussianBlur(img, (7, 7), self._quad_sigma)
if (self._debug):
print("Adaptive thresholding")
else:
if (self._debug):
print("do not have this methon")
##################################
time_end = time.time()
if self._debug:
print('preprocessor cost', time_end - time_start)
plt.figure().set_size_inches(19.2, 10.8)
plt.imshow(img)
plt.gray()
plt.show()
##################################
"""
3 find contours
"""
if (self._thresholding == 'canny'):
contours, hierarchy = cv2.findContours(img, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
else:
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
##################################
time_end = time.time()
if self._debug:
print(len(contours))
print('contours cost', time_end - time_start)
if self._debug:
plt.figure().set_size_inches(19.2, 10.8)
framecopy = np.copy(frame)
cv2.drawContours(framecopy, contours, -1, (0, 255, 0), 2)
plt.imshow(framecopy)
plt.show()
##################################
"""
4 compute convex hulls and find maximum inscribed quadrilaterals
"""
quads = [] #array of quad including four peak points
hulls = []
for i in range(len(contours)):
if (hierarchy[0, i, 3] < 0 and contours[i].shape[0] >= 4):
area = cv2.contourArea(contours[i])
if area > self._minarea:
hull = cv2.convexHull(contours[i])
if (area / cv2.contourArea(hull) > 0.8):
if (self._debug):
hulls.append(hull)
quad = cv2.approxPolyDP(hull, 8, True)#maximum_area_inscribed
if (len(quad) == 4):
areaqued = cv2.contourArea(quad)
areahull = cv2.contourArea(hull)
if areaqued / areahull > 0.8 and areahull >= areaqued:
quads.append(quad)
##################################
time_end = time.time()
if self._debug:
print('compute convex cost', time_end - time_start)
framecopy = np.copy(frame)
cv2.drawContours(frame, quads, -1, (0, 255, 0), 2)
cv2.drawContours(framecopy, hulls, -1, (0, 255, 0), 2)
plt.figure().set_size_inches(19.2, 10.8)
plt.subplot(211)
plt.imshow(frame)
plt.subplot(212)
plt.imshow(framecopy)
plt.show()
##################################
"""
5 decode and get detections
"""
return self.tagfamily.decodeQuad(quads,gray)