1 AprilTags介绍
1、AprilTags
类似与二维码QR
codes(Quick Response Code;全称为快速响应矩阵图码)
2、AprilTags可以应用于:
相机标定
- 目标大小估计
- 测量相机到目标的距离
- 3D 定位(3D positioning)
- 机器人
- SLAM
- 自主导航(autonomous navigation)
如下图是一个AprilTag Tag36h11的一张图:
我们在相机标定的时候,还会用到很多AprilTags在一起组成的标定板
,如下图(图片来源):
3、AprilTag family默认的是:Tag36h11,在AprilTags中有六个系列families:
- Tag36h11
- TagStandard41h12
- TagStandard52h13
- TagCircle21h7
- TagCircle49h12
- TagCustom48h12
2 使用python库包apriltag对AprilTag进行检测
2.1 python模块apriltag的安装
- pypi下载安装apriltag库包:https://pypi.org/project/apriltag/
pip install apriltag
2.2 python模块apriltag的测试用例
测试用例(参考)
import apriltag
import cv2
img = cv2.imread('apriltag_foto.jpg'.cv2.IMREAD_GRAYSCALE)
detector = apriltag.Detector()
result = detector.detect(img)
返回结果如下:
[DetectionBase(tag_family='tag36h11', tag_id=2, hamming=0, goodness=0.0, decision_margin=98.58241271972656, homography=array([[ -1.41302664e-01, 1.08428082e+00, 1.67512900e+01],
[ -8.75899366e-01, 1.50245469e-01, 1.70532040e+00],
[ -4.89183533e-04, 2.12210247e-03, 6.02052342e-02]]), center=array([ 278.23643912, 28.32511859]), corners=array([[ 269.8939209 , 41.50381088],
[ 269.57183838, 11.79248142],
[ 286.1383667 , 15.84242821],
[ 286.18066406, 43.48323059]])),
DetectionBase(tag_family='tag36h11', ... etc
结果中是用collections.OrderedDict
存储成字典类型的数据
3 AprilTags二维码检测,以及绘制检测框
1、代码
import cv2
import apriltag
image = cv2.imread('apriltags_36h11.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 定义AprilTag检测选项,然后检测AprilTags
print(f"[INFO] detection AprilTags ...")
options = apriltag.DetectorOptions(families="tag36h11")
detector = apriltag.Detector(options)
results = detector.detect(gray)
print(results)
print(f"[INFO] {len(results)} total AprilTags detected")
for r in results:
# extract the bounding box (x, y)-coordinates for the AprilTag
# and convert each of the (x, y)-coordinate pairs to integers
(ptA, ptB, ptC, ptD) = r.corners
ptB = (int(ptB[0]), int(ptB[1]))
ptC = (int(ptC[0]), int(ptC[1]))
ptD = (int(ptD[0]), int(ptD[1]))
ptA = (int(ptA[0]), int(ptA[1]))
# 绘制检测到的AprilTag的框
cv2.line(image, ptA, ptB, (0, 255, 0), 2, lineType=cv2.LINE_AA)
cv2.line(image, ptB, ptC, (0, 255, 0), 2, lineType=cv2.LINE_AA)
cv2.line(image, ptC, ptD, (0, 255, 0), 2, lineType=cv2.LINE_AA)
cv2.line(image, ptD, ptA, (0, 255, 0), 2, lineType=cv2.LINE_AA)
# 绘制 AprilTag 的中心坐标
(cX, cY) = (int(r.center[0]), int(r.center[1]))
cv2.circle(image, (cX, cY), 5, (0, 0, 255), -1)
# draw the tag family on the image
tagFamily = r.tag_family.decode("utf-8")
cv2.putText(image, tagFamily, (ptA[0], ptA[1] - 15),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
print("[INFO] tag family: {}".format(tagFamily))
cv2.imshow("Image", image)
cv2.waitKey(0)
2、检测结果如下图: