AprilTag的種類叫家族(family),有下面的幾種:
TAG16H5 → 0 to 29
TAG25H7 → 0 to 241
TAG25H9 → 0 to 34
TAG36H10 → 0 to 2319
TAG36H11 → 0 to 586
ARTOOLKIT → 0 to 511
注意:一般使用TAG36H11
AprilTag識別:
代碼如下:
import sensor, image, time, math sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) # we run out of memory if the resolution is much bigger... sensor.skip_frames(30) sensor.set_auto_gain(False) # must turn this off to prevent image washout... sensor.set_auto_whitebal(False) # must turn this off to prevent image washout... clock = time.clock() while(True): clock.tick() img = sensor.snapshot() for tag in img.find_apriltags(): # defaults to TAG36H11 without "families". img.draw_rectangle(tag.rect(), color = (255, 0, 0)) img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0)) degress = 180 * tag.rotation() / math.pi print(tag.id(),degress) #輸出標簽的id值和攝像頭旋轉的角度
運行結果:輸出標簽的id值和攝像頭旋轉的角度
AprilTag3D定位:
代碼如下:
import sensor, image, time, math
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) # we run out of memory if the resolution is much bigger...
sensor.skip_frames(30)
sensor.set_auto_gain(False) # must turn this off to prevent image washout...
sensor.set_auto_whitebal(False) # must turn this off to prevent image washout...
clock = time.clock()
# 注意!與find_qrcodes不同,find_apriltags 不需要軟件矯正畸變就可以工作。
# 注意,輸出的姿態的單位是弧度,可以轉換成角度,但是位置的單位是和你的大小有關,需要等比例換算
# f_x 是x的像素為單位的焦距。對於標准的OpenMV,應該等於2.8/3.984*656,這個值是用毫米為單位的焦距除以x方向的感光元件的長度,乘以x方向的感光元件的像素(OV7725)
# f_y 是y的像素為單位的焦距。對於標准的OpenMV,應該等於2.8/2.952*488,這個值是用毫米為單位的焦距除以y方向的感光元件的長度,乘以y方向的感光元件的像素(OV7725)
# c_x 是圖像的x中心位置
# c_y 是圖像的y中心位置
f_x = (2.8 / 3.984) * 160 # 默認值
f_y = (2.8 / 2.952) * 120 # 默認值
c_x = 160 * 0.5 # 默認值(image.w * 0.5)
c_y = 120 * 0.5 # 默認值(image.h * 0.5)
#實際的TX、TY、TZ的值需要通過公式計算
#Kz=實際距離(Z值)/Tz
#之后算出每一次的實際距離=Kz*Tz
def degrees(radians):
return (180 * radians) / math.pi
while(True):
clock.tick()
img = sensor.snapshot()
for tag in img.find_apriltags(fx=f_x, fy=f_y, cx=c_x, cy=c_y): # 默認為TAG36H11
img.draw_rectangle(tag.rect(), color = (255, 0, 0))
img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
print_args = (tag.x_translation(), tag.y_translation(), tag.z_translation(), \
degrees(tag.x_rotation()), degrees(tag.y_rotation()), degrees(tag.z_rotation()))
# 位置的單位是未知的,旋轉的單位是角度
print("Tx: %f, Ty %f, Tz %f, Rx %f, Ry %f, Rz %f" % print_args)
#print(clock.fps())
運行結果:物體相對攝像頭的(x,y,z)坐標位置,和(x,y,z)旋轉角度
建立攝像頭的空間直角坐標系:
正是步行者,一步步登峰!